Skip to content

Commit

Permalink
api functions for ci
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas committed Sep 5, 2023
1 parent f6cbcec commit 56b5598
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 7 deletions.
59 changes: 57 additions & 2 deletions webinstall/cibuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
include("config.php");
if (! isset($CI_TOKEN)) die("no token");
const apiBase="https://circleci.com/api/v2/";
const webApp="https://app.circleci.com/";
const apiRepo="project/gh/#user#/#repo#";
const workflowName="build-workflow";
const jobName="pio-build";

const defaultBranch='circleci-project-setup';
const defaultUser='wellenvogel';
const defaultRepo='esp32-nmea2000';

function getTokenHeaders(){
global $CI_TOKEN;
Expand Down Expand Up @@ -63,8 +66,21 @@ function getJobStatus($pipeline,$wf=workflowName,$job=jobName){
if ($pstat['state'] != 'created'){
return $pstat;
}
$pipeline_id=$pstat['id'];
$pipeline_number=$pstat['number'];
$pstat=getWorkflow($pipeline,$wf);
$workflow_id=$pstat['id'];
$workflow_number=$pstat['workflow_number'];
$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;
}

Expand Down Expand Up @@ -121,7 +137,46 @@ function getArtifactsForPipeline($pipeline,$wf=workflowName,$job=jobName){
}
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'])) {
$pipeline = $_REQUEST['download'];
Expand Down
9 changes: 6 additions & 3 deletions webinstall/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
);


function fillUserAndRepo($vars=null){
function fillUserAndRepo($vars=null,$source=null){
global $allowed;
if ($vars == null) {
$vars=array();
}
if ($source == null){
$source=$_REQUEST;
}
foreach (array('user','repo') as $n){
if (! isset($_REQUEST[$n])){
if (! isset($source[$n])){
die("missing parameter $n");
}
$v=$_REQUEST[$n];
$v=$source[$n];
$av=$allowed[$n];
if (! in_array($v,$av)){
die("value $v for $n not allowed");
Expand Down
11 changes: 9 additions & 2 deletions webinstall/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,18 @@ function getFwHeaders($aheaders=null){
}
return $outHeaders;
}
function getJson($url,$headers=null,$doThrow=false){
function getJson($url,$headers=null,$doThrow=false,$jsonData=null){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
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);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
#echo("curl exec for $url:$response:$httpcode\n");
Expand Down

0 comments on commit 56b5598

Please sign in to comment.