2013-05-10 13:28:43 +02:00
|
|
|
<?php
|
2013-05-16 03:16:56 +02:00
|
|
|
/**
|
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-10 13:28:43 +02:00
|
|
|
|
|
|
|
namespace PHPCI\Controller;
|
2013-05-16 16:25:39 +02:00
|
|
|
|
|
|
|
use b8;
|
2014-05-09 13:15:11 +02:00
|
|
|
use b8\Exception\HttpException\NotFoundException;
|
2014-02-25 18:00:05 +01:00
|
|
|
use PHPCI\BuildFactory;
|
2013-05-16 16:25:39 +02:00
|
|
|
use PHPCI\Model\Build;
|
2014-07-14 17:02:36 +02:00
|
|
|
use PHPCI\Service\BuildService;
|
2013-05-10 13:28:43 +02:00
|
|
|
|
2013-05-16 03:30:48 +02:00
|
|
|
/**
|
|
|
|
* Build Controller - Allows users to run and view builds.
|
|
|
|
* @author Dan Cryer <dan@block8.co.uk>
|
|
|
|
* @package PHPCI
|
|
|
|
* @subpackage Web
|
|
|
|
*/
|
2013-05-22 17:36:55 +02:00
|
|
|
class BuildController extends \PHPCI\Controller
|
2013-05-10 13:28:43 +02:00
|
|
|
{
|
2013-10-08 19:24:20 +02:00
|
|
|
/**
|
|
|
|
* @var \PHPCI\Store\BuildStore
|
|
|
|
*/
|
|
|
|
protected $buildStore;
|
2014-07-14 17:02:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \PHPCI\Service\BuildService
|
|
|
|
*/
|
|
|
|
protected $buildService;
|
2013-10-08 19:24:20 +02:00
|
|
|
|
2013-05-16 16:25:39 +02:00
|
|
|
public function init()
|
|
|
|
{
|
2014-07-14 17:02:36 +02:00
|
|
|
$this->buildStore = b8\Store\Factory::getStore('Build');
|
|
|
|
$this->buildService = new BuildService($this->buildStore);
|
2013-05-16 16:25:39 +02:00
|
|
|
}
|
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* View a specific build.
|
|
|
|
*/
|
2013-05-16 16:25:39 +02:00
|
|
|
public function view($buildId)
|
|
|
|
{
|
2014-05-09 13:15:11 +02:00
|
|
|
try {
|
|
|
|
$build = BuildFactory::getBuildById($buildId);
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
$build = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($build)) {
|
|
|
|
throw new NotFoundException('Build with ID: ' . $buildId . ' does not exist.');
|
|
|
|
}
|
|
|
|
|
2013-10-08 08:21:46 +02:00
|
|
|
$this->view->plugins = $this->getUiPlugins();
|
2013-05-22 17:36:55 +02:00
|
|
|
$this->view->build = $build;
|
2013-10-10 02:01:06 +02:00
|
|
|
$this->view->data = $this->getBuildData($build);
|
2014-04-24 17:25:24 +02:00
|
|
|
|
|
|
|
$title = 'Build #' . $build->getId() . ' - ' . $build->getProjectTitle();
|
|
|
|
$this->config->set('page_title', $title);
|
2013-05-16 16:25:39 +02:00
|
|
|
}
|
|
|
|
|
2013-10-08 08:21:46 +02:00
|
|
|
protected function getUiPlugins()
|
|
|
|
{
|
|
|
|
$rtn = array();
|
|
|
|
$path = APPLICATION_PATH . 'public/assets/js/build-plugins/';
|
|
|
|
$dir = opendir($path);
|
|
|
|
|
|
|
|
while ($item = readdir($dir)) {
|
|
|
|
if (substr($item, 0, 1) == '.' || substr($item, -3) != '.js') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$rtn[] = $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* AJAX call to get build data:
|
|
|
|
*/
|
2013-05-16 16:25:39 +02:00
|
|
|
public function data($buildId)
|
|
|
|
{
|
2014-02-25 18:00:05 +01:00
|
|
|
die($this->getBuildData(BuildFactory::getBuildById($buildId)));
|
2013-05-16 16:25:39 +02:00
|
|
|
}
|
|
|
|
|
2013-10-08 08:21:46 +02:00
|
|
|
/**
|
|
|
|
* AJAX call to get build meta:
|
|
|
|
*/
|
|
|
|
public function meta($buildId)
|
|
|
|
{
|
2014-02-25 18:00:05 +01:00
|
|
|
$build = BuildFactory::getBuildById($buildId);
|
2013-10-08 08:21:46 +02:00
|
|
|
$key = $this->getParam('key', null);
|
|
|
|
$numBuilds = $this->getParam('num_builds', 1);
|
|
|
|
$data = null;
|
|
|
|
|
|
|
|
if ($key && $build) {
|
2013-10-08 19:24:20 +02:00
|
|
|
$data = $this->buildStore->getMeta($key, $build->getProjectId(), $buildId, $numBuilds);
|
2013-10-08 08:21:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
die(json_encode($data));
|
|
|
|
}
|
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* Get build data from database and json encode it:
|
|
|
|
*/
|
2013-10-10 02:01:06 +02:00
|
|
|
protected function getBuildData($build)
|
2013-05-16 16:25:39 +02:00
|
|
|
{
|
|
|
|
$data = array();
|
|
|
|
$data['status'] = (int)$build->getStatus();
|
|
|
|
$data['log'] = $this->cleanLog($build->getLog());
|
|
|
|
$data['created'] = !is_null($build->getCreated()) ? $build->getCreated()->format('Y-m-d H:i:s') : null;
|
|
|
|
$data['started'] = !is_null($build->getStarted()) ? $build->getStarted()->format('Y-m-d H:i:s') : null;
|
|
|
|
$data['finished'] = !is_null($build->getFinished()) ? $build->getFinished()->format('Y-m-d H:i:s') : null;
|
|
|
|
|
|
|
|
return json_encode($data);
|
|
|
|
}
|
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* Create a build using an existing build as a template:
|
|
|
|
*/
|
2013-05-16 16:25:39 +02:00
|
|
|
public function rebuild($buildId)
|
|
|
|
{
|
2014-02-25 18:00:05 +01:00
|
|
|
$copy = BuildFactory::getBuildById($buildId);
|
2013-05-16 16:25:39 +02:00
|
|
|
|
2014-05-09 13:15:11 +02:00
|
|
|
if (empty($copy)) {
|
|
|
|
throw new NotFoundException('Build with ID: ' . $buildId . ' does not exist.');
|
|
|
|
}
|
|
|
|
|
2014-07-14 17:02:36 +02:00
|
|
|
$build = $this->buildService->createDuplicateBuild($copy);
|
2013-05-16 16:25:39 +02:00
|
|
|
|
2013-05-17 18:34:31 +02:00
|
|
|
header('Location: '.PHPCI_URL.'build/view/' . $build->getId());
|
2013-07-15 22:30:25 +02:00
|
|
|
exit;
|
2013-05-16 16:25:39 +02:00
|
|
|
}
|
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* Delete a build.
|
|
|
|
*/
|
2013-05-16 16:25:39 +02:00
|
|
|
public function delete($buildId)
|
|
|
|
{
|
2014-12-01 16:56:33 +01:00
|
|
|
if (empty($_SESSION['phpci_user']) || !$_SESSION['phpci_user']->getIsAdmin()) {
|
2013-05-16 16:25:39 +02:00
|
|
|
throw new \Exception('You do not have permission to do that.');
|
|
|
|
}
|
2013-06-04 21:17:17 +02:00
|
|
|
|
2014-05-09 13:15:11 +02:00
|
|
|
$build = BuildFactory::getBuildById($buildId);
|
2013-11-27 15:09:14 +01:00
|
|
|
|
2014-05-09 13:15:11 +02:00
|
|
|
if (empty($build)) {
|
|
|
|
throw new NotFoundException('Build with ID: ' . $buildId . ' does not exist.');
|
2013-11-27 15:09:14 +01:00
|
|
|
}
|
|
|
|
|
2014-07-29 16:07:26 +02:00
|
|
|
$this->buildService->deleteBuild($build);
|
2013-05-16 16:25:39 +02:00
|
|
|
|
2013-05-17 18:34:31 +02:00
|
|
|
header('Location: '.PHPCI_URL.'project/view/' . $build->getProjectId());
|
2013-07-15 22:30:25 +02:00
|
|
|
exit;
|
2013-05-16 16:25:39 +02:00
|
|
|
}
|
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* Parse log for unix colours and replace with HTML.
|
|
|
|
*/
|
2013-05-16 16:25:39 +02:00
|
|
|
protected function cleanLog($log)
|
|
|
|
{
|
|
|
|
$log = str_replace('[0;32m', '<span style="color: green">', $log);
|
|
|
|
$log = str_replace('[0;31m', '<span style="color: red">', $log);
|
|
|
|
$log = str_replace('[0m', '</span>', $log);
|
|
|
|
|
|
|
|
return $log;
|
|
|
|
}
|
|
|
|
}
|