diff --git a/PHPCI/Application.php b/PHPCI/Application.php index a284c7c5..fe3bcca4 100644 --- a/PHPCI/Application.php +++ b/PHPCI/Application.php @@ -27,9 +27,9 @@ class Application extends b8\Application // Validate the user's session unless it is a login/logout action or a web hook: $sessionAction = ($controllerName == 'Session' && in_array($this->action, array('login', 'logout'))); - $webhookAction = in_array($controllerName, array('Bitbucket', 'Github')); + $externalAction = in_array($controllerName, array('Bitbucket', 'Github', 'BuildStatus')); - if (!$webhookAction && !$sessionAction) { + if (!$externalAction && !$sessionAction) { $this->validateSession(); } diff --git a/PHPCI/Controller/BuildStatusController.php b/PHPCI/Controller/BuildStatusController.php new file mode 100644 index 00000000..e898405a --- /dev/null +++ b/PHPCI/Controller/BuildStatusController.php @@ -0,0 +1,50 @@ + +* @package PHPCI +* @subpackage Web +*/ +class BuildStatusController extends b8\Controller +{ + public function init() + { + $this->_projectStore = Store\Factory::getStore('Project'); + } + + /** + * Returns the appropriate build status image for a given project. + */ + public function image($projectId) + { + $branch = $this->getParam('branch', 'master'); + $project = $this->_projectStore->getById($projectId); + $status = 'ok'; + + if (isset($project) && $project instanceof Project) { + $build = $project->getLatestBuild($branch, array(2,3)); + + if (isset($build) && $build instanceof Build && $build->getStatus() != 2) { + $status = 'failed'; + } + } + + header('Content-Type: image/png'); + die(file_get_contents(APPLICATION_PATH . 'assets/img/build-' . $status . '.png')); + } +} diff --git a/PHPCI/Model/Project.php b/PHPCI/Model/Project.php index be1c808b..f2a435fd 100644 --- a/PHPCI/Model/Project.php +++ b/PHPCI/Model/Project.php @@ -10,6 +10,8 @@ namespace PHPCI\Model; use PHPCI\Model\Base\ProjectBase; +use PHPCI\Model\Build; +use b8\Store; /** * Project Model @@ -20,4 +22,25 @@ use PHPCI\Model\Base\ProjectBase; */ class Project extends ProjectBase { + public function getLatestBuild($branch = 'master', $status = null) + { + $criteria = array('branch' => $branch, 'project_id' => $this->getId()); + + if (isset($status)) { + $criteria['status'] = $status; + } + + $order = array('id' => 'DESC'); + $builds = Store\Factory::getStore('Build')->getWhere($criteria, 1, $start, array(), $order); + + if (is_array($builds['items']) && count($builds['items'])) { + $latest = array_shift($builds['items']); + + if (isset($latest) && $latest instanceof Build) { + return $latest; + } + } + + return null; + } } diff --git a/PHPCI/View/Index.phtml b/PHPCI/View/Index.phtml index 196164c4..2dcea736 100644 --- a/PHPCI/View/Index.phtml +++ b/PHPCI/View/Index.phtml @@ -10,8 +10,31 @@