2013-05-03 17:02:53 +02:00
|
|
|
<?php
|
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
namespace PHPCensor\Model;
|
2013-05-03 17:02:53 +02:00
|
|
|
|
2017-02-16 13:45:50 +01:00
|
|
|
use PHPCensor\Model;
|
2013-05-17 17:10:54 +02:00
|
|
|
use b8\Store;
|
2017-02-16 13:45:50 +01:00
|
|
|
use b8\Store\Factory;
|
2017-03-23 13:53:24 +01:00
|
|
|
use PHPCensor\Store\EnvironmentStore;
|
|
|
|
use Symfony\Component\Yaml\Parser as YamlParser;
|
|
|
|
use Symfony\Component\Yaml\Dumper as YamlDumper;
|
2013-05-03 17:02:53 +02:00
|
|
|
|
|
|
|
/**
|
2017-02-16 13:45:50 +01:00
|
|
|
* @author Dan Cryer <dan@block8.co.uk>
|
|
|
|
*/
|
|
|
|
class Project extends Model
|
2013-05-03 17:02:53 +02:00
|
|
|
{
|
2017-02-16 13:45:50 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public static $sleepable = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $tableName = 'project';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $modelName = 'Project';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $data = [
|
|
|
|
'id' => null,
|
|
|
|
'title' => null,
|
|
|
|
'reference' => null,
|
|
|
|
'branch' => null,
|
2017-09-13 16:21:49 +02:00
|
|
|
'default_branch_only' => null,
|
2017-02-16 13:45:50 +01:00
|
|
|
'ssh_private_key' => null,
|
|
|
|
'type' => null,
|
|
|
|
'access_information' => null,
|
|
|
|
'last_commit' => null,
|
|
|
|
'build_config' => null,
|
|
|
|
'ssh_public_key' => null,
|
|
|
|
'allow_public_status' => null,
|
|
|
|
'archived' => null,
|
|
|
|
'group_id' => null,
|
2017-10-19 17:05:00 +02:00
|
|
|
'create_date' => null,
|
|
|
|
'user_id' => 0,
|
2017-02-16 13:45:50 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $getters = [
|
|
|
|
// Direct property getters:
|
|
|
|
'id' => 'getId',
|
|
|
|
'title' => 'getTitle',
|
|
|
|
'reference' => 'getReference',
|
|
|
|
'branch' => 'getBranch',
|
2017-09-13 16:21:49 +02:00
|
|
|
'default_branch_only' => 'getDefaultBranchOnly',
|
2017-02-16 13:45:50 +01:00
|
|
|
'ssh_private_key' => 'getSshPrivateKey',
|
|
|
|
'type' => 'getType',
|
|
|
|
'access_information' => 'getAccessInformation',
|
|
|
|
'last_commit' => 'getLastCommit',
|
|
|
|
'build_config' => 'getBuildConfig',
|
|
|
|
'ssh_public_key' => 'getSshPublicKey',
|
|
|
|
'allow_public_status' => 'getAllowPublicStatus',
|
|
|
|
'archived' => 'getArchived',
|
|
|
|
'group_id' => 'getGroupId',
|
2017-10-19 17:05:00 +02:00
|
|
|
'create_date' => 'getCreateDate',
|
|
|
|
'user_id' => 'getUserId',
|
2017-10-15 15:05:33 +02:00
|
|
|
|
2017-02-16 13:45:50 +01:00
|
|
|
// Foreign key getters:
|
2017-10-15 15:05:33 +02:00
|
|
|
'Group' => 'getGroup',
|
2017-02-16 13:45:50 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $setters = [
|
|
|
|
// Direct property setters:
|
|
|
|
'id' => 'setId',
|
|
|
|
'title' => 'setTitle',
|
|
|
|
'reference' => 'setReference',
|
|
|
|
'branch' => 'setBranch',
|
2017-09-13 16:21:49 +02:00
|
|
|
'default_branch_only' => 'setDefaultBranchOnly',
|
2017-02-16 13:45:50 +01:00
|
|
|
'ssh_private_key' => 'setSshPrivateKey',
|
|
|
|
'type' => 'setType',
|
|
|
|
'access_information' => 'setAccessInformation',
|
|
|
|
'last_commit' => 'setLastCommit',
|
|
|
|
'build_config' => 'setBuildConfig',
|
|
|
|
'ssh_public_key' => 'setSshPublicKey',
|
|
|
|
'allow_public_status' => 'setAllowPublicStatus',
|
|
|
|
'archived' => 'setArchived',
|
|
|
|
'group_id' => 'setGroupId',
|
2017-10-19 17:05:00 +02:00
|
|
|
'create_date' => 'setCreateDate',
|
|
|
|
'user_id' => 'setUserId',
|
2017-02-16 13:45:50 +01:00
|
|
|
|
2017-10-15 15:05:33 +02:00
|
|
|
// Foreign key setters:
|
|
|
|
'Group' => 'setGroup',
|
2017-02-16 13:45:50 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getId()
|
|
|
|
{
|
2017-10-15 15:05:33 +02:00
|
|
|
$rtn = $this->data['id'];
|
2017-02-16 13:45:50 +01:00
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTitle()
|
|
|
|
{
|
2017-10-15 15:05:33 +02:00
|
|
|
$rtn = $this->data['title'];
|
2017-02-16 13:45:50 +01:00
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getReference()
|
|
|
|
{
|
2017-10-15 15:05:33 +02:00
|
|
|
$rtn = $this->data['reference'];
|
2017-02-16 13:45:50 +01:00
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSshPrivateKey()
|
|
|
|
{
|
2017-10-15 15:05:33 +02:00
|
|
|
$rtn = $this->data['ssh_private_key'];
|
2017-02-16 13:45:50 +01:00
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getType()
|
|
|
|
{
|
2017-10-15 15:05:33 +02:00
|
|
|
$rtn = $this->data['type'];
|
2017-02-16 13:45:50 +01:00
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLastCommit()
|
|
|
|
{
|
2017-10-15 15:05:33 +02:00
|
|
|
$rtn = $this->data['last_commit'];
|
2017-02-16 13:45:50 +01:00
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getBuildConfig()
|
|
|
|
{
|
|
|
|
$rtn = $this->data['build_config'];
|
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSshPublicKey()
|
|
|
|
{
|
2017-10-15 15:05:33 +02:00
|
|
|
$rtn = $this->data['ssh_public_key'];
|
2017-02-16 13:45:50 +01:00
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getAllowPublicStatus()
|
|
|
|
{
|
|
|
|
$rtn = $this->data['allow_public_status'];
|
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getArchived()
|
|
|
|
{
|
2017-09-13 16:21:49 +02:00
|
|
|
$rtn = $this->data['archived'];
|
2017-02-16 13:45:50 +01:00
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getGroupId()
|
|
|
|
{
|
2017-09-13 16:21:49 +02:00
|
|
|
$rtn = $this->data['group_id'];
|
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getDefaultBranchOnly()
|
|
|
|
{
|
|
|
|
$rtn = $this->data['default_branch_only'];
|
2017-02-16 13:45:50 +01:00
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $value int
|
|
|
|
*/
|
|
|
|
public function setId($value)
|
|
|
|
{
|
2017-10-15 16:58:36 +02:00
|
|
|
$this->validateNotNull('id', $value);
|
|
|
|
$this->validateInt('id', $value);
|
2017-02-16 13:45:50 +01:00
|
|
|
|
|
|
|
if ($this->data['id'] === $value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data['id'] = $value;
|
|
|
|
|
|
|
|
$this->setModified('id');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $value string
|
|
|
|
*/
|
|
|
|
public function setTitle($value)
|
|
|
|
{
|
2017-10-15 16:58:36 +02:00
|
|
|
$this->validateNotNull('title', $value);
|
|
|
|
$this->validateString('title', $value);
|
2017-02-16 13:45:50 +01:00
|
|
|
|
|
|
|
if ($this->data['title'] === $value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data['title'] = $value;
|
|
|
|
|
|
|
|
$this->setModified('title');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $value string
|
|
|
|
*/
|
|
|
|
public function setReference($value)
|
|
|
|
{
|
2017-10-15 16:58:36 +02:00
|
|
|
$this->validateNotNull('reference', $value);
|
|
|
|
$this->validateString('reference', $value);
|
2017-02-16 13:45:50 +01:00
|
|
|
|
|
|
|
if ($this->data['reference'] === $value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data['reference'] = $value;
|
|
|
|
|
|
|
|
$this->setModified('reference');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $value string
|
|
|
|
*/
|
|
|
|
public function setBranch($value)
|
|
|
|
{
|
2017-10-15 16:58:36 +02:00
|
|
|
$this->validateNotNull('branch', $value);
|
|
|
|
$this->validateString('branch', $value);
|
2017-02-16 13:45:50 +01:00
|
|
|
|
|
|
|
if ($this->data['branch'] === $value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data['branch'] = $value;
|
|
|
|
|
|
|
|
$this->setModified('branch');
|
|
|
|
}
|
|
|
|
|
2017-09-13 16:21:49 +02:00
|
|
|
/**
|
|
|
|
* @param $value int
|
|
|
|
*/
|
|
|
|
public function setDefaultBranchOnly($value)
|
|
|
|
{
|
2017-10-15 16:58:36 +02:00
|
|
|
$this->validateNotNull('default_branch_only', $value);
|
|
|
|
$this->validateInt('default_branch_only', $value);
|
2017-09-13 16:21:49 +02:00
|
|
|
|
|
|
|
if ($this->data['default_branch_only'] === $value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data['default_branch_only'] = $value;
|
|
|
|
|
|
|
|
$this->setModified('default_branch_only');
|
|
|
|
}
|
|
|
|
|
2017-02-16 13:45:50 +01:00
|
|
|
/**
|
|
|
|
* @param $value string
|
|
|
|
*/
|
|
|
|
public function setSshPrivateKey($value)
|
|
|
|
{
|
2017-10-15 16:58:36 +02:00
|
|
|
$this->validateString('ssh_private_key', $value);
|
2017-02-16 13:45:50 +01:00
|
|
|
|
|
|
|
if ($this->data['ssh_private_key'] === $value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data['ssh_private_key'] = $value;
|
|
|
|
|
|
|
|
$this->setModified('ssh_private_key');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $value string
|
|
|
|
*/
|
|
|
|
public function setType($value)
|
|
|
|
{
|
2017-10-15 16:58:36 +02:00
|
|
|
$this->validateNotNull('type', $value);
|
|
|
|
$this->validateString('type', $value);
|
2017-02-16 13:45:50 +01:00
|
|
|
|
|
|
|
if ($this->data['type'] === $value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data['type'] = $value;
|
|
|
|
|
|
|
|
$this->setModified('type');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $value string
|
|
|
|
*/
|
|
|
|
public function setLastCommit($value)
|
|
|
|
{
|
2017-10-15 16:58:36 +02:00
|
|
|
$this->validateString('last_commit', $value);
|
2017-02-16 13:45:50 +01:00
|
|
|
|
|
|
|
if ($this->data['last_commit'] === $value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data['last_commit'] = $value;
|
|
|
|
|
|
|
|
$this->setModified('last_commit');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $value string
|
|
|
|
*/
|
|
|
|
public function setBuildConfig($value)
|
|
|
|
{
|
2017-10-15 16:58:36 +02:00
|
|
|
$this->validateString('build_config', $value);
|
2017-02-16 13:45:50 +01:00
|
|
|
|
|
|
|
if ($this->data['build_config'] === $value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data['build_config'] = $value;
|
|
|
|
|
|
|
|
$this->setModified('build_config');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $value string
|
|
|
|
*/
|
|
|
|
public function setSshPublicKey($value)
|
|
|
|
{
|
2017-10-15 16:58:36 +02:00
|
|
|
$this->validateString('ssh_public_key', $value);
|
2017-02-16 13:45:50 +01:00
|
|
|
|
|
|
|
if ($this->data['ssh_public_key'] === $value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data['ssh_public_key'] = $value;
|
|
|
|
|
|
|
|
$this->setModified('ssh_public_key');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $value int
|
|
|
|
*/
|
|
|
|
public function setAllowPublicStatus($value)
|
|
|
|
{
|
2017-10-15 16:58:36 +02:00
|
|
|
$this->validateNotNull('allow_public_status', $value);
|
|
|
|
$this->validateInt('allow_public_status', $value);
|
2017-02-16 13:45:50 +01:00
|
|
|
|
|
|
|
if ($this->data['allow_public_status'] === $value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data['allow_public_status'] = $value;
|
|
|
|
|
|
|
|
$this->setModified('allow_public_status');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $value int
|
|
|
|
*/
|
|
|
|
public function setArchived($value)
|
|
|
|
{
|
2017-10-15 16:58:36 +02:00
|
|
|
$this->validateNotNull('archived', $value);
|
|
|
|
$this->validateInt('archived', $value);
|
2017-02-16 13:45:50 +01:00
|
|
|
|
|
|
|
if ($this->data['archived'] === $value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data['archived'] = $value;
|
|
|
|
|
|
|
|
$this->setModified('archived');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $value int
|
|
|
|
*/
|
|
|
|
public function setGroupId($value)
|
|
|
|
{
|
2017-10-15 16:58:36 +02:00
|
|
|
$this->validateNotNull('group_id', $value);
|
|
|
|
$this->validateInt('group_id', $value);
|
2017-02-16 13:45:50 +01:00
|
|
|
|
|
|
|
if ($this->data['group_id'] === $value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data['group_id'] = $value;
|
|
|
|
|
|
|
|
$this->setModified('group_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ProjectGroup model for this Project by Id.
|
|
|
|
*
|
|
|
|
* @return \PHPCensor\Model\ProjectGroup
|
|
|
|
*/
|
|
|
|
public function getGroup()
|
|
|
|
{
|
|
|
|
$key = $this->getGroupId();
|
|
|
|
|
|
|
|
if (empty($key)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-10-15 16:58:36 +02:00
|
|
|
$cacheKey = 'Cache.ProjectGroup.' . $key;
|
|
|
|
$rtn = $this->cache->get($cacheKey, null);
|
2017-02-16 13:45:50 +01:00
|
|
|
|
|
|
|
if (empty($rtn)) {
|
2017-10-15 16:58:36 +02:00
|
|
|
$rtn = Factory::getStore('ProjectGroup', 'PHPCensor')->getById($key);
|
2017-02-16 13:45:50 +01:00
|
|
|
$this->cache->set($cacheKey, $rtn);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set Group - Accepts an ID, an array representing a ProjectGroup or a ProjectGroup model.
|
|
|
|
*
|
|
|
|
* @param $value mixed
|
|
|
|
*/
|
|
|
|
public function setGroup($value)
|
|
|
|
{
|
|
|
|
// Is this an instance of ProjectGroup?
|
|
|
|
if ($value instanceof ProjectGroup) {
|
|
|
|
return $this->setGroupObject($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this an array representing a ProjectGroup item?
|
|
|
|
if (is_array($value) && !empty($value['id'])) {
|
|
|
|
return $this->setGroupId($value['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this a scalar value representing the ID of this foreign key?
|
|
|
|
return $this->setGroupId($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set Group - Accepts a ProjectGroup model.
|
|
|
|
*
|
|
|
|
* @param $value ProjectGroup
|
|
|
|
*/
|
|
|
|
public function setGroupObject(ProjectGroup $value)
|
|
|
|
{
|
|
|
|
return $this->setGroupId($value->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Build models by ProjectId for this Project.
|
|
|
|
*
|
|
|
|
* @return \PHPCensor\Model\Build[]
|
|
|
|
*/
|
|
|
|
public function getProjectBuilds()
|
|
|
|
{
|
|
|
|
return Factory::getStore('Build', 'PHPCensor')->getByProjectId($this->getId());
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Return the latest build from a specific branch, of a specific status, for this project.
|
2017-10-15 16:58:36 +02:00
|
|
|
*
|
2014-12-08 12:25:33 +01:00
|
|
|
* @param string $branch
|
2017-10-15 16:58:36 +02:00
|
|
|
* @param null $status
|
|
|
|
*
|
2014-12-08 12:25:33 +01:00
|
|
|
* @return mixed|null
|
|
|
|
*/
|
2013-05-17 17:10:54 +02:00
|
|
|
public function getLatestBuild($branch = 'master', $status = null)
|
|
|
|
{
|
2016-04-20 17:39:48 +02:00
|
|
|
$criteria = ['branch' => $branch, 'project_id' => $this->getId()];
|
2013-05-17 17:10:54 +02:00
|
|
|
|
|
|
|
if (isset($status)) {
|
|
|
|
$criteria['status'] = $status;
|
|
|
|
}
|
|
|
|
|
2016-04-20 17:39:48 +02:00
|
|
|
$order = ['id' => 'DESC'];
|
|
|
|
$builds = Store\Factory::getStore('Build')->getWhere($criteria, 1, 0, [], $order);
|
2013-05-17 17:10:54 +02:00
|
|
|
|
|
|
|
if (is_array($builds['items']) && count($builds['items'])) {
|
|
|
|
$latest = array_shift($builds['items']);
|
|
|
|
|
|
|
|
if (isset($latest) && $latest instanceof Build) {
|
|
|
|
return $latest;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2014-02-26 16:23:24 +01:00
|
|
|
|
2015-10-13 17:22:39 +02:00
|
|
|
/**
|
|
|
|
* Return the previous build from a specific branch, for this project.
|
2017-10-15 16:58:36 +02:00
|
|
|
*
|
2015-10-13 17:22:39 +02:00
|
|
|
* @param string $branch
|
2017-10-15 16:58:36 +02:00
|
|
|
*
|
2015-10-13 17:22:39 +02:00
|
|
|
* @return mixed|null
|
|
|
|
*/
|
|
|
|
public function getPreviousBuild($branch = 'master')
|
|
|
|
{
|
2016-04-20 17:39:48 +02:00
|
|
|
$criteria = ['branch' => $branch, 'project_id' => $this->getId()];
|
|
|
|
$order = ['id' => 'DESC'];
|
|
|
|
$builds = Store\Factory::getStore('Build')->getWhere($criteria, 1, 1, [], $order);
|
2015-10-13 17:22:39 +02:00
|
|
|
|
|
|
|
if (is_array($builds['items']) && count($builds['items'])) {
|
|
|
|
$previous = array_shift($builds['items']);
|
|
|
|
|
|
|
|
if (isset($previous) && $previous instanceof Build) {
|
|
|
|
return $previous;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* @param string|array $value
|
|
|
|
*/
|
2014-07-30 16:13:50 +02:00
|
|
|
public function setAccessInformation($value)
|
|
|
|
{
|
|
|
|
if (is_array($value)) {
|
|
|
|
$value = json_encode($value);
|
|
|
|
}
|
|
|
|
|
2017-10-15 16:58:36 +02:00
|
|
|
$this->validateString('access_information', $value);
|
2017-02-16 13:45:50 +01:00
|
|
|
|
|
|
|
if ($this->data['access_information'] === $value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data['access_information'] = $value;
|
|
|
|
|
|
|
|
$this->setModified('access_information');
|
2014-07-30 16:13:50 +02:00
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Get this project's access_information data. Pass a specific key or null for all data.
|
2017-10-15 16:58:36 +02:00
|
|
|
*
|
2014-12-08 12:25:33 +01:00
|
|
|
* @param string|null $key
|
2017-10-15 16:58:36 +02:00
|
|
|
*
|
2014-12-08 12:25:33 +01:00
|
|
|
* @return mixed|null|string
|
|
|
|
*/
|
2014-02-26 16:23:24 +01:00
|
|
|
public function getAccessInformation($key = null)
|
|
|
|
{
|
2014-07-30 16:13:50 +02:00
|
|
|
$info = $this->data['access_information'];
|
|
|
|
|
|
|
|
// Handle old-format (serialized) access information first:
|
2016-08-25 18:13:19 +02:00
|
|
|
if (!empty($info) && !in_array(substr($info, 0, 1), ['{', '['])) {
|
2014-07-30 16:13:50 +02:00
|
|
|
$data = unserialize($info);
|
|
|
|
} else {
|
|
|
|
$data = json_decode($info, true);
|
|
|
|
}
|
2014-02-26 16:23:24 +01:00
|
|
|
|
|
|
|
if (is_null($key)) {
|
|
|
|
$rtn = $data;
|
|
|
|
} elseif (isset($data[$key])) {
|
|
|
|
$rtn = $data[$key];
|
|
|
|
} else {
|
|
|
|
$rtn = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
2014-07-14 15:59:04 +02:00
|
|
|
|
2017-10-19 17:05:00 +02:00
|
|
|
/**
|
|
|
|
* @return \DateTime
|
|
|
|
*/
|
|
|
|
public function getCreateDate()
|
|
|
|
{
|
|
|
|
$rtn = $this->data['create_date'];
|
|
|
|
|
|
|
|
if (!empty($rtn)) {
|
|
|
|
$rtn = new \DateTime($rtn);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $value \DateTime
|
|
|
|
*/
|
|
|
|
public function setCreateDate($value)
|
|
|
|
{
|
|
|
|
$this->validateDate('create_date', $value);
|
|
|
|
|
|
|
|
if ($this->data['create_date'] === $value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data['create_date'] = $value;
|
|
|
|
|
|
|
|
$this->setModified('create_date');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getUserId()
|
|
|
|
{
|
|
|
|
$rtn = $this->data['user_id'];
|
|
|
|
|
|
|
|
return (integer)$rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $value integer
|
|
|
|
*/
|
|
|
|
public function setUserId($value)
|
|
|
|
{
|
|
|
|
$this->validateNotNull('user_id', $value);
|
|
|
|
$this->validateInt('user_id', $value);
|
|
|
|
|
|
|
|
if ($this->data['user_id'] === $value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data['user_id'] = $value;
|
|
|
|
|
|
|
|
$this->setModified('user_id');
|
|
|
|
}
|
|
|
|
|
2014-07-14 15:59:04 +02:00
|
|
|
/**
|
2017-10-15 16:58:36 +02:00
|
|
|
* Get the value of branch.
|
2014-07-14 15:59:04 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getBranch()
|
|
|
|
{
|
|
|
|
if (empty($this->data['branch'])) {
|
2017-05-17 16:46:55 +02:00
|
|
|
$projectType = $this->getType();
|
|
|
|
switch ($projectType) {
|
|
|
|
case 'hg':
|
|
|
|
$branch = 'default';
|
|
|
|
break;
|
|
|
|
case 'svn':
|
|
|
|
$branch = 'trunk';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$branch = 'master';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $branch;
|
2014-07-14 15:59:04 +02:00
|
|
|
} else {
|
|
|
|
return $this->data['branch'];
|
|
|
|
}
|
|
|
|
}
|
2014-12-02 17:26:55 +01:00
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Return the name of a FontAwesome icon to represent this project, depending on its type.
|
2017-10-15 16:58:36 +02:00
|
|
|
*
|
2014-12-08 12:25:33 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2014-12-02 17:26:55 +01:00
|
|
|
public function getIcon()
|
|
|
|
{
|
|
|
|
switch ($this->getType()) {
|
|
|
|
case 'github':
|
|
|
|
$icon = 'github';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'bitbucket':
|
2017-03-19 05:17:10 +01:00
|
|
|
case 'bitbuckethg':
|
2014-12-02 17:26:55 +01:00
|
|
|
$icon = 'bitbucket';
|
|
|
|
break;
|
|
|
|
|
2017-02-11 10:49:56 +01:00
|
|
|
case 'remote':
|
2014-12-02 17:26:55 +01:00
|
|
|
case 'gitlab':
|
|
|
|
default:
|
2017-02-11 10:49:56 +01:00
|
|
|
$icon = 'code-fork';
|
2014-12-02 17:26:55 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $icon;
|
|
|
|
}
|
2017-03-23 13:53:24 +01:00
|
|
|
|
2017-03-25 23:02:27 +01:00
|
|
|
/**
|
|
|
|
* @return EnvironmentStore
|
|
|
|
*/
|
|
|
|
protected function getEnvironmentStore()
|
|
|
|
{
|
|
|
|
/** @var EnvironmentStore $store */
|
|
|
|
$store = Factory::getStore('Environment', 'PHPCensor');
|
|
|
|
return $store;
|
|
|
|
}
|
|
|
|
|
2017-03-23 13:53:24 +01:00
|
|
|
/**
|
|
|
|
* Get Environments
|
|
|
|
*
|
|
|
|
* @return array contain items with \PHPCensor\Model\Environment
|
|
|
|
*/
|
|
|
|
public function getEnvironmentsObjects()
|
|
|
|
{
|
|
|
|
$key = $this->getId();
|
|
|
|
|
|
|
|
if (empty($key)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$cacheKey = 'Cache.ProjectEnvironments.' . $key;
|
2017-05-17 16:46:55 +02:00
|
|
|
$rtn = $this->cache->get($cacheKey, null);
|
2017-03-23 13:53:24 +01:00
|
|
|
|
|
|
|
if (empty($rtn)) {
|
2017-03-25 23:02:27 +01:00
|
|
|
$store = $this->getEnvironmentStore();
|
2017-10-15 16:58:36 +02:00
|
|
|
$rtn = $store->getByProjectId($key);
|
2017-03-23 13:53:24 +01:00
|
|
|
$this->cache->set($cacheKey, $rtn);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Environments
|
|
|
|
*
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getEnvironmentsNames()
|
|
|
|
{
|
2017-05-17 16:46:55 +02:00
|
|
|
$environments = $this->getEnvironmentsObjects();
|
2017-03-23 13:53:24 +01:00
|
|
|
$environments_names = [];
|
|
|
|
foreach($environments['items'] as $environment) {
|
|
|
|
/** @var Environment $environment */
|
|
|
|
$environments_names[] = $environment->getName();
|
|
|
|
}
|
2017-05-17 16:46:55 +02:00
|
|
|
|
2017-03-23 13:53:24 +01:00
|
|
|
return $environments_names;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Environments
|
|
|
|
*
|
|
|
|
* @return string yaml
|
|
|
|
*/
|
|
|
|
public function getEnvironments()
|
|
|
|
{
|
2017-05-17 16:46:55 +02:00
|
|
|
$environments = $this->getEnvironmentsObjects();
|
2017-03-23 13:53:24 +01:00
|
|
|
$environments_config = [];
|
|
|
|
foreach($environments['items'] as $environment) {
|
|
|
|
/** @var Environment $environment */
|
|
|
|
$environments_config[$environment->getName()] = $environment->getBranches();
|
|
|
|
}
|
2017-05-17 16:46:55 +02:00
|
|
|
|
2017-03-23 13:53:24 +01:00
|
|
|
$yaml_dumper = new YamlDumper();
|
2017-05-17 16:46:55 +02:00
|
|
|
$value = $yaml_dumper->dump($environments_config, 10, 0, true, false);
|
|
|
|
|
2017-03-23 13:53:24 +01:00
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set Environments
|
|
|
|
*
|
|
|
|
* @param string $value yaml
|
|
|
|
*/
|
|
|
|
public function setEnvironments($value)
|
|
|
|
{
|
2017-05-17 16:46:55 +02:00
|
|
|
$yaml_parser = new YamlParser();
|
|
|
|
$environments_config = $yaml_parser->parse($value);
|
|
|
|
$environments_names = !empty($environments_config) ? array_keys($environments_config) : [];
|
2017-03-23 13:53:24 +01:00
|
|
|
$current_environments = $this->getEnvironmentsObjects();
|
2017-05-17 16:46:55 +02:00
|
|
|
$store = $this->getEnvironmentStore();
|
2017-03-23 13:53:24 +01:00
|
|
|
foreach ($current_environments['items'] as $environment) {
|
|
|
|
/** @var Environment $environment */
|
|
|
|
$key = array_search($environment->getName(), $environments_names);
|
|
|
|
if ($key !== false) {
|
|
|
|
// already exist
|
|
|
|
unset($environments_names[$key]);
|
|
|
|
$environment->setBranches(!empty($environments_config[$environment->getName()]) ? $environments_config[$environment->getName()] : []);
|
2017-04-05 15:44:07 +02:00
|
|
|
$store->save($environment);
|
2017-03-23 13:53:24 +01:00
|
|
|
} else {
|
|
|
|
// remove
|
|
|
|
$store->delete($environment);
|
|
|
|
}
|
|
|
|
}
|
2017-05-17 16:46:55 +02:00
|
|
|
|
2017-03-23 13:53:24 +01:00
|
|
|
if (!empty($environments_names)) {
|
|
|
|
// add
|
|
|
|
foreach ($environments_names as $environment_name) {
|
|
|
|
$environment = new Environment();
|
|
|
|
$environment->setProjectId($this->getId());
|
|
|
|
$environment->setName($environment_name);
|
|
|
|
$environment->setBranches(!empty($environments_config[$environment->getName()]) ? $environments_config[$environment->getName()] : []);
|
|
|
|
$store->save($environment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $branch
|
2017-05-17 16:46:55 +02:00
|
|
|
*
|
2017-03-23 13:53:24 +01:00
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getEnvironmentsNamesByBranch($branch)
|
|
|
|
{
|
|
|
|
$environments_names = [];
|
2017-05-17 16:46:55 +02:00
|
|
|
$environments = $this->getEnvironmentsObjects();
|
|
|
|
$default_branch = ($branch == $this->getBranch());
|
2017-03-23 13:53:24 +01:00
|
|
|
foreach($environments['items'] as $environment) {
|
|
|
|
/** @var Environment $environment */
|
2017-05-08 07:32:01 +02:00
|
|
|
if ($default_branch || in_array($branch, $environment->getBranches())) {
|
2017-03-23 13:53:24 +01:00
|
|
|
$environments_names[] = $environment->getName();
|
|
|
|
}
|
|
|
|
}
|
2017-05-17 16:46:55 +02:00
|
|
|
|
2017-03-23 13:53:24 +01:00
|
|
|
return $environments_names;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $environment_name
|
2017-05-17 16:46:55 +02:00
|
|
|
*
|
2017-03-23 13:53:24 +01:00
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getBranchesByEnvironment($environment_name)
|
|
|
|
{
|
2017-05-17 16:46:55 +02:00
|
|
|
$branches = [];
|
2017-03-23 13:53:24 +01:00
|
|
|
$environments = $this->getEnvironmentsObjects();
|
|
|
|
foreach($environments['items'] as $environment) {
|
|
|
|
/** @var Environment $environment */
|
|
|
|
if ($environment_name == $environment->getName()) {
|
|
|
|
return $environment->getBranches();
|
|
|
|
}
|
|
|
|
}
|
2017-05-17 16:46:55 +02:00
|
|
|
|
2017-03-23 13:53:24 +01:00
|
|
|
return $branches;
|
|
|
|
}
|
2015-10-13 17:28:37 +02:00
|
|
|
}
|