* Finish task #64269.

This commit is contained in:
liyuchun
2022-08-10 06:54:20 +00:00
parent 835f066ea5
commit 273698d8e6
12 changed files with 241 additions and 21 deletions
+2
View File
@@ -0,0 +1,2 @@
ALTER TABLE `zt_job` ADD `lastSyncDate` datetime DEFAULT NULL AFTER `lastTag`;
INSERT INTO `zt_cron` (`m`, `h`, `dom`, `mon`, `dow`, `command`, `remark`, `type`, `buildin`, `status`, `lastTime`) VALUES ('*/5', '*', '*', '*', '*', 'moduleName=compile&methodName=syncCompile', '定时同步构建记录', 'zentao', 1, 'normal', '0000-00-00 00:00:00');
+2 -1
View File
@@ -182,11 +182,12 @@ class ciModel extends model
$data = new stdclass;
$data->status = $pipeline->status;
$data->updateDate = $now;
$data->logs = '';
foreach($jobs as $job)
{
if(empty($job->duration) or $job->duration == '') $job->duration = '-';
$data->logs = "<font style='font-weight:bold'>&gt;&gt;&gt; Job: $job->name, Stage: $job->stage, Status: $job->status, Duration: $job->duration Sec\r\n </font>";
$data->logs .= "<font style='font-weight:bold'>&gt;&gt;&gt; Job: $job->name, Stage: $job->stage, Status: $job->status, Duration: $job->duration Sec\r\n </font>";
$data->logs .= "Job URL: <a href=\"$job->web_url\" target='_blank'>$job->web_url</a> \r\n";
$data->logs .= $this->transformAnsiToHtml($this->gitlab->apiGetJobLog($compile->server, $compile->project, $job->id));
}
+22
View File
@@ -47,6 +47,8 @@ class compile extends control
$this->view->job = $job;
}
$this->compile->syncCompile($repoID, $jobID);
$this->app->loadLang('job');
$this->loadModel('ci')->setMenu($repoID);
@@ -77,6 +79,8 @@ class compile extends control
$build = $this->compile->getByID($buildID);
$job = $this->loadModel('job')->getByID($build->job);
if(empty($build->logs) and !in_array($build->status, array('created', 'pending'))) $build->logs = $this->compile->getLogs($job, $build);
$this->view->logs = str_replace("\r\n","<br />", $build->logs);
$this->view->build = $build;
$this->view->job = $job;
@@ -87,5 +91,23 @@ class compile extends control
$this->view->position[] = $this->lang->compile->logs;
$this->display();
}
/**
* Sync compiles.
*
* @access public
* @return bool
*/
public function syncCompile()
{
$this->compile->syncCompile();
if(dao::isError())
{
echo json_encode(dao::getError());
return true;
}
echo 'success';
}
}
+4 -3
View File
@@ -1,7 +1,8 @@
<?php
$lang->compile->common = 'Compile';
$lang->compile->browse = 'History';
$lang->compile->logs = 'Logs';
$lang->compile->common = 'Compile';
$lang->compile->browse = 'History';
$lang->compile->logs = 'Logs';
$lang->compile->syncCompile = 'Interface: Sync Compiles.';
$lang->compile->id = 'ID';
$lang->compile->name = 'Name';
+4 -3
View File
@@ -1,7 +1,8 @@
<?php
$lang->compile->common = 'Compile';
$lang->compile->browse = 'History';
$lang->compile->logs = 'Log';
$lang->compile->common = 'Compile';
$lang->compile->browse = 'History';
$lang->compile->logs = 'Log';
$lang->compile->syncCompile = 'Interface: Sync Compiles.';
$lang->compile->id = 'ID';
$lang->compile->name = 'Name';
+4 -3
View File
@@ -1,7 +1,8 @@
<?php
$lang->compile->common = 'Build';
$lang->compile->browse = 'Historique Builds';
$lang->compile->logs = 'Logs des Builds';
$lang->compile->common = 'Build';
$lang->compile->browse = 'Historique Builds';
$lang->compile->logs = 'Logs des Builds';
$lang->compile->syncCompile = 'Interface: Sync Compiles.';
$lang->compile->id = 'ID';
$lang->compile->name = 'Nom';
+4 -3
View File
@@ -1,7 +1,8 @@
<?php
$lang->compile->common = 'Compile';
$lang->compile->browse = 'Lịch sử';
$lang->compile->logs = 'Nhật ký';
$lang->compile->common = 'Compile';
$lang->compile->browse = 'Lịch sử';
$lang->compile->logs = 'Nhật ký';
$lang->compile->syncCompile = 'Interface: Sync Compiles.';
$lang->compile->id = 'ID';
$lang->compile->name = 'Tên';
+4 -3
View File
@@ -1,7 +1,8 @@
<?php
$lang->compile->common = '构建';
$lang->compile->browse = '构建历史';
$lang->compile->logs = '构建日志';
$lang->compile->common = '构建';
$lang->compile->browse = '构建历史';
$lang->compile->logs = '构建日志';
$lang->compile->syncCompile = '接口:同步构建记录';
$lang->compile->id = 'ID';
$lang->compile->name = '构建名称';
+188
View File
@@ -134,6 +134,59 @@ class compileModel extends model
return $url;
}
/**
* Get compile logs.
*
* @param object $job
* @param object $compile
* @access public
* @return string
*/
public function getLogs($job, $compile)
{
$logs = '';
if($job->engine == 'jenkins')
{
$jenkins = $this->loadModel('jenkins')->getByID($job->server);
$jenkinsUser = $jenkins->account;
$jenkinsPassword = $jenkins->token ? $jenkins->token : base64_decode($jenkins->password);
$userPWD = "$jenkinsUser:$jenkinsPassword";
$infoUrl = sprintf('%s/job/%s/api/xml?tree=builds[id,number,queueId]&xpath=//build[queueId=%s]', $jenkins->url, $job->pipeline, $compile->queue);
$response = common::http($infoUrl, '', array(CURLOPT_USERPWD => $userPWD));
if($response)
{
$buildInfo = simplexml_load_string($response);
$buildNumber = $buildInfo->number;
if(empty($buildNumber)) return '';
$logUrl = sprintf('%s/job/%s/%s/consoleText', $jenkins->url, $job->pipeline, $buildNumber);
$logs = common::http($logUrl, '', array(CURLOPT_USERPWD => $userPWD));
$this->dao->update(TABLE_COMPILE)->set('logs')->eq($logs)->where('id')->eq($compile->id)->exec();
}
}
else
{
/* Get jobs by pipeline. */
$pipeline = json_decode($job->pipeline);
$projectID = isset($pipeline->project) ? $pipeline->project : '';
$jobs = $this->loadModel('gitlab')->apiGetJobs($job->server, $projectID, $compile->queue);
$this->loadModel('ci');
foreach($jobs as $gitlabJob)
{
if(empty($gitlabJob->duration) or $gitlabJob->duration == '') $gitlabJob->duration = '-';
$logs .= "<font style='font-weight:bold'>&gt;&gt;&gt; Job: $gitlabJob->name, Stage: $gitlabJob->stage, Status: $gitlabJob->status, Duration: $gitlabJob->duration Sec\r\n </font>";
$logs .= "Job URL: <a href=\"$gitlabJob->web_url\" target='_blank'>$gitlabJob->web_url</a> \r\n";
$logs .= $this->ci->transformAnsiToHtml($this->gitlab->apiGetJobLog($job->server, $projectID, $gitlabJob->id));
}
}
if(!empty($logs)) $this->dao->update(TABLE_COMPILE)->set('logs')->eq($logs)->where('id')->eq($compile->id)->exec();
return $logs;
}
/**
* Save build by job
*
@@ -157,6 +210,141 @@ class compileModel extends model
$this->dao->insert(TABLE_COMPILE)->data($build)->exec();
}
/**
* Sync compiles.
*
* @param int $repoID
* @param int $repoID
* @access public
* @return void
*/
public function syncCompile($repoID = 0, $jobID = 0)
{
if($jobID)
{
$jobList[$jobID] = $this->loadModel('job')->getByID($jobID);
}
else
{
$jobList = $this->loadModel('job')->getList($repoID);
}
$jenkinsPairs = $this->loadModel('pipeline')->getList('jenkins');
$gitlabPairs = $this->loadModel('pipeline')->getList('gitlab');
foreach($jobList as $job)
{
if($job->engine == 'jenkins')
{
$server = zget($jenkinsPairs, $job->server);
$this->syncJenkinsBuildList($server, $job);
}
else
{
$server = zget($gitlabPairs, $job->server);
$this->syncGitLabBuildList($server, $job);
}
}
}
/**
* Sync jenkins build list.
*
* @param object $jenkins
* @param object $job
* @access public
* @return void
*/
public function syncJenkinsBuildList($jenkins, $job)
{
$jenkinsUser = $jenkins->account;
$jenkinsPassword = $jenkins->token ? $jenkins->token : base64_decode($jenkins->password);
/* Get build list by API. */
$url = sprintf('%s/job/%s/api/json?tree=builds[id,number,result,queueId,timestamp]', $jenkins->url, $job->pipeline);
$response = common::http($url, '', array(CURLOPT_USERPWD => "$jenkinsUser:$jenkinsPassword"));
if(!$response) return false;
$compilePairs = $this->dao->select('queue,job')->from(TABLE_COMPILE)->where('job')->eq($job->id)->andWhere('queue')->gt(0)->fetchPairs();
$jobInfo = json_decode($response);
foreach($jobInfo->builds as $build)
{
$lastSyncTime = strtotime($job->lastSyncDate);
if($build->timestamp < $lastSyncTime) break;
if(isset($compilePairs[$build->queueId])) continue;
$data = new stdclass();
$data->name = $job->name;
$data->job = $job->id;
$data->queue = $build->queueId;
$data->status = $build->result == 'SUCCESS' ? 'success' : 'failure';
$data->createdBy = 'guest';
$data->createdDate = date('Y-m-d H:i:s', $build->timestamp);
$data->updateDate = $data->createdDate;
$this->dao->insert(TABLE_COMPILE)->data($data)->exec();
}
$now = helper::now();
$this->dao->update(TABLE_JOB)->set('lastSyncDate')->eq($now)->where('id')->eq($job->id)->exec();
}
/**
* Sync gitlab build list.
*
* @param object $gitlab
* @param object $job
* @access public
* @return void
*/
public function syncGitlabBuildList($gitlab, $job)
{
$pipeline = json_decode($job->pipeline);
$projectID = isset($pipeline->project) ? $pipeline->project : '';
$ref = isset($pipeline->reference) ? $pipeline->reference : '';
$url = sprintf($this->loadModel('gitlab')->getApiRoot($gitlab->id, false), "/projects/{$projectID}/pipelines");
/* Get build list by API. */
$builds = array();
$compilePairs = $this->dao->select('queue,job')->from(TABLE_COMPILE)->where('job')->eq($job->id)->andWhere('queue')->gt(0)->fetchPairs();
for($page = 1; true; $page++)
{
$results = json_decode(commonModel::http($url . "&ref={$ref}&order_by=id&sort=desc&page={$page}&per_page=100"));
if(!is_array($results)) break;
if(!empty($results))
{
/* Check whether it has been synchronized by create time. */
$build = $results[0];
$createDate = date('Y-m-d H:i:s', strtotime($build->created_at));
if($createDate < $job->lastSyncDate) break;
$builds = array_merge($builds, $results);
}
if(count($results) < 100) break;
}
foreach($builds as $build)
{
$createDate = date('Y-m-d H:i:s', strtotime($build->created_at));
if($createDate < $job->lastSyncDate) break;
if(isset($compilePairs[$build->id])) continue;
$data = new stdclass();
$data->name = $job->name;
$data->job = $job->id;
$data->queue = $build->id;
$data->status = $build->status;
$data->createdBy = 'guest';
$data->createdDate = $createDate;
$data->updateDate = date('Y-m-d H:i:s', strtotime($build->updated_at));
$this->dao->insert(TABLE_COMPILE)->data($data)->exec();
}
$now = helper::now();
$this->dao->update(TABLE_JOB)->set('lastSyncDate')->eq($now)->where('id')->eq($job->id)->exec();
}
/**
* Execute compile
*
+1 -1
View File
@@ -56,7 +56,7 @@
<td><?php echo $gitlabUser->email;?></td>
<td><?php echo html::select("zentaoUsers[$gitlabUser->id]", $userPairs, $gitlabUser->zentaoAccount, "class='form-control select chosen'" );?></td>
<td>
<?php if(isset($bindedUsers[$gitlabUser->zentaoAccount])):?>
<?php if(in_array($gitlabUser->id, $bindedUsers)):?>
<?php $zentaoAccount = zget($userPairs, $gitlabUser->zentaoAccount, '');?>
<?php if(!empty($zentaoAccount)):?>
<?php echo $lang->gitlab->binded;?>
+4 -2
View File
@@ -1176,11 +1176,13 @@ $lang->ci->methodOrder[5] = 'commitResult';
$lang->ci->methodOrder[10] = 'checkCompileStatus';
$lang->resource->compile = new stdclass();
$lang->resource->compile->browse = 'browse';
$lang->resource->compile->logs = 'logs';
$lang->resource->compile->browse = 'browse';
$lang->resource->compile->logs = 'logs';
$lang->resource->compile->syncCompile = 'syncCompile';
$lang->compile->methodOrder[5] = 'browse';
$lang->compile->methodOrder[10] = 'logs';
$lang->compile->methodOrder[15] = 'syncCompile';
$lang->resource->jenkins = new stdclass();
$lang->resource->jenkins->browse = 'browseAction';
+2 -2
View File
@@ -191,8 +191,8 @@ class repoModel extends model
$data = fixer::input('post')
->setIf($isPipelineServer, 'password', $this->post->serviceToken)
->setIf($isPipelineServer and $this->post->SCM == 'Gitlab', 'path', '')
->setIf($isPipelineServer and $this->post->SCM == 'Gitlab', 'client', '')
->setIf($this->post->SCM == 'Gitlab', 'path', '')
->setIf($this->post->SCM == 'Gitlab', 'client', '')
->setIf($isPipelineServer, 'extra', $this->post->serviceProject)
->setIf($isPipelineServer, 'prefix', '')
->setIf($this->post->SCM == 'Git', 'account', '')