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) { diff --git a/module/gitlab/model.php b/module/gitlab/model.php index aa8cc892df..1a87c51110 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. * diff --git a/module/job/control.php b/module/job/control.php index 17d166f5d6..a44519e0cb 100644 --- a/module/job/control.php +++ b/module/job/control.php @@ -161,6 +161,8 @@ class job extends control $repo = $this->loadModel('repo')->getRepoByID($job->repo); $this->view->repo = $this->loadModel('repo')->getRepoByID($job->repo); + if($repo->SCM == 'Gitlab') $this->view->refList = $this->loadModel('gitlab')->getRefOptions($repo->gitlab, $repo->project); + $repoList = $this->repo->getList($this->projectID); $repoPairs = array(0 => '', $repo->id => $repo->name); $gitlabRepos = array(0 => ''); @@ -375,4 +377,19 @@ class job extends control $productName = $this->loadModel('product')->getByID($repo->product)->name; die(json_encode(array($productName => $repo->product))); } + + /** + * Ajax get reference list function. + * + * @param int $repoID + * @access public + * @return void + */ + public function ajaxGetRefList($repoID) + { + $repo = $this->loadModel('repo')->getRepoByID($repoID); + if($repo->SCM != 'Gitlab') $this->send(array('result' => 'fail')); + $refList = $this->loadModel('gitlab')->getRefOptions($repo->gitlab, $repo->project); + $this->send(array('result' => 'success', 'refList' => $refList)); + } } diff --git a/module/job/js/create.js b/module/job/js/create.js index cbe8c58a18..18b121ba44 100644 --- a/module/job/js/create.js +++ b/module/job/js/create.js @@ -129,19 +129,37 @@ $(document).ready(function() if($(this).val() == 'gitlab') { - $('#triggerType').find('[value=schedule]').remove(); $('tr.gitlabRepo').show(); $('tr.commonRepo').hide(); } else if($('#triggerType').find('[value=schedule]').size() == 0 ) { - $('#triggerType').append(scheduleOption); $('tr.gitlabRepo').hide(); $('tr.commonRepo').show(); } }); $('#engine').change(); + $('#gitlabRepo').change(function() + { + $('#reference option').remove(); + + var repoID = $(this).val(); + if(repoID > 0) + { + $.getJSON(createLink('job', 'ajaxGetRefList', "repoID=" + repoID), function(response) + { + if(response.result == 'success') + { + $.each(response.refList, function(reference, name) + { + $('#reference').append(""); + }); + } + $('#reference').trigger('chosen:updated'); + }); + } + }); $('#triggerType').change(); }); diff --git a/module/job/js/edit.js b/module/job/js/edit.js index 2b76c48b1c..f91c2aef1b 100644 --- a/module/job/js/edit.js +++ b/module/job/js/edit.js @@ -146,6 +146,27 @@ $(document).ready(function() } }); + $('#gitlabRepo').change(function() + { + $('#reference option').remove(); + + var repoID = $(this).val(); + if(repoID > 0) + { + $.getJSON(createLink('job', 'ajaxGetRefList', "repoID=" + repoID), function(response) + { + if(response.result == 'success') + { + $.each(response.refList, function(reference, name) + { + $('#reference').append(""); + }); + } + $('#reference').trigger('chosen:updated'); + }); + } + }); + $('#engine').change(); $('#jkServer').change(); diff --git a/module/job/model.php b/module/job/model.php index 1bbcf23110..73afdf517b 100644 --- a/module/job/model.php +++ b/module/job/model.php @@ -20,7 +20,14 @@ class jobModel extends model */ public function getByID($id) { - return $this->dao->select('*')->from(TABLE_JOB)->where('id')->eq($id)->fetch(); + $job = $this->dao->select('*')->from(TABLE_JOB)->where('id')->eq($id)->fetch(); + if(strtolower($job->engine) == 'gitlab') + { + $pipeline = json_decode($job->pipeline); + $job->project = $pipeline->project; + $job->reference = $pipeline->reference; + } + return $job; } /** @@ -117,7 +124,7 @@ class jobModel extends model ->setDefault('atDay', '') ->add('createdBy', $this->app->user->account) ->add('createdDate', helper::now()) - ->remove('repoType') + ->remove('repoType,reference') ->get(); if($job->engine == 'jenkins') @@ -128,10 +135,12 @@ class jobModel extends model if(strtolower($job->engine) == 'gitlab') { + $repo = $this->loadModel('repo')->getRepoByID($job->gitlabRepo); + $project = zget($repo, 'project'); + $job->repo = $job->gitlabRepo; - $repo = $this->loadModel('repo')->getRepoByID($job->repo); $job->server = (int)zget($repo, 'gitlab', 0); - $job->pipeline = zget($repo, 'project', ''); + $job->pipeline = json_encode(array('project' => $project, 'reference' => $this->post->reference)); } unset($job->jkServer); @@ -203,7 +212,7 @@ class jobModel extends model ->setIF($this->post->triggerType != 'tag', 'lastTag', '') ->add('editedBy', $this->app->user->account) ->add('editedDate', helper::now()) - ->remove('repoType') + ->remove('repoType,reference') ->get(); if($job->engine == 'jenkins') @@ -214,10 +223,12 @@ class jobModel extends model if(strtolower($job->engine) == 'gitlab') { + $repo = $this->loadModel('repo')->getRepoByID($job->gitlabRepo); + $project = zget($repo, 'project'); + $job->repo = $job->gitlabRepo; - $repo = $this->loadModel('repo')->getRepoByID($job->repo); $job->server = (int)zget($repo, 'gitlab', 0); - $job->pipeline = zget($repo, 'project', ''); + $job->pipeline = json_encode(array('project' => $project, 'reference' => $this->post->reference)); } unset($job->jkServer); @@ -252,6 +263,7 @@ class jobModel extends model if(!empty($paramName)) $customParam[$paramName] = $paramValue; } + unset($job->paramName); unset($job->paramValue); unset($job->custom); @@ -407,7 +419,8 @@ class jobModel extends model } elseif($job->engine == 'gitlab' and $reference) { - $pipeline = $this->loadModel('gitlab')->apiCreatePipeline($job->server, $job->pipeline, $reference); + list($gitlabProject, $gitlabReference) = json_decode($job->pipeline); + $pipeline = $this->loadModel('gitlab')->apiCreatePipeline($job->server, $gitlabProject, $gitlabReference); if(empty($pipeline->id)) { $compile->status = 'create_fail'; diff --git a/module/job/view/create.html.php b/module/job/view/create.html.php index e7e0090ab5..69714f9eaf 100644 --- a/module/job/view/create.html.php +++ b/module/job/view/create.html.php @@ -48,7 +48,8 @@ job->repo; ?> - + + job->product; ?> @@ -95,7 +96,7 @@
- job->pipeline; ?> + job->pipeline;?> ''), '', "class='form-control chosen'"); ?>
diff --git a/module/job/view/edit.html.php b/module/job/view/edit.html.php index 3d0894484b..9c06425b7a 100644 --- a/module/job/view/edit.html.php +++ b/module/job/view/edit.html.php @@ -43,7 +43,8 @@ job->repo; ?> - repo, "class='form-control'"); ?> + repo, "class='chosen form-control'");?> + reference, "class='chosen form-control'");?> job->product;?> diff --git a/module/mr/control.php b/module/mr/control.php index 5acb340fa0..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, ''); @@ -150,6 +159,21 @@ 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. * 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");; + }); + }); + + }); 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/model.php b/module/mr/model.php index 20f3b4bb7d..0579eff71d 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')); @@ -385,8 +387,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'))); } /** 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');?>