From 6d8b6d40459dd24075058593d7f12b7feeeaa681 Mon Sep 17 00:00:00 2001 From: aaronchen2k <462826@qq.com> Date: Mon, 13 Jan 2020 17:31:40 +0800 Subject: [PATCH] success to build jenkins job by calling remote api --- module/ci/control.php | 19 +++++++- module/ci/model.php | 65 +++++++++++++++++++++++++ module/ci/view/browsecitask.html.php | 2 + module/ci/view/showsynccomment.html.php | 3 -- 4 files changed, 84 insertions(+), 5 deletions(-) diff --git a/module/ci/control.php b/module/ci/control.php index 0a2b88277f..2343eb936c 100644 --- a/module/ci/control.php +++ b/module/ci/control.php @@ -177,7 +177,7 @@ class ci extends control $this->view->position[] = html::a(inlink('browseJenkins'), $this->lang->jenkins->common); $this->view->position[] = $this->lang->jenkins->create; - $this->view->credentialList = $this->ci->listCredentialForSelection("type='token' or type='account'"); + $this->view->credentialList = $this->ci->listCredentialForSelection("type='token'"); $this->view->module = 'jenkins'; $this->display(); @@ -208,7 +208,7 @@ class ci extends control $this->view->position[] = $this->lang->jenkins->edit; $this->view->jenkins = $jenkins; - $this->view->credentialList = $this->ci->listCredentialForSelection("type='token' or type='account'"); + $this->view->credentialList = $this->ci->listCredentialForSelection("type='token'"); $this->view->module = 'jenkins'; $this->display(); @@ -331,6 +331,21 @@ class ci extends control $this->send(array('result' => 'success')); } + /** + * Exec a ci task. + * + * @param int $id + * @access public + * @return void + */ + public function exeCitask($id) + { + $this->ci->exeCitask($id); + if(dao::isError()) $this->send(array('result' => 'fail', 'message' => dao::getError())); + + $this->send(array('result' => 'success')); + } + /** * Browse repo. * diff --git a/module/ci/model.php b/module/ci/model.php index c07309f2e4..21b74db9ac 100644 --- a/module/ci/model.php +++ b/module/ci/model.php @@ -255,6 +255,35 @@ class ciModel extends model return !dao::isError(); } + /** + * Execute ci task. + * + * @param int $id + * @access public + * @return bool + */ + public function exeCitask($taskID) + { + $po = $this->dao->select('task.id taskId, task.repo, task.jenkinsTask, jenkins.name,jenkins.serviceUrl,jenkins.credential') + ->from(TABLE_CI_TASK)->alias('task') + ->leftJoin(TABLE_JENKINS)->alias('jenkins')->on('task.jenkins=jenkins.id') + ->where('task.id')->eq($taskID) + ->fetch(); + + $credential = $this->getCredentialByID($po->credential); // jenkins must use a token credential + $jenkinsToken = $credential->token; + $jenkinsUser = $credential->username; + $jenkinsServer = $po->serviceUrl; + + $r = '://' . $jenkinsUser . ':' . $jenkinsToken . '@'; + $jenkinsServer = str_replace('://', $r, $jenkinsServer); // add token + $buildUrl = sprintf('%s/job/%s/build/api/json', $jenkinsServer, $po->jenkinsTask); + + $response = $this->post($buildUrl, new stdClass()); + + return !dao::isError(); + } + /** * Get a repo by id. * @@ -730,4 +759,40 @@ class ciModel extends model return $repos; } + /** + * Make a http post request. + * + * @param string $url + * @param string $data + * @access public + * @return void + */ + public function post($url, $data) + { + if(!function_exists('curl_init')) die('I can\'t do post action without curl extension.'); + + $curl = curl_init(); + + if(PHP_VERSION_ID >= 50500 && class_exists('CURLFile')) + { + $curlFile = true; + } + else + { + $curlFile = false; + if(defined('CURLOPT_SAFE_UPLOAD')) curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false); + } + + curl_setopt($curl, CURLOPT_URL, $url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($curl, CURLOPT_POST, true); + curl_setopt($curl, CURLOPT_POSTFIELDS, $data); + + $response = curl_exec($curl); + $errors = curl_error($curl); + curl_close($curl); + + return $response; + } } diff --git a/module/ci/view/browsecitask.html.php b/module/ci/view/browsecitask.html.php index 75948b80f1..d0f4d2188c 100644 --- a/module/ci/view/browsecitask.html.php +++ b/module/ci/view/browsecitask.html.php @@ -54,7 +54,9 @@ createLink('ci', 'deleteCitask', "citaskID=$id&confirm=yes"); echo html::a("javascript:ajaxDelete(\"$deleteURL\", \"citaskList\", confirmDelete)", '', '', "title='{$lang->citask->delete}' class='btn'"); diff --git a/module/ci/view/showsynccomment.html.php b/module/ci/view/showsynccomment.html.php index b1b0b7af0e..b4759d9091 100644 --- a/module/ci/view/showsynccomment.html.php +++ b/module/ci/view/showsynccomment.html.php @@ -26,14 +26,11 @@