From ff83efb0666cbc320709f029e8d2d841df56005e Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Wed, 1 Sep 2021 10:57:07 +0800 Subject: [PATCH] * Add manage reference of gitlab job function. --- module/job/control.php | 17 +++++++++++++++++ module/job/js/create.js | 22 ++++++++++++++++++++-- module/job/js/edit.js | 21 +++++++++++++++++++++ module/job/model.php | 29 +++++++++++++++++++++-------- module/job/view/create.html.php | 5 +++-- module/job/view/edit.html.php | 3 ++- 6 files changed, 84 insertions(+), 13 deletions(-) diff --git a/module/job/control.php b/module/job/control.php index 17d166f5d6..a44519e0cb 100644 --- a/module/job/control.php +++ b/module/job/control.php @@ -161,6 +161,8 @@ 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); + $repoList = $this->repo->getList($this->projectID); $repoPairs = array(0 => '', $repo->id => $repo->name); $gitlabRepos = array(0 => ''); @@ -375,4 +377,19 @@ class job extends control $productName = $this->loadModel('product')->getByID($repo->product)->name; die(json_encode(array($productName => $repo->product))); } + + /** + * Ajax get reference list function. + * + * @param int $repoID + * @access public + * @return void + */ + public function ajaxGetRefList($repoID) + { + $repo = $this->loadModel('repo')->getRepoByID($repoID); + if($repo->SCM != 'Gitlab') $this->send(array('result' => 'fail')); + $refList = $this->loadModel('gitlab')->getRefOptions($repo->gitlab, $repo->project); + $this->send(array('result' => 'success', 'refList' => $refList)); + } } diff --git a/module/job/js/create.js b/module/job/js/create.js index cbe8c58a18..18b121ba44 100644 --- a/module/job/js/create.js +++ b/module/job/js/create.js @@ -129,19 +129,37 @@ $(document).ready(function() if($(this).val() == 'gitlab') { - $('#triggerType').find('[value=schedule]').remove(); $('tr.gitlabRepo').show(); $('tr.commonRepo').hide(); } else if($('#triggerType').find('[value=schedule]').size() == 0 ) { - $('#triggerType').append(scheduleOption); $('tr.gitlabRepo').hide(); $('tr.commonRepo').show(); } }); $('#engine').change(); + $('#gitlabRepo').change(function() + { + $('#reference option').remove(); + + var repoID = $(this).val(); + if(repoID > 0) + { + $.getJSON(createLink('job', 'ajaxGetRefList', "repoID=" + repoID), function(response) + { + if(response.result == 'success') + { + $.each(response.refList, function(reference, name) + { + $('#reference').append(""); + }); + } + $('#reference').trigger('chosen:updated'); + }); + } + }); $('#triggerType').change(); }); diff --git a/module/job/js/edit.js b/module/job/js/edit.js index 2b76c48b1c..f91c2aef1b 100644 --- a/module/job/js/edit.js +++ b/module/job/js/edit.js @@ -146,6 +146,27 @@ $(document).ready(function() } }); + $('#gitlabRepo').change(function() + { + $('#reference option').remove(); + + var repoID = $(this).val(); + if(repoID > 0) + { + $.getJSON(createLink('job', 'ajaxGetRefList', "repoID=" + repoID), function(response) + { + if(response.result == 'success') + { + $.each(response.refList, function(reference, name) + { + $('#reference').append(""); + }); + } + $('#reference').trigger('chosen:updated'); + }); + } + }); + $('#engine').change(); $('#jkServer').change(); diff --git a/module/job/model.php b/module/job/model.php index 1bbcf23110..73afdf517b 100644 --- a/module/job/model.php +++ b/module/job/model.php @@ -20,7 +20,14 @@ class jobModel extends model */ public function getByID($id) { - return $this->dao->select('*')->from(TABLE_JOB)->where('id')->eq($id)->fetch(); + $job = $this->dao->select('*')->from(TABLE_JOB)->where('id')->eq($id)->fetch(); + if(strtolower($job->engine) == 'gitlab') + { + $pipeline = json_decode($job->pipeline); + $job->project = $pipeline->project; + $job->reference = $pipeline->reference; + } + return $job; } /** @@ -117,7 +124,7 @@ class jobModel extends model ->setDefault('atDay', '') ->add('createdBy', $this->app->user->account) ->add('createdDate', helper::now()) - ->remove('repoType') + ->remove('repoType,reference') ->get(); if($job->engine == 'jenkins') @@ -128,10 +135,12 @@ class jobModel extends model if(strtolower($job->engine) == 'gitlab') { + $repo = $this->loadModel('repo')->getRepoByID($job->gitlabRepo); + $project = zget($repo, 'project'); + $job->repo = $job->gitlabRepo; - $repo = $this->loadModel('repo')->getRepoByID($job->repo); $job->server = (int)zget($repo, 'gitlab', 0); - $job->pipeline = zget($repo, 'project', ''); + $job->pipeline = json_encode(array('project' => $project, 'reference' => $this->post->reference)); } unset($job->jkServer); @@ -203,7 +212,7 @@ class jobModel extends model ->setIF($this->post->triggerType != 'tag', 'lastTag', '') ->add('editedBy', $this->app->user->account) ->add('editedDate', helper::now()) - ->remove('repoType') + ->remove('repoType,reference') ->get(); if($job->engine == 'jenkins') @@ -214,10 +223,12 @@ class jobModel extends model if(strtolower($job->engine) == 'gitlab') { + $repo = $this->loadModel('repo')->getRepoByID($job->gitlabRepo); + $project = zget($repo, 'project'); + $job->repo = $job->gitlabRepo; - $repo = $this->loadModel('repo')->getRepoByID($job->repo); $job->server = (int)zget($repo, 'gitlab', 0); - $job->pipeline = zget($repo, 'project', ''); + $job->pipeline = json_encode(array('project' => $project, 'reference' => $this->post->reference)); } unset($job->jkServer); @@ -252,6 +263,7 @@ class jobModel extends model if(!empty($paramName)) $customParam[$paramName] = $paramValue; } + unset($job->paramName); unset($job->paramValue); unset($job->custom); @@ -407,7 +419,8 @@ class jobModel extends model } elseif($job->engine == 'gitlab' and $reference) { - $pipeline = $this->loadModel('gitlab')->apiCreatePipeline($job->server, $job->pipeline, $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'; diff --git a/module/job/view/create.html.php b/module/job/view/create.html.php index e7e0090ab5..69714f9eaf 100644 --- a/module/job/view/create.html.php +++ b/module/job/view/create.html.php @@ -48,7 +48,8 @@ job->repo; ?> - + + job->product; ?> @@ -95,7 +96,7 @@
- job->pipeline; ?> + job->pipeline;?> ''), '', "class='form-control chosen'"); ?>
diff --git a/module/job/view/edit.html.php b/module/job/view/edit.html.php index 3d0894484b..9c06425b7a 100644 --- a/module/job/view/edit.html.php +++ b/module/job/view/edit.html.php @@ -43,7 +43,8 @@ job->repo; ?> - repo, "class='form-control'"); ?> + repo, "class='chosen form-control'");?> + reference, "class='chosen form-control'");?> job->product;?>