Initial implementation CI environments

This commit is contained in:
Stepan Strelets 2017-03-23 15:53:24 +03:00 committed by Dmitry Khomutov
commit 047cedaab3
No known key found for this signature in database
GPG key ID: 7EB36C9576F9ECB9
21 changed files with 850 additions and 42 deletions

View file

@ -58,6 +58,7 @@ class ProjectController extends PHPCensor\Controller
public function view($projectId)
{
$branch = $this->getParam('branch', '');
$environment = $this->getParam('environment', '');
$project = $this->projectStore->getById($projectId);
if (empty($project)) {
@ -66,7 +67,7 @@ class ProjectController extends PHPCensor\Controller
$perPage = $_SESSION['php-censor-user']->getFinalPerPage();
$page = $this->getParam('p', 1);
$builds = $this->getLatestBuildsHtml($projectId, urldecode($branch), (($page - 1) * $perPage), $perPage);
$builds = $this->getLatestBuildsHtml($projectId, urldecode($environment), urlencode($branch), (($page - 1) * $perPage), $perPage);
$pages = $builds[1] == 0 ? 1 : ceil($builds[1] / $perPage);
if ($page > $pages) {
@ -80,12 +81,18 @@ class ProjectController extends PHPCensor\Controller
$this->view->project = $project;
$this->view->branch = urldecode($branch);
$this->view->branches = $this->projectStore->getKnownBranches($projectId);
$this->view->environment = urldecode($environment);
$this->view->environments = $project->getEnvironmentsNames();
$this->view->page = $page;
$this->view->pages = $pages;
$this->view->perPage = $perPage;
$this->layout->title = $project->getTitle();
$this->layout->subtitle = $this->view->branch;
if (!empty($this->view->environment)) {
$this->layout->subtitle = $this->view->environment;
} else {
$this->layout->subtitle = $this->view->branch;
}
return $this->view->render();
}
@ -93,11 +100,22 @@ class ProjectController extends PHPCensor\Controller
/**
* Create a new pending build for a project.
*/
public function build($projectId, $branch = '')
public function build($projectId, $type = null, $id = null)
{
/* @var \PHPCensor\Model\Project $project */
$project = $this->projectStore->getById($projectId);
$environment = null;
$branch = null;
switch($type) {
case 'environment':
$environment = $id;
break;
case 'branch':
$branch = $id;
break;
}
if (empty($branch)) {
$branch = $project->getBranch();
}
@ -115,7 +133,7 @@ class ProjectController extends PHPCensor\Controller
}
$email = $_SESSION['php-censor-user']->getEmail();
$build = $this->buildService->createBuild($project, null, urldecode($branch), $email, null, $extra);
$build = $this->buildService->createBuild($project, $environment, null, urldecode($branch), $email, null, $extra);
if ($this->buildService->queueError) {
$_SESSION['global_error'] = Lang::get('add_to_queue_failed');
@ -145,15 +163,19 @@ class ProjectController extends PHPCensor\Controller
* Render latest builds for project as HTML table.
*
* @param int $projectId
* @param string $environment A urldecoded environment name.
* @param string $branch A urldecoded branch name.
* @param int $start
* @param int $perPage
*
* @return array
*/
protected function getLatestBuildsHtml($projectId, $branch = '', $start = 0, $perPage = 10)
protected function getLatestBuildsHtml($projectId, $environment = '', $branch = '', $start = 0, $perPage = 10)
{
$criteria = ['project_id' => $projectId];
if (!empty($environment)) {
$criteria['environment'] = $environment;
}
if (!empty($branch)) {
$criteria['branch'] = $branch;
}
@ -276,6 +298,8 @@ class ProjectController extends PHPCensor\Controller
$values['key'] = $values['ssh_private_key'];
$values['pubkey'] = $values['ssh_public_key'];
$values['environments'] = $project->getEnvironments();
if ($values['type'] == 'gitlab') {
$accessInfo = $project->getAccessInformation();
$reference = $accessInfo["user"] . '@' . $accessInfo["domain"] . ':' . $accessInfo["port"] . '/' . ltrim($project->getReference(), '/') . ".git";
@ -310,6 +334,7 @@ class ProjectController extends PHPCensor\Controller
'archived' => $this->getParam('archived', 0),
'branch' => $this->getParam('branch', null),
'group' => $this->getParam('group_id', null),
'environments' => $this->getParam('environments', null),
];
$project = $this->projectService->updateProject($project, $title, $type, $reference, $options);
@ -381,6 +406,13 @@ class ProjectController extends PHPCensor\Controller
$field->setClass('form-control')->setContainerClass('form-group')->setValue('master');
$form->addField($field);
if ($type != 'add') {
$field = Form\Element\TextArea::create('environments', Lang::get('environments_label'), false);
$field->setClass('form-control')->setContainerClass('form-group');
$field->setRows(6);
$form->addField($field);
}
$field = Form\Element\Select::create('group_id', Lang::get('project_group'), true);
$field->setClass('form-control')->setContainerClass('form-group')->setValue(1);
@ -472,8 +504,9 @@ class ProjectController extends PHPCensor\Controller
public function ajaxBuilds($projectId)
{
$branch = $this->getParam('branch', '');
$environment = $this->getParam('environment', '');
$perPage = (integer)$this->getParam('per_page', 10);
$builds = $this->getLatestBuildsHtml($projectId, urldecode($branch), 0, $perPage);
$builds = $this->getLatestBuildsHtml($projectId, urldecode($environment), urldecode($branch), 0, $perPage);
$this->response->disableLayout();
$this->response->setContent($builds[0]);

View file

@ -6,6 +6,7 @@ use b8;
use b8\Store;
use Exception;
use PHPCensor\Helper\Lang;
use PHPCensor\Model\Build;
use PHPCensor\Model\Project;
use PHPCensor\Service\BuildService;
use PHPCensor\Store\BuildStore;
@ -510,17 +511,57 @@ class WebhookController extends Controller
// Check if a build already exists for this commit ID:
$builds = $this->buildStore->getByProjectAndCommit($project->getId(), $commitId);
$ignore_environments = [];
if ($builds['count']) {
return [
'status' => 'ignored',
'message' => sprintf('Duplicate of build #%d', $builds['items'][0]->getId())
];
foreach($builds['items'] as $build) {
/** @var Build $build */
$ignore_environments[$build->getId()] = $build->getEnvironment();
}
}
// If not, create a new build job for it:
$build = $this->buildService->createBuild($project, $commitId, $branch, $committer, $commitMessage, $extra);
return ['status' => 'ok', 'buildID' => $build->getID()];
$environments = $project->getEnvironmentsObjects();
if ($environments['count']) {
$created_builds = [];
$environment_names = $project->getEnvironmentsNamesByBranch($branch);
// use base branch from project
if (!empty($environment_names)) {
$duplicates = [];
foreach ($environment_names as $environment_name) {
if (!in_array($environment_name, $ignore_environments)) {
// If not, create a new build job for it:
$build = $this->buildService->createBuild($project, $environment_name, $commitId, $project->getBranch(), $committer, $commitMessage, $extra);
$created_builds[] = array(
'id' => $build->getID(),
'environment' => $environment_name,
);
} else {
$duplicates[] = array_search($environment_name, $ignore_environments);
}
}
if (!empty($created_builds)) {
if (empty($duplicates)) {
return ['status' => 'ok', 'builds' => $created_builds];
} else {
return ['status' => 'ok', 'builds' => $created_builds, 'message' => sprintf('For this commit some builds already exists (%s)', implode(', ', $duplicates))];
}
} else {
return ['status' => 'ignored', 'message' => sprintf('For this commit already created builds (%s)', implode(', ', $duplicates))];
}
} else {
return ['status' => 'ignored', 'message' => 'Branch not assigned to any environment'];
}
} else {
$environment_name = null;
if (!in_array($environment_name, $ignore_environments)) {
$build = $this->buildService->createBuild($project, null, $commitId, $branch, $committer, $commitMessage, $extra);
return ['status' => 'ok', 'buildID' => $build->getID()];
} else {
return [
'status' => 'ignored',
'message' => sprintf('Duplicate of build #%d', array_search($environment_name, $ignore_environments)),
];
}
}
}
/**