Merge branch 'jihuMR' of https://gitlab.zcorp.cc/easycorp/zentaopms into jihuMR

This commit is contained in:
lichengjun
2021-09-01 07:03:04 +00:00
13 changed files with 175 additions and 39 deletions
+11 -2
View File
@@ -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 = "<option value=''></option>";
foreach($users as $index => $user)
{
+14
View File
@@ -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.
*
+17
View File
@@ -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));
}
}
+20 -2
View File
@@ -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("<option value='" + reference + "'>" + name + "</option>");
});
}
$('#reference').trigger('chosen:updated');
});
}
});
$('#triggerType').change();
});
+21
View File
@@ -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("<option value='" + reference + "'>" + name + "</option>");
});
}
$('#reference').trigger('chosen:updated');
});
}
});
$('#engine').change();
$('#jkServer').change();
+21 -8
View File
@@ -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';
+3 -2
View File
@@ -48,7 +48,8 @@
</tr>
<tr class='gitlabRepo hide'>
<th><?php echo $lang->job->repo; ?></th>
<td><?php echo html::select('gitlabRepo', $gitlabRepos, '', "class='form-control'"); ?></td>
<td> <?php echo html::select('gitlabRepo', $gitlabRepos, '', "class='chosen form-control'");?> </td>
<td> <?php echo html::select('reference', array(), '', "class='chosen form-control'");?> </td>
</tr>
<tr>
<th><?php echo $lang->job->product; ?></th>
@@ -95,7 +96,7 @@
<div class='table-col'><?php echo html::select('jkServer', $jenkinsServerList, '', "class='form-control chosen'"); ?></div>
<div class='table-col'>
<div class='input-group'>
<span class='input-group-addon'><?php echo $lang->job->pipeline; ?></span>
<span class='input-group-addon'><?php echo $lang->job->pipeline;?></span>
<?php echo html::select('jkTask', array('' => ''), '', "class='form-control chosen'"); ?>
</div>
</div>
+2 -1
View File
@@ -43,7 +43,8 @@
</tr>
<tr class='gitlabRepo hide'>
<th><?php echo $lang->job->repo; ?></th>
<td><?php echo html::select('gitlabRepo', $gitlabRepos, $job->repo, "class='form-control'"); ?></td>
<td> <?php echo html::select('gitlabRepo', $gitlabRepos, $job->repo, "class='chosen form-control'");?> </td>
<td> <?php echo html::select('reference', $refList, $job->reference, "class='chosen form-control'");?> </td>
</tr>
<tr>
<th><?php echo $lang->job->product;?></th>
+25 -1
View File
@@ -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.
*
+18 -13
View File
@@ -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");;
});
});
});
+6 -2
View File
@@ -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
<div class='detail-title'>在本地检出、审核和合并</div>
<div class='detail-title'>在本地检出、审核和手动合并</div>
<div class='detail-content'>
<p>
第 1 步. 获取并查看此合并请求的分支
+10 -8
View File
@@ -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')));
}
/**
+7
View File
@@ -29,6 +29,9 @@
<div><?php echo $lang->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'");?></div>
</div>
<div class="detail-content article-content">
<strong><?php echo $lang->mr->status;?></strong>
<?php echo zget($lang->mr->statusList, $MR->status);?>
<br>
<?php if(isset($rawMR->head_pipeline->status)):?>
<div>
<strong><?php echo "{$lang->mr->pipeline}{$lang->mr->status}";?></strong>
@@ -51,8 +54,12 @@
</div>
</div>
</div>
<?php if($rawMR->state == 'opened'):?>
<div class="cell"><?php echo sprintf($lang->mr->commandDocument, $httpRepoURL, $MR->sourceBranch, $branchPath, $MR->targetBranch, $branchPath, $MR->targetBranch);?></div>
<?php endif;?>
</div>
</div>
<br>
<?php if($rawMR->state == 'opened' and !$rawMR->has_conflicts) echo html::linkButton('<i class="icon icon-checked"></i> ' . $lang->mr->acceptMR, inlink( 'accept', "mr=$MR->id"), 'self', '', 'btn btn-wide btn-primary');?>
<?php endif;?>
<?php include '../../common/view/footer.html.php';?>