2013-05-10 13:28:43 +02:00
|
|
|
<?php
|
2016-12-30 17:40:14 +01:00
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
namespace PHPCensor\Controller;
|
2013-05-16 16:25:39 +02:00
|
|
|
|
2018-03-04 11:14:09 +01:00
|
|
|
use PHPCensor\Exception\HttpException\NotFoundException;
|
2018-03-04 11:50:08 +01:00
|
|
|
use PHPCensor\Form;
|
2017-10-24 16:42:18 +02:00
|
|
|
use JasonGrimes\Paginator;
|
2016-07-19 20:28:11 +02:00
|
|
|
use PHPCensor;
|
|
|
|
use PHPCensor\BuildFactory;
|
|
|
|
use PHPCensor\Helper\Github;
|
|
|
|
use PHPCensor\Helper\Lang;
|
|
|
|
use PHPCensor\Helper\SshKey;
|
|
|
|
use PHPCensor\Service\BuildService;
|
|
|
|
use PHPCensor\Service\ProjectService;
|
2017-04-13 17:00:29 +02:00
|
|
|
use PHPCensor\Model\Build;
|
2018-03-04 11:22:14 +01:00
|
|
|
use PHPCensor\Http\Response\RedirectResponse;
|
2018-02-16 14:18:04 +01:00
|
|
|
use PHPCensor\View;
|
2018-03-04 08:30:34 +01:00
|
|
|
use PHPCensor\Store\Factory;
|
2013-05-10 13:28:43 +02:00
|
|
|
|
2013-05-16 03:30:48 +02:00
|
|
|
/**
|
2017-03-04 16:39:56 +01:00
|
|
|
* Project Controller - Allows users to create, edit and view projects.
|
2017-03-17 09:38:58 +01:00
|
|
|
*
|
2017-03-04 16:39:56 +01:00
|
|
|
* @author Dan Cryer <dan@block8.co.uk>
|
|
|
|
*/
|
2016-07-19 20:28:11 +02:00
|
|
|
class ProjectController extends PHPCensor\Controller
|
2013-05-10 13:28:43 +02:00
|
|
|
{
|
2013-10-08 19:24:20 +02:00
|
|
|
/**
|
2016-07-21 19:20:59 +02:00
|
|
|
* @var \PHPCensor\Store\ProjectStore
|
2013-10-08 19:24:20 +02:00
|
|
|
*/
|
|
|
|
protected $projectStore;
|
|
|
|
|
2014-07-14 15:01:29 +02:00
|
|
|
/**
|
2016-07-21 19:20:59 +02:00
|
|
|
* @var \PHPCensor\Service\ProjectService
|
2014-07-14 15:01:29 +02:00
|
|
|
*/
|
|
|
|
protected $projectService;
|
|
|
|
|
2014-07-14 17:02:36 +02:00
|
|
|
/**
|
2016-07-21 19:20:59 +02:00
|
|
|
* @var \PHPCensor\Store\BuildStore
|
2014-07-14 17:02:36 +02:00
|
|
|
*/
|
|
|
|
protected $buildStore;
|
|
|
|
|
|
|
|
/**
|
2016-07-21 19:20:59 +02:00
|
|
|
* @var \PHPCensor\Service\BuildService
|
2014-07-14 17:02:36 +02:00
|
|
|
*/
|
|
|
|
protected $buildService;
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Initialise the controller, set up stores and services.
|
|
|
|
*/
|
2013-05-16 16:25:39 +02:00
|
|
|
public function init()
|
|
|
|
{
|
2018-03-04 08:30:34 +01:00
|
|
|
$this->buildStore = Factory::getStore('Build');
|
|
|
|
$this->projectStore = Factory::getStore('Project');
|
2014-07-14 15:01:29 +02:00
|
|
|
$this->projectService = new ProjectService($this->projectStore);
|
2017-05-05 05:55:12 +02:00
|
|
|
$this->buildService = new BuildService($this->buildStore);
|
2013-05-16 16:25:39 +02:00
|
|
|
}
|
|
|
|
|
2017-05-08 09:51:32 +02:00
|
|
|
/**
|
|
|
|
* @param int $projectId
|
|
|
|
*
|
2018-03-04 11:22:14 +01:00
|
|
|
* @return PHPCensor\Http\Response
|
2017-05-08 09:51:32 +02:00
|
|
|
*/
|
|
|
|
public function ajaxBuilds($projectId)
|
|
|
|
{
|
|
|
|
$branch = $this->getParam('branch', '');
|
|
|
|
$environment = $this->getParam('environment', '');
|
|
|
|
$page = (integer)$this->getParam('page', 1);
|
|
|
|
$perPage = (integer)$this->getParam('per_page', 10);
|
2017-10-29 15:26:43 +01:00
|
|
|
$builds = $this->getLatestBuildsHtml($projectId, $branch, $environment, (($page - 1) * $perPage), $perPage);
|
2017-05-08 09:51:32 +02:00
|
|
|
|
|
|
|
$this->response->disableLayout();
|
|
|
|
$this->response->setContent($builds[0]);
|
|
|
|
|
|
|
|
return $this->response;
|
|
|
|
}
|
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
2017-04-09 17:39:20 +02:00
|
|
|
* View a specific project.
|
2017-11-05 15:48:36 +01:00
|
|
|
*
|
2017-04-09 17:39:20 +02:00
|
|
|
* @param integer $projectId
|
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-12-02 17:26:55 +01:00
|
|
|
public function view($projectId)
|
2013-05-16 16:25:39 +02:00
|
|
|
{
|
2017-04-09 17:39:20 +02:00
|
|
|
$branch = $this->getParam('branch', '');
|
2017-03-23 13:53:24 +01:00
|
|
|
$environment = $this->getParam('environment', '');
|
2017-05-08 09:51:32 +02:00
|
|
|
$page = (integer)$this->getParam('page', 1);
|
2017-04-09 17:39:20 +02:00
|
|
|
$project = $this->projectStore->getById($projectId);
|
2014-05-09 13:11:02 +02:00
|
|
|
|
|
|
|
if (empty($project)) {
|
2014-12-04 12:42:21 +01:00
|
|
|
throw new NotFoundException(Lang::get('project_x_not_found', $projectId));
|
2014-03-18 16:57:05 +01:00
|
|
|
}
|
|
|
|
|
2017-10-29 08:06:41 +01:00
|
|
|
/** @var PHPCensor\Model\User $user */
|
2018-02-17 05:59:02 +01:00
|
|
|
$user = $this->getUser();
|
2017-10-29 08:06:41 +01:00
|
|
|
$perPage = $user->getFinalPerPage();
|
2017-10-29 15:26:43 +01:00
|
|
|
$builds = $this->getLatestBuildsHtml($projectId, $branch, $environment, (($page - 1) * $perPage), $perPage);
|
2017-10-29 08:06:41 +01:00
|
|
|
$pages = ($builds[1] === 0)
|
|
|
|
? 1
|
|
|
|
: (integer)ceil($builds[1] / $perPage);
|
2013-05-16 16:25:39 +02:00
|
|
|
|
2014-06-08 19:08:46 +02:00
|
|
|
if ($page > $pages) {
|
2017-10-29 08:06:41 +01:00
|
|
|
$page = $pages;
|
2014-06-08 19:08:46 +02:00
|
|
|
}
|
|
|
|
|
2017-04-09 17:39:20 +02:00
|
|
|
$this->view->builds = $builds[0];
|
|
|
|
$this->view->total = $builds[1];
|
|
|
|
$this->view->project = $project;
|
|
|
|
$this->view->branch = urldecode($branch);
|
|
|
|
$this->view->branches = $this->projectStore->getKnownBranches($projectId);
|
|
|
|
$this->view->environment = urldecode($environment);
|
2017-03-23 13:53:24 +01:00
|
|
|
$this->view->environments = $project->getEnvironmentsNames();
|
2017-04-09 17:39:20 +02:00
|
|
|
$this->view->page = $page;
|
|
|
|
$this->view->perPage = $perPage;
|
2017-10-29 08:06:41 +01:00
|
|
|
$this->view->paginator = $this->getPaginatorHtml($projectId, $branch, $environment, $builds[1], $perPage, $page);
|
2013-05-16 16:25:39 +02:00
|
|
|
|
2017-05-05 05:55:12 +02:00
|
|
|
$this->layout->title = $project->getTitle();
|
2017-04-22 17:29:09 +02:00
|
|
|
$this->layout->subtitle = '';
|
2017-05-05 05:55:12 +02:00
|
|
|
|
2017-03-23 13:53:24 +01:00
|
|
|
if (!empty($this->view->environment)) {
|
2017-04-22 17:29:09 +02:00
|
|
|
$this->layout->subtitle = '<i class="fa fa-gear"></i> ' . $this->view->environment;
|
|
|
|
} elseif (!empty($this->view->branch)) {
|
|
|
|
$this->layout->subtitle = '<i class="fa fa-code-fork"></i> ' . $this->view->branch;
|
2017-03-23 13:53:24 +01:00
|
|
|
}
|
2014-04-24 17:25:24 +02:00
|
|
|
|
2013-05-22 17:36:55 +02:00
|
|
|
return $this->view->render();
|
2013-05-16 16:25:39 +02:00
|
|
|
}
|
|
|
|
|
2017-10-29 08:06:41 +01:00
|
|
|
/**
|
|
|
|
* @param integer $projectId
|
|
|
|
* @param string $branch
|
|
|
|
* @param string $environment
|
|
|
|
* @param integer $total
|
|
|
|
* @param integer $perPage
|
|
|
|
* @param integer $page
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getPaginatorHtml($projectId, $branch, $environment, $total, $perPage, $page)
|
|
|
|
{
|
2018-02-16 14:18:04 +01:00
|
|
|
$view = new View('pagination');
|
2017-10-29 08:06:41 +01:00
|
|
|
|
|
|
|
$urlPattern = APP_URL . 'project/view/' . $projectId;
|
|
|
|
$params = [];
|
|
|
|
if (!empty($branch)) {
|
|
|
|
$params['branch'] = $branch;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($environment)) {
|
|
|
|
$params['environment'] = $environment;
|
|
|
|
}
|
|
|
|
|
|
|
|
$urlPattern = $urlPattern . '?' . str_replace('%28%3Anum%29', '(:num)', http_build_query(array_merge($params, ['page' => '(:num)'])));
|
|
|
|
$paginator = new Paginator($total, $perPage, $page, $urlPattern);
|
|
|
|
|
|
|
|
$view->paginator = $paginator;
|
|
|
|
|
|
|
|
return $view->render();
|
|
|
|
}
|
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
2017-04-09 17:39:20 +02:00
|
|
|
* Create a new pending build for a project.
|
2017-11-05 15:48:36 +01:00
|
|
|
*
|
2017-04-09 17:39:20 +02:00
|
|
|
* @param integer $projectId
|
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
|
|
|
*
|
2017-04-13 17:00:29 +02:00
|
|
|
* @return RedirectResponse
|
2017-04-09 17:39:20 +02:00
|
|
|
*
|
|
|
|
*/
|
2017-05-09 06:25:16 +02:00
|
|
|
public function build($projectId)
|
2013-05-16 16:25:39 +02:00
|
|
|
{
|
2017-05-17 16:46:55 +02:00
|
|
|
/* @var \PHPCensor\Model\Project $project */
|
|
|
|
$project = $this->projectStore->getById($projectId);
|
|
|
|
if (empty($project) || $project->getArchived()) {
|
|
|
|
throw new NotFoundException(Lang::get('project_x_not_found', $projectId));
|
|
|
|
}
|
2018-02-16 14:18:04 +01:00
|
|
|
|
2017-05-09 06:25:16 +02:00
|
|
|
$type = $this->getParam('type', 'branch');
|
2017-05-17 16:46:55 +02:00
|
|
|
$id = $this->getParam('id');
|
2017-05-09 06:25:16 +02:00
|
|
|
$debug = (boolean)$this->getParam('debug', false);
|
2017-05-17 16:46:55 +02:00
|
|
|
|
2017-03-23 13:53:24 +01:00
|
|
|
$environment = null;
|
2017-05-05 05:55:12 +02:00
|
|
|
$branch = null;
|
|
|
|
|
2017-03-23 13:53:24 +01:00
|
|
|
switch($type) {
|
|
|
|
case 'environment':
|
|
|
|
$environment = $id;
|
|
|
|
break;
|
|
|
|
case 'branch':
|
|
|
|
$branch = $id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-05-05 05:52:20 +02:00
|
|
|
if (empty($branch)) {
|
|
|
|
$branch = $project->getBranch();
|
|
|
|
}
|
|
|
|
|
2017-02-18 18:52:13 +01:00
|
|
|
$extra = null;
|
2017-05-05 05:55:12 +02:00
|
|
|
|
2017-02-18 18:52:13 +01:00
|
|
|
if ($debug && $this->currentUserIsAdmin()) {
|
|
|
|
$extra = [
|
|
|
|
'debug' => true,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-10-15 16:58:36 +02:00
|
|
|
/** @var PHPCensor\Model\User $user */
|
2018-02-17 05:59:02 +01:00
|
|
|
$user = $this->getUser();
|
2017-04-22 15:02:24 +02:00
|
|
|
$build = $this->buildService->createBuild(
|
|
|
|
$project,
|
|
|
|
$environment,
|
2017-04-13 17:00:29 +02:00
|
|
|
'',
|
2017-05-09 06:25:16 +02:00
|
|
|
$branch,
|
2017-04-22 15:02:24 +02:00
|
|
|
null,
|
2017-10-15 16:58:36 +02:00
|
|
|
$user->getEmail(),
|
2017-04-22 15:02:24 +02:00
|
|
|
null,
|
2017-04-13 17:00:29 +02:00
|
|
|
Build::SOURCE_MANUAL_WEB,
|
2017-10-15 16:58:36 +02:00
|
|
|
$user->getId(),
|
2017-04-22 15:02:24 +02:00
|
|
|
$extra
|
|
|
|
);
|
2013-05-16 16:25:39 +02:00
|
|
|
|
2016-06-23 12:28:42 +02:00
|
|
|
if ($this->buildService->queueError) {
|
|
|
|
$_SESSION['global_error'] = Lang::get('add_to_queue_failed');
|
|
|
|
}
|
|
|
|
|
2017-04-13 17:00:29 +02:00
|
|
|
$response = new RedirectResponse();
|
2016-07-21 17:20:34 +02:00
|
|
|
$response->setHeader('Location', APP_URL.'build/view/' . $build->getId());
|
2017-04-16 16:37:55 +02:00
|
|
|
|
2015-02-12 13:37:56 +01:00
|
|
|
return $response;
|
2013-05-16 16:25:39 +02:00
|
|
|
}
|
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* Delete a project.
|
|
|
|
*/
|
2013-05-16 17:46:30 +02:00
|
|
|
public function delete($projectId)
|
2013-05-16 16:25:39 +02:00
|
|
|
{
|
2014-12-02 17:26:55 +01:00
|
|
|
$this->requireAdmin();
|
2013-05-16 16:25:39 +02:00
|
|
|
|
2013-10-11 22:51:23 +02:00
|
|
|
$project = $this->projectStore->getById($projectId);
|
2014-07-31 13:10:22 +02:00
|
|
|
$this->projectService->deleteProject($project);
|
2013-05-16 16:25:39 +02:00
|
|
|
|
2017-04-13 17:00:29 +02:00
|
|
|
$response = new RedirectResponse();
|
2016-07-21 17:20:34 +02:00
|
|
|
$response->setHeader('Location', APP_URL);
|
2017-05-05 05:55:12 +02:00
|
|
|
|
2015-02-12 13:37:56 +01:00
|
|
|
return $response;
|
2013-05-16 16:25:39 +02:00
|
|
|
}
|
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
2014-09-16 13:27:32 +02:00
|
|
|
* Render latest builds for project as HTML table.
|
|
|
|
*
|
2016-12-28 15:26:22 +01:00
|
|
|
* @param int $projectId
|
2017-10-29 15:26:43 +01:00
|
|
|
* @param string $branch A urldecoded branch name.
|
|
|
|
* @param string $environment A urldecoded environment name.
|
2016-12-28 15:26:22 +01:00
|
|
|
* @param int $start
|
|
|
|
* @param int $perPage
|
2017-03-17 09:38:58 +01:00
|
|
|
*
|
2014-09-16 13:27:32 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
2017-10-29 15:26:43 +01:00
|
|
|
protected function getLatestBuildsHtml($projectId, $branch = '', $environment = '', $start = 0, $perPage = 10)
|
2013-05-16 16:25:39 +02:00
|
|
|
{
|
2016-04-20 17:39:48 +02:00
|
|
|
$criteria = ['project_id' => $projectId];
|
2017-05-05 05:55:12 +02:00
|
|
|
|
2017-03-23 13:53:24 +01:00
|
|
|
if (!empty($environment)) {
|
|
|
|
$criteria['environment'] = $environment;
|
|
|
|
}
|
2017-05-05 05:55:12 +02:00
|
|
|
|
2014-09-16 13:27:32 +02:00
|
|
|
if (!empty($branch)) {
|
|
|
|
$criteria['branch'] = $branch;
|
|
|
|
}
|
|
|
|
|
2016-04-20 17:39:48 +02:00
|
|
|
$order = ['id' => 'DESC'];
|
2018-03-01 13:36:28 +01:00
|
|
|
$builds = $this->buildStore->getWhere($criteria, $perPage, $start, $order);
|
2018-02-16 14:18:04 +01:00
|
|
|
$view = new View('Project/ajax-builds');
|
2014-02-25 18:00:05 +01:00
|
|
|
|
|
|
|
foreach ($builds['items'] as &$build) {
|
|
|
|
$build = BuildFactory::getBuild($build);
|
|
|
|
}
|
|
|
|
|
2017-10-29 08:06:41 +01:00
|
|
|
$view->builds = $builds['items'];
|
2013-05-16 16:25:39 +02:00
|
|
|
|
2017-10-29 08:06:41 +01:00
|
|
|
return [
|
|
|
|
$view->render(),
|
|
|
|
(integer)$builds['count']
|
|
|
|
];
|
2013-05-16 16:25:39 +02:00
|
|
|
}
|
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* Add a new project. Handles both the form, and processing.
|
|
|
|
*/
|
2013-05-16 16:25:39 +02:00
|
|
|
public function add()
|
|
|
|
{
|
2014-12-04 12:42:21 +01:00
|
|
|
$this->layout->title = Lang::get('add_project');
|
2014-07-11 16:29:27 +02:00
|
|
|
$this->requireAdmin();
|
2013-05-16 16:25:39 +02:00
|
|
|
|
2017-06-20 14:51:27 +02:00
|
|
|
$method = $this->request->getMethod();
|
|
|
|
$values = $this->getParams();
|
|
|
|
$values['branch'] = '';
|
2014-05-02 18:32:18 +02:00
|
|
|
|
2018-02-23 05:27:33 +01:00
|
|
|
if ($method !== 'POST') {
|
2014-05-02 18:32:18 +02:00
|
|
|
$sshKey = new SshKey();
|
2017-05-05 05:55:12 +02:00
|
|
|
$key = $sshKey->generate();
|
2013-05-16 16:25:39 +02:00
|
|
|
|
2014-07-11 16:38:28 +02:00
|
|
|
$values['key'] = $key['private_key'];
|
|
|
|
$values['pubkey'] = $key['public_key'];
|
2013-05-16 16:25:39 +02:00
|
|
|
}
|
|
|
|
|
2013-10-11 22:51:23 +02:00
|
|
|
$form = $this->projectForm($values);
|
2013-05-16 16:25:39 +02:00
|
|
|
|
|
|
|
if ($method != 'POST' || ($method == 'POST' && !$form->validate())) {
|
2018-02-16 14:18:04 +01:00
|
|
|
$view = new View('Project/edit');
|
2013-05-16 16:25:39 +02:00
|
|
|
$view->type = 'add';
|
|
|
|
$view->project = null;
|
|
|
|
$view->form = $form;
|
2018-02-23 05:27:33 +01:00
|
|
|
$view->key = $values['pubkey'];
|
2013-05-16 16:25:39 +02:00
|
|
|
|
|
|
|
return $view->render();
|
2014-07-11 16:29:27 +02:00
|
|
|
} else {
|
2016-04-20 17:39:48 +02:00
|
|
|
$title = $this->getParam('title', 'New Project');
|
2014-07-14 15:01:29 +02:00
|
|
|
$reference = $this->getParam('reference', null);
|
2016-04-20 17:39:48 +02:00
|
|
|
$type = $this->getParam('type', null);
|
2014-07-14 15:01:29 +02:00
|
|
|
|
2016-04-20 17:39:48 +02:00
|
|
|
$options = [
|
|
|
|
'ssh_private_key' => $this->getParam('key', null),
|
|
|
|
'ssh_public_key' => $this->getParam('pubkey', null),
|
|
|
|
'build_config' => $this->getParam('build_config', null),
|
2014-07-14 15:01:29 +02:00
|
|
|
'allow_public_status' => $this->getParam('allow_public_status', 0),
|
2016-04-20 17:39:48 +02:00
|
|
|
'branch' => $this->getParam('branch', null),
|
2017-09-13 16:21:49 +02:00
|
|
|
'default_branch_only' => $this->getParam('default_branch_only', 0),
|
2016-04-20 17:39:48 +02:00
|
|
|
'group' => $this->getParam('group_id', null),
|
2017-03-25 23:38:49 +01:00
|
|
|
'environments' => $this->getParam('environments', null),
|
2016-04-20 17:39:48 +02:00
|
|
|
];
|
2014-07-14 15:01:29 +02:00
|
|
|
|
2017-10-19 17:05:00 +02:00
|
|
|
/** @var PHPCensor\Model\User $user */
|
2018-02-17 05:59:02 +01:00
|
|
|
$user = $this->getUser();
|
2017-10-19 17:05:00 +02:00
|
|
|
$project = $this->projectService->createProject($title, $type, $reference, $user->getId(), $options);
|
2015-02-12 13:37:56 +01:00
|
|
|
|
2017-04-13 17:00:29 +02:00
|
|
|
$response = new RedirectResponse();
|
2016-07-21 17:20:34 +02:00
|
|
|
$response->setHeader('Location', APP_URL.'project/view/' . $project->getId());
|
2017-02-15 14:16:40 +01:00
|
|
|
|
2015-02-12 13:37:56 +01:00
|
|
|
return $response;
|
2013-05-16 16:25:39 +02:00
|
|
|
}
|
2014-07-11 16:29:27 +02:00
|
|
|
}
|
2013-05-16 16:25:39 +02:00
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
2013-09-04 14:54:26 +02:00
|
|
|
* Edit a project. Handles both the form and processing.
|
2013-05-16 18:17:29 +02:00
|
|
|
*/
|
2013-05-16 17:46:30 +02:00
|
|
|
public function edit($projectId)
|
2013-05-16 16:25:39 +02:00
|
|
|
{
|
2014-12-02 17:26:55 +01:00
|
|
|
$this->requireAdmin();
|
2013-09-04 14:54:26 +02:00
|
|
|
|
2017-05-05 05:55:12 +02:00
|
|
|
$method = $this->request->getMethod();
|
2014-05-09 13:11:02 +02:00
|
|
|
$project = $this->projectStore->getById($projectId);
|
|
|
|
|
|
|
|
if (empty($project)) {
|
2014-12-04 12:42:21 +01:00
|
|
|
throw new NotFoundException(Lang::get('project_x_not_found', $projectId));
|
2014-05-09 13:11:02 +02:00
|
|
|
}
|
2013-05-16 16:25:39 +02:00
|
|
|
|
2017-05-05 05:55:12 +02:00
|
|
|
$this->layout->title = $project->getTitle();
|
2014-12-04 12:42:21 +01:00
|
|
|
$this->layout->subtitle = Lang::get('edit_project');
|
2014-04-24 17:25:24 +02:00
|
|
|
|
2017-05-05 05:55:12 +02:00
|
|
|
$values = $project->getDataArray();
|
|
|
|
$values['key'] = $values['ssh_private_key'];
|
|
|
|
$values['pubkey'] = $values['ssh_public_key'];
|
2017-03-23 13:53:24 +01:00
|
|
|
$values['environments'] = $project->getEnvironments();
|
|
|
|
|
2017-02-07 13:50:09 +01:00
|
|
|
if ($values['type'] == 'gitlab') {
|
|
|
|
$accessInfo = $project->getAccessInformation();
|
|
|
|
$reference = $accessInfo["user"] . '@' . $accessInfo["domain"] . ':' . $accessInfo["port"] . '/' . ltrim($project->getReference(), '/') . ".git";
|
2014-05-09 17:19:15 +02:00
|
|
|
$values['reference'] = $reference;
|
|
|
|
}
|
2014-04-24 17:25:24 +02:00
|
|
|
|
2013-05-16 16:25:39 +02:00
|
|
|
if ($method == 'POST') {
|
|
|
|
$values = $this->getParams();
|
|
|
|
}
|
|
|
|
|
2014-05-09 13:11:02 +02:00
|
|
|
$form = $this->projectForm($values, 'edit/' . $projectId);
|
2013-05-16 16:25:39 +02:00
|
|
|
|
|
|
|
if ($method != 'POST' || ($method == 'POST' && !$form->validate())) {
|
2018-02-16 14:18:04 +01:00
|
|
|
$view = new View('Project/edit');
|
2013-05-16 16:25:39 +02:00
|
|
|
$view->type = 'edit';
|
|
|
|
$view->project = $project;
|
|
|
|
$view->form = $form;
|
2015-02-16 15:00:37 +01:00
|
|
|
$view->key = $values['pubkey'];
|
2013-05-16 16:25:39 +02:00
|
|
|
|
|
|
|
return $view->render();
|
|
|
|
}
|
|
|
|
|
2017-05-05 05:55:12 +02:00
|
|
|
$title = $this->getParam('title', Lang::get('new_project'));
|
2014-07-14 15:37:51 +02:00
|
|
|
$reference = $this->getParam('reference', null);
|
2017-05-05 05:55:12 +02:00
|
|
|
$type = $this->getParam('type', null);
|
2013-05-16 16:25:39 +02:00
|
|
|
|
2016-04-20 17:39:48 +02:00
|
|
|
$options = [
|
|
|
|
'ssh_private_key' => $this->getParam('key', null),
|
|
|
|
'ssh_public_key' => $this->getParam('pubkey', null),
|
|
|
|
'build_config' => $this->getParam('build_config', null),
|
2018-03-09 19:00:53 +01:00
|
|
|
'allow_public_status' => $this->getParam('allow_public_status', false),
|
|
|
|
'archived' => $this->getParam('archived', false),
|
2016-04-20 17:39:48 +02:00
|
|
|
'branch' => $this->getParam('branch', null),
|
2018-03-09 19:00:53 +01:00
|
|
|
'default_branch_only' => $this->getParam('default_branch_only', false),
|
2016-04-20 17:39:48 +02:00
|
|
|
'group' => $this->getParam('group_id', null),
|
2017-03-23 13:53:24 +01:00
|
|
|
'environments' => $this->getParam('environments', null),
|
2016-04-20 17:39:48 +02:00
|
|
|
];
|
2013-08-23 16:05:12 +02:00
|
|
|
|
2014-07-14 15:37:51 +02:00
|
|
|
$project = $this->projectService->updateProject($project, $title, $type, $reference, $options);
|
2013-05-16 16:25:39 +02:00
|
|
|
|
2017-04-13 17:00:29 +02:00
|
|
|
$response = new RedirectResponse();
|
2016-07-21 17:20:34 +02:00
|
|
|
$response->setHeader('Location', APP_URL.'project/view/' . $project->getId());
|
2017-02-15 14:16:40 +01:00
|
|
|
|
2015-02-12 13:37:56 +01:00
|
|
|
return $response;
|
2013-05-16 16:25:39 +02:00
|
|
|
}
|
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
2013-09-04 14:54:26 +02:00
|
|
|
* Create add / edit project form.
|
2013-05-16 18:17:29 +02:00
|
|
|
*/
|
2013-05-16 16:25:39 +02:00
|
|
|
protected function projectForm($values, $type = 'add')
|
|
|
|
{
|
|
|
|
$form = new Form();
|
2017-06-20 14:51:27 +02:00
|
|
|
|
2013-05-16 16:25:39 +02:00
|
|
|
$form->setMethod('POST');
|
2016-07-21 17:20:34 +02:00
|
|
|
$form->setAction(APP_URL.'project/' . $type);
|
2017-06-20 14:51:27 +02:00
|
|
|
|
2013-05-16 16:25:39 +02:00
|
|
|
$form->addField(new Form\Element\Csrf('csrf'));
|
|
|
|
$form->addField(new Form\Element\Hidden('pubkey'));
|
|
|
|
|
2016-04-20 17:39:48 +02:00
|
|
|
$options = [
|
2018-03-08 09:54:19 +01:00
|
|
|
'choose' => Lang::get('select_repository_type'),
|
|
|
|
'github' => 'GitHub',
|
|
|
|
'bitbucket' => 'Bitbucket (Git)',
|
|
|
|
'bitbucket-hg' => 'Bitbucket (Hg)',
|
|
|
|
'gitlab' => 'GitLab',
|
|
|
|
'gogs' => 'Gogs',
|
|
|
|
'git' => 'Git',
|
|
|
|
'local' => Lang::get('local'),
|
|
|
|
'hg' => 'Hg (Mercurial)',
|
|
|
|
'svn' => 'Svn (Subversion)',
|
2016-04-20 17:39:48 +02:00
|
|
|
];
|
2013-05-16 16:25:39 +02:00
|
|
|
|
2014-12-04 12:42:21 +01:00
|
|
|
$field = Form\Element\Select::create('type', Lang::get('where_hosted'), true);
|
2018-03-08 09:54:19 +01:00
|
|
|
$field->setPattern('^(github|bitbucket|bitbucket-hg|gitlab|gogs|git|local|hg|svn)');
|
2013-05-16 16:25:39 +02:00
|
|
|
$field->setOptions($options);
|
2014-07-11 16:29:27 +02:00
|
|
|
$field->setClass('form-control')->setContainerClass('form-group');
|
2013-05-16 16:25:39 +02:00
|
|
|
$form->addField($field);
|
|
|
|
|
2013-10-11 22:51:23 +02:00
|
|
|
$container = new Form\ControlGroup('github-container');
|
|
|
|
$container->setClass('github-container');
|
|
|
|
|
2014-12-04 12:42:21 +01:00
|
|
|
$field = Form\Element\Select::create('github', Lang::get('choose_github'), false);
|
2014-07-11 16:29:27 +02:00
|
|
|
$field->setClass('form-control')->setContainerClass('form-group');
|
2013-10-11 22:51:23 +02:00
|
|
|
$container->addField($field);
|
|
|
|
$form->addField($container);
|
2013-05-16 16:25:39 +02:00
|
|
|
|
2014-12-04 12:42:21 +01:00
|
|
|
$field = Form\Element\Text::create('reference', Lang::get('repo_name'), true);
|
2014-06-11 15:18:31 +02:00
|
|
|
$field->setValidator($this->getReferenceValidator($values));
|
2014-07-11 16:29:27 +02:00
|
|
|
$field->setClass('form-control')->setContainerClass('form-group');
|
2014-06-11 15:18:31 +02:00
|
|
|
$form->addField($field);
|
|
|
|
|
2014-12-04 12:42:21 +01:00
|
|
|
$field = Form\Element\Text::create('title', Lang::get('project_title'), true);
|
2014-07-11 16:29:27 +02:00
|
|
|
$field->setClass('form-control')->setContainerClass('form-group');
|
2013-05-16 16:25:39 +02:00
|
|
|
$form->addField($field);
|
2013-09-04 14:54:26 +02:00
|
|
|
|
2017-05-17 16:46:55 +02:00
|
|
|
$field = Form\Element\Text::create('branch', Lang::get('default_branch'), false);
|
|
|
|
$field->setClass('form-control')->setContainerClass('form-group')->setValue('');
|
|
|
|
$form->addField($field);
|
|
|
|
|
2017-09-13 16:21:49 +02:00
|
|
|
$field = Form\Element\Checkbox::create(
|
|
|
|
'default_branch_only',
|
|
|
|
Lang::get('default_branch_only'),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
$field->setContainerClass('form-group');
|
|
|
|
$field->setCheckedValue(1);
|
|
|
|
$field->setValue(0);
|
|
|
|
$form->addField($field);
|
|
|
|
|
2014-12-04 12:42:21 +01:00
|
|
|
$field = Form\Element\TextArea::create('key', Lang::get('project_private_key'), false);
|
2014-07-11 16:29:27 +02:00
|
|
|
$field->setClass('form-control')->setContainerClass('form-group');
|
2013-05-16 16:25:39 +02:00
|
|
|
$field->setRows(6);
|
|
|
|
$form->addField($field);
|
|
|
|
|
2014-12-04 12:42:21 +01:00
|
|
|
$field = Form\Element\TextArea::create('build_config', Lang::get('build_config'), false);
|
2014-07-11 16:29:27 +02:00
|
|
|
$field->setClass('form-control')->setContainerClass('form-group');
|
2014-03-15 06:18:12 +01:00
|
|
|
$field->setRows(6);
|
|
|
|
$form->addField($field);
|
|
|
|
|
2017-03-25 23:38:49 +01:00
|
|
|
$field = Form\Element\TextArea::create('environments', Lang::get('environments_label'), false);
|
|
|
|
$field->setClass('form-control')->setContainerClass('form-group');
|
|
|
|
$field->setRows(6);
|
|
|
|
$form->addField($field);
|
2017-03-23 13:53:24 +01:00
|
|
|
|
2017-03-18 06:05:07 +01:00
|
|
|
$field = Form\Element\Select::create('group_id', Lang::get('project_group'), true);
|
2015-10-08 17:33:01 +02:00
|
|
|
$field->setClass('form-control')->setContainerClass('form-group')->setValue(1);
|
|
|
|
|
2016-04-20 17:39:48 +02:00
|
|
|
$groups = [];
|
2018-03-04 08:30:34 +01:00
|
|
|
$groupStore = Factory::getStore('ProjectGroup');
|
2018-03-01 13:36:28 +01:00
|
|
|
$groupList = $groupStore->getWhere([], 100, 0, ['title' => 'ASC']);
|
2015-10-08 17:33:01 +02:00
|
|
|
|
|
|
|
foreach ($groupList['items'] as $group) {
|
|
|
|
$groups[$group->getId()] = $group->getTitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
$field->setOptions($groups);
|
|
|
|
$form->addField($field);
|
|
|
|
|
2014-12-04 12:42:21 +01:00
|
|
|
$field = Form\Element\Checkbox::create('allow_public_status', Lang::get('allow_public_status'), false);
|
2014-04-30 16:00:35 +02:00
|
|
|
$field->setContainerClass('form-group');
|
|
|
|
$field->setCheckedValue(1);
|
2014-07-14 15:37:51 +02:00
|
|
|
$field->setValue(0);
|
2014-04-30 16:00:35 +02:00
|
|
|
$form->addField($field);
|
|
|
|
|
2015-01-31 10:50:57 +01:00
|
|
|
$field = Form\Element\Checkbox::create('archived', Lang::get('archived'), false);
|
|
|
|
$field->setContainerClass('form-group');
|
|
|
|
$field->setCheckedValue(1);
|
|
|
|
$field->setValue(0);
|
|
|
|
$form->addField($field);
|
|
|
|
|
2013-05-16 16:25:39 +02:00
|
|
|
$field = new Form\Element\Submit();
|
2014-12-04 12:42:21 +01:00
|
|
|
$field->setValue(Lang::get('save_project'));
|
2013-08-01 12:55:10 +02:00
|
|
|
$field->setContainerClass('form-group');
|
2013-05-16 16:25:39 +02:00
|
|
|
$field->setClass('btn-success');
|
|
|
|
$form->addField($field);
|
|
|
|
|
|
|
|
$form->setValues($values);
|
2017-06-20 14:51:27 +02:00
|
|
|
|
2013-05-16 16:25:39 +02:00
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Get the validator to use to check project references.
|
|
|
|
* @param $values
|
|
|
|
* @return callable
|
|
|
|
*/
|
2013-10-10 02:01:06 +02:00
|
|
|
protected function getReferenceValidator($values)
|
|
|
|
{
|
|
|
|
return function ($val) use ($values) {
|
|
|
|
$type = $values['type'];
|
|
|
|
|
2016-04-20 17:39:48 +02:00
|
|
|
$validators = [
|
|
|
|
'hg' => [
|
2017-03-19 05:17:10 +01:00
|
|
|
'regex' => '/^(ssh|https?):\/\//',
|
2018-03-08 09:54:19 +01:00
|
|
|
'message' => Lang::get('error_hg')
|
2016-04-20 17:39:48 +02:00
|
|
|
],
|
2018-03-08 09:54:19 +01:00
|
|
|
'git' => [
|
2016-04-20 17:39:48 +02:00
|
|
|
'regex' => '/^(git|https?):\/\//',
|
2018-03-08 09:54:19 +01:00
|
|
|
'message' => Lang::get('error_git')
|
2016-04-20 17:39:48 +02:00
|
|
|
],
|
|
|
|
'gitlab' => [
|
|
|
|
'regex' => '`^(.*)@(.*):(.*)/(.*)\.git`',
|
2014-12-04 12:42:21 +01:00
|
|
|
'message' => Lang::get('error_gitlab')
|
2016-04-20 17:39:48 +02:00
|
|
|
],
|
|
|
|
'github' => [
|
|
|
|
'regex' => '/^[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-\.]+$/',
|
2014-12-04 12:42:21 +01:00
|
|
|
'message' => Lang::get('error_github')
|
2016-04-20 17:39:48 +02:00
|
|
|
],
|
|
|
|
'bitbucket' => [
|
|
|
|
'regex' => '/^[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-\.]+$/',
|
2014-12-04 12:42:21 +01:00
|
|
|
'message' => Lang::get('error_bitbucket')
|
2016-04-20 17:39:48 +02:00
|
|
|
],
|
2018-03-08 09:54:19 +01:00
|
|
|
'bitbucket-hg' => [
|
2017-03-19 05:17:10 +01:00
|
|
|
'regex' => '/^[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-\.]+$/',
|
|
|
|
'message' => Lang::get('error_bitbucket')
|
|
|
|
],
|
2016-04-20 17:39:48 +02:00
|
|
|
];
|
2013-10-10 02:01:06 +02:00
|
|
|
|
|
|
|
if (in_array($type, $validators) && !preg_match($validators[$type]['regex'], $val)) {
|
|
|
|
throw new \Exception($validators[$type]['message']);
|
|
|
|
} elseif ($type == 'local' && !is_dir($val)) {
|
2014-12-04 12:42:21 +01:00
|
|
|
throw new \Exception(Lang::get('error_path'));
|
2013-10-10 02:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
2017-01-14 19:11:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an array of repositories from Github's API.
|
|
|
|
*/
|
|
|
|
public function ajaxGithubRepositories()
|
|
|
|
{
|
|
|
|
$github = new Github();
|
|
|
|
|
2018-03-04 11:22:14 +01:00
|
|
|
$response = new PHPCensor\Http\Response\JsonResponse();
|
2017-01-14 19:11:32 +01:00
|
|
|
$response->setContent($github->getRepositories());
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2013-05-16 16:25:39 +02:00
|
|
|
}
|