diff --git a/module/gitlab/control.php b/module/gitlab/control.php index 5ddb88f56d..e4f654be13 100644 --- a/module/gitlab/control.php +++ b/module/gitlab/control.php @@ -1071,7 +1071,7 @@ class gitlab extends control $repo = $this->loadModel('repo')->getByID($repoID); $res = $this->gitlab->addPushWebhook($repo); - if($res or is_array($res)) + if($res) { return $this->send(array('result' => 'success', 'message' => $this->lang->gitlab->addWebhookSuccess)); } diff --git a/module/gitlab/model.php b/module/gitlab/model.php index ee3a9a5189..2cdec44ed7 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -1074,17 +1074,19 @@ class gitlabModel extends model $systemURL = dirname(common::getSysURL() . $_SERVER['REQUEST_URI']); $hook = new stdClass; - $hook->url = $systemURL . '/api.php/v1/gitlab/webhook?repoID='. $repo->id; + $hook->url = $systemURL . '/api.php/v1/gitlab/webhook?repoID='. $repo->id; $hook->push_events = true; $hook->merge_requests_events = true; if($token) $hook->token = $token; /* Return an empty array if where is one existing webhook. */ - if($this->isWebhookExists($repo, $hook->url)) return array(); + if($this->isWebhookExists($repo, $hook->url)) return true; - $result = $this->apiCreateHook($repo->gitService, $repo->project, $hook); + $result = $this->apiCreateHook($repo->gitService, (int)$repo->project, $hook); if(!empty($result->id)) return true; + + if(!empty($result->message)) return array('result' => 'fail', 'message' => $result->message); return false; } @@ -1098,7 +1100,7 @@ class gitlabModel extends model */ public function isWebhookExists(object $repo, string $url = ''): bool { - $hookList = $this->apiGetHooks($repo->gitService, $repo->project); + $hookList = $this->apiGetHooks($repo->gitService, (int)$repo->project); foreach($hookList as $hook) { if(empty($hook->url)) continue; diff --git a/module/gitlab/test/gitlab.class.php b/module/gitlab/test/gitlab.class.php index cf7ec112d6..68233f22fd 100644 --- a/module/gitlab/test/gitlab.class.php +++ b/module/gitlab/test/gitlab.class.php @@ -266,7 +266,7 @@ class gitlabTest if($projectID) $repo->project = $projectID; $result = $this->gitlab->addPushWebhook($repo, $token); - if(is_array($result)) $result = true; + if(is_array($result)) $result = false; return $result; } diff --git a/module/gitlab/test/model/addpushwebhook.php b/module/gitlab/test/model/addpushwebhook.php index f01eb72dad..7ad9d549a7 100644 --- a/module/gitlab/test/model/addpushwebhook.php +++ b/module/gitlab/test/model/addpushwebhook.php @@ -6,22 +6,23 @@ su('admin'); /** -title=测试 gitlabModel::isWebhookExists(); +title=测试 gitlabModel::addPushWebhook(); timeout=0 cid=1 -- 检查url为空的webhook是否存在 @0 -- 用正常的url检查webhook是否存在 @1 +- 使用repoID为1,不存在的项目id推送webhook @0 +- 使用repoID为1,存在的项目id推送webhook @1 */ zdTable('pipeline')->gen(5); -zdTable('repo')->config('repo')->gen(1); +zdTable('repo')->gen(1); $gitlab = new gitlabTest(); $repoID = 1; -$url = 'http:/api.php/v1/gitlab/webhook?repoID=1'; +$token = ''; +$_SERVER['REQUEST_URI'] = 'http://unittest/'; -r($gitlab->isWebhookExistsTest($repoID, '')) && p() && e('0'); //检查url为空的webhook是否存在 -r($gitlab->isWebhookExistsTest($repoID, $url)) && p() && e('1'); //用正常的url检查webhook是否存在 \ No newline at end of file +r($gitlab->addPushWebhookTest($repoID, $token)) && p() && e('0'); //使用repoID为1,不存在的项目id推送webhook +r($gitlab->addPushWebhookTest($repoID, $token, 2)) && p() && e('1'); //使用repoID为1,存在的项目id推送webhook \ No newline at end of file diff --git a/module/gitlab/test/model/iswebhookexists.php b/module/gitlab/test/model/iswebhookexists.php index 7ad9d549a7..f01eb72dad 100644 --- a/module/gitlab/test/model/iswebhookexists.php +++ b/module/gitlab/test/model/iswebhookexists.php @@ -6,23 +6,22 @@ su('admin'); /** -title=测试 gitlabModel::addPushWebhook(); +title=测试 gitlabModel::isWebhookExists(); timeout=0 cid=1 -- 使用repoID为1,不存在的项目id推送webhook @0 -- 使用repoID为1,存在的项目id推送webhook @1 +- 检查url为空的webhook是否存在 @0 +- 用正常的url检查webhook是否存在 @1 */ zdTable('pipeline')->gen(5); -zdTable('repo')->gen(1); +zdTable('repo')->config('repo')->gen(1); $gitlab = new gitlabTest(); $repoID = 1; -$token = ''; -$_SERVER['REQUEST_URI'] = 'http://unittest/'; +$url = 'http:/api.php/v1/gitlab/webhook?repoID=1'; -r($gitlab->addPushWebhookTest($repoID, $token)) && p() && e('0'); //使用repoID为1,不存在的项目id推送webhook -r($gitlab->addPushWebhookTest($repoID, $token, 2)) && p() && e('1'); //使用repoID为1,存在的项目id推送webhook \ No newline at end of file +r($gitlab->isWebhookExistsTest($repoID, '')) && p() && e('0'); //检查url为空的webhook是否存在 +r($gitlab->isWebhookExistsTest($repoID, $url)) && p() && e('1'); //用正常的url检查webhook是否存在 \ No newline at end of file diff --git a/module/repo/model.php b/module/repo/model.php index 953c3f4cc8..a3aa1d4c6d 100644 --- a/module/repo/model.php +++ b/module/repo/model.php @@ -195,10 +195,10 @@ class repoModel extends model { $token = uniqid(); $res = $this->loadModel('gitlab')->addPushWebhook($repo, $token); - if($res === false) + if($res !== true) { $this->dao->delete()->from(TABLE_REPO)->where('id')->eq($repoID)->exec(); - dao::$errors['webhook'][] = $this->lang->gitlab->failCreateWebhook; + dao::$errors['webhook'][] = isset($res['message']) ? $res['message'] : $this->lang->gitlab->failCreateWebhook; return false; } else @@ -358,9 +358,9 @@ class repoModel extends model $token = uniqid(); $res = $this->loadModel('gitlab')->addPushWebhook($repo, $token); - if($res === false) + if($res !== true) { - dao::$errors['webhook'][] = $this->lang->gitlab->failCreateWebhook; + dao::$errors['webhook'][] = isset($res['message']) ? $res['message'] : $this->lang->gitlab->failCreateWebhook; return false; } else