2013-05-03 17:02:53 +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-03 17:02:53 +02:00
|
|
|
|
|
|
|
namespace PHPCI\Model;
|
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
use b8\Store\Factory;
|
2013-05-16 16:40:40 +02:00
|
|
|
use PHPCI\Model\Base\BuildBase;
|
2014-03-15 06:22:59 +01:00
|
|
|
use PHPCI\Builder;
|
|
|
|
use Symfony\Component\Yaml\Parser as YamlParser;
|
2013-05-03 17:02:53 +02:00
|
|
|
|
|
|
|
/**
|
2013-05-16 03:30:48 +02:00
|
|
|
* Build Model
|
2013-05-16 16:40:40 +02:00
|
|
|
* @uses PHPCI\Model\Base\BuildBase
|
2013-05-16 03:30:48 +02:00
|
|
|
* @author Dan Cryer <dan@block8.co.uk>
|
|
|
|
* @package PHPCI
|
|
|
|
* @subpackage Core
|
|
|
|
*/
|
2013-05-03 17:02:53 +02:00
|
|
|
class Build extends BuildBase
|
|
|
|
{
|
2013-10-10 02:01:06 +02:00
|
|
|
const STATUS_NEW = 0;
|
|
|
|
const STATUS_RUNNING = 1;
|
|
|
|
const STATUS_SUCCESS = 2;
|
|
|
|
const STATUS_FAILED = 3;
|
|
|
|
|
2014-02-25 11:16:58 +01:00
|
|
|
public $currentBuildPath = null;
|
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* Get link to commit from another source (i.e. Github)
|
|
|
|
*/
|
2013-05-16 16:40:40 +02:00
|
|
|
public function getCommitLink()
|
|
|
|
{
|
|
|
|
return '#';
|
|
|
|
}
|
2013-05-10 13:28:43 +02:00
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* Get link to branch from another source (i.e. Github)
|
|
|
|
*/
|
2013-05-16 16:40:40 +02:00
|
|
|
public function getBranchLink()
|
|
|
|
{
|
|
|
|
return '#';
|
|
|
|
}
|
2013-05-14 17:58:14 +02:00
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Return a template to use to generate a link to a specific file.
|
|
|
|
* @return null
|
|
|
|
*/
|
2014-07-14 17:02:36 +02:00
|
|
|
public function getFileLinkTemplate()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* Send status updates to any relevant third parties (i.e. Github)
|
|
|
|
*/
|
2013-05-16 16:40:40 +02:00
|
|
|
public function sendStatusPostback()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-05-14 18:49:39 +02:00
|
|
|
|
2014-07-14 17:02:36 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getProjectTitle()
|
|
|
|
{
|
|
|
|
$project = $this->getProject();
|
|
|
|
return $project ? $project->getTitle() : "";
|
|
|
|
}
|
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
2013-10-10 02:01:06 +02:00
|
|
|
* Store build metadata
|
|
|
|
*/
|
|
|
|
public function storeMeta($key, $value)
|
|
|
|
{
|
|
|
|
$value = json_encode($value);
|
|
|
|
Factory::getStore('Build')->setMeta($this->getProjectId(), $this->getId(), $key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is this build successful?
|
|
|
|
*/
|
|
|
|
public function isSuccessful()
|
2013-05-16 16:40:40 +02:00
|
|
|
{
|
2013-10-10 02:01:06 +02:00
|
|
|
return ($this->getStatus() === self::STATUS_SUCCESS);
|
2013-05-16 16:40:40 +02:00
|
|
|
}
|
2014-03-15 06:22:59 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Builder $builder
|
|
|
|
* @param string $buildPath
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function handleConfig(Builder $builder, $buildPath)
|
|
|
|
{
|
2014-04-25 11:17:39 +02:00
|
|
|
$build_config = null;
|
|
|
|
|
|
|
|
// Try phpci.yml first:
|
2014-04-10 18:41:02 +02:00
|
|
|
if (is_file($buildPath . '/phpci.yml')) {
|
|
|
|
$build_config = file_get_contents($buildPath . '/phpci.yml');
|
|
|
|
}
|
|
|
|
|
2014-04-25 11:17:39 +02:00
|
|
|
// Try getting the project build config from the database:
|
|
|
|
if (empty($build_config)) {
|
2014-04-10 18:41:02 +02:00
|
|
|
$build_config = $this->getProject()->getBuildConfig();
|
2014-04-25 11:17:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to zero config plugins:
|
|
|
|
if (empty($build_config)) {
|
|
|
|
$build_config = $this->getZeroConfigPlugins($builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_string($build_config)) {
|
|
|
|
$yamlParser = new YamlParser();
|
|
|
|
$build_config = $yamlParser->parse($build_config);
|
|
|
|
}
|
|
|
|
|
|
|
|
$builder->setConfigArray($build_config);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Get an array of plugins to run if there's no phpci.yml file.
|
|
|
|
* @param Builder $builder
|
|
|
|
* @return array
|
|
|
|
*/
|
2014-04-25 11:17:39 +02:00
|
|
|
protected function getZeroConfigPlugins(Builder $builder)
|
|
|
|
{
|
|
|
|
$pluginDir = PHPCI_DIR . 'PHPCI/Plugin/';
|
|
|
|
$dir = new \DirectoryIterator($pluginDir);
|
|
|
|
|
|
|
|
$config = array(
|
|
|
|
'build_settings' => array(
|
|
|
|
'ignore' => array(
|
2014-05-08 18:02:51 +02:00
|
|
|
'vendor',
|
2014-04-25 11:17:39 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($dir as $item) {
|
|
|
|
if ($item->isDot()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$item->isFile()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($item->getExtension() != 'php') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$className = '\PHPCI\Plugin\\'.$item->getBasename('.php');
|
|
|
|
|
|
|
|
$reflectedPlugin = new \ReflectionClass($className);
|
|
|
|
|
|
|
|
if (!$reflectedPlugin->implementsInterface('\PHPCI\ZeroConfigPlugin')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (array('setup', 'test', 'complete', 'success', 'failure') as $stage) {
|
|
|
|
if ($className::canExecute($stage, $builder, $this)) {
|
2014-04-30 15:18:58 +02:00
|
|
|
$config[$stage][$className] = array(
|
|
|
|
'zero_config' => true
|
|
|
|
);
|
2014-04-25 11:17:39 +02:00
|
|
|
}
|
2014-03-15 06:22:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-25 11:17:39 +02:00
|
|
|
return $config;
|
2014-03-15 06:22:59 +01:00
|
|
|
}
|
2014-04-25 13:28:27 +02:00
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Return a value from the build's "extra" JSON array.
|
|
|
|
* @param null $key
|
|
|
|
* @return mixed|null|string
|
|
|
|
*/
|
2014-05-12 15:07:09 +02:00
|
|
|
public function getExtra($key = null)
|
|
|
|
{
|
2014-05-12 16:22:32 +02:00
|
|
|
$data = json_decode($this->data['extra'], true);
|
2014-05-12 15:07:09 +02:00
|
|
|
|
|
|
|
if (is_null($key)) {
|
|
|
|
$rtn = $data;
|
|
|
|
} elseif (isset($data[$key])) {
|
|
|
|
$rtn = $data[$key];
|
|
|
|
} else {
|
|
|
|
$rtn = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
2015-01-31 10:23:29 +01:00
|
|
|
|
2015-02-03 12:10:22 +01:00
|
|
|
/**
|
|
|
|
* Returns the commit message for this build.
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-01-31 10:23:29 +01:00
|
|
|
public function getCommitMessage()
|
|
|
|
{
|
|
|
|
$rtn = htmlspecialchars($this->data['commit_message']);
|
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
2015-02-20 14:37:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows specific build types (e.g. Github) to report violations back to their respective services.
|
|
|
|
* @param Builder $builder
|
|
|
|
* @param $file
|
|
|
|
* @param $line
|
|
|
|
* @param $message
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function reportError(Builder $builder, $file, $line, $message)
|
|
|
|
{
|
|
|
|
return array($builder, $file, $line, $message);
|
|
|
|
}
|
2013-05-03 17:02:53 +02:00
|
|
|
}
|