diff --git a/config/filter.php b/config/filter.php index 59fe2874da..6d968118ea 100644 --- a/config/filter.php +++ b/config/filter.php @@ -57,6 +57,7 @@ $filter->git = new stdclass(); $filter->svn = new stdclass(); $filter->search = new stdclass(); $filter->gitlab = new stdclass(); +$filter->ci = new stdclass(); $filter->block->default = new stdclass(); $filter->block->main = new stdclass(); @@ -133,6 +134,7 @@ $filter->repo->ajaxsynccommit = new stdclass(); $filter->search->index = new stdclass(); $filter->gitlab->webhook = new stdclass(); $filter->gitlab->importissue = new stdclass(); +$filter->ci->checkCompileStatus = new stdclass(); $filter->bug->batchcreate->cookie['preBranch'] = 'int'; $filter->bug->browse->cookie['bugModule'] = 'int'; @@ -324,3 +326,4 @@ $filter->gitlab->importissue->get['product'] = 'string'; $filter->gitlab->importissue->get['project'] = 'int'; $filter->gitlab->importissue->get['repo'] = 'int'; +$filter->ci->checkCompileStatus->get['gitlabOnly'] = 'string'; diff --git a/module/ci/control.php b/module/ci/control.php index 1ead086056..0f7f12e1be 100644 --- a/module/ci/control.php +++ b/module/ci/control.php @@ -59,21 +59,22 @@ class ci extends control } /** - * Send a request to jenkins to check build status. + * Send a request to jenkins or gitlab to check build status. * + * @param string $gitlabOnly * @access public - * @return void + * @return string */ - public function checkCompileStatus() + public function checkCompileStatus($gitlabOnly = 'no') { - $this->ci->checkCompileStatus(); + $this->ci->checkCompileStatus($gitlabOnly); if(dao::isError()) { echo json_encode(dao::getError()); } else { - echo 'success'; + echo $gitlabOnly == 'yes' ? 'gitlab success' : 'success'; } } diff --git a/module/ci/model.php b/module/ci/model.php index 19b2a7dce1..c66909f513 100644 --- a/module/ci/model.php +++ b/module/ci/model.php @@ -26,7 +26,7 @@ class ciModel extends model * @access public * @return void */ - public function checkCompileStatus() + public function checkCompileStatus($gitlabOnly = 'no') { $compiles = $this->dao->select('compile.*, job.engine,job.pipeline, pipeline.name as jenkinsName,job.server,pipeline.url,pipeline.account,pipeline.token,pipeline.password') ->from(TABLE_COMPILE)->alias('compile') @@ -39,7 +39,14 @@ class ciModel extends model ->andWhere('compile.createdDate')->gt(date(DT_DATETIME1, strtotime("-1 day"))) ->fetchAll(); - foreach($compiles as $compile) $this->syncCompileStatus($compile); + if($gitlabOnly == 'yes') + { + foreach($compiles as $compile) if($compile->engine == 'gitlab') $this->syncCompileStatus($compile); + } + elseif($gitlabOnly == 'no') + { + foreach($compiles as $compile) $this->syncCompileStatus($compile); + } } /** @@ -57,7 +64,7 @@ class ciModel extends model return false; } - if($compile->engine == 'gitlab') return $this->syncGitlabTaskStatus($compile); + if($compile->engine == 'gitlab') $this->syncGitlabTaskStatus($compile); $jenkinsServer = $compile->url; $jenkinsUser = $compile->account; $jenkinsPassword = $compile->token ? $compile->token : base64_decode($compile->password); @@ -66,7 +73,7 @@ class ciModel extends model $response = common::http($queueUrl, '', array(CURLOPT_USERPWD => $userPWD)); - $this->dao->update(TABLE_COMPILE)->set('times = times + 1')->where('id')->eq($compile->id)->exec(); + if($compile->engine != 'gitlab') $this->dao->update(TABLE_COMPILE)->set('times = times + 1')->where('id')->eq($compile->id)->exec(); if(strripos($response, "404") > -1) { $infoUrl = sprintf("%s/job/%s/api/xml?tree=builds[id,number,result,queueId]&xpath=//build[queueId=%s]", $jenkinsServer, $compile->pipeline, $compile->queue); @@ -118,15 +125,23 @@ class ciModel extends model * * @param int $compile * @access public - * @return bool + * @return void */ public function syncGitlabTaskStatus($compile) { $now = helper::now(); $pipeline = $this->loadModel('gitlab')->apiGetSinglePipeline($compile->server, $compile->pipeline, $compile->queue); - $this->dao->update(TABLE_COMPILE)->set('status')->eq($pipeline->status)->set('updateDate')->eq($now)->where('id')->eq($compile->id)->exec(); + $jobs = $this->loadModel('gitlab')->apiGetJobs($compile->server, $compile->pipeline, $compile->queue); + + $log = ""; + foreach($jobs as $job) + { + if(empty($job->duration) or $job->duration == '') $job->duration = '-'; + $log .= "Name: $job->name, Stage: $job->stage, Status: $job->status, Duration: $job->duration \r\n Web URL: $job->web_url \r\n"; + $log .= $this->loadModel('gitlab')->apiGetJobLog($compile->server, $compile->pipeline, $job->id) . "\r\n\r\n\r\n"; + } + $this->dao->update(TABLE_COMPILE)->set('status')->eq($pipeline->status)->set('updateDate')->eq($now)->set('logs')->eq($log)->where('id')->eq($compile->id)->exec(); $this->dao->update(TABLE_JOB)->set('lastExec')->eq($now)->set('lastStatus')->eq($pipeline->status)->where('id')->eq($compile->job)->exec(); - return !dao::isError(); } /** diff --git a/module/compile/control.php b/module/compile/control.php index 8d123c97be..d40e36798b 100644 --- a/module/compile/control.php +++ b/module/compile/control.php @@ -64,8 +64,11 @@ class compile extends control public function logs($buildID) { $build = $this->compile->getByID($buildID); + $job = $this->loadModel('job')->getByID($build->job); + $this->view->logs = str_replace("\r\n","
", $build->logs); $this->view->build = $build; + $this->view->job = $job; $this->view->title = $this->lang->ci->job . $this->lang->colon . $this->lang->compile->logs; $this->view->position[] = html::a($this->createLink('job', 'browse'), $this->lang->ci->job); diff --git a/module/compile/lang/zh-cn.php b/module/compile/lang/zh-cn.php index 702cee9896..462d23b881 100644 --- a/module/compile/lang/zh-cn.php +++ b/module/compile/lang/zh-cn.php @@ -9,6 +9,7 @@ $lang->compile->buildType = '构建引擎'; $lang->compile->status = '构建状态'; $lang->compile->time = '构建时间'; $lang->compile->result = '构建结果'; +$lang->compile->refresh = '刷新'; $lang->compile->statusList['success'] = '成功'; $lang->compile->statusList['failure'] = '失败'; diff --git a/module/compile/view/logs.html.php b/module/compile/view/logs.html.php index b1bbdacc49..8f4c132b41 100644 --- a/module/compile/view/logs.html.php +++ b/module/compile/view/logs.html.php @@ -18,6 +18,7 @@
+ engine == 'gitlab') echo html::a(helper::createLink('ci', "checkCompileStatus", "gitlabOnly=yes"), " ". $lang->compile->refresh, '', "class='btn btn-secondary'");?> job"), " ". $lang->goback, '', "class='btn btn-secondary'");?>
diff --git a/module/gitlab/model.php b/module/gitlab/model.php index 7c13202a6f..bd7a6bb336 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -619,7 +619,7 @@ class gitlabModel extends model { if(!isset($object->id)) $object->id = $objectID; - $issue = $this->loadModel('gitlab')->parseObjectToIssue($gitlabID, $projectID, $objectType, $object); + $issue = $this->parseObjectToIssue($gitlabID, $projectID, $objectType, $object); $label = $this->createZentaoObjectLabel($gitlabID, $projectID, $objectType, $objectID); if(isset($label->name)) $issue->labels = $label->name; @@ -695,7 +695,7 @@ class gitlabModel extends model */ public function apiCreatePipeline($gitlabID, $projectID, $reference) { - $url = sprintf($this->loadModel('gitlab')->getApiRoot($gitlabID), "/projects/{$projectID}/pipeline"); + $url = sprintf($this->getApiRoot($gitlabID), "/projects/{$projectID}/pipeline"); return json_decode(commonModel::http($url, $reference)); } @@ -711,7 +711,7 @@ class gitlabModel extends model */ public function apiGetSinglePipeline($gitlabID, $projectID, $pipelineID) { - $url = sprintf($this->loadModel('gitlab')->getApiRoot($gitlabID), "/projects/{$projectID}/pipelines/{$pipelineID}"); + $url = sprintf($this->getApiRoot($gitlabID), "/projects/{$projectID}/pipelines/{$pipelineID}"); return json_decode(commonModel::http($url)); } @@ -726,7 +726,7 @@ class gitlabModel extends model */ public function apiGetJobs($gitlabID, $projectID, $pipelineID) { - $url = sprintf($this->loadModel('gitlab')->getApiRoot($gitlabID), "/projects/{$projectID}/pipelines/{$pipelineID}/jobs"); + $url = sprintf($this->getApiRoot($gitlabID), "/projects/{$projectID}/pipelines/{$pipelineID}/jobs"); return json_decode(commonModel::http($url)); } @@ -741,7 +741,7 @@ class gitlabModel extends model */ public function apiGetSingleJob($gitlabID, $projectID, $jobID) { - $url = sprintf($this->loadModel('gitlab')->getApiRoot($gitlabID), "/projects/{$projectID}/jobs/{$jobID}"); + $url = sprintf($this->getApiRoot($gitlabID), "/projects/{$projectID}/jobs/{$jobID}"); return json_decode(commonModel::http($url)); } @@ -756,7 +756,7 @@ class gitlabModel extends model */ public function apiGetJobLog($gitlabID, $projectID, $jobID) { - $url = sprintf($this->loadModel('gitlab')->getApiRoot($gitlabID), "/projects/{$projectID}/jobs/{$jobID}/trace"); + $url = sprintf($this->getApiRoot($gitlabID), "/projects/{$projectID}/jobs/{$jobID}/trace"); return json_decode(commonModel::http($url)); }