* Finish task #41976, adjust exec gitlab function.

This commit is contained in:
Guan Xiying
2021-09-06 16:46:07 +08:00
parent 81795244b3
commit ddfe2479ac
3 changed files with 57 additions and 54 deletions

View File

@@ -161,7 +161,7 @@ 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);
if($repo->SCM == 'Gitlab') $this->view->refList = $this->loadModel('gitlab')->getReferenceOptions($repo->gitlab, $repo->project);
$repoList = $this->repo->getList($this->projectID);
$repoPairs = array(0 => '', $repo->id => $repo->name);
@@ -301,40 +301,12 @@ class job extends control
* @access public
* @return void
*/
public function exec($id, $showForm = 'no')
public function exec($id)
{
if($showForm == 'yes' or $_POST)
$job = $this->job->getByID($id);
if(strtolower($job->engine) == 'gitlab')
{
$job = $this->job->getByID($id);
$refList = $this->loadModel('gitlab')->getRefOptions($job->server, $job->pipeline);
if($_POST)
{
$reference = new stdclass;
$reference->ref = explode("::", $refList[$this->post->ref])[1];
$variables = array();
foreach($this->post->keys as $key => $field)
{
$variable = array();
if(!trim($this->post->values[$key])) continue;
$variable['key'] = $field;
$variable['value'] = $this->post->values[$key];
$variable['variable_type'] = "env_var";
$variables[] = $variable;
}
$reference->variables = $variables;
$compile = $this->job->exec($id, json_encode($reference));
return $this->send(array('result' => 'success', 'message' => $this->lang->job->execSuccess, 'locate' => $this->createLink('compile', 'logs', "buildID={$compile->id}")));
}
$this->view->title = $this->lang->job->runPipeline;
$this->view->refList = $refList;
$this->view->job = $job;
$this->view->pipelineTips = $this->lang->job->pipelineTips;
return $this->display();
if(!isset($job->reference) or !$job->reference) $this->locate(inlink('edit', "id=$id"));
}
$compile = $this->job->exec($id);
@@ -389,7 +361,7 @@ class job extends control
{
$repo = $this->loadModel('repo')->getRepoByID($repoID);
if($repo->SCM != 'Gitlab') $this->send(array('result' => 'fail'));
$refList = $this->loadModel('gitlab')->getRefOptions($repo->gitlab, $repo->project);
$refList = $this->loadModel('gitlab')->getReferenceOptions($repo->gitlab, $repo->project);
$this->send(array('result' => 'success', 'refList' => $refList));
}
}

View File

@@ -338,7 +338,7 @@ class jobModel extends model
* @access public
* @return string|bool
*/
public function exec($id, $reference = null)
public function exec($id)
{
$job = $this->dao->select('t1.id,t1.name,t1.product,t1.repo,t1.server,t1.pipeline,t1.triggerType,t1.atTime,t1.customParam,t1.engine,t2.name as jenkinsName,t2.url,t2.account,t2.token,t2.password')
->from(TABLE_JOB)->alias('t1')
@@ -408,33 +408,65 @@ class jobModel extends model
$data->$paramName = $paramValue;
}
$compile = new stdclass();
$compile->id = $compileID;
if($job->engine == 'jenkins')
{
$url = $this->loadModel('compile')->getBuildUrl($job);
$compile = new stdclass();
$compile->id = $compileID;
$compile->queue = $this->loadModel('ci')->sendRequest($url->url, $data, $url->userPWD);
$compile->status = $compile->queue ? 'created' : 'create_fail';
}
elseif($job->engine == 'gitlab' and $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';
}
else
{
$compile->queue = $pipeline->id;
$compile->status = zget($pipeline, 'status', 'create_fail');
}
}
if($job->engine == 'gitlab') $compile = $this->execGitlabPipeline($job, $compileID);
$this->dao->update(TABLE_COMPILE)->data($compile)->where('id')->eq($compileID)->exec();
$this->dao->update(TABLE_JOB)->set('lastExec')->eq($now)->set('lastStatus')->eq($compile->status)->where('id')->eq($job->id)->exec();
return $compile;
}
/**
* Exec gitlab pipeline.
*
* @param int $job
* @param int $compileID
* @access public
* @return void
*/
public function execGitlabPipeline($job, $compileID)
{
list($gitlabProject, $gitlabReference) = json_decode($job->pipeline);
$pipelineParams = new stdclass;
$pipelineParams->ref = $gitlabReference;
$customParams = json_decode($job->customParam);
$variables = array();
foreach($customParams as $paramName => $paramValue)
{
$variable = array();
$variable['key'] = $paramName;
$variable['value'] = $paramValue;
$variable['variable_type'] = "env_var";
$variables[] = $variable;
}
$pipelineParams->variables = $variables;
$compile = new stdclass;
$compile->id = $compileID;
$pipeline = $this->loadModel('gitlab')->apiCreatePipeline($job->server, $gitlabProject, $pipelineParams);
if(empty($pipeline->id)) $compile->status = 'create_fail';
if(!empty($pipeline->id))
{
$compile->queue = $pipeline->id;
$compile->status = zget($pipeline, 'status', 'create_fail');
}
return $compile;
}
}

View File

@@ -67,8 +67,7 @@
<?php
common::printIcon('compile', 'browse', "jobID=$id", '', 'list', 'history');
common::printIcon('job', 'edit', "jobID=$id", '', 'list', 'edit');
if(strtolower($job->engine) == 'jenkins') common::printIcon('job', 'exec', "jobID=$id", '', 'list', 'play', 'hiddenwin');
if(strtolower($job->engine) == 'gitlab') common::printIcon('job', 'exec', "jobID=$id&showForm=yes", '', 'list', 'play', '', '', false, "data-toggle='modal' data-type='ajax'");
common::printIcon('job', 'exec', "jobID=$id", '', 'list', 'play', 'hiddenwin');
if(common::hasPriv('job', 'delete')) echo html::a($this->createLink('job', 'delete', "jobID=$id"), '<i class="icon-trash"></i>', 'hiddenwin', "title='{$lang->job->delete}' class='btn'");
?>
</td>