Initial implementation CI environments
This commit is contained in:
parent
7f8b0234b9
commit
047cedaab3
21 changed files with 850 additions and 42 deletions
|
|
@ -52,6 +52,7 @@ class Build extends Model
|
|||
'committer_email' => null,
|
||||
'commit_message' => null,
|
||||
'extra' => null,
|
||||
'environment' => null,
|
||||
];
|
||||
|
||||
/**
|
||||
|
|
@ -71,6 +72,7 @@ class Build extends Model
|
|||
'committer_email' => 'getCommitterEmail',
|
||||
'commit_message' => 'getCommitMessage',
|
||||
'extra' => 'getExtra',
|
||||
'environment' => 'getEnvironment',
|
||||
|
||||
// Foreign key getters:
|
||||
'Project' => 'getProject',
|
||||
|
|
@ -93,6 +95,7 @@ class Build extends Model
|
|||
'committer_email' => 'setCommitterEmail',
|
||||
'commit_message' => 'setCommitMessage',
|
||||
'extra' => 'setExtra',
|
||||
'environment' => 'setEnvironment',
|
||||
|
||||
// Foreign key setters:
|
||||
'Project' => 'setProject',
|
||||
|
|
@ -165,6 +168,11 @@ class Build extends Model
|
|||
'nullable' => true,
|
||||
'default' => null,
|
||||
],
|
||||
'environment' => [
|
||||
'type' => 'varchar',
|
||||
'length' => 20,
|
||||
'default' => null,
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
|
|
@ -542,6 +550,38 @@ class Build extends Model
|
|||
$this->setModified('extra');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of Extra / extra.
|
||||
*
|
||||
* @param $name string
|
||||
* @param $value mixed
|
||||
*/
|
||||
public function setExtraValue($name, $value)
|
||||
{
|
||||
$extra = json_decode($this->data['extra'], true);
|
||||
if ($extra === false) {
|
||||
$extra = [];
|
||||
}
|
||||
$extra[$name] = $value;
|
||||
$this->setExtra(json_encode($extra));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the values of Extra / extra.
|
||||
*
|
||||
* @param $name string
|
||||
* @param $values mixed
|
||||
*/
|
||||
public function setExtraValues($values)
|
||||
{
|
||||
$extra = json_decode($this->data['extra'], true);
|
||||
if ($extra === false) {
|
||||
$extra = [];
|
||||
}
|
||||
$extra = array_replace($extra, $values);
|
||||
$this->setExtra(json_encode($extra));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Project model for this Build by Id.
|
||||
*
|
||||
|
|
@ -931,6 +971,36 @@ class Build extends Model
|
|||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of Environment / environment.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getEnvironment()
|
||||
{
|
||||
$rtn = $this->data['environment'];
|
||||
|
||||
return $rtn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of Environment / environment.
|
||||
*
|
||||
* @param $value string
|
||||
*/
|
||||
public function setEnvironment($value)
|
||||
{
|
||||
$this->validateString('Environment', $value);
|
||||
|
||||
if ($this->data['environment'] === $value) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->data['environment'] = $value;
|
||||
|
||||
$this->setModified('environment');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an SSH key file on disk for this build.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ namespace PHPCensor\Model\Build;
|
|||
|
||||
use PHPCensor\Model\Build;
|
||||
use PHPCensor\Builder;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
/**
|
||||
* Remote Git Build Model
|
||||
|
|
@ -33,6 +34,10 @@ class RemoteGitBuild extends Build
|
|||
$success = $this->cloneByHttp($builder, $buildPath);
|
||||
}
|
||||
|
||||
if ($success) {
|
||||
$success = $this->mergeBranches($builder, $buildPath);
|
||||
}
|
||||
|
||||
if (!$success) {
|
||||
$builder->logFailure('Failed to clone remote git repository.');
|
||||
return false;
|
||||
|
|
@ -41,6 +46,28 @@ class RemoteGitBuild extends Build
|
|||
return $this->handleConfig($builder, $buildPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Builder $builder
|
||||
* @param string $buildPath
|
||||
* @return bool
|
||||
*/
|
||||
protected function mergeBranches(Builder $builder, $buildPath)
|
||||
{
|
||||
$branches = $this->getExtra('branches');
|
||||
if (!empty($branches)) {
|
||||
$cmd = 'cd "%s" && git merge --quiet origin/%s';
|
||||
foreach ($branches as $branch) {
|
||||
$success = $builder->executeCommand($cmd, $buildPath, $branch);
|
||||
if (!$success) {
|
||||
$builder->log('Fail merge branch origin/'.$branch, LogLevel::ERROR);
|
||||
return false;
|
||||
}
|
||||
$builder->log('Merged branch origin/'.$branch, LogLevel::INFO);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use an HTTP-based git clone.
|
||||
*/
|
||||
|
|
|
|||
233
src/PHPCensor/Model/Environment.php
Normal file
233
src/PHPCensor/Model/Environment.php
Normal file
|
|
@ -0,0 +1,233 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Model;
|
||||
|
||||
use PHPCensor\Model;
|
||||
|
||||
class Environment extends Model
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public static $sleepable = [];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $tableName = 'environment';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $modelName = 'Environment';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $data = [
|
||||
'id' => null,
|
||||
'project_id' => null,
|
||||
'name' => null,
|
||||
'branches' => null,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $getters = [
|
||||
// Direct property getters:
|
||||
'id' => 'getId',
|
||||
'project_id' => 'getProjectId',
|
||||
'name' => 'getName',
|
||||
'branches' => 'getBranches',
|
||||
// Foreign key getters:
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $setters = [
|
||||
// Direct property setters:
|
||||
'id' => 'setId',
|
||||
'project_id' => 'setProjectId',
|
||||
'name' => 'setName',
|
||||
'branches' => 'setBranches',
|
||||
// Foreign key setters:
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $columns = [
|
||||
'id' => [
|
||||
'type' => 'int',
|
||||
'length' => 11,
|
||||
'primary_key' => true,
|
||||
'auto_increment' => true,
|
||||
'default' => null,
|
||||
],
|
||||
'project_id' => [
|
||||
'type' => 'int',
|
||||
'length' => 11,
|
||||
'primary_key' => true,
|
||||
'default' => null,
|
||||
],
|
||||
'name' => [
|
||||
'type' => 'varchar',
|
||||
'length' => 20,
|
||||
'default' => null,
|
||||
],
|
||||
'branches' => [
|
||||
'type' => 'text',
|
||||
'default' => '',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $indexes = [
|
||||
'PRIMARY' => ['unique' => true, 'columns' => 'project_id, name'],
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $foreignKeys = [
|
||||
'environment_ibfk_1' => [
|
||||
'local_col' => 'project_id',
|
||||
'update' => 'CASCADE',
|
||||
'delete' => '',
|
||||
'table' => 'project',
|
||||
'col' => 'id'
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the value of Id / id.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
$rtn = $this->data['id'];
|
||||
|
||||
return $rtn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of Id / id.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getProjectId()
|
||||
{
|
||||
$rtn = $this->data['project_id'];
|
||||
|
||||
return $rtn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of Title / title.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
$rtn = $this->data['name'];
|
||||
|
||||
return $rtn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of Title / title.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getBranches()
|
||||
{
|
||||
$rtn = array_filter(array_map('trim', explode("\n", $this->data['branches'])));
|
||||
|
||||
return $rtn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of Id / id.
|
||||
*
|
||||
* Must not be null.
|
||||
* @param $value int
|
||||
*/
|
||||
public function setId($value)
|
||||
{
|
||||
$this->validateNotNull('Id', $value);
|
||||
$this->validateInt('Id', $value);
|
||||
|
||||
if ($this->data['id'] === $value) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->data['id'] = $value;
|
||||
|
||||
$this->setModified('id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of Id / id.
|
||||
*
|
||||
* Must not be null.
|
||||
* @param $value int
|
||||
*/
|
||||
public function setProjectId($value)
|
||||
{
|
||||
$this->validateNotNull('ProjectId', $value);
|
||||
$this->validateInt('ProjectId', $value);
|
||||
|
||||
if ($this->data['project_id'] === $value) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->data['project_id'] = $value;
|
||||
|
||||
$this->setModified('project_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of Name / name
|
||||
*
|
||||
* Must not be null.
|
||||
* @param $value string
|
||||
*/
|
||||
public function setName($value)
|
||||
{
|
||||
$this->validateNotNull('Name', $value);
|
||||
$this->validateString('Name', $value);
|
||||
|
||||
if ($this->data['name'] === $value) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->data['name'] = $value;
|
||||
|
||||
$this->setModified('name');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of Branches / branches
|
||||
*
|
||||
* Must not be null.
|
||||
* @param $value array
|
||||
*/
|
||||
public function setBranches($value)
|
||||
{
|
||||
$this->validateNotNull('Branches', $value);
|
||||
$value = implode("\n", $value);
|
||||
|
||||
if ($this->data['branches'] === $value) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->data['branches'] = $value;
|
||||
|
||||
$this->setModified('branches');
|
||||
}
|
||||
}
|
||||
|
|
@ -5,6 +5,9 @@ namespace PHPCensor\Model;
|
|||
use PHPCensor\Model;
|
||||
use b8\Store;
|
||||
use b8\Store\Factory;
|
||||
use PHPCensor\Store\EnvironmentStore;
|
||||
use Symfony\Component\Yaml\Parser as YamlParser;
|
||||
use Symfony\Component\Yaml\Dumper as YamlDumper;
|
||||
|
||||
/**
|
||||
* @author Dan Cryer <dan@block8.co.uk>
|
||||
|
|
@ -767,4 +770,135 @@ class Project extends Model
|
|||
|
||||
return $icon;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
$rtn = $this->cache->get($cacheKey, null);
|
||||
|
||||
if (empty($rtn)) {
|
||||
/** @var EnvironmentStore $store */
|
||||
$store = Factory::getStore('Environment', 'PHPCensor');
|
||||
$rtn = $store->getByProjectId($key);
|
||||
$this->cache->set($cacheKey, $rtn);
|
||||
}
|
||||
|
||||
return $rtn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Environments
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getEnvironmentsNames()
|
||||
{
|
||||
$environments = $this->getEnvironmentsObjects();
|
||||
$environments_names = [];
|
||||
foreach($environments['items'] as $environment) {
|
||||
/** @var Environment $environment */
|
||||
$environments_names[] = $environment->getName();
|
||||
}
|
||||
return $environments_names;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Environments
|
||||
*
|
||||
* @return string yaml
|
||||
*/
|
||||
public function getEnvironments()
|
||||
{
|
||||
$environments = $this->getEnvironmentsObjects();
|
||||
$environments_config = [];
|
||||
foreach($environments['items'] as $environment) {
|
||||
/** @var Environment $environment */
|
||||
$environments_config[$environment->getName()] = $environment->getBranches();
|
||||
}
|
||||
$yaml_dumper = new YamlDumper();
|
||||
$value = $yaml_dumper->dump($environments_config, 10, 0, true, false);
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Environments
|
||||
*
|
||||
* @param string $value yaml
|
||||
*/
|
||||
public function setEnvironments($value)
|
||||
{
|
||||
$yaml_parser = new YamlParser();
|
||||
$environments_config = $yaml_parser->parse($value);
|
||||
$environments_names = !empty($environments_config) ? array_keys($environments_config) : [];
|
||||
|
||||
$current_environments = $this->getEnvironmentsObjects();
|
||||
$store = Factory::getStore('Environment', 'PHPCensor');
|
||||
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()] : []);
|
||||
} else {
|
||||
// remove
|
||||
$store->delete($environment);
|
||||
}
|
||||
}
|
||||
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
|
||||
* @return string[]
|
||||
*/
|
||||
public function getEnvironmentsNamesByBranch($branch)
|
||||
{
|
||||
$environments_names = [];
|
||||
$environments = $this->getEnvironmentsObjects();
|
||||
foreach($environments['items'] as $environment) {
|
||||
/** @var Environment $environment */
|
||||
if (in_array($branch, $environment->getBranches())) {
|
||||
$environments_names[] = $environment->getName();
|
||||
}
|
||||
}
|
||||
return $environments_names;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $environment_name
|
||||
* @return string[]
|
||||
*/
|
||||
public function getBranchesByEnvironment($environment_name)
|
||||
{
|
||||
$branches = [];
|
||||
$environments = $this->getEnvironmentsObjects();
|
||||
foreach($environments['items'] as $environment) {
|
||||
/** @var Environment $environment */
|
||||
if ($environment_name == $environment->getName()) {
|
||||
return $environment->getBranches();
|
||||
}
|
||||
}
|
||||
return $branches;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue