From b9d2a58b623cd3a5a5dd1bf30d16f4382a449db8 Mon Sep 17 00:00:00 2001 From: caoyanyi Date: Fri, 11 Oct 2024 13:55:30 +0800 Subject: [PATCH] * [task#129930,doing,1h] adjust job exec logic. --- module/job/config/form.php | 2 +- module/job/model.php | 88 +++++++++++++++++--------------------- module/job/tao.php | 2 +- 3 files changed, 42 insertions(+), 50 deletions(-) diff --git a/module/job/config/form.php b/module/job/config/form.php index 55a30cb04f..50a9dd0a3b 100644 --- a/module/job/config/form.php +++ b/module/job/config/form.php @@ -30,7 +30,7 @@ $config->job->form->edit['repo'] = array('type' => 'int', 'require $config->job->form->edit['reference'] = array('type' => 'string', 'required' => false, 'default' => ''); $config->job->form->edit['frame'] = array('type' => 'string', 'required' => false, 'default' => ''); $config->job->form->edit['triggerType'] = array('type' => 'array', 'required' => false, 'default' => array(), 'filter' => 'join'); -$config->job->form->edit['svnDir'] = array('type' => 'array', 'required' => false, 'default' => array()); +$config->job->form->edit['svnDir'] = array('type' => 'array', 'required' => false, 'default' => array(), 'filter' => 'join'); $config->job->form->edit['product'] = array('type' => 'int', 'required' => false, 'default' => 0); $config->job->form->edit['sonarqubeServer'] = array('type' => 'int', 'required' => false, 'default' => 0); $config->job->form->edit['projectKey'] = array('type' => 'string', 'required' => false, 'default' => ''); diff --git a/module/job/model.php b/module/job/model.php index 7ef8224506..08a3364d59 100644 --- a/module/job/model.php +++ b/module/job/model.php @@ -118,7 +118,7 @@ class jobModel extends model { return $this->dao->select('*')->from(TABLE_JOB) ->where('deleted')->eq('0') - ->andWhere('triggerType')->eq($triggerType) + ->andWhere('triggerType')->like('%' . $triggerType . '%') ->beginIF($repoIdList)->andWhere('repo')->in($repoIdList)->fi() ->fetchAll('id'); } @@ -132,26 +132,26 @@ class jobModel extends model */ public function getTriggerConfig(object $job): string { - $triggerType = zget($this->lang->job->triggerTypeList, $job->triggerType); - if($job->triggerType == 'tag') - { - if(empty($job->svnDir)) return $triggerType; + $triggerList = array(); + if(strpos($job->triggerType, 'tag') !== false) + { + $triggerType = $this->lang->job->triggerTypeList['tag']; + if(!empty($job->svnDir)) $triggerType = $this->lang->job->dirChange . "({$job->svnDir})"; - $triggerType = $this->lang->job->dirChange; - return "{$triggerType}({$job->svnDir})"; - } + $triggerList[] = $triggerType; + } - if($job->triggerType == 'commit') return "{$triggerType}({$job->comment})"; + if(strpos($job->triggerType, 'commit') !== false) $triggerList[] = "{$this->lang->job->triggerTypeList['commit']}({$job->comment})"; - if($job->triggerType == 'schedule') - { - $atDay = ''; - foreach(explode(',', $job->atDay) as $day) $atDay .= zget($this->lang->datepicker->dayNames, trim($day), '') . ','; - $atDay = trim($atDay, ','); - return "{$triggerType}({$atDay}, {$job->atTime})"; - } + if(strpos($job->triggerType, 'schedule') !== false) + { + $atDay = ''; + foreach(explode(',', $job->atDay) as $day) $atDay .= zget($this->lang->datepicker->dayNames, trim($day), '') . ','; + $atDay = trim($atDay, ','); + $triggerList[] = "{$this->lang->job->triggerTypeList['schedule']}({$atDay}, {$job->atTime})"; + } - return $this->lang->job->auto; + return implode('; ', $triggerList); } /** @@ -180,35 +180,21 @@ class jobModel extends model */ public function create(object $job): int|bool { - $repo = $this->loadModel('repo')->getByID($job->repo); - + $repo = $this->loadModel('repo')->getByID($job->repo); $result = $this->jobTao->getServerAndPipeline($job, $repo); if(!$result) return false; $result = $this->jobTao->checkIframe($job); if(!$result) return false; - if($job->triggerType == 'schedule') $job->atDay = empty($_POST['atDay']) ? '' : implode(',', $this->post->atDay); - - $this->jobTao->getSvnDir($job, $repo); - - $result = $this->jobTao->getCustomParam($job); - if(!$result) return false; - $this->dao->insert(TABLE_JOB)->data($job) ->batchCheck($this->config->job->create->requiredFields, 'notempty') - ->batchCheckIF($job->triggerType === 'schedule' and $job->atDay !== '0', "atDay", 'notempty') - ->batchCheckIF($job->triggerType === 'schedule', "atTime", 'notempty') - ->batchCheckIF($job->triggerType === 'commit', "comment", 'notempty') - ->batchCheckIF(($repo->SCM == 'Subversion' and $job->triggerType == 'tag'), "svnDir", 'notempty') ->batchCheckIF($job->frame === 'sonarqube', "sonarqubeServer,projectKey", 'notempty') ->autoCheck() ->exec(); if(dao::isError()) return false; - $id = $this->dao->lastInsertId(); - if(strtolower($job->engine) == 'jenkins') $this->initJob($id, $job); - return $id; + return $this->dao->lastInsertId(); } /** @@ -264,8 +250,9 @@ class jobModel extends model */ public function initJob(int $id, object $job): bool { - if(empty($id)) return false; - if($job->triggerType == 'schedule' and strpos($job->atDay, date('w')) !== false) + if(empty($id) || empty($job->triggerType)) return false; + + if(strpos($job->triggerType, 'schedule') !== false && strpos($job->atDay, date('w')) !== false) { $compiles = $this->dao->select('*')->from(TABLE_COMPILE)->where('job')->eq($id)->andWhere('LEFT(createdDate, 10)')->eq(date('Y-m-d'))->fetchAll(); foreach($compiles as $compile) @@ -276,7 +263,7 @@ class jobModel extends model $this->loadModel('compile')->createByJob($id, $job->atTime, 'atTime'); } - if($job->triggerType == 'tag') + if(strpos($job->triggerType, 'tag') !== false) { $repo = $this->loadModel('repo')->getByID($job->repo); if(!$repo) return false; @@ -294,27 +281,33 @@ class jobModel extends model * * @param int $id * @param array $extraParam + * @param string $triggerType commit|tag|schedule * @access public * @return object|false */ - public function exec(int $id, array $extraParam = array()): object|false + public function exec(int $id, array $extraParam = array(), string $triggerType = ''): object|false { $job = $this->dao->select('t1.id,t1.name,t1.product,t1.repo,t1.server,t1.pipeline,t1.triggerType,t1.atTime,t1.customParam,t1.engine,t1.svnDir,t2.name as jenkinsName,t2.url,t2.account,t2.token,t2.password') ->from(TABLE_JOB)->alias('t1') ->leftJoin(TABLE_PIPELINE)->alias('t2')->on('t1.server=t2.id') ->where('t1.id')->eq($id) ->fetch(); - if(!$job) return false; $repo = $this->loadModel('repo')->getByID($job->repo); if(!$repo) return false; - if($job->triggerType == 'schedule') + $method = 'exec' . ucfirst($job->engine) . 'Pipeline'; + if(!method_exists($this, $method)) return false; + + if(in_array($triggerType, array('', 'schedule')) && strpos($job->triggerType, 'schedule') !== false) { $compileID = $this->loadModel('compile')->createByJob($job->id, $job->atTime, 'atTime'); + $compile = $this->$method($job, $repo, $compileID, $extraParam); + $this->dao->update(TABLE_COMPILE)->data($compile)->where('id')->eq($compileID)->exec(); } - elseif($job->triggerType == 'tag') + + if(in_array($triggerType, array('', 'tag')) && strpos($job->triggerType, 'tag') !== false) { $job->lastTag = $this->getLastTagByRepo($repo, $job); @@ -326,18 +319,17 @@ class jobModel extends model } $compileID = $this->loadModel('compile')->createByJob($job->id, $tag, 'tag'); + $compile = $this->$method($job, $repo, $compileID, $extraParam); + $this->dao->update(TABLE_COMPILE)->data($compile)->where('id')->eq($compileID)->exec(); } - else + + if(in_array($triggerType, array('', 'commit')) && strpos($job->triggerType, 'commit') !== false) { $compileID = $this->loadModel('compile')->createByJob($job->id); + $compile = $this->$method($job, $repo, $compileID, $extraParam); + $this->dao->update(TABLE_COMPILE)->data($compile)->where('id')->eq($compileID)->exec(); } - $method = 'exec' . ucfirst($job->engine) . 'Pipeline'; - if(!method_exists($this, $method)) return false; - $compile = $this->$method($job, $repo, $compileID, $extraParam); - - $this->dao->update(TABLE_COMPILE)->data($compile)->where('id')->eq($compileID)->exec(); - $this->dao->update(TABLE_JOB) ->set('lastExec')->eq(helper::now()) ->set('lastStatus')->eq($compile->status) @@ -364,7 +356,7 @@ class jobModel extends model $pipeline = new stdclass(); $pipeline->PARAM_TAG = ''; $pipeline->ZENTAO_DATA = "compile={$compileID}"; - if($job->triggerType == 'tag') $pipeline->PARAM_TAG = $job->lastTag; + if(strpos($job->triggerType, 'tag') !== false) $pipeline->PARAM_TAG = $job->lastTag; /* Add custom parameters to the data. */ foreach(json_decode($job->customParam) as $paramName => $paramValue) diff --git a/module/job/tao.php b/module/job/tao.php index 31e57a8f5e..4e3b1274b8 100644 --- a/module/job/tao.php +++ b/module/job/tao.php @@ -106,7 +106,7 @@ class jobTao extends jobModel protected function getSvnDir(object &$job, object $repo): void { $job->svnDir = ''; - if($job->triggerType == 'tag' and $repo->SCM == 'Subversion') + if(strpos($job->triggerType, 'tag') !== false && $repo->SCM == 'Subversion') { $job->svnDir = array_pop($_POST['svnDir']); if($job->svnDir == '/' and $_POST['svnDir']) $job->svnDir = array_pop($_POST['svnDir']);