success to build jenkins job by calling remote api
This commit is contained in:
+17
-2
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +54,9 @@
|
||||
|
||||
<td class='c-actions text-right'>
|
||||
<?php
|
||||
common::printIcon('ci', 'exeCitask', "citaskID=$id", '', 'list', 'play');
|
||||
common::printIcon('ci', 'editCitask', "citaskID=$id", '', 'list', 'edit');
|
||||
|
||||
if (common::hasPriv('ci', 'deleteCitask')) {
|
||||
$deleteURL = $this->createLink('ci', 'deleteCitask', "citaskID=$id&confirm=yes");
|
||||
echo html::a("javascript:ajaxDelete(\"$deleteURL\", \"citaskList\", confirmDelete)", '<i class="icon-trash"></i>', '', "title='{$lang->citask->delete}' class='btn'");
|
||||
|
||||
@@ -26,14 +26,11 @@
|
||||
<script language='Javascript'>
|
||||
$(function(){
|
||||
var link = createLink('ci', 'ajaxSyncComment', "repoID=<?php echo $repoID?>&type=batch&needPull=<?php echo $needPull?>");
|
||||
alert(link);
|
||||
|
||||
function syncComments()
|
||||
{
|
||||
$.get(link, function(data)
|
||||
{
|
||||
console.log(data);
|
||||
|
||||
if(data == 'finish')
|
||||
{
|
||||
$('#caption').text('<?php echo $lang->repo->notice->syncComplete?>');
|
||||
|
||||
Reference in New Issue
Block a user