From cebaa0cad165d3cbe69f216df22443da632bd6c9 Mon Sep 17 00:00:00 2001 From: dingguodong Date: Tue, 31 Aug 2021 10:56:41 +0800 Subject: [PATCH 01/12] + Accept a MR. --- module/mr/control.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/module/mr/control.php b/module/mr/control.php index cf3cfe938d..c2bddf98c2 100644 --- a/module/mr/control.php +++ b/module/mr/control.php @@ -131,6 +131,21 @@ class mr extends control $this->display(); } + /** + * Accept a MR. + * + * @param int $MRID + * @access public + * @return void + */ + public function accept($MRID) + { + $MR = $this->mr->getByID($MRID); + if(isset($MR->gitlabID)) $rawMR = $this->mr->apiAcceptMR($MR->gitlabID, $MR->targetProject, $MR->mriid); + if(isset($rawMR->state) and $rawMR->state == 'merged') return array('result' => 'success', 'message' => $this->lang->mr->mergeSuccess); + return array('result' => 'fail', 'message' => $this->lang->mr->mergeFailed); + } + /** * AJAX: Get MR target projects. * From df881ebb35d088cee1cf62ebf9a71db62987e98f Mon Sep 17 00:00:00 2001 From: dingguodong Date: Tue, 31 Aug 2021 10:57:17 +0800 Subject: [PATCH 02/12] * Correct apiAcceptMR and update functions. --- module/mr/model.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/module/mr/model.php b/module/mr/model.php index 1cba029e8f..bdb58b44dc 100644 --- a/module/mr/model.php +++ b/module/mr/model.php @@ -153,15 +153,16 @@ class mrModel extends model /* Update MR in GitLab. */ $newMR = new stdclass; - $newMR->title = $MR->title; - $newMR->description = $MR->description; - $newMR->assignee = $MR->assignee; - $newMR->reviewer = $MR->reviewer; - $newMR->targetBranch = $MR->targetBranch; + $newMR->title = $MR->title; + $newMR->description = $MR->description; + $newMR->assignee_ids = $MR->assignee; + $newMR->reviewer_ids = $MR->reviewer; + $newMR->target_branch = $MR->targetBranch; $oldMR = $this->getByID($MRID); - $this->apiUpdateMR($oldMR->gitlabID, $oldMR->targetProject, $oldMR->mriid, $newMR); + /* Known issue: `reviewer_ids` takes no effect. */ + $rawMR = $this->apiUpdateMR($oldMR->gitlabID, $oldMR->targetProject, $oldMR->mriid, $newMR); /* Change gitlab user ID to zentao account. */ $gitlabUsers = $this->gitlab->getUserIdAccountPairs($oldMR->gitlabID); @@ -173,6 +174,7 @@ class mrModel extends model ->where('id')->eq($MRID) ->autoCheck() ->exec(); + $MR = $this->getByID($MRID); if(dao::isError()) return array('result' => 'fail', 'message' => dao::getError()); return array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => helper::createLink('mr', 'browse')); @@ -314,8 +316,8 @@ class mrModel extends model */ public function apiAcceptMR($gitlabID, $projectID, $MRID) { - $url = sprintf($this->gitlab->getApiRoot($gitlabID), "/projects/$projectID/merge_requests/$MRID"); - return json_decode(commonModel::http($url, $data, $options = array(CURLOPT_CUSTOMREQUEST => 'PUT'))); + $url = sprintf($this->gitlab->getApiRoot($gitlabID), "/projects/$projectID/merge_requests/$MRID/merge"); + return json_decode(commonModel::http($url, array(), $options = array(CURLOPT_CUSTOMREQUEST => 'PUT'))); } /** From 587dd38b63164a043d73bc4ca648b54c458bad68 Mon Sep 17 00:00:00 2001 From: dingguodong Date: Tue, 31 Aug 2021 11:01:29 +0800 Subject: [PATCH 03/12] * Update MR view and lang. --- module/mr/lang/zh-cn.php | 8 ++++++-- module/mr/view/view.html.php | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/module/mr/lang/zh-cn.php b/module/mr/lang/zh-cn.php index 0498e9afd2..9a83adbd96 100644 --- a/module/mr/lang/zh-cn.php +++ b/module/mr/lang/zh-cn.php @@ -30,7 +30,7 @@ $lang->mr->statusList['merged'] = '已合并'; $lang->mr->mergeStatusList = array(); $lang->mr->mergeStatusList['checking'] = '检查中'; $lang->mr->mergeStatusList['can_be_merged'] = '可合并'; -$lang->mr->mergeStatusList['cannot_be_merged'] = '不可合并'; +$lang->mr->mergeStatusList['cannot_be_merged'] = '不可自动合并'; $lang->mr->description = '描述'; $lang->mr->confirmDelete = '确认删除该合并请求吗?'; @@ -68,6 +68,10 @@ $lang->mr->hasConflicts = "存在合并冲突"; $lang->mr->hasNoConflict = "可以合并请求"; $lang->mr->mergeByManual = "此合并请求可以手动合并,请使用以下"; $lang->mr->commandLine = "合并命令"; +$lang->mr->acceptMR = "合并"; +$lang->mr->mergeFailed = "无法合并,请核对合并请求状态"; +$lang->mr->mergeSuccess = "已成功合并"; + /** * Merge Command Document. @@ -80,7 +84,7 @@ $lang->mr->commandLine = "合并命令"; * %s mr::target_branch */ $lang->mr->commandDocument = <<< EOD -
在本地检出、审核和合并
+
在本地检出、审核和手动合并

第 1 步. 获取并查看此合并请求的分支 diff --git a/module/mr/view/view.html.php b/module/mr/view/view.html.php index b5f7c68ca4..89cada7c09 100644 --- a/module/mr/view/view.html.php +++ b/module/mr/view/view.html.php @@ -29,6 +29,9 @@

mr->from . html::a($sourceProjectURL, $sourceProjectName . ":" . $MR->sourceBranch, "_blank", "class='btn btn-link btn-active-text' style='color: blue'") . $lang->mr->to . html::a($targetProjectURL, $targetProjectName . ":" . $MR->targetBranch, "_blank", "class='btn btn-link btn-active-text' style='color: blue'");?>
+ mr->status;?> + mr->statusList, $MR->status);?> +
head_pipeline->status)):?>
mr->pipeline}{$lang->mr->status}";?> @@ -51,8 +54,12 @@
+ state == 'opened'):?>
mr->commandDocument, $httpRepoURL, $MR->sourceBranch, $branchPath, $MR->targetBranch, $branchPath, $MR->targetBranch);?>
+ +
+ state == 'opened' and !$rawMR->has_conflicts) echo html::linkButton(' ' . $lang->mr->acceptMR, inlink( 'accept', "mr=$MR->id"), 'self', '', 'btn btn-wide btn-primary');?> From b2f1fd3f3cdba385403bf8ddd3f7005387d4838a Mon Sep 17 00:00:00 2001 From: dingguodong Date: Tue, 31 Aug 2021 11:05:03 +0800 Subject: [PATCH 04/12] + Accept a MR. --- module/mr/control.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/module/mr/control.php b/module/mr/control.php index 5acb340fa0..a3f9ee28fb 100644 --- a/module/mr/control.php +++ b/module/mr/control.php @@ -132,6 +132,7 @@ class mr extends control } /** +<<<<<<< HEAD * Crontab sync MR from GitLab API to Zentao database, default time 5 minutes to execute once. * * @access public @@ -150,6 +151,20 @@ class mr extends control echo 'success'; } + /** Accept a MR. + * + * @param int $MRID + * @access public + * @return void + */ + public function accept($MRID) + { + $MR = $this->mr->getByID($MRID); + if(isset($MR->gitlabID)) $rawMR = $this->mr->apiAcceptMR($MR->gitlabID, $MR->targetProject, $MR->mriid); + if(isset($rawMR->state) and $rawMR->state == 'merged') return array('result' => 'success', 'message' => $this->lang->mr->mergeSuccess); + return array('result' => 'fail', 'message' => $this->lang->mr->mergeFailed); + } + /** * AJAX: Get MR target projects. * From 95c2adad703f2ca0f08f00104b8b2943b824e210 Mon Sep 17 00:00:00 2001 From: dingguodong Date: Tue, 31 Aug 2021 10:57:17 +0800 Subject: [PATCH 05/12] * Correct apiAcceptMR and update functions. --- module/mr/model.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/module/mr/model.php b/module/mr/model.php index 1cba029e8f..bdb58b44dc 100644 --- a/module/mr/model.php +++ b/module/mr/model.php @@ -153,15 +153,16 @@ class mrModel extends model /* Update MR in GitLab. */ $newMR = new stdclass; - $newMR->title = $MR->title; - $newMR->description = $MR->description; - $newMR->assignee = $MR->assignee; - $newMR->reviewer = $MR->reviewer; - $newMR->targetBranch = $MR->targetBranch; + $newMR->title = $MR->title; + $newMR->description = $MR->description; + $newMR->assignee_ids = $MR->assignee; + $newMR->reviewer_ids = $MR->reviewer; + $newMR->target_branch = $MR->targetBranch; $oldMR = $this->getByID($MRID); - $this->apiUpdateMR($oldMR->gitlabID, $oldMR->targetProject, $oldMR->mriid, $newMR); + /* Known issue: `reviewer_ids` takes no effect. */ + $rawMR = $this->apiUpdateMR($oldMR->gitlabID, $oldMR->targetProject, $oldMR->mriid, $newMR); /* Change gitlab user ID to zentao account. */ $gitlabUsers = $this->gitlab->getUserIdAccountPairs($oldMR->gitlabID); @@ -173,6 +174,7 @@ class mrModel extends model ->where('id')->eq($MRID) ->autoCheck() ->exec(); + $MR = $this->getByID($MRID); if(dao::isError()) return array('result' => 'fail', 'message' => dao::getError()); return array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => helper::createLink('mr', 'browse')); @@ -314,8 +316,8 @@ class mrModel extends model */ public function apiAcceptMR($gitlabID, $projectID, $MRID) { - $url = sprintf($this->gitlab->getApiRoot($gitlabID), "/projects/$projectID/merge_requests/$MRID"); - return json_decode(commonModel::http($url, $data, $options = array(CURLOPT_CUSTOMREQUEST => 'PUT'))); + $url = sprintf($this->gitlab->getApiRoot($gitlabID), "/projects/$projectID/merge_requests/$MRID/merge"); + return json_decode(commonModel::http($url, array(), $options = array(CURLOPT_CUSTOMREQUEST => 'PUT'))); } /** From 1632859fafb9aef5299a7c91d6471929a320ff09 Mon Sep 17 00:00:00 2001 From: dingguodong Date: Tue, 31 Aug 2021 11:01:29 +0800 Subject: [PATCH 06/12] * Update MR view and lang. --- module/mr/lang/zh-cn.php | 8 ++++++-- module/mr/view/view.html.php | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/module/mr/lang/zh-cn.php b/module/mr/lang/zh-cn.php index 0498e9afd2..9a83adbd96 100644 --- a/module/mr/lang/zh-cn.php +++ b/module/mr/lang/zh-cn.php @@ -30,7 +30,7 @@ $lang->mr->statusList['merged'] = '已合并'; $lang->mr->mergeStatusList = array(); $lang->mr->mergeStatusList['checking'] = '检查中'; $lang->mr->mergeStatusList['can_be_merged'] = '可合并'; -$lang->mr->mergeStatusList['cannot_be_merged'] = '不可合并'; +$lang->mr->mergeStatusList['cannot_be_merged'] = '不可自动合并'; $lang->mr->description = '描述'; $lang->mr->confirmDelete = '确认删除该合并请求吗?'; @@ -68,6 +68,10 @@ $lang->mr->hasConflicts = "存在合并冲突"; $lang->mr->hasNoConflict = "可以合并请求"; $lang->mr->mergeByManual = "此合并请求可以手动合并,请使用以下"; $lang->mr->commandLine = "合并命令"; +$lang->mr->acceptMR = "合并"; +$lang->mr->mergeFailed = "无法合并,请核对合并请求状态"; +$lang->mr->mergeSuccess = "已成功合并"; + /** * Merge Command Document. @@ -80,7 +84,7 @@ $lang->mr->commandLine = "合并命令"; * %s mr::target_branch */ $lang->mr->commandDocument = <<< EOD -
在本地检出、审核和合并
+
在本地检出、审核和手动合并

第 1 步. 获取并查看此合并请求的分支 diff --git a/module/mr/view/view.html.php b/module/mr/view/view.html.php index b5f7c68ca4..89cada7c09 100644 --- a/module/mr/view/view.html.php +++ b/module/mr/view/view.html.php @@ -29,6 +29,9 @@

mr->from . html::a($sourceProjectURL, $sourceProjectName . ":" . $MR->sourceBranch, "_blank", "class='btn btn-link btn-active-text' style='color: blue'") . $lang->mr->to . html::a($targetProjectURL, $targetProjectName . ":" . $MR->targetBranch, "_blank", "class='btn btn-link btn-active-text' style='color: blue'");?>
+ mr->status;?> + mr->statusList, $MR->status);?> +
head_pipeline->status)):?>
mr->pipeline}{$lang->mr->status}";?> @@ -51,8 +54,12 @@
+ state == 'opened'):?>
mr->commandDocument, $httpRepoURL, $MR->sourceBranch, $branchPath, $MR->targetBranch, $branchPath, $MR->targetBranch);?>
+ +
+ state == 'opened' and !$rawMR->has_conflicts) echo html::linkButton(' ' . $lang->mr->acceptMR, inlink( 'accept', "mr=$MR->id"), 'self', '', 'btn btn-wide btn-primary');?> From 33b287b4279b5fba6a02110b6c0be9f2d8082862 Mon Sep 17 00:00:00 2001 From: dingguodong Date: Tue, 31 Aug 2021 11:10:50 +0800 Subject: [PATCH 07/12] - Remove extra lines which should be deleted. --- module/mr/control.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/module/mr/control.php b/module/mr/control.php index 40198033cf..dd90d86d04 100644 --- a/module/mr/control.php +++ b/module/mr/control.php @@ -132,8 +132,6 @@ class mr extends control } /** -<<<<<<< HEAD -<<<<<<< HEAD * Crontab sync MR from GitLab API to Zentao database, default time 5 minutes to execute once. * * @access public From 3209790874beb0d2746b21a539031416670a8259 Mon Sep 17 00:00:00 2001 From: dingguodong Date: Wed, 1 Sep 2021 10:28:06 +0800 Subject: [PATCH 08/12] + Add apiGetProjectUsers function. --- module/gitlab/model.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/module/gitlab/model.php b/module/gitlab/model.php index 087fadabc5..1ebc19862a 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -470,6 +470,20 @@ class gitlabModel extends model return json_decode(commonModel::http($url)); } + /** + * Get project users. + * + * @param int $gitlabID + * @param int $projectID + * @access public + * @return void + */ + public function apiGetProjectUsers($gitlabID, $projectID) + { + $url = sprintf($this->getApiRoot($gitlabID), "/projects/$projectID/users"); + return json_decode(commonModel::http($url)); + } + /** * Get single branch by API. * From e6799383792bf059ae373c27f8321dee5d0303d2 Mon Sep 17 00:00:00 2001 From: dingguodong Date: Wed, 1 Sep 2021 10:29:03 +0800 Subject: [PATCH 09/12] * Update ajaxGetMRUserPairs function. --- module/gitlab/control.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/module/gitlab/control.php b/module/gitlab/control.php index 7646ce74b2..dd06f754eb 100644 --- a/module/gitlab/control.php +++ b/module/gitlab/control.php @@ -417,16 +417,25 @@ class gitlab extends control /** * AJAX: Get MR user pairs to select assignee_ids and reviewer_ids. + * Attention: The user must be a member of the GitLab project. * * @param int $gitlabID + * @param int $projectID * @access public * @return void */ - public function ajaxGetMRUserPairs($gitlabID) + public function ajaxGetMRUserPairs($gitlabID, $projectID) { if(!$gitlabID) return $this->send(array('message' => array())); - $users = $this->gitlab->getUserIdRealnamePairs($gitlabID); + $bindedUsers = $this->gitlab->getUserIdRealnamePairs($gitlabID); + $rawProjectUsers = $this->gitlab->apiGetProjectUsers($gitlabID, $projectID); + $users = array(); + foreach($rawProjectUsers as $rawProjectUser) + { + if(!empty($bindedUsers[$rawProjectUser->id])) $users[$rawProjectUser->id] = $bindedUsers[$rawProjectUser->id]; + } + $options = ""; foreach($users as $index => $user) { From 5ebad79a991cced602a1b80240e46fec42abc01d Mon Sep 17 00:00:00 2001 From: dingguodong Date: Wed, 1 Sep 2021 10:33:55 +0800 Subject: [PATCH 10/12] * Fix bug when getting gitlab projects list. --- module/gitlab/model.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/module/gitlab/model.php b/module/gitlab/model.php index 1ebc19862a..1a87c51110 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -448,9 +448,9 @@ class gitlabModel extends model $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")); + $results = json_decode(commonModel::http($host . "?private_token={$gitlab->token}&simple=true&page={$page}&per_page=100")); if(empty($results) or $page > 10) break; - $allResults = $allResults + $results; + $allResults = array_merge($allResults, $results); } return $allResults; From 8b17a74a1ccce29dc68b04da49043a6d276fc6a2 Mon Sep 17 00:00:00 2001 From: dingguodong Date: Wed, 1 Sep 2021 10:36:02 +0800 Subject: [PATCH 11/12] * Update fetching users list when creating a MR. --- module/mr/js/create.js | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/module/mr/js/create.js b/module/mr/js/create.js index 98583c829e..2194a15d88 100644 --- a/module/mr/js/create.js +++ b/module/mr/js/create.js @@ -11,19 +11,7 @@ $(function() $('#sourceProject').html('').append(response); $('#sourceProject').chosen().trigger("chosen:updated");; }); - - var assignee = $("#assignee").parents('td').find('select[name*=assignee]'); - var reviewer = $("#reviewer").parents('td').find('select[name*=reviewer]'); - usersUrl = createLink('gitlab', 'ajaxgetmruserpairs', "gitlabID=" + gitlabID); - $.get(usersUrl, function(response) - { - assignee.html('').append(response); - assignee.chosen().trigger("chosen:updated");; - reviewer.html('').append(response); - reviewer.chosen().trigger("chosen:updated");; - }); - - }); + }); $('#sourceProject,#targetProject').change(function() { @@ -48,4 +36,21 @@ $(function() $('#targetProject').chosen().trigger("chosen:updated");; }); }); + + $('#targetProject').change(function() + { + targetProject = $(this).val(); + var assignee = $("#assignee").parents('td').find('select[name*=assignee]'); + var reviewer = $("#reviewer").parents('td').find('select[name*=reviewer]'); + usersUrl = createLink('gitlab', 'ajaxgetmruserpairs', "gitlabID=" + gitlabID + "&projectID=" + targetProject); + $.get(usersUrl, function(response) + { + assignee.html('').append(response); + assignee.chosen().trigger("chosen:updated");; + reviewer.html('').append(response); + reviewer.chosen().trigger("chosen:updated");; + }); + }); + + }); From 5e5cbe2d2fafe6e1ab4861df4eebf54141129208 Mon Sep 17 00:00:00 2001 From: dingguodong Date: Wed, 1 Sep 2021 11:08:34 +0800 Subject: [PATCH 12/12] * Update and correct editing a MR. --- module/mr/control.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/module/mr/control.php b/module/mr/control.php index dd90d86d04..ba743b6ba6 100644 --- a/module/mr/control.php +++ b/module/mr/control.php @@ -67,12 +67,21 @@ class mr extends control $targetBranchList = array(); foreach($branchList as $branch) $targetBranchList[$branch] = $branch; + /* Fetch user list both in Zentao and current GitLab project. */ + $bindedUsers = $this->gitlab->getUserIdRealnamePairs($MR->gitlabID); + $rawProjectUsers = $this->gitlab->apiGetProjectUsers($MR->gitlabID, $MR->targetProject); + $users = array(); + foreach($rawProjectUsers as $rawProjectUser) + { + if(!empty($bindedUsers[$rawProjectUser->id])) $users[$rawProjectUser->id] = $bindedUsers[$rawProjectUser->id]; + } + $gitlabUsers = $this->gitlab->getUserAccountIdPairs($MR->gitlabID); $this->view->title = $this->lang->mr->edit; $this->view->MR = $MR; $this->view->targetBranchList = $targetBranchList; - $this->view->users = array("" => "") + $this->loadModel('gitlab')->getUserIdRealnamePairs($MR->gitlabID); + $this->view->users = array("" => "") + $users; $this->view->assignee = zget($gitlabUsers, $MR->assignee, ''); $this->view->reviewer = zget($gitlabUsers, $MR->reviewer, '');