From 51b2a70308f4442ae3830f8509a2279abfb95c9f Mon Sep 17 00:00:00 2001 From: dingguodong Date: Fri, 18 Jun 2021 10:51:31 +0800 Subject: [PATCH 1/8] * Create webhook for each products. --- module/gitlab/model.php | 9 ++++++--- module/repo/model.php | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/module/gitlab/model.php b/module/gitlab/model.php index 3e2375e6d1..419a94d231 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -237,9 +237,12 @@ class gitlabModel extends model */ public function createWebhook($products, $gitlabID, $projectID) { - $webhook = sprintf($this->config->gitlab->zentaoApiWebhookUrl, commonModel::getSysURL(), $gitlabID); - $response = $this->apiCreateHook($gitlabID, $projectID, $webhook, $this->config->gitlab->zentaoApiWebhookToken); - return $response; + foreach($products as $index => $product) + { + $webhook = sprintf($this->config->gitlab->zentaoApiWebhookUrl, commonModel::getSysURL(), $product, $gitlabID, $projectID); + $response = $this->apiCreateHook($gitlabID, $projectID, $webhook, $this->config->gitlab->zentaoApiWebhookToken); + } + return true; } /** diff --git a/module/repo/model.php b/module/repo/model.php index 165533741a..abe3a502af 100644 --- a/module/repo/model.php +++ b/module/repo/model.php @@ -236,7 +236,7 @@ class repoModel extends model // save the relationship between zentao product and gitlab project to zt_relation table. /* create webhook for zentao */ - $this->loadModel("gitlab")->createWebhook($this->post->gitlabHost, $this->post->gitlabProject); + $this->loadModel("gitlab")->createWebhook($this->post->product, $this->post->gitlabHost, $this->post->gitlabProject); } From 52dde68865bc328268841dde7eb28d40f154a738 Mon Sep 17 00:00:00 2001 From: dingguodong Date: Fri, 18 Jun 2021 11:01:37 +0800 Subject: [PATCH 2/8] * Create webhook when update product and project mapping. --- module/repo/model.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/module/repo/model.php b/module/repo/model.php index abe3a502af..df6ec01cb9 100644 --- a/module/repo/model.php +++ b/module/repo/model.php @@ -303,7 +303,13 @@ class repoModel extends model { $this->dao->delete()->from(TABLE_REPOHISTORY)->where('repo')->eq($id)->exec(); $this->dao->delete()->from(TABLE_REPOFILES)->where('repo')->eq($id)->exec(); - if($repo->SCM == 'Gitlab') $this->loadModel("gitlab")->initLabels($this->post->gitlabHost, $this->post->gitlabProject); + if($repo->SCM == 'Gitlab') + { + $this->loadModel("gitlab")->initLabels($this->post->gitlabHost, $this->post->gitlabProject); + + $this->loadModel("gitlab")->createWebhook($this->post->product, $this->post->gitlabHost, $this->post->gitlabProject); + + } return false; } From 8e176c30010e6fd9905bd1421f25b6d79ce5a1f7 Mon Sep 17 00:00:00 2001 From: dingguodong Date: Fri, 18 Jun 2021 11:05:45 +0800 Subject: [PATCH 3/8] * Fix bug when empty labels exists. --- module/gitlab/model.php | 1 + 1 file changed, 1 insertion(+) diff --git a/module/gitlab/model.php b/module/gitlab/model.php index 419a94d231..5877268dfd 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -406,6 +406,7 @@ class gitlabModel extends model public function isLabelExists($gitlabID, $projectID) { $labels = $this->apiGetLabels($gitlabID, $projectID); + if(empty($labels)) return false; foreach($labels as $label) { if(strpos($label->name, $this->config->gitlab->taskLabel->name) == 0) return true; From cdbcb418a874e5f299dcf995edb66993dca36fc0 Mon Sep 17 00:00:00 2001 From: dingguodong Date: Fri, 18 Jun 2021 13:38:24 +0800 Subject: [PATCH 4/8] * Improve code robustness. --- module/gitlab/model.php | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/module/gitlab/model.php b/module/gitlab/model.php index 5877268dfd..f7c102d75e 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -237,10 +237,15 @@ class gitlabModel extends model */ public function createWebhook($products, $gitlabID, $projectID) { + $urls = $this->getWebhookUrls($gitlabID, $projectID); + foreach($products as $index => $product) { - $webhook = sprintf($this->config->gitlab->zentaoApiWebhookUrl, commonModel::getSysURL(), $product, $gitlabID, $projectID); - $response = $this->apiCreateHook($gitlabID, $projectID, $webhook, $this->config->gitlab->zentaoApiWebhookToken); + $url = sprintf($this->config->gitlab->zentaoApiWebhookUrl, commonModel::getSysURL(), $product, $gitlabID, $projectID); + if(! array_key_exists($url, array_flip($urls))) + { + $response = $this->apiCreateHook($gitlabID, $projectID, $url, $this->config->gitlab->zentaoApiWebhookToken); + } } return true; } @@ -258,7 +263,7 @@ class gitlabModel extends model $apiRoot = $this->getApiRoot($gitlabID); $apiPath = "/projects/{$projectID}/hooks"; $url = sprintf($apiRoot, $apiPath); - $response = commonModel::http($url); + $response = json_decode(commonModel::http($url)); return $response; } @@ -277,9 +282,28 @@ class gitlabModel extends model $apiPath = "/projects/$projectID/hooks/$hookID)"; $url = sprintf($apiRoot, $apiPath); $response = commonModel::http($url); - return; + return $response; } + /** + * Get webhook urls + * + * @param int $gitlabID + * @param int $projectID + * @access public + * @return array $urls; + */ + public function getWebhookUrls($gitlabID, $projectID) + { + $urls = array(); + $webhooks = $this->apiGetHooks($gitlabID, $projectID); + foreach($webhooks as $index => $webhook) + { + $urls[] = $webhook->url; + } + return $urls; + } + /** * Create hook. * From 0fce5f06f37ba869b4a12d8a063367b40d64bbfe Mon Sep 17 00:00:00 2001 From: dingguodong Date: Fri, 18 Jun 2021 15:05:01 +0800 Subject: [PATCH 5/8] * Remove bad blank lines. --- module/repo/model.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/module/repo/model.php b/module/repo/model.php index df6ec01cb9..ff5bdfe8de 100644 --- a/module/repo/model.php +++ b/module/repo/model.php @@ -240,9 +240,6 @@ class repoModel extends model } - - - return $this->dao->lastInsertID(); } From 7a67b762c4140f7c5f2417a4db41a7acf9425f51 Mon Sep 17 00:00:00 2001 From: lichengjun Date: Fri, 18 Jun 2021 17:35:53 +0800 Subject: [PATCH 6/8] * Add Associat function --- module/gitlab/model.php | 34 +++++++++++++++++++++++++++++++--- module/repo/model.php | 3 ++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/module/gitlab/model.php b/module/gitlab/model.php index f7c102d75e..db0a24aa17 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -209,9 +209,6 @@ class gitlabModel extends model return array_key_exists($gitlabID, $projectID) ? $this->gitlab->getProjectPairs($gitlabID)[$projectID]: ""; } - - - /** * Get gitlab api base url with access_token * @@ -227,6 +224,37 @@ class gitlabModel extends model return $gitlab_url; } + /** + * Create relationship between zentao product and gitlab project. + * + * @param int $gitlabID + * @param int $projectID + * @access public + * @return void + */ + public function createAssociat($products, $gitlabID, $projectID) + { + $gitlabNamespace = $this->getProjectPairs($gitlabID); + + $gitlabAssociat = new stdclass; + $gitlabAssociat->execution = 0; + $gitlabAssociat->AVersion = 0; + $gitlabAssociat->relation = 'interrated'; + $gitlabAssociat->BVersion = 0; + $gitlabAssociat->extra = 0; + + foreach($products as $index => $prodcuct) + { + $gitlabAssociat->BType = $gitlabNamespace[$gitlabID]; + $gitlabAssociat->BID = $gitlabID; + $gitlabAssociat->Project = $ProjectID;; + $gitlabAssociat->Product = $product; + + $this->dao->insert(TABLE_RELATION)->data($gitlabAssociat)->exec(); + } + return true; + } + /** * Create webhook for zentao. * diff --git a/module/repo/model.php b/module/repo/model.php index ff5bdfe8de..9f7ee2f3ae 100644 --- a/module/repo/model.php +++ b/module/repo/model.php @@ -233,7 +233,8 @@ class repoModel extends model { $this->loadModel("gitlab")->initLabels($this->post->gitlabHost, $this->post->gitlabProject); - // save the relationship between zentao product and gitlab project to zt_relation table. + /* save the relationship between zentao product and gitlab project to zt_relation table.*/ + $this->loadModel("gitlab")->createAssociat($this->post->product, $this->post->gitlabHost, $this->post->gitlabProject); /* create webhook for zentao */ $this->loadModel("gitlab")->createWebhook($this->post->product, $this->post->gitlabHost, $this->post->gitlabProject); From b90639355dc6ee4b8c670b88c171573fc26e3d4d Mon Sep 17 00:00:00 2001 From: dingguodong Date: Sat, 19 Jun 2021 11:35:15 +0800 Subject: [PATCH 7/8] * Reformat codes and prepare for call. --- module/gitlab/model.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/module/gitlab/model.php b/module/gitlab/model.php index db0a24aa17..21a7cc26ec 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -359,7 +359,6 @@ class gitlabModel extends model $url = sprintf($apiRoot, $apiPath); $response = commonModel::http($url, $postData); return $response; - } /** @@ -466,8 +465,6 @@ class gitlabModel extends model } return false; - - } /** @@ -505,18 +502,21 @@ class gitlabModel extends model $apiRoot = $this->getApiRoot($gitlabID); $apiPath = "/projects/{$projectID}/issues/"; $url = sprintf($apiRoot, $apiPath); - $response = commonModel::http($url, $issue); + $response = json_decode(commonModel::http($url, $issue)); return $response; } - public function pushTask($task, $gitlabID,$projectID) + public function pushTask($gitlabID, $projectID, $task) { $task->label = $this->config->gitlab->taskLabel->name; + $response = $this->apiCreateIssue($gitlabID, $projectID, $task); + return $response; } - public function pushBug($bug, $gitlabID,$projectID) + public function pushBug($gitlabID, $projectID, $bug) { - $bug->label = $this->config->gitlab->bugLabel->name; + $response = $this->apiCreateIssue($gitlabID, $projectID, $bug); + return $response; } } From 6b9867c68345b430c12ce78ede3453ae43cf1750 Mon Sep 17 00:00:00 2001 From: lichengjun Date: Sat, 19 Jun 2021 15:23:59 +0800 Subject: [PATCH 8/8] * fix Associat bug --- module/gitlab/model.php | 25 ++++++++++++++----------- module/repo/model.php | 10 +++------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/module/gitlab/model.php b/module/gitlab/model.php index db0a24aa17..47c4fdc614 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -179,12 +179,12 @@ class gitlabModel extends model { $gitlab = $this->getByID($gitlabID); if(!$gitlab) return array(); - $host = rtrim($gitlab->url, '/'); + $host = rtrim($gitlab->url, '/'); $host .= '/api/v4/projects'; $allResults = array(); for($page = 1; true; $page ++) - { + { $results = json_decode(commonModel::http($host . "?private_token={$gitlab->token}&simple=true&membership=true&page={$page}&per_page=100")); if(empty($results) or $page > 10) break; $allResults = $allResults + $results; @@ -232,9 +232,12 @@ class gitlabModel extends model * @access public * @return void */ - public function createAssociat($products, $gitlabID, $projectID) + public function createAssociat($products, $gitlabID, $gitlabProjectID) { - $gitlabNamespace = $this->getProjectPairs($gitlabID); + $productIDs = $this->dao->select('id,program')->from(TABLE_PRODUCT)->fetchAll(); + + $projectID = array(); + foreach($productIDs as $project) $projectID[$project->id] = $project->program; $gitlabAssociat = new stdclass; $gitlabAssociat->execution = 0; @@ -242,15 +245,15 @@ class gitlabModel extends model $gitlabAssociat->relation = 'interrated'; $gitlabAssociat->BVersion = 0; $gitlabAssociat->extra = 0; + $gitlabAssociat->BID = $gitlabID; - foreach($products as $index => $prodcuct) - { - $gitlabAssociat->BType = $gitlabNamespace[$gitlabID]; - $gitlabAssociat->BID = $gitlabID; - $gitlabAssociat->Project = $ProjectID;; - $gitlabAssociat->Product = $product; + foreach($products as $index => $prodcut) + { + $gitlabAssociat->BType = $this->getprojectpairs($gitlabID)[$gitlabProjectID]; + $gitlabAssociat->Project = $projectID[$prodcut]; + $gitlabAssociat->Product = $prodcut; - $this->dao->insert(TABLE_RELATION)->data($gitlabAssociat)->exec(); + $this->dao->replace(TABLE_RELATION)->data($gitlabAssociat)->exec(); } return true; } diff --git a/module/repo/model.php b/module/repo/model.php index 9f7ee2f3ae..220dae63e4 100644 --- a/module/repo/model.php +++ b/module/repo/model.php @@ -231,16 +231,12 @@ class repoModel extends model if($this->post->SCM == 'Gitlab') { - $this->loadModel("gitlab")->initLabels($this->post->gitlabHost, $this->post->gitlabProject); - - /* save the relationship between zentao product and gitlab project to zt_relation table.*/ - $this->loadModel("gitlab")->createAssociat($this->post->product, $this->post->gitlabHost, $this->post->gitlabProject); + $this->loadModel("gitlab")->initLabels($this->post->gitlabHost, $this->post->gitlabProject); + $this->loadModel("gitlab")->createAssociat($this->post->product, $this->post->gitlabHost, $this->post->gitlabProject); /* create webhook for zentao */ - $this->loadModel("gitlab")->createWebhook($this->post->product, $this->post->gitlabHost, $this->post->gitlabProject); - + $this->loadModel("gitlab")->createWebhook($this->post->product, $this->post->gitlabHost, $this->post->gitlabProject); } - return $this->dao->lastInsertID(); }