api functions for ci
This commit is contained in:
parent
f6cbcecb78
commit
56b5598c15
|
@ -4,10 +4,13 @@ include("functions.php");
|
||||||
include("config.php");
|
include("config.php");
|
||||||
if (! isset($CI_TOKEN)) die("no token");
|
if (! isset($CI_TOKEN)) die("no token");
|
||||||
const apiBase="https://circleci.com/api/v2/";
|
const apiBase="https://circleci.com/api/v2/";
|
||||||
|
const webApp="https://app.circleci.com/";
|
||||||
const apiRepo="project/gh/#user#/#repo#";
|
const apiRepo="project/gh/#user#/#repo#";
|
||||||
const workflowName="build-workflow";
|
const workflowName="build-workflow";
|
||||||
const jobName="pio-build";
|
const jobName="pio-build";
|
||||||
|
const defaultBranch='circleci-project-setup';
|
||||||
|
const defaultUser='wellenvogel';
|
||||||
|
const defaultRepo='esp32-nmea2000';
|
||||||
|
|
||||||
function getTokenHeaders(){
|
function getTokenHeaders(){
|
||||||
global $CI_TOKEN;
|
global $CI_TOKEN;
|
||||||
|
@ -63,8 +66,21 @@ function getJobStatus($pipeline,$wf=workflowName,$job=jobName){
|
||||||
if ($pstat['state'] != 'created'){
|
if ($pstat['state'] != 'created'){
|
||||||
return $pstat;
|
return $pstat;
|
||||||
}
|
}
|
||||||
|
$pipeline_id=$pstat['id'];
|
||||||
|
$pipeline_number=$pstat['number'];
|
||||||
$pstat=getWorkflow($pipeline,$wf);
|
$pstat=getWorkflow($pipeline,$wf);
|
||||||
|
$workflow_id=$pstat['id'];
|
||||||
|
$workflow_number=$pstat['workflow_number'];
|
||||||
$pstat=getJob($pipeline,$pstat['id'],$job);
|
$pstat=getJob($pipeline,$pstat['id'],$job);
|
||||||
|
$pstat['pipeline_id']=$pipeline_id;
|
||||||
|
$pstat['pipeline_number']=$pipeline_number;
|
||||||
|
$pstat['workflow_id']=$workflow_id;
|
||||||
|
$pstat['workflow_number']=$workflow_number;
|
||||||
|
if (isset($pstat['project_slug'])){
|
||||||
|
$pstat['status_url']=webApp."/pipelines/".
|
||||||
|
preg_replace('/^gh/','github',$pstat['project_slug'])."/".
|
||||||
|
$pipeline_number."/workflows/".$workflow_id."/jobs/".$pstat['job_number'];
|
||||||
|
}
|
||||||
return $pstat;
|
return $pstat;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,7 +137,46 @@ try {
|
||||||
}
|
}
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
die("invalid api $action");
|
if ($action == 'pipeline'){
|
||||||
|
addVars(
|
||||||
|
$par,
|
||||||
|
['number','user','repo'],
|
||||||
|
array('user'=>defaultUser,'repo'=>defaultRepo)
|
||||||
|
);
|
||||||
|
$url=apiBase."/".replaceVars(apiRepo,fillUserAndRepo(null,$par))."/pipeline/".$par['number'];
|
||||||
|
$rt=getJson($url,getTokenHeaders(),true);
|
||||||
|
echo(json_encode($rt));
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
if ($action == 'start'){
|
||||||
|
addVars(
|
||||||
|
$par,
|
||||||
|
['environment','buildflags','config','suffix','branch','user','repo'],
|
||||||
|
array('suffix'=>'',
|
||||||
|
'branch'=>defaultBranch,
|
||||||
|
'config'=>'{}',
|
||||||
|
'user'=>defaultUser,
|
||||||
|
'repo'=>defaultRepo,
|
||||||
|
'buildflags'=>''
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$requestParam=array(
|
||||||
|
'branch'=>$par['branch'],
|
||||||
|
'parameters'=> array(
|
||||||
|
'run_build'=>true,
|
||||||
|
'environment'=>$par['environment'],
|
||||||
|
'suffix'=>$par['suffix'],
|
||||||
|
'config'=>$par['config'],
|
||||||
|
'build_flags'=>$par['buildflags']
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$userRepo=fillUserAndRepo(null,$par);
|
||||||
|
$url=apiBase."/".replaceVars(apiRepo,$userRepo)."/pipeline";
|
||||||
|
$rt=getJson($url,getTokenHeaders(),true,$requestParam);
|
||||||
|
echo (json_encode($rt));
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
throw new Exception("invalid api $action");
|
||||||
}
|
}
|
||||||
if (isset($_REQUEST['download'])) {
|
if (isset($_REQUEST['download'])) {
|
||||||
$pipeline = $_REQUEST['download'];
|
$pipeline = $_REQUEST['download'];
|
||||||
|
|
|
@ -5,16 +5,19 @@ $allowed=array(
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
function fillUserAndRepo($vars=null){
|
function fillUserAndRepo($vars=null,$source=null){
|
||||||
global $allowed;
|
global $allowed;
|
||||||
if ($vars == null) {
|
if ($vars == null) {
|
||||||
$vars=array();
|
$vars=array();
|
||||||
}
|
}
|
||||||
|
if ($source == null){
|
||||||
|
$source=$_REQUEST;
|
||||||
|
}
|
||||||
foreach (array('user','repo') as $n){
|
foreach (array('user','repo') as $n){
|
||||||
if (! isset($_REQUEST[$n])){
|
if (! isset($source[$n])){
|
||||||
die("missing parameter $n");
|
die("missing parameter $n");
|
||||||
}
|
}
|
||||||
$v=$_REQUEST[$n];
|
$v=$source[$n];
|
||||||
$av=$allowed[$n];
|
$av=$allowed[$n];
|
||||||
if (! in_array($v,$av)){
|
if (! in_array($v,$av)){
|
||||||
die("value $v for $n not allowed");
|
die("value $v for $n not allowed");
|
||||||
|
|
|
@ -121,11 +121,18 @@ function getFwHeaders($aheaders=null){
|
||||||
}
|
}
|
||||||
return $outHeaders;
|
return $outHeaders;
|
||||||
}
|
}
|
||||||
function getJson($url,$headers=null,$doThrow=false){
|
function getJson($url,$headers=null,$doThrow=false,$jsonData=null){
|
||||||
$curl = curl_init();
|
$curl = curl_init();
|
||||||
curl_setopt($curl, CURLOPT_URL,$url);
|
curl_setopt($curl, CURLOPT_URL,$url);
|
||||||
curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
|
curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
|
||||||
curl_setopt($curl, CURLOPT_HTTPHEADER, getFwHeaders($headers));
|
$outHeaders=getFwHeaders($headers);
|
||||||
|
if ($jsonData != null){
|
||||||
|
$json=json_encode($jsonData);
|
||||||
|
array_push($outHeaders,"Content-Type: application/json");
|
||||||
|
array_push($outHeaders,"Content-length: ".strlen($json));
|
||||||
|
curl_setopt($curl, CURLOPT_POSTFIELDS,$json);
|
||||||
|
}
|
||||||
|
curl_setopt($curl, CURLOPT_HTTPHEADER, $outHeaders);
|
||||||
$response = curl_exec($curl);
|
$response = curl_exec($curl);
|
||||||
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
||||||
#echo("curl exec for $url:$response:$httpcode\n");
|
#echo("curl exec for $url:$response:$httpcode\n");
|
||||||
|
|
Loading…
Reference in New Issue