From 65a6b38fae1d68cfd44679d457cdaa874e9e48eb Mon Sep 17 00:00:00 2001 From: dingguodong Date: Wed, 16 Jun 2021 17:34:44 +0800 Subject: [PATCH] + Create labels when gitlab server added successfully. --- module/common/model.php | 3 +- module/gitlab/model.php | 95 ++++++++++++++++++++++++++++++++++++--- module/pipeline/model.php | 6 +++ module/repo/model.php | 7 ++- 4 files changed, 102 insertions(+), 9 deletions(-) diff --git a/module/common/model.php b/module/common/model.php index 31181f9425..7e872be987 100644 --- a/module/common/model.php +++ b/module/common/model.php @@ -2236,10 +2236,11 @@ EOD; if(!empty($data)) { + if(is_object($data)) $data = (array) $data; curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } - + if($options) curl_setopt_array($curl, $options); $response = curl_exec($curl); diff --git a/module/gitlab/model.php b/module/gitlab/model.php index d59d760e5b..c0619ce7f3 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -242,10 +242,19 @@ class gitlabModel extends model public function apiCreateHook($gitlabID, $projectID, $url, $token) { $apiRoot = $this->getApiRoot($gitlabID); - $args = "&enable_ssl_verification=false&issues_events=true&merge_requests_events=true&push_events=true&tag_push_events=true&url={$url}&token={$token}"; + + $postData = new stdclass; + $postData->enable_ssl_verification = "false"; + $postData->issues_events = "true"; + $postData->merge_requests_events = "true"; + $postData->push_events = "true"; + $postData->tag_push_events = "true"; + $postData->url = $url; + $postData->token = $token; + $apiPath = "/projects/{$projectID}/hooks"; - $url = sprintf($apiRoot, $apiPath) . $args; - $apiJsonRes = commonModel::http($url,"options=array('CURLOPT_CUSTOMREQUEST'=>'post')"); + $url = sprintf($apiRoot, $apiPath); + $apiJsonRes = commonModel::http($url, $postData); return $apiJsonRes; } @@ -264,7 +273,7 @@ class gitlabModel extends model $apiRoot = $this->getApiRoot($gitlabID); $apiPath = "/projects/{$projectID}/hooks/{$hookID}"; $url = sprintf($apiRoot, $apiPath); - $apiJsonRes = commonModel::http($url,"options=array('CURLOPT_CUSTOMREQUEST'=>'delete')"); + $apiJsonRes = commonModel::http($url, null, array(CURLOPT_CUSTOMREQUEST => 'delete')); return $apiJsonRes; } @@ -280,13 +289,85 @@ class gitlabModel extends model public function apiUpdateHook($gitlabID, $projectID, $hookID) { $apiRoot = $this->getApiRoot($gitlabID); - $args = "&enable_ssl_verification=false&issues_events=true&merge_requests_events=true&push_events=true&tag_push_events=true&url={$url}&token={$token}"; + + $postData = new stdclass; + $postData->enable_ssl_verification = "false"; + $postData->issues_events = "true"; + $postData->merge_requests_events = "true"; + $postData->push_events = "true"; + $postData->tag_push_events = "true"; + $postData->url = $url; + $postData->token = $token; + $apiPath = "/projects/{$projectID}/hooks/{$hookID}"; - $url = sprintf($apiRoot, $apiPath) . $args; - $apiJsonRes = commonModel::http($url,"options=array('CURLOPT_CUSTOMREQUEST'=>'put')"); + $url = sprintf($apiRoot, $apiPath); + $apiJsonRes = commonModel::http($url, $postData, $options = array(CURLOPT_CUSTOMREQUEST => 'put')); return $apiJsonRes; } + + public function apiCreateLabel($gitlabID, $projectID, $label) + { + $apiRoot = $this->getApiRoot($gitlabID); + + if(empty($label->name) or empty($label->color)) return false; + + $apiPath = "/projects/{$projectID}/labels/"; + $url = sprintf($apiRoot, $apiPath); + $response = commonModel::http($url, $label); + return $response; + } + + public function apiGetLabels($gitlabID, $projectID) + { + $apiRoot = $this->getApiRoot($gitlabID); + $apiPath = "/projects/{$projectID}/labels/"; + $url = sprintf($apiRoot, $apiPath); + $response = commonModel::http($url); + $labels = json_decode($response); + return $labels; + } + + public function isLabelExists($gitlabID, $projectID) + { + $labels = $this->apiGetLabels($gitlabID, $projectID); + foreach($labels as $label) + { + if(strpos($label->title, "zentao task") == 0) return true; + } + + return false; + + } + + public function initLabels($gitlabID, $projectID) + { + if($this->isLabelExists($gitlabID, $projectID)) return true; + + $taskLabel = new stdclass(); + $taskLabel->name = "zentao task"; + $taskLabel->description = "task label from zentao"; + $taskLabel->color = "#0033CC"; + $taskLabel->priority = "0"; + + $bugLabel = new stdclass(); + $bugLabel->name = "zentao bug"; + $bugLabel->description = "bug label from zentao"; + $bugLabel->color = "#D10069"; + $bugLabel->priority = "0"; + + $this->apiCreateLabel($gitlabID, $projectID, $taskLabel); + $this->apiCreateLabel($gitlabID, $projectID, $bugLabel); + + return; + } + + public function apiCreateIssue($gitlabID, $projectID) + { + + } + + public function pushTask($task, $gitlabID,$projectID) { diff --git a/module/pipeline/model.php b/module/pipeline/model.php index 7a2d669a1f..67f562cdc6 100644 --- a/module/pipeline/model.php +++ b/module/pipeline/model.php @@ -87,6 +87,9 @@ class pipelineModel extends model ->autoCheck() ->exec(); if(dao::isError()) return false; + + $this->loadModel("gitlab")->initLabels($this->post->gitlabID, $this->post->projectID); + return $this->dao->lastInsertId(); } @@ -115,6 +118,9 @@ class pipelineModel extends model ->autoCheck() ->where('id')->eq($id) ->exec(); + + if(!dao::isError()) $this->loadModel("gitlab")->initLabels($this->post->gitlabID, $this->post->projectID); + return !dao::isError(); } } diff --git a/module/repo/model.php b/module/repo/model.php index 32feb9147a..1d50f2efbf 100644 --- a/module/repo/model.php +++ b/module/repo/model.php @@ -229,6 +229,8 @@ class repoModel extends model if(!dao::isError()) $this->rmClientVersionFile(); + if($this->post->SCM == 'Gitlab') $this->loadModel("gitlab")->initLabels($this->post->gitlabHost, $this->post->gitlabProject); + return $this->dao->lastInsertID(); } @@ -274,6 +276,7 @@ class repoModel extends model if($data->client != $repo->client and !$this->checkClient()) return false; if(!$this->checkConnection()) return false; + if($data->encrypt == 'base64') $data->password = base64_encode($data->password); $this->dao->update(TABLE_REPO)->data($data, $skip = 'gitlabHost,gitlabToken,gitlabProject') ->batchCheck($this->config->repo->edit->requiredFields, 'notempty') @@ -288,8 +291,10 @@ 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(); - return false; + if($repo->SCM == 'Gitlab') $this->loadModel("gitlab")->initLabels($this->post->gitlabHost, $this->post->gitlabProject); + return false; } + return true; }