From ddfe2479ac327e3cd9eceafbbc3dcbf32b9edd66 Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Mon, 6 Sep 2021 16:46:07 +0800 Subject: [PATCH] * Finish task #41976, adjust exec gitlab function. --- module/job/control.php | 40 +++---------------- module/job/model.php | 68 ++++++++++++++++++++++++--------- module/job/view/browse.html.php | 3 +- 3 files changed, 57 insertions(+), 54 deletions(-) diff --git a/module/job/control.php b/module/job/control.php index a44519e0cb..6b634e4686 100644 --- a/module/job/control.php +++ b/module/job/control.php @@ -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)); } } diff --git a/module/job/model.php b/module/job/model.php index 73afdf517b..0a41b7b99f 100644 --- a/module/job/model.php +++ b/module/job/model.php @@ -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; + } } diff --git a/module/job/view/browse.html.php b/module/job/view/browse.html.php index 7e0b15f941..8b4b068dc6 100644 --- a/module/job/view/browse.html.php +++ b/module/job/view/browse.html.php @@ -67,8 +67,7 @@ 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"), '', 'hiddenwin', "title='{$lang->job->delete}' class='btn'"); ?>