diff --git a/module/gitlab/model.php b/module/gitlab/model.php index 0294ec1668..21d832df8b 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -794,7 +794,7 @@ class gitlabModel extends model * @access public * @return bool */ - public function createWebhook($products, $gitlabID, $projectID) + public function initWebhooks($products, $gitlabID, $projectID) { $gitlab = $this->getByID($gitlabID); $webhooks = $this->apiGetHooks($gitlabID, $projectID); @@ -822,14 +822,9 @@ class gitlabModel extends model { $object = $this->loadModel($objectType)->getByID($objectID); if(empty($object)) return false; - if(!$gitlabID or !$projectID) - { - $result = $this->getRelationByObject($objectType, $objectID); - $gitlabID = $result->gitlabID; - $projectID = $result->projectID; - } + if(!$gitlabID or !$projectID) return false; - $syncedIssue = $this->getSyncedIssue($objectType = $objectType, $objectID = $objectID, $gitlabID); + $syncedIssue = $this->getRelationByObject($objectType, $objectID); $issue = $this->parseObjectToIssue($gitlabID, $projectID, $object, $objectType); if($syncedIssue) return $this->apiUpdateIssue($gitlabID, $projectID, $syncedIssue->BID, $issue); diff --git a/module/repo/model.php b/module/repo/model.php index 463dc12510..bccba49194 100644 --- a/module/repo/model.php +++ b/module/repo/model.php @@ -235,7 +235,7 @@ class repoModel extends model $this->loadModel("gitlab")->saveProjectRelation($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")->initWebhooks($this->post->product, $this->post->gitlabHost, $this->post->gitlabProject); } return $this->dao->lastInsertID(); } @@ -298,7 +298,7 @@ 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")->createWebhook($this->post->product, $this->post->gitlabHost, $this->post->gitlabProject); + if($repo->SCM == 'Gitlab') $this->loadModel("gitlab")->initWebhooks($this->post->product, $this->post->gitlabHost, $this->post->gitlabProject); return false; } diff --git a/module/upgrade/model.php b/module/upgrade/model.php index 53b185d58a..7dd700b77c 100644 --- a/module/upgrade/model.php +++ b/module/upgrade/model.php @@ -4991,4 +4991,30 @@ class upgradeModel extends model $this->dao->update(TABLE_PRODUCT)->set('createdVersion')->eq($this->config->version)->where('createdVersion')->eq('')->exec(); return true; } + + public function processGitlabRepo() + { + $repoList = $this->dao->select('*')->from(TABLE_REPO)->where('SCM')->eq('Gitlab')->fetchAll(); + foreach($repoList as $repo) + { + /* Create gitlab from repo. */ + $gitlab = new stdclass; + $gitlab->type = 'gitlab'; + $gitlab->name = $repo->client; + $gitlab->token = base64_decode($repo->password); + $gitlab->private = md5(uniqid()); + $this->dao->insert(TABLE_PIPELINE)->data($gitlab)->exec(); + + $gitlabID = $this->dao->lastInsertID(); + + preg_match('/projects\/(\d+)\/repository/', $repo->path, $matches); + $gitlabProject = $matches[1]; + $products = explode(',', $repo->product); + $this->loadModel('gitlab')->saveProjectRelation($products, $gitlabID, $gitlabProject); + + $this->dao->update(TABLE_REPO)->set('client')->eq($gitlabID)->set('path')->eq($gitlabProject)->where('id')->eq($repo->id)->exec(); + + $this->loadModel("gitlab")->initWebhooks($products, $gitlabID, $gitlabProject); + } + } }