better handling of db connections, return timestamp for found builds
This commit is contained in:
parent
7a5da47e17
commit
707c00b850
|
@ -2,8 +2,6 @@
|
||||||
include("token.php");
|
include("token.php");
|
||||||
include("functions.php");
|
include("functions.php");
|
||||||
include("config.php");
|
include("config.php");
|
||||||
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
|
|
||||||
include("cibuild_connection.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 webApp="https://app.circleci.com/";
|
||||||
|
@ -95,7 +93,7 @@ function getArtifacts($job,$slug){
|
||||||
}
|
}
|
||||||
|
|
||||||
function insertPipeline($id,$param){
|
function insertPipeline($id,$param){
|
||||||
global $database;
|
$database=openDb();
|
||||||
if (! isset($database)) return false;
|
if (! isset($database)) return false;
|
||||||
try {
|
try {
|
||||||
$status='created';
|
$status='created';
|
||||||
|
@ -115,7 +113,7 @@ function insertPipeline($id,$param){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function updatePipeline($id,$status,$tag=null){
|
function updatePipeline($id,$status,$tag=null){
|
||||||
global $database;
|
$database=openDb();
|
||||||
if (! isset($database)) return false;
|
if (! isset($database)) return false;
|
||||||
try{
|
try{
|
||||||
$stmt=null;
|
$stmt=null;
|
||||||
|
@ -139,25 +137,26 @@ function updatePipeline($id,$status,$tag=null){
|
||||||
|
|
||||||
function findPipeline($param)
|
function findPipeline($param)
|
||||||
{
|
{
|
||||||
global $database;
|
$database=openDb();
|
||||||
if (!isset($database))
|
if (!isset($database))
|
||||||
return false;
|
return false;
|
||||||
try {
|
try {
|
||||||
$stmt = null;
|
$stmt = null;
|
||||||
if (isset($param['tag'])) {
|
if (isset($param['tag'])) {
|
||||||
$stmt = $database->prepare("SELECT * from " . TABLENAME .
|
$stmt = $database->prepare("SELECT id,UNIX_TIMESTAMP(timestamp) from " . TABLENAME .
|
||||||
" where status='success' and environment=? and buildflags=? and tag=? order by timestamp desc");
|
" where status='success' and environment=? and buildflags=? and tag=? order by timestamp desc");
|
||||||
$stmt->bind_param("sss", $param['environment'], $param['buildflags'], $param['tag']);
|
$stmt->bind_param("sss", $param['environment'], $param['buildflags'], $param['tag']);
|
||||||
} else {
|
} else {
|
||||||
$stmt = $database->prepare("SELECT id from " . TABLENAME .
|
$stmt = $database->prepare("SELECT id,UNIX_TIMESTAMP(timestamp) from " . TABLENAME .
|
||||||
" where status='success' and environment=? and buildflags=? order by timestamp desc");
|
" where status='success' and environment=? and buildflags=? order by timestamp desc");
|
||||||
$stmt->bind_param("ss", $param['environment'], $param['buildflags']);
|
$stmt->bind_param("ss", $param['environment'], $param['buildflags']);
|
||||||
}
|
}
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
$id=null;
|
$id=null;
|
||||||
$stmt->bind_result($id);
|
$timestamp=null;
|
||||||
|
$stmt->bind_result($id,$timestamp);
|
||||||
if ($stmt->fetch()){
|
if ($stmt->fetch()){
|
||||||
return $id;
|
return array('pipeline'=>$id,'timestamp'=>$timestamp);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
|
@ -295,12 +294,12 @@ try {
|
||||||
$par=array();
|
$par=array();
|
||||||
addVars($par,['environment','buildflags']);
|
addVars($par,['environment','buildflags']);
|
||||||
if (isset($_REQUEST['tag'])) $par['tag']=$_REQUEST['tag'];
|
if (isset($_REQUEST['tag'])) $par['tag']=$_REQUEST['tag'];
|
||||||
$id=findPipeline($par);
|
$rt=findPipeline($par);
|
||||||
header("Content-Type: application/json");
|
header("Content-Type: application/json");
|
||||||
$rt=array('status'=>'OK');
|
if (!$rt){
|
||||||
if ($id){
|
$rt=array();
|
||||||
$rt['pipeline']=$id;
|
|
||||||
}
|
}
|
||||||
|
$rt['status']='OK';
|
||||||
echo(json_encode($rt));
|
echo(json_encode($rt));
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
<?php
|
||||||
|
echo "CREATEDB\n";
|
||||||
|
include("functions.php");
|
||||||
|
$database=openDb();
|
||||||
|
if (! isset($database)){
|
||||||
|
die("ERROR: db not set\n");
|
||||||
|
}
|
||||||
|
try{
|
||||||
|
$sql="CREATE TABLE `CIBUILDS` (
|
||||||
|
`id` varchar(255) NOT NULL,
|
||||||
|
`tag` varchar(255),
|
||||||
|
`status` varchar(255) NOT NULL,
|
||||||
|
`config` longtext,
|
||||||
|
`environment` VARCHAR(255),
|
||||||
|
`buildflags` longtext,
|
||||||
|
`timestamp` timestamp ,
|
||||||
|
PRIMARY KEY (`id`)
|
||||||
|
);";
|
||||||
|
|
||||||
|
echo "executing $sql<br>";
|
||||||
|
$rt=$database->query($sql);
|
||||||
|
echo "execute OK<br>";
|
||||||
|
} catch (Exception $e){
|
||||||
|
echo "ERROR: ".$e;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
?>
|
|
@ -1,4 +1,17 @@
|
||||||
<?php
|
<?php
|
||||||
|
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
|
||||||
|
include("../../../cibuild_db.php");
|
||||||
|
function openDb(){
|
||||||
|
if (! defined('database::SERVER')) return null;
|
||||||
|
try{
|
||||||
|
$db=new mysqli(database::SERVER, database::USER, database::PASS,database::DB);
|
||||||
|
$db->query("SET CHARACTER SET 'utf8'");
|
||||||
|
return $db;
|
||||||
|
}catch (Exception $e){
|
||||||
|
error_log("openDB error $e");
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
function safeName($name)
|
function safeName($name)
|
||||||
{
|
{
|
||||||
return preg_replace('[^0-9_a-zA-Z.-]', '', $name);
|
return preg_replace('[^0-9_a-zA-Z.-]', '', $name);
|
||||||
|
|
Loading…
Reference in New Issue