Skip to content

Instantly share code, notes, and snippets.

@dotStart
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotStart/9209296 to your computer and use it in GitHub Desktop.
Save dotStart/9209296 to your computer and use it in GitHub Desktop.
Atlassian OnDemand status image
<?php
// register error handler
set_error_handler(function($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});
// defines
define ('IMAGE_DIR', dirname (__FILE__).'/image/'); // by default images (success.png, failed.png and unknown.png) are stored in ./image/
define ('KEY_REGEX', '~^([A-Za-z0-9]+)\-([A-Za-z0-9]+)$~'); // You usually won't need to change this
define ('SERVER_URL', 'https://yourinstance.atlassian.net/builds'); // set your Bamboo URL
define ('QUERY_STRING', '/rest/api/latest/result/%s?expand=results[0].result'); // you usually won't need to change this
define ('ERROR_REPORTING', false); // enable if you got trouble (e.g. all builds return the unknown graphic)
define ('CACHE_TIME', 600); // this caching method is only useful when using a CDN
try {
// send expiration header
// header ('Cache-Control: max-age=' + CACHE_TIME); // XXX: The Cache-Control header seems to cause problems when used by PHP
header ('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', (time() + CACHE_TIME)));
// get build key
if (!isset($_REQUEST['buildKey'])) throw new Exception ('No build key supplied');
$buildKey = $_REQUEST['buildKey'];
// verify build key
if (empty ($buildKey)) throw new Exception ('No build key supplied');
if (!preg_match (KEY_REGEX, $buildKey)) throw new Exception ('Invalid build key supplied');
// query API
$resource = fopen (SERVER_URL.sprintf (QUERY_STRING, $buildKey), 'r');
// get XML
$xmlString = stream_get_contents ($resource);
// create XML document
$document = new DOMDocument ();
$document->loadXML ($xmlString);
// create XPath
$xpath = new DOMXPath ($document);
$elements = $xpath->query ('//result[@successful]');
// verify length
if ($elements->length == 0) throw new Exception ('No results returned');
// get element
$element = $elements->item (0);
// verify state
if ($element->getAttribute ('successful') == 'true') {
header ('Content-Type: image/png');
readFile (IMAGE_DIR.'success.png');
} else {
header ('Content-Type: image/png');
readFile (IMAGE_DIR.'failed.png');
}
} catch (Exception $ex) {
// show unknown graphic
if (!ERROR_REPORTING) {
header ('Content-Type: image/png');
readFile (IMAGE_DIR.'unknown.png');
exit;
}
// display error information
print $ex;
}
#####################################
# Build Status Image Rewrites
#------------------------------------
# Use this Apache .htaccess for
# rewriting your status images
# from index.php?buildKey=TEST-MASTER
# to TEST-MASTER.png
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([A-Za-z0-9]+)\-([A-Za-z0-9]+).png$ index.php?buildKey=$1-$2 [L,QSA]
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment