Refactored project structure.
This commit is contained in:
parent
cfe93434ad
commit
c015d8c58b
308 changed files with 39 additions and 47 deletions
374
src/Controller/BuildController.php
Normal file
374
src/Controller/BuildController.php
Normal file
|
|
@ -0,0 +1,374 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use PHPCensor\Exception\HttpException\NotFoundException;
|
||||
use PHPCensor\Http\Response\JsonResponse;
|
||||
use JasonGrimes\Paginator;
|
||||
use PHPCensor\BuildFactory;
|
||||
use PHPCensor\Helper\AnsiConverter;
|
||||
use PHPCensor\Helper\Lang;
|
||||
use PHPCensor\Http\Response\RedirectResponse;
|
||||
use PHPCensor\Model\Build;
|
||||
use PHPCensor\Model\User;
|
||||
use PHPCensor\Service\BuildService;
|
||||
use PHPCensor\Controller;
|
||||
use PHPCensor\View;
|
||||
use PHPCensor\Store\Factory;
|
||||
|
||||
/**
|
||||
* Build Controller - Allows users to run and view builds.
|
||||
*
|
||||
* @author Dan Cryer <dan@block8.co.uk>
|
||||
*/
|
||||
class BuildController extends Controller
|
||||
{
|
||||
/**
|
||||
* @var \PHPCensor\Store\BuildStore
|
||||
*/
|
||||
protected $buildStore;
|
||||
|
||||
/**
|
||||
* @var \PHPCensor\Service\BuildService
|
||||
*/
|
||||
protected $buildService;
|
||||
|
||||
/**
|
||||
* Initialise the controller, set up stores and services.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->buildStore = Factory::getStore('Build');
|
||||
$this->buildService = new BuildService($this->buildStore);
|
||||
}
|
||||
|
||||
/**
|
||||
* View a specific build.
|
||||
*
|
||||
* @param integer $buildId
|
||||
*
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function view($buildId)
|
||||
{
|
||||
$page = (integer)$this->getParam('page', 1);
|
||||
$plugin = $this->getParam('plugin', '');
|
||||
$isNew = $this->getParam('is_new', '');
|
||||
|
||||
$severity = $this->getParam('severity', null);
|
||||
if (null !== $severity && '' !== $severity) {
|
||||
$severity = (integer)$severity;
|
||||
} else {
|
||||
$severity = null;
|
||||
}
|
||||
|
||||
$build = BuildFactory::getBuildById($buildId);
|
||||
|
||||
if (!$build) {
|
||||
throw new NotFoundException(Lang::get('build_x_not_found', $buildId));
|
||||
}
|
||||
|
||||
/** @var User $user */
|
||||
$user = $this->getUser();
|
||||
$perPage = $user->getFinalPerPage();
|
||||
$data = $this->getBuildData($build, $plugin, $severity, $isNew, (($page - 1) * $perPage), $perPage);
|
||||
$pages = ($data['errors'] === 0)
|
||||
? 1
|
||||
: (integer)ceil($data['errors'] / $perPage);
|
||||
|
||||
if ($page > $pages) {
|
||||
$page = $pages;
|
||||
}
|
||||
|
||||
/** @var \PHPCensor\Store\BuildErrorStore $errorStore */
|
||||
$errorStore = Factory::getStore('BuildError');
|
||||
|
||||
$this->view->uiPlugins = $this->getUiPlugins();
|
||||
$this->view->build = $build;
|
||||
$this->view->data = $data;
|
||||
|
||||
$this->view->plugin = urldecode($plugin);
|
||||
$this->view->plugins = $errorStore->getKnownPlugins($buildId, $severity, $isNew);
|
||||
$this->view->severity = urldecode(null !== $severity ? $severity : '');
|
||||
$this->view->severities = $errorStore->getKnownSeverities($buildId, $plugin, $isNew);
|
||||
$this->view->isNew = urldecode($isNew);
|
||||
$this->view->isNews = ['only_new', 'only_old'];
|
||||
|
||||
$this->view->page = $page;
|
||||
$this->view->perPage = $perPage;
|
||||
$this->view->paginator = $this->getPaginatorHtml($buildId, $plugin, $severity, $isNew, $data['errors'], $perPage, $page);
|
||||
|
||||
$this->layout->title = Lang::get('build_n', $buildId);
|
||||
$this->layout->subtitle = $build->getProjectTitle();
|
||||
|
||||
switch ($build->getStatus()) {
|
||||
case 0:
|
||||
$this->layout->skin = 'blue';
|
||||
break;
|
||||
|
||||
case 1:
|
||||
$this->layout->skin = 'yellow';
|
||||
break;
|
||||
|
||||
case 2:
|
||||
$this->layout->skin = 'green';
|
||||
break;
|
||||
|
||||
case 3:
|
||||
$this->layout->skin = 'red';
|
||||
break;
|
||||
}
|
||||
|
||||
$rebuild = Lang::get('rebuild_now');
|
||||
$rebuildLink = APP_URL . 'build/rebuild/' . $build->getId();
|
||||
|
||||
$delete = Lang::get('delete_build');
|
||||
$deleteLink = APP_URL . 'build/delete/' . $build->getId();
|
||||
|
||||
$project = Factory::getStore('Project')->getByPrimaryKey($build->getProjectId());
|
||||
|
||||
$actions = '';
|
||||
if (!$project->getArchived()) {
|
||||
$actions .= "<a class=\"btn btn-default\" href=\"{$rebuildLink}\">{$rebuild}</a> ";
|
||||
}
|
||||
|
||||
if ($this->currentUserIsAdmin()) {
|
||||
$actions .= " <a class=\"btn btn-danger\" id=\"delete-build\" href=\"{$deleteLink}\">{$delete}</a>";
|
||||
}
|
||||
|
||||
$this->layout->actions = $actions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of the JS plugins to include.
|
||||
* @return array
|
||||
*/
|
||||
protected function getUiPlugins()
|
||||
{
|
||||
$rtn = [];
|
||||
$path = PUBLIC_DIR . 'assets' . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR . 'build-plugins' . DIRECTORY_SEPARATOR;
|
||||
$dir = opendir($path);
|
||||
|
||||
while ($item = readdir($dir)) {
|
||||
if (substr($item, 0, 1) == '.' || substr($item, -3) != '.js') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$rtn[] = $item;
|
||||
}
|
||||
|
||||
return $rtn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get build data from database and json encode it.
|
||||
*
|
||||
* @param Build $build
|
||||
* @param string $plugin
|
||||
* @param integer $severity
|
||||
* @param string $isNew
|
||||
* @param integer $start
|
||||
* @param integer $perPage
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getBuildData(Build $build, $plugin, $severity, $isNew, $start = 0, $perPage = 10)
|
||||
{
|
||||
$data = [];
|
||||
$data['status'] = (int)$build->getStatus();
|
||||
$data['log'] = $this->cleanLog($build->getLog());
|
||||
$data['create_date'] = !is_null($build->getCreateDate()) ? $build->getCreateDate()->format('Y-m-d H:i:s') : null;
|
||||
$data['start_date'] = !is_null($build->getStartDate()) ? $build->getStartDate()->format('Y-m-d H:i:s') : null;
|
||||
$data['finish_date'] = !is_null($build->getFinishDate()) ? $build->getFinishDate()->format('Y-m-d H:i:s') : null;
|
||||
$data['duration'] = $build->getDuration();
|
||||
|
||||
/** @var \PHPCensor\Store\BuildErrorStore $errorStore */
|
||||
$errorStore = Factory::getStore('BuildError');
|
||||
$errors = $errorStore->getByBuildId($build->getId(), $perPage, $start, $plugin, $severity, $isNew);
|
||||
|
||||
$errorView = new View('Build/errors');
|
||||
$errorView->build = $build;
|
||||
$errorView->errors = $errors['items'];
|
||||
|
||||
$data['errors'] = $errorStore->getErrorTotalForBuild($build->getId(), $plugin, $severity, $isNew);
|
||||
$data['errors_total'] = $errorStore->getErrorTotalForBuild($build->getId());
|
||||
$data['error_html'] = $errorView->render();
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param integer $buildId
|
||||
* @param string $plugin
|
||||
* @param integer $severity
|
||||
* @param string $isNew
|
||||
* @param integer $total
|
||||
* @param integer $perPage
|
||||
* @param integer $page
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getPaginatorHtml($buildId, $plugin, $severity, $isNew, $total, $perPage, $page)
|
||||
{
|
||||
$view = new View('pagination');
|
||||
|
||||
$urlPattern = APP_URL . 'build/view/' . $buildId;
|
||||
$params = [];
|
||||
if (!empty($plugin)) {
|
||||
$params['plugin'] = $plugin;
|
||||
}
|
||||
|
||||
if (null !== $severity) {
|
||||
$params['severity'] = $severity;
|
||||
}
|
||||
|
||||
if (!empty($isNew)) {
|
||||
$params['is_new'] = $isNew;
|
||||
}
|
||||
|
||||
$urlPattern = $urlPattern . '?' . str_replace('%28%3Anum%29', '(:num)', http_build_query(array_merge($params, ['page' => '(:num)']))) . '#errors';
|
||||
$paginator = new Paginator($total, $perPage, $page, $urlPattern);
|
||||
|
||||
$view->paginator = $paginator;
|
||||
|
||||
return $view->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a build using an existing build as a template:
|
||||
*/
|
||||
public function rebuild($buildId)
|
||||
{
|
||||
$copy = BuildFactory::getBuildById($buildId);
|
||||
$project = Factory::getStore('Project')->getByPrimaryKey($copy->getProjectId());
|
||||
|
||||
if (!$copy || $project->getArchived()) {
|
||||
throw new NotFoundException(Lang::get('build_x_not_found', $buildId));
|
||||
}
|
||||
|
||||
$build = $this->buildService->createDuplicateBuild($copy);
|
||||
|
||||
if ($this->buildService->queueError) {
|
||||
$_SESSION['global_error'] = Lang::get('add_to_queue_failed');
|
||||
}
|
||||
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', APP_URL.'build/view/' . $build->getId());
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a build.
|
||||
*/
|
||||
public function delete($buildId)
|
||||
{
|
||||
$this->requireAdmin();
|
||||
|
||||
$build = BuildFactory::getBuildById($buildId);
|
||||
|
||||
if (!$build) {
|
||||
throw new NotFoundException(Lang::get('build_x_not_found', $buildId));
|
||||
}
|
||||
|
||||
$this->buildService->deleteBuild($build);
|
||||
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', APP_URL.'project/view/' . $build->getProjectId());
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse log for unix colours and replace with HTML.
|
||||
*/
|
||||
protected function cleanLog($log)
|
||||
{
|
||||
return AnsiConverter::convert($log);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a list of builds into rows suitable for the dropdowns in the header bar.
|
||||
*
|
||||
* @param $builds
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function formatBuilds($builds)
|
||||
{
|
||||
$rtn = ['count' => $builds['count'], 'items' => []];
|
||||
|
||||
/** @var Build $build */
|
||||
foreach ($builds['items'] as $build) {
|
||||
$header = new View('Build/header-row');
|
||||
$header->build = $build;
|
||||
|
||||
$rtn['items'][$build->getId()]['header_row'] = $header->render();
|
||||
}
|
||||
|
||||
ksort($rtn['items']);
|
||||
return $rtn;
|
||||
}
|
||||
|
||||
public function ajaxData($buildId)
|
||||
{
|
||||
$page = (integer)$this->getParam('page', 1);
|
||||
$perPage = (integer)$this->getParam('per_page', 10);
|
||||
$plugin = $this->getParam('plugin', '');
|
||||
$isNew = $this->getParam('is_new', '');
|
||||
|
||||
$severity = $this->getParam('severity', null);
|
||||
if (null !== $severity && '' !== $severity) {
|
||||
$severity = (integer)$severity;
|
||||
} else {
|
||||
$severity = null;
|
||||
}
|
||||
|
||||
$response = new JsonResponse();
|
||||
$build = BuildFactory::getBuildById($buildId);
|
||||
|
||||
if (!$build) {
|
||||
$response->setResponseCode(404);
|
||||
$response->setContent([]);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
$data = $this->getBuildData($build, $plugin, $severity, $isNew, (($page - 1) * $perPage), $perPage);
|
||||
$data['paginator'] = $this->getPaginatorHtml($buildId, $plugin, $severity, $isNew, $data['errors'], $perPage, $page);
|
||||
|
||||
$response->setContent($data);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function ajaxMeta($buildId)
|
||||
{
|
||||
$build = BuildFactory::getBuildById($buildId);
|
||||
$key = $this->getParam('key', null);
|
||||
$numBuilds = $this->getParam('num_builds', 1);
|
||||
$data = null;
|
||||
|
||||
if ($key && $build) {
|
||||
$data = $this->buildStore->getMeta($key, $build->getProjectId(), $buildId, $build->getBranch(), $numBuilds);
|
||||
}
|
||||
|
||||
$response = new JsonResponse();
|
||||
$response->setContent($data);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function ajaxQueue()
|
||||
{
|
||||
$rtn = [
|
||||
'pending' => $this->formatBuilds($this->buildStore->getByStatus(Build::STATUS_PENDING)),
|
||||
'running' => $this->formatBuilds($this->buildStore->getByStatus(Build::STATUS_RUNNING)),
|
||||
];
|
||||
|
||||
$response = new JsonResponse();
|
||||
$response->setContent($rtn);
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
237
src/Controller/BuildStatusController.php
Normal file
237
src/Controller/BuildStatusController.php
Normal file
|
|
@ -0,0 +1,237 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use PHPCensor\Http\Response;
|
||||
use PHPCensor\Http\Response\RedirectResponse;
|
||||
use PHPCensor\Exception\HttpException\NotFoundException;
|
||||
use PHPCensor\Store\Factory;
|
||||
use PHPCensor\BuildFactory;
|
||||
use PHPCensor\Model\Project;
|
||||
use PHPCensor\Model\Build;
|
||||
use PHPCensor\Service\BuildStatusService;
|
||||
use PHPCensor\Controller;
|
||||
|
||||
/**
|
||||
* Build Status Controller - Allows external access to build status information / images.
|
||||
*
|
||||
* @author Dan Cryer <dan@block8.co.uk>
|
||||
*/
|
||||
class BuildStatusController extends Controller
|
||||
{
|
||||
/* @var \PHPCensor\Store\ProjectStore */
|
||||
protected $projectStore;
|
||||
|
||||
/* @var \PHPCensor\Store\BuildStore */
|
||||
protected $buildStore;
|
||||
|
||||
/**
|
||||
* Initialise the controller, set up stores and services.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->response->disableLayout();
|
||||
|
||||
$this->buildStore = Factory::getStore('Build');
|
||||
$this->projectStore = Factory::getStore('Project');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns status of the last build
|
||||
*
|
||||
* @param $projectId
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getStatus($projectId)
|
||||
{
|
||||
$status = null;
|
||||
$branch = $this->getParam('branch', 'master');
|
||||
|
||||
try {
|
||||
$project = $this->projectStore->getById($projectId);
|
||||
$status = 'passing';
|
||||
|
||||
if (isset($project) && $project instanceof Project) {
|
||||
$build = $project->getLatestBuild($branch, [
|
||||
Build::STATUS_SUCCESS,
|
||||
Build::STATUS_FAILED,
|
||||
]);
|
||||
|
||||
if (isset($build) && $build instanceof Build && $build->getStatus() !== Build::STATUS_SUCCESS) {
|
||||
$status = 'failed';
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$status = 'error';
|
||||
}
|
||||
|
||||
return $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays projects information in ccmenu format
|
||||
*
|
||||
* @param $projectId
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function ccxml($projectId)
|
||||
{
|
||||
/* @var Project $project */
|
||||
$project = $this->projectStore->getById($projectId);
|
||||
$xml = new \SimpleXMLElement('<Projects/>');
|
||||
|
||||
if (!$project instanceof Project || !$project->getAllowPublicStatus()) {
|
||||
return $this->renderXml($xml);
|
||||
}
|
||||
|
||||
try {
|
||||
$branchList = $this->buildStore->getBuildBranches($projectId);
|
||||
|
||||
if (!$branchList) {
|
||||
$branchList = [$project->getBranch()];
|
||||
}
|
||||
|
||||
foreach ($branchList as $branch) {
|
||||
$buildStatusService = new BuildStatusService($branch, $project, $project->getLatestBuild($branch));
|
||||
if ($attributes = $buildStatusService->toArray()) {
|
||||
$projectXml = $xml->addChild('Project');
|
||||
foreach ($attributes as $attributeKey => $attributeValue) {
|
||||
$projectXml->addAttribute($attributeKey, $attributeValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$xml = new \SimpleXMLElement('<projects/>');
|
||||
}
|
||||
|
||||
return $this->renderXml($xml);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \SimpleXMLElement $xml
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
protected function renderXml(\SimpleXMLElement $xml = null)
|
||||
{
|
||||
$this->response->setHeader('Content-Type', 'text/xml');
|
||||
$this->response->setContent($xml->asXML());
|
||||
$this->response->flush();
|
||||
echo $xml->asXML();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the appropriate build status image in SVG format for a given project.
|
||||
*
|
||||
* @param $projectId
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function image($projectId)
|
||||
{
|
||||
// plastic|flat|flat-squared|social
|
||||
$style = $this->getParam('style', 'flat');
|
||||
$label = $this->getParam('label', 'build');
|
||||
|
||||
$optionalParams = [
|
||||
'logo' => $this->getParam('logo'),
|
||||
'logoWidth' => $this->getParam('logoWidth'),
|
||||
'link' => $this->getParam('link'),
|
||||
'maxAge' => $this->getParam('maxAge'),
|
||||
];
|
||||
|
||||
$status = $this->getStatus($projectId);
|
||||
|
||||
if (is_null($status)) {
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', '/');
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
$color = ($status == 'passing') ? 'green' : 'red';
|
||||
$imageUrl = sprintf(
|
||||
'http://img.shields.io/badge/%s-%s-%s.svg?style=%s',
|
||||
$label,
|
||||
$status,
|
||||
$color,
|
||||
$style
|
||||
);
|
||||
|
||||
foreach ($optionalParams as $paramName => $param) {
|
||||
if ($param) {
|
||||
$imageUrl .= '&' . $paramName . '=' . $param;
|
||||
}
|
||||
}
|
||||
|
||||
$cacheDir = RUNTIME_DIR . 'status_cache/';
|
||||
$cacheFile = $cacheDir . md5($imageUrl) . '.svg';
|
||||
if (!is_file($cacheFile)) {
|
||||
$image = file_get_contents($imageUrl);
|
||||
file_put_contents($cacheFile, $image);
|
||||
}
|
||||
|
||||
$image = file_get_contents($cacheFile);
|
||||
|
||||
$this->response->disableLayout();
|
||||
$this->response->setHeader('Content-Type', 'image/svg+xml');
|
||||
$this->response->setContent($image);
|
||||
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
/**
|
||||
* View the public status page of a given project, if enabled.
|
||||
*
|
||||
* @param integer $projectId
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function view($projectId)
|
||||
{
|
||||
$project = $this->projectStore->getById($projectId);
|
||||
|
||||
if (empty($project) || !$project->getAllowPublicStatus()) {
|
||||
throw new NotFoundException('Project with id: ' . $projectId . ' not found');
|
||||
}
|
||||
|
||||
$builds = $this->getLatestBuilds($projectId);
|
||||
|
||||
if (count($builds)) {
|
||||
$this->view->latest = $builds[0];
|
||||
}
|
||||
|
||||
$this->view->builds = $builds;
|
||||
$this->view->project = $project;
|
||||
|
||||
return $this->view->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Render latest builds for project as HTML table.
|
||||
*
|
||||
* @param integer $projectId
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getLatestBuilds($projectId)
|
||||
{
|
||||
$criteria = ['project_id' => $projectId];
|
||||
$order = ['id' => 'DESC'];
|
||||
$builds = $this->buildStore->getWhere($criteria, 10, 0, $order);
|
||||
|
||||
foreach ($builds['items'] as &$build) {
|
||||
$build = BuildFactory::getBuild($build);
|
||||
}
|
||||
|
||||
return $builds['items'];
|
||||
}
|
||||
}
|
||||
129
src/Controller/GroupController.php
Normal file
129
src/Controller/GroupController.php
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use PHPCensor\Form;
|
||||
use PHPCensor\Controller;
|
||||
use PHPCensor\Http\Response\RedirectResponse;
|
||||
use PHPCensor\Model\ProjectGroup;
|
||||
use PHPCensor\Helper\Lang;
|
||||
use PHPCensor\Model\User;
|
||||
use PHPCensor\Store\Factory;
|
||||
|
||||
/**
|
||||
* Project Controller - Allows users to create, edit and view projects.
|
||||
*
|
||||
* @author Dan Cryer <dan@block8.co.uk>
|
||||
*/
|
||||
class GroupController extends Controller
|
||||
{
|
||||
/**
|
||||
* @var \PHPCensor\Store\ProjectGroupStore
|
||||
*/
|
||||
protected $groupStore;
|
||||
|
||||
/**
|
||||
* Set up this controller.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->groupStore = Factory::getStore('ProjectGroup');
|
||||
}
|
||||
|
||||
/**
|
||||
* List project groups.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$this->requireAdmin();
|
||||
|
||||
$groups = [];
|
||||
$groupList = $this->groupStore->getWhere([], 100, 0, ['title' => 'ASC']);
|
||||
|
||||
foreach ($groupList['items'] as $group) {
|
||||
$thisGroup = [
|
||||
'title' => $group->getTitle(),
|
||||
'id' => $group->getId(),
|
||||
];
|
||||
$projects_active = Factory::getStore('Project')->getByGroupId($group->getId(), false);
|
||||
$projects_archived = Factory::getStore('Project')->getByGroupId($group->getId(), true);
|
||||
|
||||
$thisGroup['projects'] = array_merge($projects_active['items'], $projects_archived['items']);
|
||||
$groups[] = $thisGroup;
|
||||
}
|
||||
|
||||
$this->layout->title = Lang::get('group_projects');
|
||||
$this->view->groups = $groups;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add or edit a project group.
|
||||
*
|
||||
* @param null $groupId
|
||||
*
|
||||
* @return RedirectResponse
|
||||
*/
|
||||
public function edit($groupId = null)
|
||||
{
|
||||
$this->requireAdmin();
|
||||
|
||||
if (!is_null($groupId)) {
|
||||
$group = $this->groupStore->getById($groupId);
|
||||
} else {
|
||||
$group = new ProjectGroup();
|
||||
}
|
||||
|
||||
if ($this->request->getMethod() == 'POST') {
|
||||
$group->setTitle($this->getParam('title'));
|
||||
if (is_null($groupId)) {
|
||||
/** @var User $user */
|
||||
$user = $this->getUser();
|
||||
|
||||
$group->setCreateDate(new \DateTime());
|
||||
$group->setUserId($user->getId());
|
||||
}
|
||||
|
||||
$this->groupStore->save($group);
|
||||
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', APP_URL.'group');
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
$form = new Form();
|
||||
$form->setMethod('POST');
|
||||
$form->setAction(APP_URL . 'group/edit' . (!is_null($groupId) ? '/' . $groupId : ''));
|
||||
|
||||
$title = new Form\Element\Text('title');
|
||||
$title->setContainerClass('form-group');
|
||||
$title->setClass('form-control');
|
||||
$title->setLabel(Lang::get('group_title'));
|
||||
$title->setValue($group->getTitle());
|
||||
|
||||
$submit = new Form\Element\Submit();
|
||||
$submit->setClass('btn btn-success');
|
||||
$submit->setValue(Lang::get('group_save'));
|
||||
|
||||
$form->addField($title);
|
||||
$form->addField($submit);
|
||||
|
||||
$this->view->form = $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a project group.
|
||||
* @param $groupId
|
||||
* @return RedirectResponse
|
||||
*/
|
||||
public function delete($groupId)
|
||||
{
|
||||
$this->requireAdmin();
|
||||
$group = $this->groupStore->getById($groupId);
|
||||
|
||||
$this->groupStore->delete($group);
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', APP_URL.'group');
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
42
src/Controller/HomeController.php
Normal file
42
src/Controller/HomeController.php
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use PHPCensor\Config;
|
||||
use PHPCensor\Helper\Lang;
|
||||
use PHPCensor\Controller;
|
||||
|
||||
/**
|
||||
* Home Controller - Displays the Dashboard.
|
||||
*/
|
||||
class HomeController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display dashboard:
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$this->layout->title = Lang::get('dashboard');
|
||||
|
||||
$widgets = [
|
||||
'left' => [],
|
||||
'right' => [],
|
||||
];
|
||||
$widgets_config = Config::getInstance()->get('php-censor.dashboard_widgets', [
|
||||
'all_projects' => [
|
||||
'side' => 'left',
|
||||
],
|
||||
'last_builds' => [
|
||||
'side' => 'right',
|
||||
],
|
||||
]);
|
||||
foreach($widgets_config as $name => $params) {
|
||||
$side = (isset($params['side']) and ($params['side'] == 'right')) ? 'right' : 'left';
|
||||
$widgets[$side][$name] = $params;
|
||||
}
|
||||
|
||||
$this->view->widgets = $widgets;
|
||||
|
||||
return $this->view->render();
|
||||
}
|
||||
}
|
||||
589
src/Controller/ProjectController.php
Normal file
589
src/Controller/ProjectController.php
Normal file
|
|
@ -0,0 +1,589 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use PHPCensor\Exception\HttpException\NotFoundException;
|
||||
use PHPCensor\Form;
|
||||
use JasonGrimes\Paginator;
|
||||
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;
|
||||
use PHPCensor\Model\Build;
|
||||
use PHPCensor\Http\Response\RedirectResponse;
|
||||
use PHPCensor\View;
|
||||
use PHPCensor\Store\Factory;
|
||||
|
||||
/**
|
||||
* Project Controller - Allows users to create, edit and view projects.
|
||||
*
|
||||
* @author Dan Cryer <dan@block8.co.uk>
|
||||
*/
|
||||
class ProjectController extends PHPCensor\Controller
|
||||
{
|
||||
/**
|
||||
* @var \PHPCensor\Store\ProjectStore
|
||||
*/
|
||||
protected $projectStore;
|
||||
|
||||
/**
|
||||
* @var \PHPCensor\Service\ProjectService
|
||||
*/
|
||||
protected $projectService;
|
||||
|
||||
/**
|
||||
* @var \PHPCensor\Store\BuildStore
|
||||
*/
|
||||
protected $buildStore;
|
||||
|
||||
/**
|
||||
* @var \PHPCensor\Service\BuildService
|
||||
*/
|
||||
protected $buildService;
|
||||
|
||||
/**
|
||||
* Initialise the controller, set up stores and services.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->buildStore = Factory::getStore('Build');
|
||||
$this->projectStore = Factory::getStore('Project');
|
||||
$this->projectService = new ProjectService($this->projectStore);
|
||||
$this->buildService = new BuildService($this->buildStore);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $projectId
|
||||
*
|
||||
* @return PHPCensor\Http\Response
|
||||
*/
|
||||
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);
|
||||
$builds = $this->getLatestBuildsHtml($projectId, $branch, $environment, (($page - 1) * $perPage), $perPage);
|
||||
|
||||
$this->response->disableLayout();
|
||||
$this->response->setContent($builds[0]);
|
||||
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
/**
|
||||
* View a specific project.
|
||||
*
|
||||
* @param integer $projectId
|
||||
*
|
||||
* @throws NotFoundException
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function view($projectId)
|
||||
{
|
||||
$branch = $this->getParam('branch', '');
|
||||
$environment = $this->getParam('environment', '');
|
||||
$page = (integer)$this->getParam('page', 1);
|
||||
$project = $this->projectStore->getById($projectId);
|
||||
|
||||
if (empty($project)) {
|
||||
throw new NotFoundException(Lang::get('project_x_not_found', $projectId));
|
||||
}
|
||||
|
||||
/** @var PHPCensor\Model\User $user */
|
||||
$user = $this->getUser();
|
||||
$perPage = $user->getFinalPerPage();
|
||||
$builds = $this->getLatestBuildsHtml($projectId, $branch, $environment, (($page - 1) * $perPage), $perPage);
|
||||
$pages = ($builds[1] === 0)
|
||||
? 1
|
||||
: (integer)ceil($builds[1] / $perPage);
|
||||
|
||||
if ($page > $pages) {
|
||||
$page = $pages;
|
||||
}
|
||||
|
||||
$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);
|
||||
$this->view->environments = $project->getEnvironmentsNames();
|
||||
$this->view->page = $page;
|
||||
$this->view->perPage = $perPage;
|
||||
$this->view->paginator = $this->getPaginatorHtml($projectId, $branch, $environment, $builds[1], $perPage, $page);
|
||||
|
||||
$this->layout->title = $project->getTitle();
|
||||
$this->layout->subtitle = '';
|
||||
|
||||
if (!empty($this->view->environment)) {
|
||||
$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;
|
||||
}
|
||||
|
||||
return $this->view->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* @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)
|
||||
{
|
||||
$view = new View('pagination');
|
||||
|
||||
$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();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new pending build for a project.
|
||||
*
|
||||
* @param integer $projectId
|
||||
*
|
||||
* @throws NotFoundException
|
||||
*
|
||||
* @return RedirectResponse
|
||||
*
|
||||
*/
|
||||
public function build($projectId)
|
||||
{
|
||||
/* @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));
|
||||
}
|
||||
|
||||
$type = $this->getParam('type', 'branch');
|
||||
$id = $this->getParam('id');
|
||||
$debug = (boolean)$this->getParam('debug', false);
|
||||
|
||||
$environment = null;
|
||||
$branch = null;
|
||||
|
||||
switch($type) {
|
||||
case 'environment':
|
||||
$environment = $id;
|
||||
break;
|
||||
case 'branch':
|
||||
$branch = $id;
|
||||
break;
|
||||
}
|
||||
|
||||
if (empty($branch)) {
|
||||
$branch = $project->getBranch();
|
||||
}
|
||||
|
||||
$extra = null;
|
||||
|
||||
if ($debug && $this->currentUserIsAdmin()) {
|
||||
$extra = [
|
||||
'debug' => true,
|
||||
];
|
||||
}
|
||||
|
||||
/** @var PHPCensor\Model\User $user */
|
||||
$user = $this->getUser();
|
||||
$build = $this->buildService->createBuild(
|
||||
$project,
|
||||
$environment,
|
||||
'',
|
||||
$branch,
|
||||
null,
|
||||
$user->getEmail(),
|
||||
null,
|
||||
Build::SOURCE_MANUAL_WEB,
|
||||
$user->getId(),
|
||||
$extra
|
||||
);
|
||||
|
||||
if ($this->buildService->queueError) {
|
||||
$_SESSION['global_error'] = Lang::get('add_to_queue_failed');
|
||||
}
|
||||
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', APP_URL.'build/view/' . $build->getId());
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a project.
|
||||
*/
|
||||
public function delete($projectId)
|
||||
{
|
||||
$this->requireAdmin();
|
||||
|
||||
$project = $this->projectStore->getById($projectId);
|
||||
$this->projectService->deleteProject($project);
|
||||
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', APP_URL);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render latest builds for project as HTML table.
|
||||
*
|
||||
* @param int $projectId
|
||||
* @param string $branch A urldecoded branch name.
|
||||
* @param string $environment A urldecoded environment name.
|
||||
* @param int $start
|
||||
* @param int $perPage
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getLatestBuildsHtml($projectId, $branch = '', $environment = '', $start = 0, $perPage = 10)
|
||||
{
|
||||
$criteria = ['project_id' => $projectId];
|
||||
|
||||
if (!empty($environment)) {
|
||||
$criteria['environment'] = $environment;
|
||||
}
|
||||
|
||||
if (!empty($branch)) {
|
||||
$criteria['branch'] = $branch;
|
||||
}
|
||||
|
||||
$order = ['id' => 'DESC'];
|
||||
$builds = $this->buildStore->getWhere($criteria, $perPage, $start, $order);
|
||||
$view = new View('Project/ajax-builds');
|
||||
|
||||
foreach ($builds['items'] as &$build) {
|
||||
$build = BuildFactory::getBuild($build);
|
||||
}
|
||||
|
||||
$view->builds = $builds['items'];
|
||||
|
||||
return [
|
||||
$view->render(),
|
||||
(integer)$builds['count']
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new project. Handles both the form, and processing.
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
$this->layout->title = Lang::get('add_project');
|
||||
$this->requireAdmin();
|
||||
|
||||
$method = $this->request->getMethod();
|
||||
$values = $this->getParams();
|
||||
$values['branch'] = '';
|
||||
|
||||
if ($method !== 'POST') {
|
||||
$sshKey = new SshKey();
|
||||
$key = $sshKey->generate();
|
||||
|
||||
$values['key'] = $key['private_key'];
|
||||
$values['pubkey'] = $key['public_key'];
|
||||
}
|
||||
|
||||
$form = $this->projectForm($values);
|
||||
|
||||
if ($method != 'POST' || ($method == 'POST' && !$form->validate())) {
|
||||
$view = new View('Project/edit');
|
||||
$view->type = 'add';
|
||||
$view->project = null;
|
||||
$view->form = $form;
|
||||
$view->key = $values['pubkey'];
|
||||
|
||||
return $view->render();
|
||||
} else {
|
||||
$title = $this->getParam('title', 'New Project');
|
||||
$reference = $this->getParam('reference', null);
|
||||
$type = $this->getParam('type', null);
|
||||
|
||||
$options = [
|
||||
'ssh_private_key' => $this->getParam('key', null),
|
||||
'ssh_public_key' => $this->getParam('pubkey', null),
|
||||
'build_config' => $this->getParam('build_config', null),
|
||||
'allow_public_status' => $this->getParam('allow_public_status', 0),
|
||||
'branch' => $this->getParam('branch', null),
|
||||
'default_branch_only' => $this->getParam('default_branch_only', 0),
|
||||
'group' => $this->getParam('group_id', null),
|
||||
'environments' => $this->getParam('environments', null),
|
||||
];
|
||||
|
||||
/** @var PHPCensor\Model\User $user */
|
||||
$user = $this->getUser();
|
||||
$project = $this->projectService->createProject($title, $type, $reference, $user->getId(), $options);
|
||||
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', APP_URL.'project/view/' . $project->getId());
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit a project. Handles both the form and processing.
|
||||
*/
|
||||
public function edit($projectId)
|
||||
{
|
||||
$this->requireAdmin();
|
||||
|
||||
$method = $this->request->getMethod();
|
||||
$project = $this->projectStore->getById($projectId);
|
||||
|
||||
if (empty($project)) {
|
||||
throw new NotFoundException(Lang::get('project_x_not_found', $projectId));
|
||||
}
|
||||
|
||||
$this->layout->title = $project->getTitle();
|
||||
$this->layout->subtitle = Lang::get('edit_project');
|
||||
|
||||
$values = $project->getDataArray();
|
||||
$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";
|
||||
$values['reference'] = $reference;
|
||||
}
|
||||
|
||||
if ($method == 'POST') {
|
||||
$values = $this->getParams();
|
||||
}
|
||||
|
||||
$form = $this->projectForm($values, 'edit/' . $projectId);
|
||||
|
||||
if ($method != 'POST' || ($method == 'POST' && !$form->validate())) {
|
||||
$view = new View('Project/edit');
|
||||
$view->type = 'edit';
|
||||
$view->project = $project;
|
||||
$view->form = $form;
|
||||
$view->key = $values['pubkey'];
|
||||
|
||||
return $view->render();
|
||||
}
|
||||
|
||||
$title = $this->getParam('title', Lang::get('new_project'));
|
||||
$reference = $this->getParam('reference', null);
|
||||
$type = $this->getParam('type', null);
|
||||
|
||||
$options = [
|
||||
'ssh_private_key' => $this->getParam('key', null),
|
||||
'ssh_public_key' => $this->getParam('pubkey', null),
|
||||
'build_config' => $this->getParam('build_config', null),
|
||||
'allow_public_status' => $this->getParam('allow_public_status', 0),
|
||||
'archived' => $this->getParam('archived', 0),
|
||||
'branch' => $this->getParam('branch', null),
|
||||
'default_branch_only' => $this->getParam('default_branch_only', 0),
|
||||
'group' => $this->getParam('group_id', null),
|
||||
'environments' => $this->getParam('environments', null),
|
||||
];
|
||||
|
||||
$project = $this->projectService->updateProject($project, $title, $type, $reference, $options);
|
||||
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', APP_URL.'project/view/' . $project->getId());
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create add / edit project form.
|
||||
*/
|
||||
protected function projectForm($values, $type = 'add')
|
||||
{
|
||||
$form = new Form();
|
||||
|
||||
$form->setMethod('POST');
|
||||
$form->setAction(APP_URL.'project/' . $type);
|
||||
|
||||
$form->addField(new Form\Element\Csrf('csrf'));
|
||||
$form->addField(new Form\Element\Hidden('pubkey'));
|
||||
|
||||
$options = [
|
||||
'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)',
|
||||
];
|
||||
|
||||
$field = Form\Element\Select::create('type', Lang::get('where_hosted'), true);
|
||||
$field->setPattern('^(github|bitbucket|bitbucket-hg|gitlab|gogs|git|local|hg|svn)');
|
||||
$field->setOptions($options);
|
||||
$field->setClass('form-control')->setContainerClass('form-group');
|
||||
$form->addField($field);
|
||||
|
||||
$container = new Form\ControlGroup('github-container');
|
||||
$container->setClass('github-container');
|
||||
|
||||
$field = Form\Element\Select::create('github', Lang::get('choose_github'), false);
|
||||
$field->setClass('form-control')->setContainerClass('form-group');
|
||||
$container->addField($field);
|
||||
$form->addField($container);
|
||||
|
||||
$field = Form\Element\Text::create('reference', Lang::get('repo_name'), true);
|
||||
$field->setValidator($this->getReferenceValidator($values));
|
||||
$field->setClass('form-control')->setContainerClass('form-group');
|
||||
$form->addField($field);
|
||||
|
||||
$field = Form\Element\Text::create('title', Lang::get('project_title'), true);
|
||||
$field->setClass('form-control')->setContainerClass('form-group');
|
||||
$form->addField($field);
|
||||
|
||||
$field = Form\Element\Text::create('branch', Lang::get('default_branch'), false);
|
||||
$field->setClass('form-control')->setContainerClass('form-group')->setValue('');
|
||||
$form->addField($field);
|
||||
|
||||
$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);
|
||||
|
||||
$field = Form\Element\TextArea::create('key', Lang::get('project_private_key'), false);
|
||||
$field->setClass('form-control')->setContainerClass('form-group');
|
||||
$field->setRows(6);
|
||||
$form->addField($field);
|
||||
|
||||
$field = Form\Element\TextArea::create('build_config', Lang::get('build_config'), false);
|
||||
$field->setClass('form-control')->setContainerClass('form-group');
|
||||
$field->setRows(6);
|
||||
$form->addField($field);
|
||||
|
||||
$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);
|
||||
|
||||
$groups = [];
|
||||
$groupStore = Factory::getStore('ProjectGroup');
|
||||
$groupList = $groupStore->getWhere([], 100, 0, ['title' => 'ASC']);
|
||||
|
||||
foreach ($groupList['items'] as $group) {
|
||||
$groups[$group->getId()] = $group->getTitle();
|
||||
}
|
||||
|
||||
$field->setOptions($groups);
|
||||
$form->addField($field);
|
||||
|
||||
$field = Form\Element\Checkbox::create('allow_public_status', Lang::get('allow_public_status'), false);
|
||||
$field->setContainerClass('form-group');
|
||||
$field->setCheckedValue(1);
|
||||
$field->setValue(0);
|
||||
$form->addField($field);
|
||||
|
||||
$field = Form\Element\Checkbox::create('archived', Lang::get('archived'), false);
|
||||
$field->setContainerClass('form-group');
|
||||
$field->setCheckedValue(1);
|
||||
$field->setValue(0);
|
||||
$form->addField($field);
|
||||
|
||||
$field = new Form\Element\Submit();
|
||||
$field->setValue(Lang::get('save_project'));
|
||||
$field->setContainerClass('form-group');
|
||||
$field->setClass('btn-success');
|
||||
$form->addField($field);
|
||||
|
||||
$form->setValues($values);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validator to use to check project references.
|
||||
* @param $values
|
||||
* @return callable
|
||||
*/
|
||||
protected function getReferenceValidator($values)
|
||||
{
|
||||
return function ($val) use ($values) {
|
||||
$type = $values['type'];
|
||||
|
||||
$validators = [
|
||||
'hg' => [
|
||||
'regex' => '/^(ssh|https?):\/\//',
|
||||
'message' => Lang::get('error_hg')
|
||||
],
|
||||
'git' => [
|
||||
'regex' => '/^(git|https?):\/\//',
|
||||
'message' => Lang::get('error_git')
|
||||
],
|
||||
'gitlab' => [
|
||||
'regex' => '`^(.*)@(.*):(.*)/(.*)\.git`',
|
||||
'message' => Lang::get('error_gitlab')
|
||||
],
|
||||
'github' => [
|
||||
'regex' => '/^[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-\.]+$/',
|
||||
'message' => Lang::get('error_github')
|
||||
],
|
||||
'bitbucket' => [
|
||||
'regex' => '/^[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-\.]+$/',
|
||||
'message' => Lang::get('error_bitbucket')
|
||||
],
|
||||
'bitbucket-hg' => [
|
||||
'regex' => '/^[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-\.]+$/',
|
||||
'message' => Lang::get('error_bitbucket')
|
||||
],
|
||||
];
|
||||
|
||||
if (in_array($type, $validators) && !preg_match($validators[$type]['regex'], $val)) {
|
||||
throw new \Exception($validators[$type]['message']);
|
||||
} elseif ($type == 'local' && !is_dir($val)) {
|
||||
throw new \Exception(Lang::get('error_path'));
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array of repositories from Github's API.
|
||||
*/
|
||||
public function ajaxGithubRepositories()
|
||||
{
|
||||
$github = new Github();
|
||||
|
||||
$response = new PHPCensor\Http\Response\JsonResponse();
|
||||
$response->setContent($github->getRepositories());
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
282
src/Controller/SessionController.php
Normal file
282
src/Controller/SessionController.php
Normal file
|
|
@ -0,0 +1,282 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use PHPCensor\Helper\Email;
|
||||
use PHPCensor\Helper\Lang;
|
||||
use PHPCensor\Controller;
|
||||
use PHPCensor\Http\Response\RedirectResponse;
|
||||
use PHPCensor\Security\Authentication\Service;
|
||||
use PHPCensor\Store\UserStore;
|
||||
use PHPCensor\Store\Factory;
|
||||
|
||||
/**
|
||||
* Session Controller - Handles user login / logout.
|
||||
*
|
||||
* @author Dan Cryer <dan@block8.co.uk>
|
||||
*/
|
||||
class SessionController extends Controller
|
||||
{
|
||||
/**
|
||||
* @var UserStore
|
||||
*/
|
||||
protected $userStore;
|
||||
|
||||
/**
|
||||
* @var Service
|
||||
*/
|
||||
protected $authentication;
|
||||
|
||||
/**
|
||||
* Initialise the controller, set up stores and services.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->response->disableLayout();
|
||||
|
||||
$this->userStore = Factory::getStore('User');
|
||||
$this->authentication = Service::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles user login (form and processing)
|
||||
*/
|
||||
public function login()
|
||||
{
|
||||
if (!empty($_COOKIE['remember_key'])) {
|
||||
$user = $this->userStore->getByRememberKey($_COOKIE['remember_key']);
|
||||
if ($user) {
|
||||
$_SESSION['php-censor-user-id'] = $user->getId();
|
||||
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', $this->getLoginRedirect());
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
$isLoginFailure = false;
|
||||
|
||||
if ($this->request->getMethod() == 'POST') {
|
||||
$token = $this->getParam('token');
|
||||
if (!isset($token, $_SESSION['login_token']) || $token !== $_SESSION['login_token']) {
|
||||
$isLoginFailure = true;
|
||||
} else {
|
||||
unset($_SESSION['login_token']);
|
||||
|
||||
$email = $this->getParam('email');
|
||||
$password = $this->getParam('password', '');
|
||||
$rememberMe = (bool)$this->getParam('remember_me', 0);
|
||||
$isLoginFailure = true;
|
||||
|
||||
$user = $this->userStore->getByEmailOrName($email);
|
||||
$providers = $this->authentication->getLoginPasswordProviders();
|
||||
|
||||
if (null !== $user) {
|
||||
// Delegate password verification to the user provider, if found
|
||||
$key = $user->getProviderKey();
|
||||
$isLoginFailure = !isset($providers[$key]) || !$providers[$key]->verifyPassword($user, $password);
|
||||
} else {
|
||||
// Ask each providers to provision the user
|
||||
foreach ($providers as $provider) {
|
||||
$user = $provider->provisionUser($email);
|
||||
if ($user && $provider->verifyPassword($user, $password)) {
|
||||
$this->userStore->save($user);
|
||||
$isLoginFailure = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$isLoginFailure) {
|
||||
$_SESSION['php-censor-user-id'] = $user->getId();
|
||||
|
||||
if ($rememberMe) {
|
||||
$rememberKey = md5(microtime(true));
|
||||
|
||||
$user->setRememberKey($rememberKey);
|
||||
$this->userStore->save($user);
|
||||
|
||||
setcookie(
|
||||
'remember_key',
|
||||
$rememberKey,
|
||||
(time() + 60 * 60 * 24 * 30),
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', $this->getLoginRedirect());
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$form = new \PHPCensor\Form();
|
||||
$form->setMethod('POST');
|
||||
$form->setAction(APP_URL . 'session/login');
|
||||
|
||||
$email = new \PHPCensor\Form\Element\Text('email');
|
||||
$email->setLabel(Lang::get('login'));
|
||||
$email->setRequired(true);
|
||||
$email->setContainerClass('form-group');
|
||||
$email->setClass('form-control');
|
||||
$form->addField($email);
|
||||
|
||||
$pwd = new \PHPCensor\Form\Element\Password('password');
|
||||
$pwd->setLabel(Lang::get('password'));
|
||||
$pwd->setRequired(true);
|
||||
$pwd->setContainerClass('form-group');
|
||||
$pwd->setClass('form-control');
|
||||
$form->addField($pwd);
|
||||
|
||||
$remember = \PHPCensor\Form\Element\Checkbox::create('remember_me', Lang::get('remember_me'), false);
|
||||
$remember->setContainerClass('form-group');
|
||||
$remember->setCheckedValue(1);
|
||||
$remember->setValue(0);
|
||||
$form->addField($remember);
|
||||
|
||||
$pwd = new \PHPCensor\Form\Element\Submit();
|
||||
$pwd->setValue(Lang::get('log_in'));
|
||||
$pwd->setClass('btn-success');
|
||||
$form->addField($pwd);
|
||||
|
||||
$tokenValue = $this->generateToken();
|
||||
$_SESSION['login_token'] = $tokenValue;
|
||||
$token = new \PHPCensor\Form\Element\Hidden('token');
|
||||
$token->setValue($tokenValue);
|
||||
$form->addField($token);
|
||||
|
||||
$this->view->form = $form->render();
|
||||
$this->view->failed = $isLoginFailure;
|
||||
|
||||
return $this->view->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles user logout.
|
||||
*/
|
||||
public function logout()
|
||||
{
|
||||
unset($_SESSION['php-censor-user-id']);
|
||||
|
||||
session_destroy();
|
||||
|
||||
setcookie(
|
||||
'remember_key',
|
||||
null,
|
||||
(time() - 1),
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
true
|
||||
);
|
||||
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', APP_URL);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows the user to request a password reset email.
|
||||
* @return string
|
||||
*/
|
||||
public function forgotPassword()
|
||||
{
|
||||
if ($this->request->getMethod() == 'POST') {
|
||||
$email = $this->getParam('email', null);
|
||||
$user = $this->userStore->getByEmail($email);
|
||||
|
||||
if (empty($user)) {
|
||||
$this->view->error = Lang::get('reset_no_user_exists');
|
||||
return $this->view->render();
|
||||
}
|
||||
|
||||
$key = md5(date('Y-m-d') . $user->getHash());
|
||||
$url = APP_URL;
|
||||
|
||||
$message = Lang::get('reset_email_body', $user->getName(), $url, $user->getId(), $key);
|
||||
|
||||
$email = new Email();
|
||||
$email->setEmailTo($user->getEmail(), $user->getName());
|
||||
$email->setSubject(Lang::get('reset_email_title', $user->getName()));
|
||||
$email->setBody($message);
|
||||
$email->send();
|
||||
|
||||
$this->view->emailed = true;
|
||||
}
|
||||
|
||||
return $this->view->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows the user to change their password after a password reset email.
|
||||
* @param $userId
|
||||
* @param $key
|
||||
* @return string
|
||||
*/
|
||||
public function resetPassword($userId, $key)
|
||||
{
|
||||
$user = $this->userStore->getById($userId);
|
||||
$userKey = md5(date('Y-m-d') . $user->getHash());
|
||||
|
||||
if (empty($user) || $key != $userKey) {
|
||||
$this->view->error = Lang::get('reset_invalid');
|
||||
return $this->view->render();
|
||||
}
|
||||
|
||||
if ($this->request->getMethod() == 'POST') {
|
||||
$hash = password_hash($this->getParam('password'), PASSWORD_DEFAULT);
|
||||
$user->setHash($hash);
|
||||
|
||||
$this->userStore->save($user);
|
||||
|
||||
$_SESSION['php-censor-user-id'] = $user->getId();
|
||||
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', APP_URL);
|
||||
return $response;
|
||||
}
|
||||
|
||||
$this->view->id = $userId;
|
||||
$this->view->key = $key;
|
||||
|
||||
return $this->view->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the URL the user was trying to go to prior to being asked to log in.
|
||||
* @return string
|
||||
*/
|
||||
protected function getLoginRedirect()
|
||||
{
|
||||
$rtn = APP_URL;
|
||||
|
||||
if (!empty($_SESSION['php-censor-login-redirect'])) {
|
||||
$rtn .= $_SESSION['php-censor-login-redirect'];
|
||||
$_SESSION['php-censor-login-redirect'] = null;
|
||||
}
|
||||
|
||||
return $rtn;
|
||||
}
|
||||
|
||||
/** Generate a random token.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function generateToken()
|
||||
{
|
||||
if (function_exists('openssl_random_pseudo_bytes')) {
|
||||
return bin2hex(openssl_random_pseudo_bytes(16));
|
||||
}
|
||||
|
||||
return sprintf("%04x", mt_rand(0, 0xFFFF))
|
||||
. sprintf("%04x", mt_rand(0, 0xFFFF))
|
||||
. sprintf("%04x", mt_rand(0, 0xFFFF))
|
||||
. sprintf("%04x", mt_rand(0, 0xFFFF));
|
||||
}
|
||||
}
|
||||
306
src/Controller/UserController.php
Normal file
306
src/Controller/UserController.php
Normal file
|
|
@ -0,0 +1,306 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use PHPCensor\Config;
|
||||
use PHPCensor\Exception\HttpException\NotFoundException;
|
||||
use PHPCensor\Form;
|
||||
use PHPCensor\Controller;
|
||||
use PHPCensor\Helper\Lang;
|
||||
use PHPCensor\Http\Response\RedirectResponse;
|
||||
use PHPCensor\Model\User;
|
||||
use PHPCensor\Service\UserService;
|
||||
use PHPCensor\View;
|
||||
use PHPCensor\Store\Factory;
|
||||
|
||||
/**
|
||||
* User Controller - Allows an administrator to view, add, edit and delete users.
|
||||
*
|
||||
* @author Dan Cryer <dan@block8.co.uk>
|
||||
*/
|
||||
class UserController extends Controller
|
||||
{
|
||||
/**
|
||||
* @var \PHPCensor\Store\UserStore
|
||||
*/
|
||||
protected $userStore;
|
||||
|
||||
/**
|
||||
* @var \PHPCensor\Service\UserService
|
||||
*/
|
||||
protected $userService;
|
||||
|
||||
/**
|
||||
* Initialise the controller, set up stores and services.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->userStore = Factory::getStore('User');
|
||||
$this->userService = new UserService($this->userStore);
|
||||
}
|
||||
|
||||
/**
|
||||
* View user list.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$users = $this->userStore->getWhere([], 1000, 0, ['email' => 'ASC']);
|
||||
$this->view->users = $users;
|
||||
$this->layout->title = Lang::get('manage_users');
|
||||
|
||||
return $this->view->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows the user to edit their profile.
|
||||
* @return string
|
||||
*/
|
||||
public function profile()
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = $this->getUser();
|
||||
|
||||
if ($this->request->getMethod() == 'POST') {
|
||||
$name = $this->getParam('name', null);
|
||||
$email = $this->getParam('email', null);
|
||||
$password = $this->getParam('password', null);
|
||||
|
||||
$language = $this->getParam('language', null);
|
||||
if (!$language) {
|
||||
$language = null;
|
||||
}
|
||||
|
||||
$perPage = $this->getParam('per_page', null);
|
||||
if (!$perPage) {
|
||||
$perPage = null;
|
||||
}
|
||||
|
||||
$user = $this->userService->updateUser($user, $name, $email, $password, null, $language, $perPage);
|
||||
|
||||
$this->view->updated = 1;
|
||||
}
|
||||
|
||||
$this->layout->title = $user->getName();
|
||||
$this->layout->subtitle = Lang::get('edit_profile');
|
||||
|
||||
$form = new Form();
|
||||
$form->setAction(APP_URL.'user/profile');
|
||||
$form->setMethod('POST');
|
||||
|
||||
$name = new Form\Element\Text('name');
|
||||
$name->setClass('form-control');
|
||||
$name->setContainerClass('form-group');
|
||||
$name->setLabel(Lang::get('name'));
|
||||
$name->setRequired(true);
|
||||
$name->setValue($user->getName());
|
||||
$form->addField($name);
|
||||
|
||||
$email = new Form\Element\Email('email');
|
||||
$email->setClass('form-control');
|
||||
$email->setContainerClass('form-group');
|
||||
$email->setLabel(Lang::get('email_address'));
|
||||
$email->setRequired(true);
|
||||
$email->setValue($user->getEmail());
|
||||
$form->addField($email);
|
||||
|
||||
$password = new Form\Element\Password('password');
|
||||
$password->setClass('form-control');
|
||||
$password->setContainerClass('form-group');
|
||||
$password->setLabel(Lang::get('password_change'));
|
||||
$password->setRequired(false);
|
||||
$password->setValue(null);
|
||||
$form->addField($password);
|
||||
|
||||
$language = new Form\Element\Select('language');
|
||||
$language->setClass('form-control');
|
||||
$language->setContainerClass('form-group');
|
||||
$language->setLabel(Lang::get('language'));
|
||||
$language->setRequired(true);
|
||||
$language->setOptions(array_merge(
|
||||
[null => Lang::get('default') . ' (' . Config::getInstance()->get('php-censor.language') . ')'],
|
||||
Lang::getLanguageOptions())
|
||||
);
|
||||
$language->setValue($user->getLanguage());
|
||||
$form->addField($language);
|
||||
|
||||
$perPage = new Form\Element\Select('per_page');
|
||||
$perPage->setClass('form-control');
|
||||
$perPage->setContainerClass('form-group');
|
||||
$perPage->setLabel(Lang::get('per_page'));
|
||||
$perPage->setRequired(true);
|
||||
$perPage->setOptions([
|
||||
null => Lang::get('default') . ' (' . Config::getInstance()->get('php-censor.per_page') . ')',
|
||||
10 => 10,
|
||||
25 => 25,
|
||||
50 => 50,
|
||||
100 => 100,
|
||||
]);
|
||||
$perPage->setValue($user->getPerPage());
|
||||
$form->addField($perPage);
|
||||
|
||||
$submit = new Form\Element\Submit();
|
||||
$submit->setClass('btn btn-success');
|
||||
$submit->setValue(Lang::get('save'));
|
||||
$form->addField($submit);
|
||||
|
||||
$this->view->form = $form;
|
||||
|
||||
return $this->view->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a user - handles both form and processing.
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
$this->requireAdmin();
|
||||
|
||||
$this->layout->title = Lang::get('add_user');
|
||||
|
||||
$method = $this->request->getMethod();
|
||||
|
||||
if ($method == 'POST') {
|
||||
$values = $this->getParams();
|
||||
} else {
|
||||
$values = [];
|
||||
}
|
||||
|
||||
$form = $this->userForm($values);
|
||||
|
||||
if ($method != 'POST' || ($method == 'POST' && !$form->validate())) {
|
||||
$view = new View('User/edit');
|
||||
$view->type = 'add';
|
||||
$view->user = null;
|
||||
$view->form = $form;
|
||||
|
||||
return $view->render();
|
||||
}
|
||||
|
||||
|
||||
$name = $this->getParam('name', null);
|
||||
$email = $this->getParam('email', null);
|
||||
$password = $this->getParam('password', null);
|
||||
$isAdmin = (int)$this->getParam('is_admin', 0);
|
||||
|
||||
$this->userService->createUser($name, $email, 'internal', json_encode(['type' => 'internal']), $password, $isAdmin);
|
||||
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', APP_URL . 'user');
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit a user - handles both form and processing.
|
||||
*/
|
||||
public function edit($userId)
|
||||
{
|
||||
$this->requireAdmin();
|
||||
|
||||
$method = $this->request->getMethod();
|
||||
$user = $this->userStore->getById($userId);
|
||||
|
||||
if (empty($user)) {
|
||||
throw new NotFoundException(Lang::get('user_n_not_found', $userId));
|
||||
}
|
||||
|
||||
$this->layout->title = $user->getName();
|
||||
$this->layout->subtitle = Lang::get('edit_user');
|
||||
|
||||
$values = array_merge($user->getDataArray(), $this->getParams());
|
||||
$form = $this->userForm($values, 'edit/' . $userId);
|
||||
|
||||
if ($method != 'POST' || ($method == 'POST' && !$form->validate())) {
|
||||
$view = new View('User/edit');
|
||||
$view->type = 'edit';
|
||||
$view->user = $user;
|
||||
$view->form = $form;
|
||||
|
||||
return $view->render();
|
||||
}
|
||||
|
||||
$name = $this->getParam('name', null);
|
||||
$email = $this->getParam('email', null);
|
||||
$password = $this->getParam('password', null);
|
||||
$isAdmin = (int)$this->getParam('is_admin', 0);
|
||||
|
||||
$this->userService->updateUser($user, $name, $email, $password, $isAdmin);
|
||||
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', APP_URL . 'user');
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create user add / edit form.
|
||||
*/
|
||||
protected function userForm($values, $type = 'add')
|
||||
{
|
||||
$form = new Form();
|
||||
$form->setMethod('POST');
|
||||
$form->setAction(APP_URL.'user/' . $type);
|
||||
$form->addField(new Form\Element\Csrf('csrf'));
|
||||
|
||||
$field = new Form\Element\Email('email');
|
||||
$field->setRequired(true);
|
||||
$field->setLabel(Lang::get('email_address'));
|
||||
$field->setClass('form-control');
|
||||
$field->setContainerClass('form-group');
|
||||
$form->addField($field);
|
||||
|
||||
$field = new Form\Element\Text('name');
|
||||
$field->setRequired(true);
|
||||
$field->setLabel(Lang::get('name'));
|
||||
$field->setClass('form-control');
|
||||
$field->setContainerClass('form-group');
|
||||
$form->addField($field);
|
||||
|
||||
$field = new Form\Element\Password('password');
|
||||
|
||||
if ($type == 'add') {
|
||||
$field->setRequired(true);
|
||||
$field->setLabel(Lang::get('password'));
|
||||
} else {
|
||||
$field->setRequired(false);
|
||||
$field->setLabel(Lang::get('password_change'));
|
||||
}
|
||||
|
||||
$field->setClass('form-control');
|
||||
$field->setContainerClass('form-group');
|
||||
$form->addField($field);
|
||||
|
||||
$field = new Form\Element\Checkbox('is_admin');
|
||||
$field->setRequired(false);
|
||||
$field->setCheckedValue(1);
|
||||
$field->setLabel(Lang::get('is_user_admin'));
|
||||
$field->setContainerClass('form-group');
|
||||
$form->addField($field);
|
||||
|
||||
$field = new Form\Element\Submit();
|
||||
$field->setValue(Lang::get('save_user'));
|
||||
$field->setClass('btn-success');
|
||||
$form->addField($field);
|
||||
|
||||
$form->setValues($values);
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a user.
|
||||
*/
|
||||
public function delete($userId)
|
||||
{
|
||||
$this->requireAdmin();
|
||||
|
||||
$user = $this->userStore->getById($userId);
|
||||
|
||||
if (empty($user)) {
|
||||
throw new NotFoundException(Lang::get('user_n_not_found', $userId));
|
||||
}
|
||||
|
||||
$this->userService->deleteUser($user);
|
||||
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', APP_URL . 'user');
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
904
src/Controller/WebhookController.php
Normal file
904
src/Controller/WebhookController.php
Normal file
|
|
@ -0,0 +1,904 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use Exception;
|
||||
use GuzzleHttp\Client;
|
||||
use PHPCensor\Helper\Lang;
|
||||
use PHPCensor\Model\Build;
|
||||
use PHPCensor\Model\Project;
|
||||
use PHPCensor\Service\BuildService;
|
||||
use PHPCensor\Store\BuildStore;
|
||||
use PHPCensor\Store\ProjectStore;
|
||||
use PHPCensor\Controller;
|
||||
use PHPCensor\Config;
|
||||
use PHPCensor\Exception\HttpException\NotFoundException;
|
||||
use PHPCensor\Store\Factory;
|
||||
use PHPCensor\Http\Request;
|
||||
use PHPCensor\Http\Response;
|
||||
|
||||
/**
|
||||
* Webhook Controller - Processes webhook pings from BitBucket, Github, Gitlab, Gogs, etc.
|
||||
*
|
||||
* @author Dan Cryer <dan@block8.co.uk>
|
||||
* @author Sami Tikka <stikka@iki.fi>
|
||||
* @author Alex Russell <alex@clevercherry.com>
|
||||
* @author Guillaume Perréal <adirelle@gmail.com>
|
||||
*
|
||||
*/
|
||||
class WebhookController extends Controller
|
||||
{
|
||||
/**
|
||||
* @var BuildStore
|
||||
*/
|
||||
protected $buildStore;
|
||||
|
||||
/**
|
||||
* @var ProjectStore
|
||||
*/
|
||||
protected $projectStore;
|
||||
|
||||
/**
|
||||
* @var BuildService
|
||||
*/
|
||||
protected $buildService;
|
||||
|
||||
/**
|
||||
* @param Config $config
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
*/
|
||||
public function __construct(Config $config, Request $request, Response $response)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->request = $request;
|
||||
$this->response = $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise the controller, set up stores and services.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->buildStore = Factory::getStore('Build');
|
||||
$this->projectStore = Factory::getStore('Project');
|
||||
$this->buildService = new BuildService($this->buildStore);
|
||||
}
|
||||
|
||||
/** Handle the action, Ensuring to return a JsonResponse.
|
||||
*
|
||||
* @param string $action
|
||||
* @param mixed $actionParams
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function handleAction($action, $actionParams)
|
||||
{
|
||||
$response = new Response\JsonResponse();
|
||||
try {
|
||||
$data = call_user_func_array([$this, $action], $actionParams);
|
||||
if (isset($data['responseCode'])) {
|
||||
$response->setResponseCode($data['responseCode']);
|
||||
unset($data['responseCode']);
|
||||
}
|
||||
$response->setContent($data);
|
||||
} catch (Exception $ex) {
|
||||
$response->setResponseCode(500);
|
||||
$response->setContent(['status' => 'failed', 'error' => $ex->getMessage()]);
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by Bitbucket.
|
||||
*/
|
||||
public function bitbucket($projectId)
|
||||
{
|
||||
$project = $this->fetchProject($projectId, ['bitbucket', 'bitbucket-hg', 'hg', 'git']);
|
||||
|
||||
// Support both old services and new webhooks
|
||||
if ($payload = $this->getParam('payload')) {
|
||||
return $this->bitbucketService(json_decode($payload, true), $project);
|
||||
}
|
||||
|
||||
$payload = json_decode(file_get_contents("php://input"), true);
|
||||
|
||||
// Handle Pull Request webhooks:
|
||||
if (!empty($payload['pullrequest'])) {
|
||||
return $this->bitbucketPullRequest($project, $payload);
|
||||
}
|
||||
|
||||
// Handle Push (and Tag) webhooks:
|
||||
if (!empty($payload['push']['changes'])) {
|
||||
return $this->bitbucketCommitRequest($project, $payload);
|
||||
}
|
||||
|
||||
// Invalid event from bitbucket
|
||||
return [
|
||||
'status' => 'failed',
|
||||
'commits' => []
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the payload when Bitbucket sends a commit webhook.
|
||||
*
|
||||
* @param Project $project
|
||||
* @param array $payload
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function bitbucketCommitRequest(Project $project, array $payload)
|
||||
{
|
||||
$results = [];
|
||||
$status = 'failed';
|
||||
foreach ($payload['push']['changes'] as $commit) {
|
||||
try {
|
||||
$email = $commit['new']['target']['author']['raw'];
|
||||
if (strpos($email, '>') !== false) {
|
||||
// In order not to loose email if it is RAW, w/o "<>" symbols
|
||||
$email = substr($email, 0, strpos($email, '>'));
|
||||
$email = substr($email, strpos($email, '<') + 1);
|
||||
}
|
||||
|
||||
$results[$commit['new']['target']['hash']] = $this->createBuild(
|
||||
Build::SOURCE_WEBHOOK,
|
||||
$project,
|
||||
$commit['new']['target']['hash'],
|
||||
$commit['new']['name'],
|
||||
null,
|
||||
$email,
|
||||
$commit['new']['target']['message']
|
||||
);
|
||||
$status = 'ok';
|
||||
} catch (Exception $ex) {
|
||||
$results[$commit['new']['target']['hash']] = ['status' => 'failed', 'error' => $ex->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
return ['status' => $status, 'commits' => $results];
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the payload when Bitbucket sends a Pull Request webhook.
|
||||
*
|
||||
* @param Project $project
|
||||
* @param array $payload
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function bitbucketPullRequest(Project $project, array $payload)
|
||||
{
|
||||
// We only want to know about open pull requests:
|
||||
if (!in_array($_SERVER['HTTP_X_EVENT_KEY'], ['pullrequest:created', 'pullrequest:updated'])) {
|
||||
return ['status' => 'ok'];
|
||||
}
|
||||
|
||||
$username = Config::getInstance()->get('php-censor.bitbucket.username');
|
||||
$appPassword = Config::getInstance()->get('php-censor.bitbucket.app_password');
|
||||
|
||||
if (empty($username) || empty($appPassword)) {
|
||||
throw new Exception('Please provide Username and App Password of your Bitbucket account.');
|
||||
}
|
||||
|
||||
$commitsUrl = $payload['pullrequest']['links']['commits']['href'];
|
||||
|
||||
$client = new Client();
|
||||
$commitsResponse = $client->get($commitsUrl, [
|
||||
'auth' => [$username, $appPassword],
|
||||
]);
|
||||
$httpStatus = (integer)$commitsResponse->getStatusCode();
|
||||
|
||||
// Check we got a success response:
|
||||
if ($httpStatus < 200 || $httpStatus >= 300) {
|
||||
throw new Exception('Could not get commits, failed API request.');
|
||||
}
|
||||
|
||||
$results = [];
|
||||
$status = 'failed';
|
||||
$commits = json_decode($commitsResponse->getBody(), true)['values'];
|
||||
foreach ($commits as $commit) {
|
||||
// Skip all but the current HEAD commit ID:
|
||||
$id = $commit['hash'];
|
||||
if (strpos($id, $payload['pullrequest']['source']['commit']['hash']) !== 0) {
|
||||
$results[$id] = ['status' => 'ignored', 'message' => 'not branch head'];
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$branch = $payload['pullrequest']['destination']['branch']['name'];
|
||||
$committer = $commit['author']['raw'];
|
||||
if (strpos($committer, '>') !== false) {
|
||||
// In order not to loose email if it is RAW, w/o "<>" symbols
|
||||
$committer = substr($committer, 0, strpos($committer, '>'));
|
||||
$committer = substr($committer, strpos($committer, '<') + 1);
|
||||
}
|
||||
$message = $commit['message'];
|
||||
|
||||
$extra = [
|
||||
'pull_request_number' => $payload['pullrequest']['id'],
|
||||
'remote_branch' => $payload['pullrequest']['source']['branch']['name'],
|
||||
'remote_reference' => $payload['pullrequest']['source']['repository']['full_name'],
|
||||
];
|
||||
|
||||
$results[$id] = $this->createBuild(
|
||||
Build::SOURCE_WEBHOOK_PULL_REQUEST,
|
||||
$project,
|
||||
$id,
|
||||
$branch,
|
||||
null,
|
||||
$committer,
|
||||
$message,
|
||||
$extra
|
||||
);
|
||||
$status = 'ok';
|
||||
} catch (Exception $ex) {
|
||||
$results[$id] = ['status' => 'failed', 'error' => $ex->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
return ['status' => $status, 'commits' => $results];
|
||||
}
|
||||
|
||||
/**
|
||||
* Bitbucket POST service.
|
||||
*/
|
||||
protected function bitbucketService($payload, $project)
|
||||
{
|
||||
$payload = json_decode($this->getParam('payload'), true);
|
||||
|
||||
$results = [];
|
||||
$status = 'failed';
|
||||
foreach ($payload['commits'] as $commit) {
|
||||
try {
|
||||
$email = $commit['raw_author'];
|
||||
$email = substr($email, 0, strpos($email, '>'));
|
||||
$email = substr($email, strpos($email, '<') + 1);
|
||||
|
||||
$results[$commit['raw_node']] = $this->createBuild(
|
||||
Build::SOURCE_WEBHOOK,
|
||||
$project,
|
||||
$commit['raw_node'],
|
||||
$commit['branch'],
|
||||
null,
|
||||
$email,
|
||||
$commit['message']
|
||||
);
|
||||
$status = 'ok';
|
||||
} catch (Exception $ex) {
|
||||
$results[$commit['raw_node']] = ['status' => 'failed', 'error' => $ex->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
return ['status' => $status, 'commits' => $results];
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by POSTing to /webhook/git/<project_id>?branch=<branch>&commit=<commit>
|
||||
*
|
||||
* @param string $projectId
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function git($projectId)
|
||||
{
|
||||
$project = $this->fetchProject($projectId, ['local', 'git']);
|
||||
$branch = $this->getParam('branch', $project->getBranch());
|
||||
$commit = $this->getParam('commit');
|
||||
$commitMessage = $this->getParam('message');
|
||||
$committer = $this->getParam('committer');
|
||||
|
||||
return $this->createBuild(
|
||||
Build::SOURCE_WEBHOOK,
|
||||
$project,
|
||||
$commit,
|
||||
$branch,
|
||||
null,
|
||||
$committer,
|
||||
$commitMessage
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by Github Webhooks:
|
||||
*/
|
||||
public function github($projectId)
|
||||
{
|
||||
$project = $this->fetchProject($projectId, ['github', 'git']);
|
||||
|
||||
switch ($_SERVER['CONTENT_TYPE']) {
|
||||
case 'application/json':
|
||||
$payload = json_decode(file_get_contents('php://input'), true);
|
||||
break;
|
||||
case 'application/x-www-form-urlencoded':
|
||||
$payload = json_decode($this->getParam('payload'), true);
|
||||
break;
|
||||
default:
|
||||
return [
|
||||
'status' => 'failed',
|
||||
'error' => 'Content type not supported.',
|
||||
'responseCode' => 401
|
||||
];
|
||||
}
|
||||
|
||||
// Handle Pull Request webhooks:
|
||||
if (array_key_exists('pull_request', $payload)) {
|
||||
return $this->githubPullRequest($project, $payload);
|
||||
}
|
||||
|
||||
// Handle Push (and Tag) webhooks:
|
||||
if (array_key_exists('head_commit', $payload)) {
|
||||
return $this->githubCommitRequest($project, $payload);
|
||||
}
|
||||
|
||||
return ['status' => 'ignored', 'message' => 'Unusable payload.'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the payload when Github sends a commit webhook.
|
||||
*
|
||||
* @param Project $project
|
||||
* @param array $payload
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function githubCommitRequest(Project $project, array $payload)
|
||||
{
|
||||
// Github sends a payload when you close a pull request with a non-existent commit. We don't want this.
|
||||
if (
|
||||
array_key_exists('after', $payload) &&
|
||||
$payload['after'] === '0000000000000000000000000000000000000000'
|
||||
) {
|
||||
return ['status' => 'ignored'];
|
||||
}
|
||||
|
||||
if (isset($payload['head_commit']) && $payload['head_commit']) {
|
||||
$isTag = (substr($payload['ref'], 0, 10) == 'refs/tags/') ? true : false;
|
||||
$commit = $payload['head_commit'];
|
||||
$results = [];
|
||||
$status = 'failed';
|
||||
|
||||
if (!$commit['distinct']) {
|
||||
$results[$commit['id']] = ['status' => 'ignored'];
|
||||
} else {
|
||||
try {
|
||||
$tag = null;
|
||||
if ($isTag) {
|
||||
$tag = str_replace('refs/tags/', '', $payload['ref']);
|
||||
$branch = str_replace('refs/heads/', '', $payload['base_ref']);
|
||||
$committer = $payload['pusher']['email'];
|
||||
} else {
|
||||
$branch = str_replace('refs/heads/', '', $payload['ref']);
|
||||
$committer = $commit['committer']['email'];
|
||||
}
|
||||
|
||||
$results[$commit['id']] = $this->createBuild(
|
||||
Build::SOURCE_WEBHOOK,
|
||||
$project,
|
||||
$commit['id'],
|
||||
$branch,
|
||||
$tag,
|
||||
$committer,
|
||||
$commit['message']
|
||||
);
|
||||
|
||||
$status = 'ok';
|
||||
} catch (Exception $ex) {
|
||||
$results[$commit['id']] = ['status' => 'failed', 'error' => $ex->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
return ['status' => $status, 'commits' => $results];
|
||||
}
|
||||
|
||||
return ['status' => 'ignored', 'message' => 'Unusable payload.'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the payload when Github sends a Pull Request webhook.
|
||||
*
|
||||
* @param Project $project
|
||||
* @param array $payload
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function githubPullRequest(Project $project, array $payload)
|
||||
{
|
||||
// We only want to know about open pull requests:
|
||||
if (!in_array($payload['action'], ['opened', 'synchronize', 'reopened'])) {
|
||||
return ['status' => 'ok'];
|
||||
}
|
||||
|
||||
$headers = [];
|
||||
$token = Config::getInstance()->get('php-censor.github.token');
|
||||
|
||||
if (!empty($token)) {
|
||||
$headers['Authorization'] = 'token ' . $token;
|
||||
}
|
||||
|
||||
$url = $payload['pull_request']['commits_url'];
|
||||
|
||||
//for large pull requests, allow grabbing more then the default number of commits
|
||||
$custom_per_page = Config::getInstance()->get('php-censor.github.per_page');
|
||||
$params = [];
|
||||
if ($custom_per_page) {
|
||||
$params['per_page'] = $custom_per_page;
|
||||
}
|
||||
|
||||
$client = new Client();
|
||||
$response = $client->get($url, [
|
||||
'headers' => $headers,
|
||||
'query' => $params,
|
||||
]);
|
||||
$status = (integer)$response->getStatusCode();
|
||||
|
||||
// Check we got a success response:
|
||||
if ($status < 200 || $status >= 300) {
|
||||
throw new Exception('Could not get commits, failed API request.');
|
||||
}
|
||||
|
||||
$results = [];
|
||||
$status = 'failed';
|
||||
$commits = json_decode($response->getBody(), true);
|
||||
foreach ($commits as $commit) {
|
||||
// Skip all but the current HEAD commit ID:
|
||||
$id = $commit['sha'];
|
||||
if ($id !== $payload['pull_request']['head']['sha']) {
|
||||
$results[$id] = ['status' => 'ignored', 'message' => 'not branch head'];
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$branch = str_replace('refs/heads/', '', $payload['pull_request']['base']['ref']);
|
||||
$committer = $commit['commit']['author']['email'];
|
||||
$message = $commit['commit']['message'];
|
||||
|
||||
$extra = [
|
||||
'pull_request_number' => $payload['number'],
|
||||
'remote_branch' => $payload['pull_request']['head']['ref'],
|
||||
'remote_reference' => $payload['pull_request']['head']['repo']['full_name'],
|
||||
];
|
||||
|
||||
$results[$id] = $this->createBuild(
|
||||
Build::SOURCE_WEBHOOK_PULL_REQUEST,
|
||||
$project,
|
||||
$id,
|
||||
$branch,
|
||||
null,
|
||||
$committer,
|
||||
$message,
|
||||
$extra
|
||||
);
|
||||
$status = 'ok';
|
||||
} catch (Exception $ex) {
|
||||
$results[$id] = ['status' => 'failed', 'error' => $ex->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
return ['status' => $status, 'commits' => $results];
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by Gitlab Webhooks:
|
||||
*/
|
||||
public function gitlab($projectId)
|
||||
{
|
||||
$project = $this->fetchProject($projectId, ['gitlab', 'git']);
|
||||
|
||||
$payloadString = file_get_contents("php://input");
|
||||
$payload = json_decode($payloadString, true);
|
||||
|
||||
// build on merge request events
|
||||
if (isset($payload['object_kind']) && $payload['object_kind'] == 'merge_request') {
|
||||
$attributes = $payload['object_attributes'];
|
||||
if ($attributes['state'] == 'opened' || $attributes['state'] == 'reopened') {
|
||||
$branch = $attributes['source_branch'];
|
||||
$commit = $attributes['last_commit'];
|
||||
$committer = $commit['author']['email'];
|
||||
|
||||
return $this->createBuild(
|
||||
Build::SOURCE_WEBHOOK_PULL_REQUEST,
|
||||
$project,
|
||||
$commit['id'],
|
||||
$branch,
|
||||
null,
|
||||
$committer,
|
||||
$commit['message']
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// build on push events
|
||||
if (isset($payload['commits']) && is_array($payload['commits'])) {
|
||||
// If we have a list of commits, then add them all as builds to be tested:
|
||||
|
||||
$results = [];
|
||||
$status = 'failed';
|
||||
foreach ($payload['commits'] as $commit) {
|
||||
try {
|
||||
$branch = str_replace('refs/heads/', '', $payload['ref']);
|
||||
$committer = $commit['author']['email'];
|
||||
$results[$commit['id']] = $this->createBuild(
|
||||
Build::SOURCE_WEBHOOK,
|
||||
$project,
|
||||
$commit['id'],
|
||||
$branch,
|
||||
null,
|
||||
$committer,
|
||||
$commit['message']
|
||||
);
|
||||
$status = 'ok';
|
||||
} catch (Exception $ex) {
|
||||
$results[$commit['id']] = ['status' => 'failed', 'error' => $ex->getMessage()];
|
||||
}
|
||||
}
|
||||
return ['status' => $status, 'commits' => $results];
|
||||
}
|
||||
|
||||
return ['status' => 'ignored', 'message' => 'Unusable payload.'];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Called by POSTing to /webhook/svn/<project_id>?branch=<branch>&commit=<commit>
|
||||
*
|
||||
* @author Sylvain Lévesque <slevesque@gezere.com>
|
||||
*
|
||||
* @param string $projectId
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function svn($projectId)
|
||||
{
|
||||
$project = $this->fetchProject($projectId, 'svn');
|
||||
$branch = $this->getParam('branch', $project->getBranch());
|
||||
$commit = $this->getParam('commit');
|
||||
$commitMessage = $this->getParam('message');
|
||||
$committer = $this->getParam('committer');
|
||||
|
||||
return $this->createBuild(
|
||||
Build::SOURCE_WEBHOOK,
|
||||
$project,
|
||||
$commit,
|
||||
$branch,
|
||||
null,
|
||||
$committer,
|
||||
$commitMessage
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by Gogs Webhooks:
|
||||
*
|
||||
* @param string $projectId
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function gogs($projectId)
|
||||
{
|
||||
$project = $this->fetchProject($projectId, ['gogs', 'git']);
|
||||
switch ($_SERVER['CONTENT_TYPE']) {
|
||||
case 'application/json':
|
||||
$payload = json_decode(file_get_contents('php://input'), true);
|
||||
break;
|
||||
case 'application/x-www-form-urlencoded':
|
||||
$payload = json_decode($this->getParam('payload'), true);
|
||||
break;
|
||||
default:
|
||||
return ['status' => 'failed', 'error' => 'Content type not supported.', 'responseCode' => 401];
|
||||
}
|
||||
|
||||
// Handle Push web hooks:
|
||||
if (array_key_exists('commits', $payload)) {
|
||||
return $this->gogsCommitRequest($project, $payload);
|
||||
}
|
||||
|
||||
if (array_key_exists('pull_request', $payload)) {
|
||||
return $this->gogsPullRequest($project, $payload);
|
||||
}
|
||||
|
||||
return ['status' => 'ignored', 'message' => 'Unusable payload.'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the payload when Gogs sends a commit webhook.
|
||||
*
|
||||
* @param Project $project
|
||||
* @param array $payload
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function gogsCommitRequest(Project $project, array $payload)
|
||||
{
|
||||
if (isset($payload['commits']) && is_array($payload['commits'])) {
|
||||
// If we have a list of commits, then add them all as builds to be tested:
|
||||
$results = [];
|
||||
$status = 'failed';
|
||||
foreach ($payload['commits'] as $commit) {
|
||||
try {
|
||||
$branch = str_replace('refs/heads/', '', $payload['ref']);
|
||||
$committer = $commit['author']['email'];
|
||||
$results[$commit['id']] = $this->createBuild(
|
||||
Build::SOURCE_WEBHOOK,
|
||||
$project,
|
||||
$commit['id'],
|
||||
$branch,
|
||||
null,
|
||||
$committer,
|
||||
$commit['message']
|
||||
);
|
||||
$status = 'ok';
|
||||
} catch (Exception $ex) {
|
||||
$results[$commit['id']] = ['status' => 'failed', 'error' => $ex->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
return ['status' => $status, 'commits' => $results];
|
||||
}
|
||||
|
||||
return ['status' => 'ignored', 'message' => 'Unusable payload.'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the payload when Gogs sends a pull request webhook.
|
||||
*
|
||||
* @param Project $project
|
||||
* @param array $payload
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function gogsPullRequest(Project $project, array $payload)
|
||||
{
|
||||
$pullRequest = $payload['pull_request'];
|
||||
$headBranch = $pullRequest['head_branch'];
|
||||
|
||||
$action = $payload['action'];
|
||||
$activeActions = ['opened', 'reopened', 'label_updated', 'label_cleared'];
|
||||
$inactiveActions = ['closed'];
|
||||
|
||||
$state = $pullRequest['state'];
|
||||
$activeStates = ['open'];
|
||||
$inactiveStates = ['closed'];
|
||||
|
||||
if (!in_array($action, $activeActions) and !in_array($action, $inactiveActions)) {
|
||||
return ['status' => 'ignored', 'message' => 'Action ' . $action . ' ignored'];
|
||||
}
|
||||
if (!in_array($state, $activeStates) and !in_array($state, $inactiveStates)) {
|
||||
return ['status' => 'ignored', 'message' => 'State ' . $state . ' ignored'];
|
||||
}
|
||||
|
||||
$envs = [];
|
||||
|
||||
// Get environment form labels
|
||||
if (in_array($action, $activeActions) and in_array($state, $activeStates)) {
|
||||
if (isset($pullRequest['labels']) && is_array($pullRequest['labels'])) {
|
||||
foreach ($pullRequest['labels'] as $label) {
|
||||
if (strpos($label['name'], 'env:') === 0) {
|
||||
$envs[] = substr($label['name'], 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$envsUpdated = [];
|
||||
$envObjects = $project->getEnvironmentsObjects();
|
||||
$store = Factory::getStore('Environment');
|
||||
foreach ($envObjects['items'] as $environment) {
|
||||
$branches = $environment->getBranches();
|
||||
if (in_array($environment->getName(), $envs)) {
|
||||
if (!in_array($headBranch, $branches)) {
|
||||
// Add branch to environment
|
||||
$branches[] = $headBranch;
|
||||
$environment->setBranches($branches);
|
||||
$store->save($environment);
|
||||
$envsUpdated[] = $environment->getName();
|
||||
}
|
||||
} else {
|
||||
if (in_array($headBranch, $branches)) {
|
||||
// Remove branch from environment
|
||||
$branches = array_diff($branches, [$headBranch]);
|
||||
$environment->setBranches($branches);
|
||||
$store->save($environment);
|
||||
$envsUpdated[] = $environment->getName();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (($state == 'closed') and $pullRequest['merged']) {
|
||||
// update base branch environments
|
||||
$environmentNames = $project->getEnvironmentsNamesByBranch($pullRequest['base_branch']);
|
||||
$envsUpdated = array_merge($envsUpdated, $environmentNames);
|
||||
}
|
||||
|
||||
$envsUpdated = array_unique($envsUpdated);
|
||||
if (!empty($envsUpdated)) {
|
||||
foreach ($envsUpdated as $environmentName) {
|
||||
$this->buildService->createBuild(
|
||||
$project,
|
||||
$environmentName,
|
||||
'',
|
||||
$project->getBranch(),
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
Build::SOURCE_WEBHOOK,
|
||||
0,
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
return ['status' => 'ok', 'message' => 'Branch environments updated ' . join(', ', $envsUpdated)];
|
||||
}
|
||||
|
||||
return ['status' => 'ignored', 'message' => 'Branch environments not changed'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for creating a new build.
|
||||
*
|
||||
* @param integer $source
|
||||
* @param Project $project
|
||||
* @param string $commitId
|
||||
* @param string $branch
|
||||
* @param string $tag
|
||||
* @param string $committer
|
||||
* @param string $commitMessage
|
||||
* @param array $extra
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function createBuild(
|
||||
$source,
|
||||
Project $project,
|
||||
$commitId,
|
||||
$branch,
|
||||
$tag,
|
||||
$committer,
|
||||
$commitMessage,
|
||||
array $extra = null
|
||||
) {
|
||||
if ($project->getArchived()) {
|
||||
throw new NotFoundException(Lang::get('project_x_not_found', $project->getId()));
|
||||
}
|
||||
|
||||
// Check if a build already exists for this commit ID:
|
||||
$builds = $this->buildStore->getByProjectAndCommit($project->getId(), $commitId);
|
||||
|
||||
$ignore_environments = [];
|
||||
$ignore_tags = [];
|
||||
if ($builds['count']) {
|
||||
foreach($builds['items'] as $build) {
|
||||
/** @var Build $build */
|
||||
$ignore_environments[$build->getId()] = $build->getEnvironment();
|
||||
$ignore_tags[$build->getId()] = $build->getTag();
|
||||
}
|
||||
}
|
||||
|
||||
// Check if this branch is to be built.
|
||||
if ($project->getDefaultBranchOnly() && ($branch !== $project->getBranch())) {
|
||||
return [
|
||||
'status' => 'ignored',
|
||||
'message' => 'The branch is not a branch by default. Build is allowed only for the branch by default.'
|
||||
];
|
||||
}
|
||||
|
||||
$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) ||
|
||||
($tag && !in_array($tag, $ignore_tags, true))
|
||||
) {
|
||||
// If not, create a new build job for it:
|
||||
$build = $this->buildService->createBuild(
|
||||
$project,
|
||||
$environment_name,
|
||||
$commitId,
|
||||
$project->getBranch(),
|
||||
$tag,
|
||||
$committer,
|
||||
$commitMessage,
|
||||
(integer)$source,
|
||||
0,
|
||||
$extra
|
||||
);
|
||||
|
||||
$created_builds[] = [
|
||||
'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, true) ||
|
||||
($tag && !in_array($tag, $ignore_tags, true))
|
||||
) {
|
||||
$build = $this->buildService->createBuild(
|
||||
$project,
|
||||
null,
|
||||
$commitId,
|
||||
$branch,
|
||||
$tag,
|
||||
$committer,
|
||||
$commitMessage,
|
||||
(integer)$source,
|
||||
0,
|
||||
$extra
|
||||
);
|
||||
|
||||
return ['status' => 'ok', 'buildID' => $build->getID()];
|
||||
} else {
|
||||
return [
|
||||
'status' => 'ignored',
|
||||
'message' => sprintf('Duplicate of build #%d', array_search($environment_name, $ignore_environments)),
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a project and check its type.
|
||||
*
|
||||
* @param int|string $projectId id or title of project
|
||||
* @param array|string $expectedType
|
||||
*
|
||||
* @return Project
|
||||
*
|
||||
* @throws Exception If the project does not exist or is not of the expected type.
|
||||
*/
|
||||
protected function fetchProject($projectId, $expectedType)
|
||||
{
|
||||
if (empty($projectId)) {
|
||||
throw new Exception('Project does not exist: ' . $projectId);
|
||||
}
|
||||
|
||||
if (is_numeric($projectId)) {
|
||||
$project = $this->projectStore->getById((integer)$projectId);
|
||||
} else {
|
||||
$projects = $this->projectStore->getByTitle($projectId, 2);
|
||||
if ($projects['count'] < 1) {
|
||||
throw new Exception('Project does not found: ' . $projectId);
|
||||
}
|
||||
if ($projects['count'] > 1) {
|
||||
throw new Exception('Project id is ambiguous: ' . $projectId);
|
||||
}
|
||||
$project = reset($projects['items']);
|
||||
}
|
||||
|
||||
if (is_array($expectedType)
|
||||
? !in_array($project->getType(), $expectedType)
|
||||
: $project->getType() !== $expectedType
|
||||
) {
|
||||
throw new Exception('Wrong project type: ' . $project->getType());
|
||||
}
|
||||
|
||||
return $project;
|
||||
}
|
||||
}
|
||||
150
src/Controller/WidgetAllProjectsController.php
Normal file
150
src/Controller/WidgetAllProjectsController.php
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use PHPCensor\Model\Build;
|
||||
use PHPCensor\Controller;
|
||||
use PHPCensor\Store\Factory;
|
||||
use PHPCensor\View;
|
||||
use PHPCensor\Model\Project;
|
||||
use PHPCensor\Http\Response;
|
||||
use PHPCensor\Store\BuildStore;
|
||||
use PHPCensor\Store\ProjectStore;
|
||||
use PHPCensor\Store\ProjectGroupStore;
|
||||
|
||||
/**
|
||||
* Widget All Projects Controller
|
||||
*/
|
||||
class WidgetAllProjectsController extends Controller
|
||||
{
|
||||
/**
|
||||
* @var BuildStore
|
||||
*/
|
||||
protected $buildStore;
|
||||
|
||||
/**
|
||||
* @var ProjectStore
|
||||
*/
|
||||
protected $projectStore;
|
||||
|
||||
/**
|
||||
* @var ProjectGroupStore
|
||||
*/
|
||||
protected $groupStore;
|
||||
|
||||
/**
|
||||
* Initialise the controller, set up stores and services.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->buildStore = Factory::getStore('Build');
|
||||
$this->projectStore = Factory::getStore('Project');
|
||||
$this->groupStore = Factory::getStore('ProjectGroup');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display dashboard.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$this->view->groups = $this->getGroupInfo();
|
||||
|
||||
$this->response->disableLayout();
|
||||
$this->response->setContent($this->view->render());
|
||||
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the HTML for the project overview section of the dashboard.
|
||||
*
|
||||
* @param Project[] $projects
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getSummaryHtml($projects)
|
||||
{
|
||||
$summaryBuilds = [];
|
||||
$successes = [];
|
||||
$failures = [];
|
||||
$counts = [];
|
||||
|
||||
foreach ($projects as $project) {
|
||||
$summaryBuilds[$project->getId()] = $this->buildStore->getLatestBuilds($project->getId());
|
||||
|
||||
$count = $this->buildStore->getWhere(
|
||||
['project_id' => $project->getId()],
|
||||
1,
|
||||
0,
|
||||
['id' => 'DESC']
|
||||
);
|
||||
$counts[$project->getId()] = $count['count'];
|
||||
|
||||
$success = $this->buildStore->getLastBuildByStatus($project->getId(), Build::STATUS_SUCCESS);
|
||||
$failure = $this->buildStore->getLastBuildByStatus($project->getId(), Build::STATUS_FAILED);
|
||||
|
||||
$successes[$project->getId()] = $success;
|
||||
$failures[$project->getId()] = $failure;
|
||||
}
|
||||
|
||||
$view = new View('WidgetAllProjects/index-projects');
|
||||
|
||||
$view->projects = $projects;
|
||||
$view->builds = $summaryBuilds;
|
||||
$view->successful = $successes;
|
||||
$view->failed = $failures;
|
||||
$view->counts = $counts;
|
||||
|
||||
return $view->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a summary of the project groups we have, and what projects they have in them.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getGroupInfo()
|
||||
{
|
||||
$rtn = [];
|
||||
$groups = $this->groupStore->getWhere([], 100, 0, ['title' => 'ASC']);
|
||||
|
||||
foreach ($groups['items'] as $group) {
|
||||
$thisGroup = ['title' => $group->getTitle()];
|
||||
$projects = $this->projectStore->getByGroupId($group->getId(), false);
|
||||
|
||||
$thisGroup['projects'] = $projects['items'];
|
||||
$thisGroup['summary'] = $this->getSummaryHtml($thisGroup['projects']);
|
||||
|
||||
$rtn[] = $thisGroup;
|
||||
}
|
||||
|
||||
return $rtn;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param integer $projectId
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function update($projectId)
|
||||
{
|
||||
$count = $this->buildStore->getWhere(
|
||||
['project_id' => $projectId],
|
||||
1,
|
||||
0,
|
||||
['id' => 'DESC']
|
||||
);
|
||||
$counts = $count['count'];
|
||||
|
||||
$this->view->project = $this->projectStore->getById($projectId);
|
||||
$this->view->builds = $this->buildStore->getLatestBuilds($projectId);
|
||||
$this->view->successful = $this->buildStore->getLastBuildByStatus($projectId, Build::STATUS_SUCCESS);
|
||||
$this->view->failed = $this->buildStore->getLastBuildByStatus($projectId, Build::STATUS_FAILED);
|
||||
$this->view->counts = $counts;
|
||||
|
||||
$this->response->disableLayout();
|
||||
$this->response->setContent($this->view->render());
|
||||
|
||||
return $this->response;
|
||||
}
|
||||
}
|
||||
90
src/Controller/WidgetBuildErrorsController.php
Normal file
90
src/Controller/WidgetBuildErrorsController.php
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use PHPCensor\Store\Factory;
|
||||
use PHPCensor\View;
|
||||
use PHPCensor\Http\Response;
|
||||
use PHPCensor\Controller;
|
||||
use PHPCensor\Store\BuildStore;
|
||||
use PHPCensor\Store\ProjectStore;
|
||||
|
||||
/**
|
||||
* Widget Build Errors Controller
|
||||
*/
|
||||
class WidgetBuildErrorsController extends Controller
|
||||
{
|
||||
/**
|
||||
* @var BuildStore
|
||||
*/
|
||||
protected $buildStore;
|
||||
|
||||
/**
|
||||
* @var ProjectStore
|
||||
*/
|
||||
protected $projectStore;
|
||||
|
||||
/**
|
||||
* Initialise the controller, set up stores and services.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->buildStore = Factory::getStore('Build');
|
||||
$this->projectStore = Factory::getStore('Project');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display dashboard.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$view = new View('WidgetBuildErrors/update');
|
||||
|
||||
$this->view->projects = $this->renderAllProjectsLatestBuilds($view);
|
||||
|
||||
$this->response->disableLayout();
|
||||
$this->response->setContent($this->view->render());
|
||||
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Response
|
||||
*/
|
||||
public function update()
|
||||
{
|
||||
$this->response->disableLayout();
|
||||
$this->response->setContent($this->renderAllProjectsLatestBuilds($this->view));
|
||||
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param View $view
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function renderAllProjectsLatestBuilds($view)
|
||||
{
|
||||
$builds = $this->buildStore->getAllProjectsLatestBuilds();
|
||||
|
||||
if (!empty($builds['projects'])) {
|
||||
$view->builds = $builds['projects'];
|
||||
$projects = $this->projectStore->getByIds(array_keys($builds['projects']));
|
||||
|
||||
$view_projects = [];
|
||||
foreach($projects as $id => $project) {
|
||||
if (!$project->getArchived()) {
|
||||
$view_projects[$id] = $project;
|
||||
} else {
|
||||
unset($builds['projects'][$id]);
|
||||
}
|
||||
}
|
||||
$view->projects = $view_projects;
|
||||
} else {
|
||||
$view = new View('WidgetBuildErrors/empty');
|
||||
}
|
||||
|
||||
return $view->render();
|
||||
}
|
||||
}
|
||||
70
src/Controller/WidgetLastBuildsController.php
Normal file
70
src/Controller/WidgetLastBuildsController.php
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use PHPCensor\Store\Factory;
|
||||
use PHPCensor\View;
|
||||
use PHPCensor\Http\Response;
|
||||
use PHPCensor\BuildFactory;
|
||||
use PHPCensor\Controller;
|
||||
use PHPCensor\Store\BuildStore;
|
||||
|
||||
/**
|
||||
* Widget Last Builds Controller
|
||||
*/
|
||||
class WidgetLastBuildsController extends Controller
|
||||
{
|
||||
/**
|
||||
* @var BuildStore
|
||||
*/
|
||||
protected $buildStore;
|
||||
|
||||
/**
|
||||
* Initialise the controller, set up stores and services.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->buildStore = Factory::getStore('Build');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display dashboard.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$builds = $this->buildStore->getLatestBuilds(null, 10);
|
||||
|
||||
foreach ($builds as &$build) {
|
||||
$build = BuildFactory::getBuild($build);
|
||||
}
|
||||
|
||||
$view = new View('WidgetLastBuilds/update');
|
||||
|
||||
$view->builds = $builds;
|
||||
$this->view->timeline = $view->render();
|
||||
|
||||
$this->response->disableLayout();
|
||||
$this->response->setContent($this->view->render());
|
||||
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Response
|
||||
*/
|
||||
public function update()
|
||||
{
|
||||
$builds = $this->buildStore->getLatestBuilds(null, 10);
|
||||
|
||||
foreach ($builds as &$build) {
|
||||
$build = BuildFactory::getBuild($build);
|
||||
}
|
||||
|
||||
$this->view->builds = $builds;
|
||||
|
||||
$this->response->disableLayout();
|
||||
$this->response->setContent($this->view->render());
|
||||
|
||||
return $this->response;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue