2013-05-10 13:28:43 +02:00
|
|
|
<?php
|
2013-05-16 03:16:56 +02:00
|
|
|
/**
|
|
|
|
* PHPCI - Continuous Integration for PHP
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2013, Block 8 Limited.
|
|
|
|
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
|
|
|
* @link http://www.phptesting.org/
|
|
|
|
*/
|
2013-05-10 13:28:43 +02:00
|
|
|
|
|
|
|
namespace PHPCI\Controller;
|
|
|
|
use b8,
|
2013-05-10 17:36:25 +02:00
|
|
|
b8\Registry,
|
2013-05-10 13:28:43 +02:00
|
|
|
PHPCI\Model\Build;
|
|
|
|
|
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-10 13:28:43 +02:00
|
|
|
class BuildController extends b8\Controller
|
|
|
|
{
|
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
$this->_buildStore = b8\Store\Factory::getStore('Build');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function view($buildId)
|
|
|
|
{
|
|
|
|
$build = $this->_buildStore->getById($buildId);
|
|
|
|
|
|
|
|
$view = new b8\View('Build');
|
|
|
|
$view->build = $build;
|
|
|
|
$view->data = $this->getBuildData($buildId);
|
|
|
|
|
|
|
|
return $view->render();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function data($buildId)
|
|
|
|
{
|
|
|
|
die($this->getBuildData($buildId));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getBuildData($buildId)
|
|
|
|
{
|
|
|
|
$build = $this->_buildStore->getById($buildId);
|
|
|
|
|
|
|
|
$data = array();
|
|
|
|
$data['status'] = (int)$build->getStatus();
|
|
|
|
$data['log'] = $this->cleanLog($build->getLog());
|
2013-05-14 14:09:54 +02:00
|
|
|
$data['plugins'] = json_decode($build->getPlugins(), true);
|
2013-05-10 13:28:43 +02:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function rebuild($buildId)
|
|
|
|
{
|
|
|
|
$copy = $this->_buildStore->getById($buildId);
|
|
|
|
|
|
|
|
$build = new Build();
|
|
|
|
$build->setProjectId($copy->getProjectId());
|
|
|
|
$build->setCommitId($copy->getCommitId());
|
|
|
|
$build->setStatus(0);
|
|
|
|
$build->setBranch($copy->getBranch());
|
|
|
|
$build->setCreated(new \DateTime());
|
|
|
|
|
|
|
|
$build = $this->_buildStore->save($build);
|
|
|
|
|
|
|
|
header('Location: /build/view/' . $build->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete($buildId)
|
|
|
|
{
|
2013-05-10 17:25:51 +02:00
|
|
|
if(!Registry::getInstance()->get('user')->getIsAdmin())
|
|
|
|
{
|
|
|
|
throw new \Exception('You do not have permission to do that.');
|
|
|
|
}
|
|
|
|
|
2013-05-10 13:28:43 +02:00
|
|
|
$build = $this->_buildStore->getById($buildId);
|
|
|
|
$this->_buildStore->delete($build);
|
|
|
|
|
|
|
|
header('Location: /project/view/' . $build->getProjectId());
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|