Skip to content
Snippets Groups Projects
Commit 1c4e255a authored by hahn's avatar hahn
Browse files

task#3869 - initial stuff for API

parent c1d3313b
No related branches found
No related tags found
No related merge requests found
<?php
/*
* IDEA TO REALIZE:
/* ======================================================================
*
* A P I F O R C I S E R V E R
*
* /api/v1/projects/
* /api/v1/project/[Name]/build/[name-of-branch]]
* /api/v1/project/[Name]/accept/[phase]
* GET /api/v1/projects/
* GET /api/v1/project/[ID]/build/[name-of-branch]
* POST /api/v1/project/[ID]/build/[name-of-branch]
* get /api/v1/project/[ID]/phases
*
* ----------------------------------------------------------------------
* 2020-06-16 v0.9 <axel.hahn@iml.unibe.ch>
* ======================================================================
*/
$bDebug=false;
......@@ -171,6 +176,7 @@
$sPrjId = isset($aUriSplit[2]) ? $aUriSplit[2] : false;
$sPrjAction = isset($aUriSplit[3]) ? $aUriSplit[3] : false;
$sParam4 = isset($aUriSplit[4]) ? $aUriSplit[4] : false;
$sParam5 = isset($aUriSplit[5]) ? $aUriSplit[5] : false;
$sMethod = $_SERVER['REQUEST_METHOD'];
_wd('$sPrjId = '.$sPrjId);
_wd('$sPrjAction = '.$sPrjAction);
......@@ -205,14 +211,12 @@
switch($sPrjAction){
case "build":
if ($sParam4){
$aResult=$oProject->setBranchname("origin/".$sParam4);
// echo "branch was set.\n";
$aResult=$oProject->setBranchname($sParam4 . ($sParam5 ? '/'.$sParam5 : ''));
}
$sBranchname=$oProject->getBranchname();
$aRepodata = $oProject->getRemoteBranches(true);
if(!isset($aRepodata[$sBranchname])){
_quit('ERROR: branch not found '.$sBranchname);
_quit('ERROR: branch not found: '.$sBranchname, 404);
}
......@@ -228,10 +232,11 @@
if ($sMethod==='POST'){
echo "starting build() ...";
flush();
die("ABORT");
echo $oProject->build();
}
break;;
case "phases":
_done($oProject->getAllPhaseInfos());
break;;
default:
_quit('ERROR: Wrong action ['.$sApiItem.'].');
......
......@@ -6,6 +6,11 @@
# This is a demo api client
#
# ----------------------------------------------------------------------
#
# my projects and secrets
# DATA:ci-webgui:cOiScVAElvcJKmJ1eGrKXZvv6ZROlSgZ9VpSVFK1uxZI8J5ITXuZZb8jIYobuoAB
#
# ----------------------------------------------------------------------
# 2020-06-12 first lines <axel.hahn@iml.unibe.ch>
# ======================================================================
......@@ -13,73 +18,45 @@
# CONFIG
# ----------------------------------------------------------------------
myProject=ci-webgui
secret="cOiScVAElvcJKmJ1eGrKXZvv6ZROlSgZ9VpSVFK1uxZI8J5ITXuZZb8jIYobuoAB"
myAction=build
myBranch=
# myBranch=helloworld
# myBranch="task3869-api"
apiHost="http://dev.ci.iml.unibe.ch:8002"
apiBaseUrl="/api/v1"
apiMethod=GET
line="----------------------------------------------------------------------"
# ----------------------------------------------------------------------
# FUNCTIONS
# ----------------------------------------------------------------------
function showhelp(){
echo "
SYNTAX
projects show projects
list list branches
info [branch] show infos about what happens on build
build [branch] build
PARAMETERS
branch name of branch (optional), i.e. feature-123 (without "origin/")
"
}
buildinfo [project] [branch]
show infos about what happens on build
build [project] [branch]
execute build
# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------
phases [project]
show status of phases
if [ $# -lt 1 ]; then
showhelp
exit 1
fi
PARAMETERS:
project project id in ci; see output or projects
branch name of branch (optional), i.e. origin/feature-123
"
}
# ----------------------------------------------------------------------
# REQ #1 - list project ids
# ----------------------------------------------------------------------
function makeRequest(){
echo "[1] GET Request ${apiHost}${apiBaseUrl}/projects"
echo $line
curl -i \
-H "Accept: application/json" -H "Content-Type: application/json" \
-H "Date: ${apiTS}" \
-H "Authorization: demo-bash-client:${myHash}" \
-X $apiMethod \
${apiHost}${apiBaseUrl}/projects
echo
echo $line
echo
echo
local apiMethod=$1
local apiRequest=$2
local secret=$3
# ----------------------------------------------------------------------
# REQ #2 - build ci webgui
# ----------------------------------------------------------------------
echo $apiMethod ${apiHost}${apiRequest}
echo $line
# --- build url
apiRequest="${apiBaseUrl}/project/${myProject}/${myAction}"
test -z $myBranch || apiRequest="${apiRequest}/${myBranch}"
if [ ! -z $secret ]; then
# --- date in http format
LANG=en_EN
......@@ -97,21 +74,59 @@ ${apiTS}
myHash=`echo -n "$data" | openssl sha1 -hmac "${secret}" | cut -f 2 -d" "`
myHash=`echo -n "$myHash" | base64`
# --- send htp request
echo "[2] $apiMethod Request ${apiHost}${apiRequest}"
echo $line
curl -i \
-H "Accept: application/json" -H "Content-Type: application/json" \
-H "Date: ${apiTS}" \
-H "Authorization: demo-bash-client:${myHash}" \
-X $apiMethod \
${apiHost}${apiRequest}
else
curl -i \
-H "Accept: application/json" -H "Content-Type: application/json" \
-X $apiMethod \
${apiHost}${apiRequest}
fi
}
rc=$?
# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------
if [ $# -lt 1 ]; then
showhelp
exit 1
fi
myProject=$2
secret=`grep "^#\ DATA:${myProject}:" $0 | cut -f 3- -d ":"`
case $1 in
# --- projects is an access without autorization
"projects")
makeRequest GET /api/v1/projects
;;
# --- access WITH autorization only
"build")
makeRequest POST /api/v1/project/$myProject/build/$3 "$secret"
;;
"buildinfo")
makeRequest GET /api/v1/project/$myProject/build/$3 "$secret"
;;
"phases")
makeRequest GET /api/v1/project/$myProject/phases "$secret"
;;
*)
echo "ERROR: unknown parameter $1"
exit 2
esac
rc=$?
echo
echo $line
echo rc=$rc
exit $rc
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment