2013-05-17 17:10:54 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2014-05-12 18:26:17 +02:00
|
|
|
* PHPCI - Continuous Integration for PHP
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2014, Block 8 Limited.
|
|
|
|
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
|
|
|
* @link https://www.phptesting.org/
|
|
|
|
*/
|
2013-05-17 17:10:54 +02:00
|
|
|
|
|
|
|
namespace PHPCI\Controller;
|
|
|
|
|
|
|
|
use b8;
|
2014-04-30 16:00:35 +02:00
|
|
|
use b8\Exception\HttpException\NotFoundException;
|
2013-05-17 17:10:54 +02:00
|
|
|
use b8\Store;
|
2014-04-30 16:00:35 +02:00
|
|
|
use PHPCI\BuildFactory;
|
2013-05-17 17:10:54 +02:00
|
|
|
use PHPCI\Model\Project;
|
|
|
|
use PHPCI\Model\Build;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build Status Controller - Allows external access to build status information / images.
|
|
|
|
* @author Dan Cryer <dan@block8.co.uk>
|
|
|
|
* @package PHPCI
|
|
|
|
* @subpackage Web
|
|
|
|
*/
|
2013-05-22 17:36:55 +02:00
|
|
|
class BuildStatusController extends \PHPCI\Controller
|
2013-05-17 17:10:54 +02:00
|
|
|
{
|
2013-10-08 19:24:20 +02:00
|
|
|
/**
|
|
|
|
* @var \PHPCI\Store\ProjectStore
|
|
|
|
*/
|
|
|
|
protected $projectStore;
|
2014-04-30 16:00:35 +02:00
|
|
|
protected $buildStore;
|
2013-10-08 19:24:20 +02:00
|
|
|
|
2013-05-17 17:10:54 +02:00
|
|
|
public function init()
|
|
|
|
{
|
2014-04-30 16:00:35 +02:00
|
|
|
$this->response->disableLayout();
|
|
|
|
$this->buildStore = Store\Factory::getStore('Build');
|
|
|
|
$this->projectStore = Store\Factory::getStore('Project');
|
2013-05-17 17:10:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-16 20:10:47 +02:00
|
|
|
* Returns status of the last build
|
|
|
|
* @param $projectId
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-05-16 20:10:47 +02:00
|
|
|
protected function getStatus($projectId)
|
2013-05-17 17:10:54 +02:00
|
|
|
{
|
2013-10-08 19:24:20 +02:00
|
|
|
$branch = $this->getParam('branch', 'master');
|
2014-05-16 20:10:47 +02:00
|
|
|
try {
|
|
|
|
$project = $this->projectStore->getById($projectId);
|
|
|
|
$status = 'passing';
|
2013-05-17 17:10:54 +02:00
|
|
|
|
2014-05-16 20:10:47 +02:00
|
|
|
if (!$project->getAllowPublicStatus()) {
|
|
|
|
die();
|
|
|
|
}
|
2014-04-30 16:00:35 +02:00
|
|
|
|
2014-05-16 20:10:47 +02:00
|
|
|
if (isset($project) && $project instanceof Project) {
|
|
|
|
$build = $project->getLatestBuild($branch, array(2,3));
|
2013-05-17 17:10:54 +02:00
|
|
|
|
2014-05-16 20:10:47 +02:00
|
|
|
if (isset($build) && $build instanceof Build && $build->getStatus() != 2) {
|
|
|
|
$status = 'failed';
|
|
|
|
}
|
2013-05-17 17:10:54 +02:00
|
|
|
}
|
2014-05-16 20:10:47 +02:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
$status = 'error';
|
2013-05-17 17:10:54 +02:00
|
|
|
}
|
|
|
|
|
2014-05-16 20:10:47 +02:00
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the appropriate build status image for a given project.
|
|
|
|
*/
|
|
|
|
public function image($projectId)
|
|
|
|
{
|
2014-05-16 20:10:47 +02:00
|
|
|
$status = $this->getStatus($projectId);
|
2013-05-17 17:10:54 +02:00
|
|
|
header('Content-Type: image/png');
|
2013-10-08 13:41:12 +02:00
|
|
|
die(file_get_contents(APPLICATION_PATH . 'public/assets/img/build-' . $status . '.png'));
|
2013-05-17 17:10:54 +02:00
|
|
|
}
|
2014-04-30 16:00:35 +02:00
|
|
|
|
2014-05-16 20:10:47 +02:00
|
|
|
/**
|
|
|
|
* Returns the appropriate build status image in SVG format for a given project.
|
|
|
|
*/
|
|
|
|
public function svg($projectId)
|
|
|
|
{
|
2014-05-16 20:10:47 +02:00
|
|
|
$status = $this->getStatus($projectId);
|
2014-05-16 20:10:47 +02:00
|
|
|
header('Content-Type: image/svg+xml');
|
|
|
|
die(file_get_contents(APPLICATION_PATH . 'public/assets/img/build-' . $status . '.svg'));
|
|
|
|
}
|
|
|
|
|
2014-04-30 16:00:35 +02:00
|
|
|
public function view($projectId)
|
|
|
|
{
|
|
|
|
$project = $this->projectStore->getById($projectId);
|
2014-05-09 13:16:24 +02:00
|
|
|
|
|
|
|
if (empty($project)) {
|
2014-04-30 16:00:35 +02:00
|
|
|
throw new NotFoundException('Project with id: ' . $projectId . ' not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$project->getAllowPublicStatus()) {
|
|
|
|
throw new NotFoundException('Project with id: ' . $projectId . ' not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
$builds = $this->getLatestBuilds($projectId);
|
|
|
|
|
|
|
|
if (count($builds)) {
|
|
|
|
$this->view->latest = $builds[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->view->builds = $builds;
|
|
|
|
$this->view->project = $project;
|
|
|
|
|
|
|
|
return $this->view->render();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render latest builds for project as HTML table.
|
|
|
|
*/
|
|
|
|
protected function getLatestBuilds($projectId)
|
|
|
|
{
|
|
|
|
$criteria = array('project_id' => $projectId);
|
|
|
|
$order = array('id' => 'DESC');
|
|
|
|
$builds = $this->buildStore->getWhere($criteria, 10, 0, array(), $order);
|
|
|
|
|
|
|
|
foreach ($builds['items'] as &$build) {
|
|
|
|
$build = BuildFactory::getBuild($build);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $builds['items'];
|
|
|
|
}
|
2013-05-17 17:10:54 +02:00
|
|
|
}
|