From c1d3313bf40cbce208bbaabf7ccbebd8a298f1ce Mon Sep 17 00:00:00 2001 From: hahn <axel.hahn@iml.unibe.ch> Date: Mon, 15 Jun 2020 21:10:42 +0200 Subject: [PATCH] task#3869 - initial stuff for API --- public_html/api/index.php | 43 ++++++++++++++++--- .../deployment/classes/project.class.php | 6 +-- public_html/deployment/classes/user.class.php | 12 ++++-- .../deployment/classes/vcs.git.class.php | 3 ++ shellscripts/api-client.sh | 33 ++++++++++++++ 5 files changed, 84 insertions(+), 13 deletions(-) diff --git a/public_html/api/index.php b/public_html/api/index.php index 3e45c621..7f2b6599 100644 --- a/public_html/api/index.php +++ b/public_html/api/index.php @@ -170,14 +170,18 @@ $sPrjId = isset($aUriSplit[2]) ? $aUriSplit[2] : false; $sPrjAction = isset($aUriSplit[3]) ? $aUriSplit[3] : false; + $sParam4 = isset($aUriSplit[4]) ? $aUriSplit[4] : false; + $sMethod = $_SERVER['REQUEST_METHOD']; _wd('$sPrjId = '.$sPrjId); _wd('$sPrjAction = '.$sPrjAction); + $oCLog = new logger(); // try to init the given project try{ ob_start(); $oProject=new project($sPrjId); - $oProject->setProjectById($sPrjId); + + // $oProject->setProjectById($sPrjId); ob_end_clean(); } catch (Exception $exc) { @@ -193,14 +197,41 @@ // check authorization _checkAuth($sProjectSecret); - - echo "OK: request was authorized successfully.\n"; + + // echo "OK: request was authorized successfully.\n"; + + $oProject->oUser->setUser('api'); + switch($sPrjAction){ case "build": - echo "build ..."; + if ($sParam4){ + $aResult=$oProject->setBranchname("origin/".$sParam4); + // echo "branch was set.\n"; + + } + $sBranchname=$oProject->getBranchname(); + $aRepodata = $oProject->getRemoteBranches(true); + if(!isset($aRepodata[$sBranchname])){ + _quit('ERROR: branch not found '.$sBranchname); + } + + + // echo "branch is set to ".$oProject->getBranchname()."\n"; + if ($sMethod==='GET'){ + $sNext=$oProject->getNextPhase(); + _done(array( + 'branch'=>$sBranchname, + 'phase'=>$sNext, + 'repo'=>$aRepodata[$sBranchname] + )); + } + if ($sMethod==='POST'){ + echo "starting build() ..."; + flush(); + die("ABORT"); + echo $oProject->build(); + } - $oCLog = new logger(); - echo $oProject->build(); break;; default: _quit('ERROR: Wrong action ['.$sApiItem.'].'); diff --git a/public_html/deployment/classes/project.class.php b/public_html/deployment/classes/project.class.php index 687ba48f..dd56aceb 100644 --- a/public_html/deployment/classes/project.class.php +++ b/public_html/deployment/classes/project.class.php @@ -1353,8 +1353,8 @@ class project extends base { if ($this->_sProcessTempOut && file_exists($this->_sProcessTempOut)) { unlink($this->_sProcessTempOut); } - $sNewTempfile = sys_get_temp_dir() . "/" . basename($sNewTempfile); - $this->_sProcessTempOut = $sNewTempfile; + // $sNewTempfile = sys_get_temp_dir() . "/" . basename($sNewTempfile); + $this->_sProcessTempOut = $sNewTempfile ? sys_get_temp_dir() . "/" . basename($sNewTempfile) : false; return $this->_sProcessTempOut; } @@ -1452,7 +1452,7 @@ class project extends base { } return $this->_sBranchname; } - + // ---------------------------------------------------------------------- // ACTIONS // ---------------------------------------------------------------------- diff --git a/public_html/deployment/classes/user.class.php b/public_html/deployment/classes/user.class.php index 13dc9d68..756b6ae9 100644 --- a/public_html/deployment/classes/user.class.php +++ b/public_html/deployment/classes/user.class.php @@ -162,7 +162,7 @@ class user { // set a session - it must correspondent with _autoDetectUser() $_SESSION["PHP_AUTH_USER"]=$sUser; - $this->setUser(); + $this->setUser(''); return true; } } @@ -182,9 +182,13 @@ class user { /** * set an authenticated user and get its roles */ - public function setUser(){ - $this->_sUsername=$this->_autoDetectUser(); - + public function setUser($sUser=false){ + if($sUser!==false){ + $this->_sUsername=$sUser; + $_SESSION["PHP_AUTH_USER"]=$sUser; + } else { + $this->_sUsername=$this->_autoDetectUser(); + } $this->_getUserGroups(); $this->_getUserPermission(); } diff --git a/public_html/deployment/classes/vcs.git.class.php b/public_html/deployment/classes/vcs.git.class.php index c994e342..d361b364 100644 --- a/public_html/deployment/classes/vcs.git.class.php +++ b/public_html/deployment/classes/vcs.git.class.php @@ -357,6 +357,9 @@ class vcs implements iVcs { } else { $a = $this->getRevision(false); } + if(!isset($a['branch'])){ + return false; + } // merge with cached info ... to add type and label if(isset($this->_aRemoteBranches[$a['branch']])){ $this->_aRemoteBranches[$a['branch']]=array_merge($this->_aRemoteBranches[$a['branch']], $a); diff --git a/shellscripts/api-client.sh b/shellscripts/api-client.sh index 4568b21a..0c1700c3 100644 --- a/shellscripts/api-client.sh +++ b/shellscripts/api-client.sh @@ -18,12 +18,44 @@ 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/") +" +} + + +# ---------------------------------------------------------------------- +# MAIN +# ---------------------------------------------------------------------- + +if [ $# -lt 1 ]; then + showhelp + exit 1 +fi + + # ---------------------------------------------------------------------- # REQ #1 - list project ids # ---------------------------------------------------------------------- @@ -47,6 +79,7 @@ echo # --- build url apiRequest="${apiBaseUrl}/project/${myProject}/${myAction}" +test -z $myBranch || apiRequest="${apiRequest}/${myBranch}" # --- date in http format LANG=en_EN -- GitLab