From f3de4473291ed98a7fc7ec00e78cb36d84ce1af0 Mon Sep 17 00:00:00 2001 From: dingguodong Date: Fri, 10 May 2024 15:11:08 +0800 Subject: [PATCH] + Add the method to import the pipeline. --- module/job/model.php | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/module/job/model.php b/module/job/model.php index b4d31517bd..fa2a6e0186 100644 --- a/module/job/model.php +++ b/module/job/model.php @@ -9,6 +9,7 @@ declare(strict_types=1); * @package job * @version $Id$ * @link https://www.zentao.net + * @property jobTao $jobTao */ class jobModel extends model { @@ -578,4 +579,50 @@ class jobModel extends model { $this->dao->update(TABLE_JOB)->set('lastTag')->eq($lastTag)->where('id')->eq($jobID)->exec(); } + + /** + * 通过代码库ID导入该代码库的流水线。 + * Import the pipeline of the repository with the repoID. + * + * @param mixed $repoID + * @return bool + */ + public function import(string|int $repoID) + { + $repo = $this->loadModel('repo')->getByID((int)$repoID); + if(!in_array($repo->SCM, array('Gitlab', 'GitFox'))) return false; + + $pipelines = $this->loadModel(strtolower($repo->SCM))->apiGetPipeline($repo->serviceHost, $repo->serviceProject, ''); + if(!is_array($pipelines) or empty($pipelines)) return false; + + $job = new stdclass; + $job->name = $repo->name; + $job->repo = $repoID; + $job->product = $repo->product; + $job->engine = strtolower($repo->SCM); + $job->server = $repo->serviceHost; + $job->createdBy = $this->app->user->account; + + $addedPipelines = array(); + foreach($pipelines as $pipeline) + { + if(!empty($pipeline->disabled)) continue; + + $job->pipeline = json_encode(array('project' => $repo->serviceProject, 'reference' => isset($pipeline->ref) ? $pipeline->ref : $pipeline->default_branch)); + + $hash = md5($job->pipeline); + if(isset($addedPipelines[$hash])) continue; + $addedPipelines[] = $hash; + + $this->dao->insert(TABLE_JOB)->data($job) + ->batchCheck($this->config->job->create->requiredFields, 'notempty') + ->autoCheck() + ->exec(); + if(dao::isError()) return false; + + $this->loadModel('action')->create('job', $this->dao->lastInsertId(), 'imported'); + } + + return true; + } }