Fixed namespaces (PHPCI -> PHPCensor)
This commit is contained in:
parent
60d74b0b44
commit
60a2b7282a
238 changed files with 1014 additions and 863 deletions
280
src/PHPCensor/Controller/BuildController.php
Normal file
280
src/PHPCensor/Controller/BuildController.php
Normal file
|
|
@ -0,0 +1,280 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPCI - Continuous Integration for PHP
|
||||
*
|
||||
* @copyright Copyright 2014, Block 8 Limited.
|
||||
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
||||
* @link https://www.phptesting.org/
|
||||
*/
|
||||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use b8;
|
||||
use b8\Exception\HttpException\NotFoundException;
|
||||
use b8\Http\Response\JsonResponse;
|
||||
use PHPCensor\BuildFactory;
|
||||
use PHPCensor\Helper\AnsiConverter;
|
||||
use PHPCensor\Helper\Lang;
|
||||
use PHPCensor\Model\Build;
|
||||
use PHPCensor\Model\Project;
|
||||
use PHPCensor\Service\BuildService;
|
||||
use PHPCensor\Controller;
|
||||
|
||||
/**
|
||||
* Build Controller - Allows users to run and view builds.
|
||||
* @author Dan Cryer <dan@block8.co.uk>
|
||||
* @package PHPCI
|
||||
* @subpackage Web
|
||||
*/
|
||||
class BuildController extends Controller
|
||||
{
|
||||
/**
|
||||
* @var \PHPCI\Store\BuildStore
|
||||
*/
|
||||
protected $buildStore;
|
||||
|
||||
/**
|
||||
* @var \PHPCI\Service\BuildService
|
||||
*/
|
||||
protected $buildService;
|
||||
|
||||
/**
|
||||
* Initialise the controller, set up stores and services.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->buildStore = b8\Store\Factory::getStore('Build');
|
||||
$this->buildService = new BuildService($this->buildStore);
|
||||
}
|
||||
|
||||
/**
|
||||
* View a specific build.
|
||||
*/
|
||||
public function view($buildId)
|
||||
{
|
||||
try {
|
||||
$build = BuildFactory::getBuildById($buildId);
|
||||
} catch (\Exception $ex) {
|
||||
$build = null;
|
||||
}
|
||||
|
||||
if (empty($build)) {
|
||||
throw new NotFoundException(Lang::get('build_x_not_found', $buildId));
|
||||
}
|
||||
|
||||
$this->view->plugins = $this->getUiPlugins();
|
||||
$this->view->build = $build;
|
||||
$this->view->data = $this->getBuildData($build);
|
||||
|
||||
$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 = PHPCI_URL . 'build/rebuild/' . $build->getId();
|
||||
|
||||
$delete = Lang::get('delete_build');
|
||||
$deleteLink = PHPCI_URL . 'build/delete/' . $build->getId();
|
||||
|
||||
$actions = "<a class=\"btn btn-default\" href=\"{$rebuildLink}\">{$rebuild}</a> ";
|
||||
|
||||
if ($this->currentUserIsAdmin()) {
|
||||
$actions .= " <a class=\"btn btn-danger\" href=\"{$deleteLink}\">{$delete}</a>";
|
||||
}
|
||||
|
||||
$this->layout->actions = $actions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of the JS plugins to include.
|
||||
* @return array
|
||||
*/
|
||||
protected function getUiPlugins()
|
||||
{
|
||||
$rtn = [];
|
||||
$path = PHPCI_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;
|
||||
}
|
||||
|
||||
/**
|
||||
* AJAX call to get build data:
|
||||
*/
|
||||
public function data($buildId)
|
||||
{
|
||||
$response = new JsonResponse();
|
||||
$build = BuildFactory::getBuildById($buildId);
|
||||
|
||||
if (!$build) {
|
||||
$response->setResponseCode(404);
|
||||
$response->setContent([]);
|
||||
return $response;
|
||||
}
|
||||
|
||||
$response->setContent($this->getBuildData($build));
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* AJAX call to get build meta:
|
||||
*/
|
||||
public function meta($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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get build data from database and json encode it:
|
||||
*/
|
||||
protected function getBuildData(Build $build)
|
||||
{
|
||||
$data = [];
|
||||
$data['status'] = (int)$build->getStatus();
|
||||
$data['log'] = $this->cleanLog($build->getLog());
|
||||
$data['created'] = !is_null($build->getCreated()) ? $build->getCreated()->format('Y-m-d H:i:s') : null;
|
||||
$data['started'] = !is_null($build->getStarted()) ? $build->getStarted()->format('Y-m-d H:i:s') : null;
|
||||
$data['finished'] = !is_null($build->getFinished()) ? $build->getFinished()->format('Y-m-d H:i:s') : null;
|
||||
$data['duration'] = $build->getDuration();
|
||||
|
||||
/** @var \PHPCI\Store\BuildErrorStore $errorStore */
|
||||
$errorStore = b8\Store\Factory::getStore('BuildError');
|
||||
$errors = $errorStore->getErrorsForBuild($build->getId());
|
||||
|
||||
$errorView = new b8\View('Build/errors');
|
||||
$errorView->build = $build;
|
||||
$errorView->errors = $errors;
|
||||
|
||||
$data['errors'] = $errorStore->getErrorTotalForBuild($build->getId());
|
||||
$data['error_html'] = $errorView->render();
|
||||
$data['since'] = (new \DateTime())->format('Y-m-d H:i:s');
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a build using an existing build as a template:
|
||||
*/
|
||||
public function rebuild($buildId)
|
||||
{
|
||||
$copy = BuildFactory::getBuildById($buildId);
|
||||
|
||||
if (empty($copy)) {
|
||||
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 b8\Http\Response\RedirectResponse();
|
||||
$response->setHeader('Location', PHPCI_URL.'build/view/' . $build->getId());
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a build.
|
||||
*/
|
||||
public function delete($buildId)
|
||||
{
|
||||
$this->requireAdmin();
|
||||
|
||||
$build = BuildFactory::getBuildById($buildId);
|
||||
|
||||
if (empty($build)) {
|
||||
throw new NotFoundException(Lang::get('build_x_not_found', $buildId));
|
||||
}
|
||||
|
||||
$this->buildService->deleteBuild($build);
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response->setHeader('Location', PHPCI_URL.'project/view/' . $build->getProjectId());
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse log for unix colours and replace with HTML.
|
||||
*/
|
||||
protected function cleanLog($log)
|
||||
{
|
||||
return AnsiConverter::convert($log);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows the UI to poll for the latest running and pending builds.
|
||||
*/
|
||||
public function latest()
|
||||
{
|
||||
$rtn = [
|
||||
'pending' => $this->formatBuilds($this->buildStore->getByStatus(Build::STATUS_NEW)),
|
||||
'running' => $this->formatBuilds($this->buildStore->getByStatus(Build::STATUS_RUNNING)),
|
||||
];
|
||||
|
||||
$response = new JsonResponse();
|
||||
$response->setContent($rtn);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a list of builds into rows suitable for the dropdowns in the PHPCI header bar.
|
||||
* @param $builds
|
||||
* @return array
|
||||
*/
|
||||
protected function formatBuilds($builds)
|
||||
{
|
||||
Project::$sleepable = ['id', 'title', 'reference', 'type'];
|
||||
|
||||
$rtn = ['count' => $builds['count'], 'items' => []];
|
||||
|
||||
foreach ($builds['items'] as $build) {
|
||||
$item = $build->toArray(1);
|
||||
|
||||
$header = new b8\View('Build/header-row');
|
||||
$header->build = $build;
|
||||
|
||||
$item['header_row'] = $header->render();
|
||||
$rtn['items'][$item['id']] = $item;
|
||||
}
|
||||
|
||||
ksort($rtn['items']);
|
||||
return $rtn;
|
||||
}
|
||||
}
|
||||
205
src/PHPCensor/Controller/BuildStatusController.php
Normal file
205
src/PHPCensor/Controller/BuildStatusController.php
Normal file
|
|
@ -0,0 +1,205 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPCI - Continuous Integration for PHP
|
||||
*
|
||||
* @copyright Copyright 2014, Block 8 Limited.
|
||||
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
||||
* @link https://www.phptesting.org/
|
||||
*/
|
||||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use b8;
|
||||
use b8\Exception\HttpException\NotFoundException;
|
||||
use b8\Store;
|
||||
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>
|
||||
* @package PHPCI
|
||||
* @subpackage Web
|
||||
*/
|
||||
class BuildStatusController extends Controller
|
||||
{
|
||||
/* @var \PHPCI\Store\ProjectStore */
|
||||
protected $projectStore;
|
||||
/* @var \PHPCI\Store\BuildStore */
|
||||
protected $buildStore;
|
||||
|
||||
/**
|
||||
* Initialise the controller, set up stores and services.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->response->disableLayout();
|
||||
$this->buildStore = Store\Factory::getStore('Build');
|
||||
$this->projectStore = Store\Factory::getStore('Project');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns status of the last build
|
||||
* @param $projectId
|
||||
* @return string
|
||||
*/
|
||||
protected function getStatus($projectId)
|
||||
{
|
||||
$branch = $this->getParam('branch', 'master');
|
||||
try {
|
||||
$project = $this->projectStore->getById($projectId);
|
||||
$status = 'passing';
|
||||
|
||||
if (!$project->getAllowPublicStatus()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isset($project) && $project instanceof Project) {
|
||||
$build = $project->getLatestBuild($branch, [2,3]);
|
||||
|
||||
if (isset($build) && $build instanceof Build && $build->getStatus() != 2) {
|
||||
$status = 'failed';
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$status = 'error';
|
||||
}
|
||||
|
||||
return $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays projects information in ccmenu format
|
||||
*
|
||||
* @param $projectId
|
||||
* @return bool
|
||||
* @throws \Exception
|
||||
* @throws b8\Exception\HttpException
|
||||
*/
|
||||
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 bool
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
public function image($projectId)
|
||||
{
|
||||
$style = $this->getParam('style', 'plastic');
|
||||
$label = $this->getParam('label', 'build');
|
||||
|
||||
$status = $this->getStatus($projectId);
|
||||
|
||||
if (is_null($status)) {
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response->setHeader('Location', '/');
|
||||
return $response;
|
||||
}
|
||||
|
||||
$color = ($status == 'passing') ? 'green' : 'red';
|
||||
$image = file_get_contents(sprintf(
|
||||
'http://img.shields.io/badge/%s-%s-%s.svg?style=%s',
|
||||
$label,
|
||||
$status,
|
||||
$color,
|
||||
$style
|
||||
));
|
||||
|
||||
$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 $projectId
|
||||
* @return string
|
||||
* @throws \b8\Exception\HttpException\NotFoundException
|
||||
*/
|
||||
public function view($projectId)
|
||||
{
|
||||
$project = $this->projectStore->getById($projectId);
|
||||
|
||||
if (empty($project)) {
|
||||
throw new NotFoundException('Project with id: ' . $projectId . ' not found');
|
||||
}
|
||||
|
||||
if (!$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.
|
||||
*/
|
||||
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'];
|
||||
}
|
||||
}
|
||||
121
src/PHPCensor/Controller/GroupController.php
Normal file
121
src/PHPCensor/Controller/GroupController.php
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPCI - Continuous Integration for PHP
|
||||
*
|
||||
* @copyright Copyright 2015, Block 8 Limited.
|
||||
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
||||
* @link https://www.phptesting.org/
|
||||
*/
|
||||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use b8;
|
||||
use b8\Form;
|
||||
use b8\Store;
|
||||
use PHPCensor\Controller;
|
||||
use PHPCensor\Model\ProjectGroup;
|
||||
use PHPCensor\Helper\Lang;
|
||||
|
||||
/**
|
||||
* Project Controller - Allows users to create, edit and view projects.
|
||||
* @author Dan Cryer <dan@block8.co.uk>
|
||||
* @package PHPCI
|
||||
* @subpackage Web
|
||||
*/
|
||||
class GroupController extends Controller
|
||||
{
|
||||
/**
|
||||
* @var \PHPCI\Store\ProjectGroupStore
|
||||
*/
|
||||
protected $groupStore;
|
||||
|
||||
/**
|
||||
* Set up this controller.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->groupStore = b8\Store\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 = b8\Store\Factory::getStore('Project')->getByGroupId($group->getId());
|
||||
$thisGroup['projects'] = $projects['items'];
|
||||
$groups[] = $thisGroup;
|
||||
}
|
||||
|
||||
$this->view->groups = $groups;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add or edit a project group.
|
||||
* @param null $groupId
|
||||
* @return void|b8\Http\Response\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'));
|
||||
$this->groupStore->save($group);
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response->setHeader('Location', PHPCI_URL.'group');
|
||||
return $response;
|
||||
}
|
||||
|
||||
$form = new Form();
|
||||
$form->setMethod('POST');
|
||||
$form->setAction(PHPCI_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->setValue(Lang::get('group_save'));
|
||||
|
||||
$form->addField($title);
|
||||
$form->addField($submit);
|
||||
|
||||
$this->view->form = $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a project group.
|
||||
* @param $groupId
|
||||
* @return b8\Http\Response\RedirectResponse
|
||||
*/
|
||||
public function delete($groupId)
|
||||
{
|
||||
$this->requireAdmin();
|
||||
$group = $this->groupStore->getById($groupId);
|
||||
|
||||
$this->groupStore->delete($group);
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response->setHeader('Location', PHPCI_URL.'group');
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
167
src/PHPCensor/Controller/HomeController.php
Normal file
167
src/PHPCensor/Controller/HomeController.php
Normal file
|
|
@ -0,0 +1,167 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPCI - Continuous Integration for PHP
|
||||
*
|
||||
* @copyright Copyright 2014, Block 8 Limited.
|
||||
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
||||
* @link https://www.phptesting.org/
|
||||
*/
|
||||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use b8;
|
||||
use PHPCensor\BuildFactory;
|
||||
use PHPCensor\Helper\Lang;
|
||||
use PHPCensor\Model\Build;
|
||||
use PHPCensor\Controller;
|
||||
|
||||
/**
|
||||
* Home Controller - Displays the PHPCI Dashboard.
|
||||
* @author Dan Cryer <dan@block8.co.uk>
|
||||
* @package PHPCI
|
||||
* @subpackage Web
|
||||
*/
|
||||
class HomeController extends Controller
|
||||
{
|
||||
/**
|
||||
* @var \PHPCI\Store\BuildStore
|
||||
*/
|
||||
protected $buildStore;
|
||||
|
||||
/**
|
||||
* @var \PHPCI\Store\ProjectStore
|
||||
*/
|
||||
protected $projectStore;
|
||||
|
||||
/**
|
||||
* @var \PHPCI\Store\ProjectGroupStore
|
||||
*/
|
||||
protected $groupStore;
|
||||
|
||||
/**
|
||||
* Initialise the controller, set up stores and services.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->buildStore = b8\Store\Factory::getStore('Build');
|
||||
$this->projectStore = b8\Store\Factory::getStore('Project');
|
||||
$this->groupStore = b8\Store\Factory::getStore('ProjectGroup');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display PHPCI dashboard:
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$this->layout->title = Lang::get('dashboard');
|
||||
$builds = $this->buildStore->getLatestBuilds(null, 10);
|
||||
|
||||
foreach ($builds as &$build) {
|
||||
$build = BuildFactory::getBuild($build);
|
||||
}
|
||||
|
||||
$this->view->builds = $builds;
|
||||
$this->view->groups = $this->getGroupInfo();
|
||||
|
||||
return $this->view->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* AJAX get latest builds table (HTML)
|
||||
*/
|
||||
public function latest()
|
||||
{
|
||||
$this->response->disableLayout();
|
||||
$this->response->setContent($this->getLatestBuildsHtml());
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ajax request for the project overview section of the dashboard.
|
||||
*/
|
||||
public function summary()
|
||||
{
|
||||
$this->response->disableLayout();
|
||||
$projects = $this->projectStore->getWhere([], 50, 0, [], ['title' => 'ASC']);
|
||||
$this->response->setContent($this->getSummaryHtml($projects));
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the HTML for the project overview section of the dashboard.
|
||||
* @param $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;
|
||||
}
|
||||
|
||||
$summaryView = new b8\View('SummaryTable');
|
||||
$summaryView->projects = $projects;
|
||||
$summaryView->builds = $summaryBuilds;
|
||||
$summaryView->successful = $successes;
|
||||
$summaryView->failed = $failures;
|
||||
$summaryView->counts = $counts;
|
||||
|
||||
return $summaryView->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get latest builds and render as a table.
|
||||
*/
|
||||
protected function getLatestBuildsHtml()
|
||||
{
|
||||
$builds = $this->buildStore->getWhere([], 5, 0, [], ['id' => 'DESC']);
|
||||
$view = new b8\View('BuildsTable');
|
||||
|
||||
foreach ($builds['items'] as &$build) {
|
||||
$build = BuildFactory::getBuild($build);
|
||||
}
|
||||
|
||||
$view->builds = $builds['items'];
|
||||
|
||||
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());
|
||||
$thisGroup['projects'] = $projects['items'];
|
||||
$thisGroup['summary'] = $this->getSummaryHtml($thisGroup['projects']);
|
||||
$rtn[] = $thisGroup;
|
||||
}
|
||||
|
||||
return $rtn;
|
||||
}
|
||||
}
|
||||
60
src/PHPCensor/Controller/PluginController.php
Normal file
60
src/PHPCensor/Controller/PluginController.php
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPCI - Continuous Integration for PHP
|
||||
*
|
||||
* @copyright Copyright 2014, Block 8 Limited.
|
||||
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
||||
* @link https://www.phptesting.org/
|
||||
*/
|
||||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use b8;
|
||||
use PHPCensor\Helper\Lang;
|
||||
use PHPCensor\Plugin\Util\ComposerPluginInformation;
|
||||
use PHPCensor\Plugin\Util\FilesPluginInformation;
|
||||
use PHPCensor\Plugin\Util\PluginInformationCollection;
|
||||
use PHPCensor\Controller;
|
||||
|
||||
/**
|
||||
* Plugin Controller - Provides support for installing Composer packages.
|
||||
* @author Dan Cryer <dan@block8.co.uk>
|
||||
* @package PHPCI
|
||||
* @subpackage Web
|
||||
*/
|
||||
class PluginController extends Controller
|
||||
{
|
||||
/**
|
||||
* List all enabled plugins, installed and recommend packages.
|
||||
* @return string
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$this->requireAdmin();
|
||||
|
||||
$json = $this->getComposerJson();
|
||||
$this->view->installedPackages = $json['require'];
|
||||
|
||||
$pluginInfo = new PluginInformationCollection();
|
||||
$pluginInfo->add(FilesPluginInformation::newFromDir(PHPCI_DIR . "Plugin" . DIRECTORY_SEPARATOR));
|
||||
$pluginInfo->add(ComposerPluginInformation::buildFromYaml(
|
||||
ROOT_DIR . "vendor" . DIRECTORY_SEPARATOR . "composer" . DIRECTORY_SEPARATOR . "installed.json"
|
||||
));
|
||||
|
||||
$this->view->plugins = $pluginInfo->getInstalledPlugins();
|
||||
|
||||
$this->layout->title = Lang::get('plugins');
|
||||
|
||||
return $this->view->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the json-decoded contents of the composer.json file.
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getComposerJson()
|
||||
{
|
||||
$json = file_get_contents(ROOT_DIR . 'composer.json');
|
||||
return json_decode($json, true);
|
||||
}
|
||||
}
|
||||
451
src/PHPCensor/Controller/ProjectController.php
Normal file
451
src/PHPCensor/Controller/ProjectController.php
Normal file
|
|
@ -0,0 +1,451 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPCI - Continuous Integration for PHP
|
||||
*
|
||||
* @copyright Copyright 2014, Block 8 Limited.
|
||||
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
||||
* @link https://www.phptesting.org/
|
||||
*/
|
||||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use b8;
|
||||
use b8\Form;
|
||||
use b8\Exception\HttpException\NotFoundException;
|
||||
use b8\Store;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Project Controller - Allows users to create, edit and view projects.
|
||||
* @author Dan Cryer <dan@block8.co.uk>
|
||||
* @package PHPCI
|
||||
* @subpackage Web
|
||||
*/
|
||||
class ProjectController extends PHPCensor\Controller
|
||||
{
|
||||
/**
|
||||
* @var \PHPCI\Store\ProjectStore
|
||||
*/
|
||||
protected $projectStore;
|
||||
|
||||
/**
|
||||
* @var \PHPCI\Service\ProjectService
|
||||
*/
|
||||
protected $projectService;
|
||||
|
||||
/**
|
||||
* @var \PHPCI\Store\BuildStore
|
||||
*/
|
||||
protected $buildStore;
|
||||
|
||||
/**
|
||||
* @var \PHPCI\Service\BuildService
|
||||
*/
|
||||
protected $buildService;
|
||||
|
||||
/**
|
||||
* Initialise the controller, set up stores and services.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->buildStore = Store\Factory::getStore('Build');
|
||||
$this->projectStore = Store\Factory::getStore('Project');
|
||||
$this->projectService = new ProjectService($this->projectStore);
|
||||
$this->buildService = new BuildService($this->buildStore);
|
||||
}
|
||||
|
||||
/**
|
||||
* View a specific project.
|
||||
*/
|
||||
public function view($projectId)
|
||||
{
|
||||
$branch = $this->getParam('branch', '');
|
||||
$project = $this->projectStore->getById($projectId);
|
||||
|
||||
if (empty($project)) {
|
||||
throw new NotFoundException(Lang::get('project_x_not_found', $projectId));
|
||||
}
|
||||
|
||||
$per_page = 10;
|
||||
$page = $this->getParam('p', 1);
|
||||
$builds = $this->getLatestBuildsHtml($projectId, urldecode($branch), (($page - 1) * $per_page));
|
||||
$pages = $builds[1] == 0 ? 1 : ceil($builds[1] / $per_page);
|
||||
|
||||
if ($page > $pages) {
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response->setHeader('Location', PHPCI_URL.'project/view/'.$projectId);
|
||||
return $response;
|
||||
}
|
||||
|
||||
$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->page = $page;
|
||||
$this->view->pages = $pages;
|
||||
|
||||
$this->layout->title = $project->getTitle();
|
||||
$this->layout->subtitle = $this->view->branch;
|
||||
|
||||
return $this->view->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new pending build for a project.
|
||||
*/
|
||||
public function build($projectId, $branch = '')
|
||||
{
|
||||
/* @var \PHPCI\Model\Project $project */
|
||||
$project = $this->projectStore->getById($projectId);
|
||||
|
||||
if (empty($branch)) {
|
||||
$branch = $project->getBranch();
|
||||
}
|
||||
|
||||
if (empty($project)) {
|
||||
throw new NotFoundException(Lang::get('project_x_not_found', $projectId));
|
||||
}
|
||||
|
||||
$email = $_SESSION['phpci_user']->getEmail();
|
||||
$build = $this->buildService->createBuild($project, null, urldecode($branch), $email);
|
||||
|
||||
if ($this->buildService->queueError) {
|
||||
$_SESSION['global_error'] = Lang::get('add_to_queue_failed');
|
||||
}
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response->setHeader('Location', PHPCI_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 b8\Http\Response\RedirectResponse();
|
||||
$response->setHeader('Location', PHPCI_URL);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* AJAX get latest builds.
|
||||
*/
|
||||
public function builds($projectId)
|
||||
{
|
||||
$branch = $this->getParam('branch', '');
|
||||
$builds = $this->getLatestBuildsHtml($projectId, urldecode($branch));
|
||||
|
||||
$this->response->disableLayout();
|
||||
$this->response->setContent($builds[0]);
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render latest builds for project as HTML table.
|
||||
*
|
||||
* @param $projectId
|
||||
* @param string $branch A urldecoded branch name.
|
||||
* @param int $start
|
||||
* @return array
|
||||
*/
|
||||
protected function getLatestBuildsHtml($projectId, $branch = '', $start = 0)
|
||||
{
|
||||
$criteria = ['project_id' => $projectId];
|
||||
if (!empty($branch)) {
|
||||
$criteria['branch'] = $branch;
|
||||
}
|
||||
|
||||
$order = ['id' => 'DESC'];
|
||||
$builds = $this->buildStore->getWhere($criteria, 10, $start, [], $order);
|
||||
$view = new b8\View('BuildsTable');
|
||||
|
||||
foreach ($builds['items'] as &$build) {
|
||||
$build = BuildFactory::getBuild($build);
|
||||
}
|
||||
|
||||
$view->builds = $builds['items'];
|
||||
|
||||
return [$view->render(), $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();
|
||||
|
||||
$pub = null;
|
||||
$values = $this->getParams();
|
||||
|
||||
if ($method != 'POST') {
|
||||
$sshKey = new SshKey();
|
||||
$key = $sshKey->generate();
|
||||
|
||||
$values['key'] = $key['private_key'];
|
||||
$values['pubkey'] = $key['public_key'];
|
||||
$pub = $key['public_key'];
|
||||
}
|
||||
|
||||
$form = $this->projectForm($values);
|
||||
|
||||
if ($method != 'POST' || ($method == 'POST' && !$form->validate())) {
|
||||
$view = new b8\View('ProjectForm');
|
||||
$view->type = 'add';
|
||||
$view->project = null;
|
||||
$view->form = $form;
|
||||
$view->key = $pub;
|
||||
|
||||
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),
|
||||
'group' => $this->getParam('group_id', null),
|
||||
];
|
||||
|
||||
$project = $this->projectService->createProject($title, $type, $reference, $options);
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response->setHeader('Location', PHPCI_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'];
|
||||
|
||||
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 b8\View('ProjectForm');
|
||||
$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),
|
||||
'group' => $this->getParam('group_id', null),
|
||||
];
|
||||
|
||||
$project = $this->projectService->updateProject($project, $title, $type, $reference, $options);
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response->setHeader('Location', PHPCI_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(PHPCI_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' => Lang::get('github'),
|
||||
'bitbucket' => Lang::get('bitbucket'),
|
||||
'gitlab' => Lang::get('gitlab'),
|
||||
'remote' => Lang::get('remote'),
|
||||
'local' => Lang::get('local'),
|
||||
'hg' => Lang::get('hg'),
|
||||
'svn' => Lang::get('svn'),
|
||||
];
|
||||
|
||||
$field = Form\Element\Select::create('type', Lang::get('where_hosted'), true);
|
||||
$field->setPattern('^(github|bitbucket|gitlab|remote|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\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\Text::create('branch', Lang::get('default_branch'), true);
|
||||
$field->setClass('form-control')->setContainerClass('form-group')->setValue('master');
|
||||
$form->addField($field);
|
||||
|
||||
$field = Form\Element\Select::create('group_id', 'Project Group', true);
|
||||
$field->setClass('form-control')->setContainerClass('form-group')->setValue(1);
|
||||
|
||||
$groups = [];
|
||||
$groupStore = b8\Store\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 an array of repositories from Github's API.
|
||||
*/
|
||||
protected function githubRepositories()
|
||||
{
|
||||
$github = new Github();
|
||||
|
||||
$response = new b8\Http\Response\JsonResponse();
|
||||
$response->setContent($github->getRepositories());
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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' => '/^(https?):\/\//',
|
||||
'message' => Lang::get('error_mercurial')
|
||||
],
|
||||
'remote' => [
|
||||
'regex' => '/^(git|https?):\/\//',
|
||||
'message' => Lang::get('error_remote')
|
||||
],
|
||||
'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')
|
||||
],
|
||||
];
|
||||
|
||||
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;
|
||||
};
|
||||
}
|
||||
}
|
||||
214
src/PHPCensor/Controller/SessionController.php
Normal file
214
src/PHPCensor/Controller/SessionController.php
Normal file
|
|
@ -0,0 +1,214 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPCI - Continuous Integration for PHP
|
||||
*
|
||||
* @copyright Copyright 2014, Block 8 Limited.
|
||||
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
||||
* @link https://www.phptesting.org/
|
||||
*/
|
||||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use b8;
|
||||
use PHPCensor\Helper\Email;
|
||||
use PHPCensor\Helper\Lang;
|
||||
use PHPCensor\Controller;
|
||||
|
||||
/**
|
||||
* Session Controller - Handles user login / logout.
|
||||
* @author Dan Cryer <dan@block8.co.uk>
|
||||
* @package PHPCI
|
||||
* @subpackage Web
|
||||
*/
|
||||
class SessionController extends Controller
|
||||
{
|
||||
/**
|
||||
* @var \PHPCI\Store\UserStore
|
||||
*/
|
||||
protected $userStore;
|
||||
|
||||
/**
|
||||
* Initialise the controller, set up stores and services.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->response->disableLayout();
|
||||
$this->userStore = b8\Store\Factory::getStore('User');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles user login (form and processing)
|
||||
*/
|
||||
public function login()
|
||||
{
|
||||
$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']);
|
||||
|
||||
$user = $this->userStore->getByEmail($this->getParam('email'));
|
||||
|
||||
if ($user && password_verify($this->getParam('password', ''), $user->getHash())) {
|
||||
session_regenerate_id(true);
|
||||
$_SESSION['phpci_user_id'] = $user->getId();
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response->setHeader('Location', $this->getLoginRedirect());
|
||||
return $response;
|
||||
} else {
|
||||
$isLoginFailure = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$form = new b8\Form();
|
||||
$form->setMethod('POST');
|
||||
$form->setAction(PHPCI_URL.'session/login');
|
||||
|
||||
$email = new b8\Form\Element\Email('email');
|
||||
$email->setLabel(Lang::get('email_address'));
|
||||
$email->setRequired(true);
|
||||
$email->setContainerClass('form-group');
|
||||
$email->setClass('form-control');
|
||||
$form->addField($email);
|
||||
|
||||
$pwd = new b8\Form\Element\Password('password');
|
||||
$pwd->setLabel(Lang::get('password'));
|
||||
$pwd->setRequired(true);
|
||||
$pwd->setContainerClass('form-group');
|
||||
$pwd->setClass('form-control');
|
||||
$form->addField($pwd);
|
||||
|
||||
$pwd = new b8\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 b8\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['phpci_user']);
|
||||
unset($_SESSION['phpci_user_id']);
|
||||
|
||||
session_destroy();
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response->setHeader('Location', PHPCI_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 = PHPCI_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);
|
||||
|
||||
$_SESSION['phpci_user'] = $this->userStore->save($user);
|
||||
$_SESSION['phpci_user_id'] = $user->getId();
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response->setHeader('Location', PHPCI_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 = PHPCI_URL;
|
||||
|
||||
if (!empty($_SESSION['phpci_login_redirect'])) {
|
||||
$rtn .= $_SESSION['phpci_login_redirect'];
|
||||
$_SESSION['phpci_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));
|
||||
}
|
||||
}
|
||||
496
src/PHPCensor/Controller/SettingsController.php
Normal file
496
src/PHPCensor/Controller/SettingsController.php
Normal file
|
|
@ -0,0 +1,496 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPCI - Continuous Integration for PHP
|
||||
*
|
||||
* @copyright Copyright 2014, Block 8 Limited.
|
||||
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
||||
* @link https://www.phptesting.org/
|
||||
*/
|
||||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use b8;
|
||||
use b8\Form;
|
||||
use b8\HttpClient;
|
||||
use PHPCensor\Controller;
|
||||
use PHPCensor\Helper\Lang;
|
||||
use Symfony\Component\Yaml\Dumper;
|
||||
use Symfony\Component\Yaml\Parser;
|
||||
|
||||
/**
|
||||
* Settings Controller
|
||||
*
|
||||
* @author Dan Cryer <dan@block8.co.uk>
|
||||
* @package PHPCI
|
||||
* @subpackage Web
|
||||
*/
|
||||
class SettingsController extends Controller
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $settings;
|
||||
|
||||
/**
|
||||
* Initialise the controller, set up stores and services.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
parent::init();
|
||||
|
||||
$parser = new Parser();
|
||||
$yaml = file_get_contents(PHPCI_APP_DIR . 'config.yml');
|
||||
$this->settings = $parser->parse($yaml);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display settings forms.
|
||||
* @return string
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$this->requireAdmin();
|
||||
|
||||
$this->layout->title = Lang::get('settings');
|
||||
|
||||
$this->view->settings = $this->settings;
|
||||
|
||||
$basicSettings = [];
|
||||
if (isset($this->settings['phpci']['basic'])) {
|
||||
$basicSettings = $this->settings['phpci']['basic'];
|
||||
}
|
||||
|
||||
$buildSettings = [];
|
||||
if (isset($this->settings['phpci']['build'])) {
|
||||
$buildSettings = $this->settings['phpci']['build'];
|
||||
}
|
||||
|
||||
$emailSettings = [];
|
||||
if (isset($this->settings['phpci']['email_settings'])) {
|
||||
$emailSettings = $this->settings['phpci']['email_settings'];
|
||||
}
|
||||
|
||||
$authSettings = [];
|
||||
if (isset($this->settings['phpci']['authentication_settings'])) {
|
||||
$authSettings = $this->settings['phpci']['authentication_settings'];
|
||||
}
|
||||
|
||||
$this->view->configFile = PHPCI_APP_DIR . 'config.yml';
|
||||
$this->view->basicSettings = $this->getBasicForm($basicSettings);
|
||||
$this->view->buildSettings = $this->getBuildForm($buildSettings);
|
||||
$this->view->github = $this->getGithubForm();
|
||||
$this->view->emailSettings = $this->getEmailForm($emailSettings);
|
||||
$this->view->authenticationSettings = $this->getAuthenticationForm($authSettings);
|
||||
$this->view->isWriteable = $this->canWriteConfig();
|
||||
|
||||
if (!empty($this->settings['phpci']['github']['token'])) {
|
||||
$this->view->githubUser = $this->getGithubUser($this->settings['phpci']['github']['token']);
|
||||
}
|
||||
|
||||
return $this->view->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Save Github settings.
|
||||
*/
|
||||
public function github()
|
||||
{
|
||||
$this->requireAdmin();
|
||||
|
||||
$this->settings['phpci']['github']['id'] = $this->getParam('githubid', '');
|
||||
$this->settings['phpci']['github']['secret'] = $this->getParam('githubsecret', '');
|
||||
$error = $this->storeSettings();
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
|
||||
if ($error) {
|
||||
$response->setHeader('Location', PHPCI_URL . 'settings?saved=2');
|
||||
} else {
|
||||
$response->setHeader('Location', PHPCI_URL . 'settings?saved=1');
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save email settings.
|
||||
*/
|
||||
public function email()
|
||||
{
|
||||
$this->requireAdmin();
|
||||
|
||||
$this->settings['phpci']['email_settings'] = $this->getParams();
|
||||
$this->settings['phpci']['email_settings']['smtp_encryption'] = $this->getParam('smtp_encryption', 0);
|
||||
|
||||
$error = $this->storeSettings();
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
|
||||
if ($error) {
|
||||
$response->setHeader('Location', PHPCI_URL . 'settings?saved=2');
|
||||
} else {
|
||||
$response->setHeader('Location', PHPCI_URL . 'settings?saved=1');
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save build settings.
|
||||
*/
|
||||
public function build()
|
||||
{
|
||||
$this->requireAdmin();
|
||||
|
||||
$this->settings['phpci']['build'] = $this->getParams();
|
||||
|
||||
$error = $this->storeSettings();
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
|
||||
if ($error) {
|
||||
$response->setHeader('Location', PHPCI_URL . 'settings?saved=2');
|
||||
} else {
|
||||
$response->setHeader('Location', PHPCI_URL . 'settings?saved=1');
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save basic settings.
|
||||
*/
|
||||
public function basic()
|
||||
{
|
||||
$this->requireAdmin();
|
||||
|
||||
$this->settings['phpci']['basic'] = $this->getParams();
|
||||
$error = $this->storeSettings();
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
|
||||
if ($error) {
|
||||
$response->setHeader('Location', PHPCI_URL . 'settings?saved=2');
|
||||
} else {
|
||||
$response->setHeader('Location', PHPCI_URL . 'settings?saved=1');
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle authentication settings
|
||||
*/
|
||||
public function authentication()
|
||||
{
|
||||
$this->requireAdmin();
|
||||
|
||||
$this->settings['phpci']['authentication_settings']['state'] = $this->getParam('disable_authentication', 0);
|
||||
$this->settings['phpci']['authentication_settings']['user_id'] = $_SESSION['phpci_user_id'];
|
||||
|
||||
$error = $this->storeSettings();
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
|
||||
if ($error) {
|
||||
$response->setHeader('Location', PHPCI_URL . 'settings?saved=2');
|
||||
} else {
|
||||
$response->setHeader('Location', PHPCI_URL . 'settings?saved=1');
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Github redirects users back to this URL when t
|
||||
*/
|
||||
public function githubCallback()
|
||||
{
|
||||
$code = $this->getParam('code', null);
|
||||
$github = $this->settings['phpci']['github'];
|
||||
|
||||
if (!is_null($code)) {
|
||||
$http = new HttpClient();
|
||||
$url = 'https://github.com/login/oauth/access_token';
|
||||
$params = ['client_id' => $github['id'], 'client_secret' => $github['secret'], 'code' => $code];
|
||||
$resp = $http->post($url, $params);
|
||||
|
||||
if ($resp['success']) {
|
||||
parse_str($resp['body'], $resp);
|
||||
|
||||
$this->settings['phpci']['github']['token'] = $resp['access_token'];
|
||||
$this->storeSettings();
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response->setHeader('Location', PHPCI_URL . 'settings?linked=1');
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response->setHeader('Location', PHPCI_URL . 'settings?linked=2');
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert config to yaml and store to file.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function storeSettings()
|
||||
{
|
||||
$dumper = new Dumper();
|
||||
$yaml = $dumper->dump($this->settings, 4);
|
||||
file_put_contents(PHPCI_APP_DIR . 'config.yml', $yaml);
|
||||
|
||||
if (error_get_last()) {
|
||||
$error_get_last = error_get_last();
|
||||
return $error_get_last['message'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Github settings form.
|
||||
* @return Form
|
||||
*/
|
||||
protected function getGithubForm()
|
||||
{
|
||||
$form = new Form();
|
||||
$form->setMethod('POST');
|
||||
$form->setAction(PHPCI_URL . 'settings/github');
|
||||
$form->addField(new Form\Element\Csrf('csrf'));
|
||||
|
||||
$field = new Form\Element\Text('githubid');
|
||||
$field->setRequired(true);
|
||||
$field->setPattern('[a-zA-Z0-9]+');
|
||||
$field->setLabel(Lang::get('application_id'));
|
||||
$field->setClass('form-control');
|
||||
$field->setContainerClass('form-group');
|
||||
$form->addField($field);
|
||||
|
||||
if (isset($this->settings['phpci']['github']['id'])) {
|
||||
$field->setValue($this->settings['phpci']['github']['id']);
|
||||
}
|
||||
|
||||
$field = new Form\Element\Text('githubsecret');
|
||||
$field->setRequired(true);
|
||||
$field->setPattern('[a-zA-Z0-9]+');
|
||||
$field->setLabel(Lang::get('application_secret'));
|
||||
$field->setClass('form-control');
|
||||
$field->setContainerClass('form-group');
|
||||
$form->addField($field);
|
||||
|
||||
if (isset($this->settings['phpci']['github']['secret'])) {
|
||||
$field->setValue($this->settings['phpci']['github']['secret']);
|
||||
}
|
||||
|
||||
$field = new Form\Element\Submit();
|
||||
$field->setValue(Lang::get('save'));
|
||||
$field->setClass('btn btn-success pull-right');
|
||||
$form->addField($field);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the email settings form.
|
||||
* @param array $values
|
||||
* @return Form
|
||||
*/
|
||||
protected function getEmailForm($values = [])
|
||||
{
|
||||
$form = new Form();
|
||||
$form->setMethod('POST');
|
||||
$form->setAction(PHPCI_URL . 'settings/email');
|
||||
$form->addField(new Form\Element\Csrf('csrf'));
|
||||
|
||||
$field = new Form\Element\Text('smtp_address');
|
||||
$field->setRequired(false);
|
||||
$field->setLabel(Lang::get('smtp_server'));
|
||||
$field->setClass('form-control');
|
||||
$field->setContainerClass('form-group');
|
||||
$field->setValue('localhost');
|
||||
$form->addField($field);
|
||||
|
||||
$field = new Form\Element\Text('smtp_port');
|
||||
$field->setRequired(false);
|
||||
$field->setPattern('[0-9]+');
|
||||
$field->setLabel(Lang::get('smtp_port'));
|
||||
$field->setClass('form-control');
|
||||
$field->setContainerClass('form-group');
|
||||
$field->setValue(25);
|
||||
$form->addField($field);
|
||||
|
||||
$field = new Form\Element\Text('smtp_username');
|
||||
$field->setRequired(false);
|
||||
$field->setLabel(Lang::get('smtp_username'));
|
||||
$field->setClass('form-control');
|
||||
$field->setContainerClass('form-group');
|
||||
$form->addField($field);
|
||||
|
||||
$field = new Form\Element\Password('smtp_password');
|
||||
$field->setRequired(false);
|
||||
$field->setLabel(Lang::get('smtp_password'));
|
||||
$field->setClass('form-control');
|
||||
$field->setContainerClass('form-group');
|
||||
$form->addField($field);
|
||||
|
||||
$field = new Form\Element\Email('from_address');
|
||||
$field->setRequired(false);
|
||||
$field->setLabel(Lang::get('from_email_address'));
|
||||
$field->setClass('form-control');
|
||||
$field->setContainerClass('form-group');
|
||||
$form->addField($field);
|
||||
|
||||
$field = new Form\Element\Email('default_mailto_address');
|
||||
$field->setRequired(false);
|
||||
$field->setLabel(Lang::get('default_notification_address'));
|
||||
$field->setClass('form-control');
|
||||
$field->setContainerClass('form-group');
|
||||
$form->addField($field);
|
||||
|
||||
$field = new Form\Element\Select('smtp_encryption');
|
||||
$field->setOptions(['' => Lang::get('none'), 'tls' => Lang::get('tls'), 'ssl' => Lang::get('ssl')]);
|
||||
$field->setRequired(false);
|
||||
$field->setLabel(Lang::get('use_smtp_encryption'));
|
||||
$field->setContainerClass('form-group');
|
||||
$field->setValue(1);
|
||||
$form->addField($field);
|
||||
|
||||
$field = new Form\Element\Submit();
|
||||
$field->setValue(Lang::get('save'));
|
||||
$field->setClass('btn btn-success pull-right');
|
||||
$form->addField($field);
|
||||
|
||||
$form->setValues($values);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call Github API for our Github user object.
|
||||
* @param $token
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getGithubUser($token)
|
||||
{
|
||||
$http = new HttpClient('https://api.github.com');
|
||||
$user = $http->get('/user', ['access_token' => $token]);
|
||||
|
||||
return $user['body'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if we can write the PHPCI config file.
|
||||
* @return bool
|
||||
*/
|
||||
protected function canWriteConfig()
|
||||
{
|
||||
return is_writeable(PHPCI_APP_DIR . 'config.yml');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Build settings form.
|
||||
* @param array $values
|
||||
* @return Form
|
||||
*/
|
||||
protected function getBuildForm($values = [])
|
||||
{
|
||||
$form = new Form();
|
||||
$form->setMethod('POST');
|
||||
$form->setAction(PHPCI_URL . 'settings/build');
|
||||
|
||||
$field = new Form\Element\Select('failed_after');
|
||||
$field->setRequired(false);
|
||||
$field->setLabel(Lang::get('failed_after'));
|
||||
$field->setClass('form-control');
|
||||
$field->setContainerClass('form-group');
|
||||
$field->setOptions([
|
||||
300 => Lang::get('5_mins'),
|
||||
900 => Lang::get('15_mins'),
|
||||
1800 => Lang::get('30_mins'),
|
||||
3600 => Lang::get('1_hour'),
|
||||
10800 => Lang::get('3_hours'),
|
||||
]);
|
||||
$field->setValue(1800);
|
||||
$form->addField($field);
|
||||
|
||||
|
||||
$field = new Form\Element\Submit();
|
||||
$field->setValue(Lang::get('save'));
|
||||
$field->setClass('btn btn-success pull-right');
|
||||
$form->addField($field);
|
||||
|
||||
$form->setValues($values);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Basic settings form.
|
||||
* @param array $values
|
||||
* @return Form
|
||||
*/
|
||||
protected function getBasicForm($values = [])
|
||||
{
|
||||
$form = new Form();
|
||||
$form->setMethod('POST');
|
||||
$form->setAction(PHPCI_URL . 'settings/basic');
|
||||
|
||||
$field = new Form\Element\Select('language');
|
||||
$field->setRequired(true);
|
||||
$field->setLabel(Lang::get('language'));
|
||||
$field->setClass('form-control');
|
||||
$field->setContainerClass('form-group');
|
||||
$field->setOptions(Lang::getLanguageOptions());
|
||||
$field->setValue(Lang::getLanguage());
|
||||
$form->addField($field);
|
||||
|
||||
|
||||
$field = new Form\Element\Submit();
|
||||
$field->setValue(Lang::get('save'));
|
||||
$field->setClass('btn btn-success pull-right');
|
||||
$form->addField($field);
|
||||
|
||||
$form->setValues($values);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Form for disabling user authentication while using a default user
|
||||
*
|
||||
* @param array $values
|
||||
* @return Form
|
||||
*/
|
||||
protected function getAuthenticationForm($values = [])
|
||||
{
|
||||
$form = new Form();
|
||||
$form->setMethod('POST');
|
||||
$form->setAction(PHPCI_URL . 'settings/authentication');
|
||||
$form->addField(new Form\Element\Csrf('csrf'));
|
||||
|
||||
$field = new Form\Element\Checkbox('disable_authentication');
|
||||
$field->setCheckedValue(1);
|
||||
$field->setRequired(false);
|
||||
$field->setLabel('Disable Authentication?');
|
||||
$field->setContainerClass('form-group');
|
||||
$field->setValue(0);
|
||||
|
||||
if (isset($values['state'])) {
|
||||
$field->setValue((int)$values['state']);
|
||||
}
|
||||
|
||||
$form->addField($field);
|
||||
|
||||
$field = new Form\Element\Submit();
|
||||
$field->setValue('Save »');
|
||||
$field->setClass('btn btn-success pull-right');
|
||||
$form->addField($field);
|
||||
|
||||
$form->setValues($values);
|
||||
|
||||
return $form;
|
||||
}
|
||||
}
|
||||
295
src/PHPCensor/Controller/UserController.php
Normal file
295
src/PHPCensor/Controller/UserController.php
Normal file
|
|
@ -0,0 +1,295 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPCI - Continuous Integration for PHP
|
||||
*
|
||||
* @copyright Copyright 2014, Block 8 Limited.
|
||||
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
||||
* @link https://www.phptesting.org/
|
||||
*/
|
||||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use b8;
|
||||
use b8\Exception\HttpException\NotFoundException;
|
||||
use b8\Form;
|
||||
use PHPCensor\Controller;
|
||||
use PHPCensor\Helper\Lang;
|
||||
use PHPCensor\Service\UserService;
|
||||
|
||||
/**
|
||||
* User Controller - Allows an administrator to view, add, edit and delete users.
|
||||
* @author Dan Cryer <dan@block8.co.uk>
|
||||
* @package PHPCI
|
||||
* @subpackage Web
|
||||
*/
|
||||
class UserController extends Controller
|
||||
{
|
||||
/**
|
||||
* @var \PHPCI\Store\UserStore
|
||||
*/
|
||||
protected $userStore;
|
||||
|
||||
/**
|
||||
* @var \PHPCI\Service\UserService
|
||||
*/
|
||||
protected $userService;
|
||||
|
||||
/**
|
||||
* Initialise the controller, set up stores and services.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->userStore = b8\Store\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()
|
||||
{
|
||||
$user = $_SESSION['phpci_user'];
|
||||
|
||||
if ($this->request->getMethod() == 'POST') {
|
||||
$name = $this->getParam('name', null);
|
||||
$email = $this->getParam('email', null);
|
||||
$password = $this->getParam('password', null);
|
||||
|
||||
$currentLang = Lang::getLanguage();
|
||||
$chosenLang = $this->getParam('language', $currentLang);
|
||||
|
||||
if ($chosenLang !== $currentLang) {
|
||||
setcookie('phpcilang', $chosenLang, time() + (10 * 365 * 24 * 60 * 60), '/');
|
||||
Lang::setLanguage($chosenLang);
|
||||
}
|
||||
|
||||
$_SESSION['phpci_user'] = $this->userService->updateUser($user, $name, $email, $password);
|
||||
$user = $_SESSION['phpci_user'];
|
||||
|
||||
$this->view->updated = 1;
|
||||
}
|
||||
|
||||
$this->layout->title = $user->getName();
|
||||
$this->layout->subtitle = Lang::get('edit_profile');
|
||||
|
||||
$values = $user->getDataArray();
|
||||
|
||||
if (array_key_exists('phpcilang', $_COOKIE)) {
|
||||
$values['language'] = $_COOKIE['phpcilang'];
|
||||
}
|
||||
|
||||
$form = new Form();
|
||||
$form->setAction(PHPCI_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);
|
||||
$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);
|
||||
$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);
|
||||
$form->addField($password);
|
||||
|
||||
$lang = new Form\Element\Select('language');
|
||||
$lang->setClass('form-control');
|
||||
$lang->setContainerClass('form-group');
|
||||
$lang->setLabel(Lang::get('language'));
|
||||
$lang->setRequired(true);
|
||||
$lang->setOptions(Lang::getLanguageOptions());
|
||||
$lang->setValue(Lang::getLanguage());
|
||||
$form->addField($lang);
|
||||
|
||||
$submit = new Form\Element\Submit();
|
||||
$submit->setClass('btn btn-success');
|
||||
$submit->setValue(Lang::get('save'));
|
||||
$form->addField($submit);
|
||||
|
||||
$form->setValues($values);
|
||||
|
||||
$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 b8\View('UserForm');
|
||||
$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, $password, $isAdmin);
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response->setHeader('Location', PHPCI_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 b8\View('UserForm');
|
||||
$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 b8\Http\Response\RedirectResponse();
|
||||
$response->setHeader('Location', PHPCI_URL . 'user');
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create user add / edit form.
|
||||
*/
|
||||
protected function userForm($values, $type = 'add')
|
||||
{
|
||||
$form = new Form();
|
||||
$form->setMethod('POST');
|
||||
$form->setAction(PHPCI_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 b8\Http\Response\RedirectResponse();
|
||||
$response->setHeader('Location', PHPCI_URL . 'user');
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
468
src/PHPCensor/Controller/WebhookController.php
Normal file
468
src/PHPCensor/Controller/WebhookController.php
Normal file
|
|
@ -0,0 +1,468 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPCI - Continuous Integration for PHP
|
||||
*
|
||||
* @copyright Copyright 2014-2015, Block 8 Limited.
|
||||
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
||||
* @link https://www.phptesting.org/
|
||||
*/
|
||||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use b8;
|
||||
use b8\Store;
|
||||
use Exception;
|
||||
use PHPCensor\Model\Project;
|
||||
use PHPCensor\Service\BuildService;
|
||||
use PHPCensor\Store\BuildStore;
|
||||
use PHPCensor\Store\ProjectStore;
|
||||
use b8\Controller;
|
||||
use b8\Config;
|
||||
use b8\HttpClient;
|
||||
|
||||
/**
|
||||
* Webhook Controller - Processes webhook pings from BitBucket, Github, Gitlab, 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>
|
||||
* @package PHPCI
|
||||
* @subpackage Web
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
|
||||
*/
|
||||
class WebhookController extends Controller
|
||||
{
|
||||
/**
|
||||
* @var BuildStore
|
||||
*/
|
||||
protected $buildStore;
|
||||
|
||||
/**
|
||||
* @var ProjectStore
|
||||
*/
|
||||
protected $projectStore;
|
||||
|
||||
/**
|
||||
* @var BuildService
|
||||
*/
|
||||
protected $buildService;
|
||||
|
||||
/**
|
||||
* Initialise the controller, set up stores and services.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->buildStore = Store\Factory::getStore('Build');
|
||||
$this->projectStore = Store\Factory::getStore('Project');
|
||||
$this->buildService = new BuildService($this->buildStore);
|
||||
}
|
||||
|
||||
/** Handle the action, Ensuring to return a JsonResponse.
|
||||
*
|
||||
* @param string $action
|
||||
* @param mixed $actionParams
|
||||
*
|
||||
* @return \b8\Http\Response
|
||||
*/
|
||||
public function handleAction($action, $actionParams)
|
||||
{
|
||||
$response = new b8\Http\Response\JsonResponse();
|
||||
try {
|
||||
$data = parent::handleAction($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');
|
||||
|
||||
// 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);
|
||||
|
||||
if (empty($payload['push']['changes'])) {
|
||||
// Invalid event from bitbucket
|
||||
return [
|
||||
'status' => 'failed',
|
||||
'commits' => []
|
||||
];
|
||||
}
|
||||
|
||||
return $this->bitbucketWebhook($payload, $project);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bitbucket webhooks.
|
||||
*/
|
||||
protected function bitbucketWebhook($payload, $project)
|
||||
{
|
||||
$results = array();
|
||||
$status = 'failed';
|
||||
foreach ($payload['push']['changes'] as $commit) {
|
||||
try {
|
||||
$email = $commit['new']['target']['author']['raw'];
|
||||
$email = substr($email, 0, strpos($email, '>'));
|
||||
$email = substr($email, strpos($email, '<') + 1);
|
||||
|
||||
$results[$commit['new']['target']['hash']] = $this->createBuild(
|
||||
$project,
|
||||
$commit['new']['target']['hash'],
|
||||
$commit['new']['name'],
|
||||
$email,
|
||||
$commit['new']['target']['message']
|
||||
);
|
||||
$status = 'ok';
|
||||
} catch (Exception $ex) {
|
||||
$results[$commit['new']['target']['hash']] = array('status' => 'failed', 'error' => $ex->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return array('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(
|
||||
$project,
|
||||
$commit['raw_node'],
|
||||
$commit['branch'],
|
||||
$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', 'remote']);
|
||||
$branch = $this->getParam('branch', $project->getBranch());
|
||||
$commit = $this->getParam('commit');
|
||||
$commitMessage = $this->getParam('message');
|
||||
$committer = $this->getParam('committer');
|
||||
|
||||
return $this->createBuild($project, $commit, $branch, $committer, $commitMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by Github Webhooks:
|
||||
*/
|
||||
public function github($projectId)
|
||||
{
|
||||
$project = $this->fetchProject($projectId, 'github');
|
||||
|
||||
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 web hooks:
|
||||
if (array_key_exists('pull_request', $payload)) {
|
||||
return $this->githubPullRequest($project, $payload);
|
||||
}
|
||||
|
||||
// Handle Push web hooks:
|
||||
if (array_key_exists('commits', $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
|
||||
* @param b8\Http\Response\JsonResponse $response
|
||||
*
|
||||
* @return b8\Http\Response\JsonResponse
|
||||
*/
|
||||
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['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) {
|
||||
if (!$commit['distinct']) {
|
||||
$results[$commit['id']] = ['status' => 'ignored'];
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$branch = str_replace('refs/heads/', '', $payload['ref']);
|
||||
$committer = $commit['committer']['email'];
|
||||
$results[$commit['id']] = $this->createBuild(
|
||||
$project,
|
||||
$commit['id'],
|
||||
$branch,
|
||||
$committer,
|
||||
$commit['message']
|
||||
);
|
||||
$status = 'ok';
|
||||
} catch (Exception $ex) {
|
||||
$results[$commit['id']] = ['status' => 'failed', 'error' => $ex->getMessage()];
|
||||
}
|
||||
}
|
||||
return ['status' => $status, 'commits' => $results];
|
||||
}
|
||||
|
||||
if (substr($payload['ref'], 0, 10) == 'refs/tags/') {
|
||||
// If we don't, but we're dealing with a tag, add that instead:
|
||||
$branch = str_replace('refs/tags/', 'Tag: ', $payload['ref']);
|
||||
$committer = $payload['pusher']['email'];
|
||||
$message = $payload['head_commit']['message'];
|
||||
return $this->createBuild($project, $payload['after'], $branch, $committer, $message);
|
||||
}
|
||||
|
||||
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('phpci.github.token');
|
||||
|
||||
if (!empty($token)) {
|
||||
$headers[] = 'Authorization: token ' . $token;
|
||||
}
|
||||
|
||||
$url = $payload['pull_request']['commits_url'];
|
||||
$http = new HttpClient();
|
||||
$http->setHeaders($headers);
|
||||
|
||||
//for large pull requests, allow grabbing more then the default number of commits
|
||||
$custom_per_page = Config::getInstance()->get('phpci.github.per_page');
|
||||
$params = [];
|
||||
if ($custom_per_page) {
|
||||
$params["per_page"] = $custom_per_page;
|
||||
}
|
||||
$response = $http->get($url, $params);
|
||||
|
||||
// Check we got a success response:
|
||||
if (!$response['success']) {
|
||||
throw new Exception('Could not get commits, failed API request.');
|
||||
}
|
||||
|
||||
$results = [];
|
||||
$status = 'failed';
|
||||
foreach ($response['body'] 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'];
|
||||
|
||||
$remoteUrlKey = $payload['pull_request']['head']['repo']['private'] ? 'ssh_url' : 'clone_url';
|
||||
|
||||
$extra = [
|
||||
'build_type' => 'pull_request',
|
||||
'pull_request_id' => $payload['pull_request']['id'],
|
||||
'pull_request_number' => $payload['number'],
|
||||
'remote_branch' => $payload['pull_request']['head']['ref'],
|
||||
'remote_url' => $payload['pull_request']['head']['repo'][$remoteUrlKey],
|
||||
];
|
||||
|
||||
$results[$id] = $this->createBuild($project, $id, $branch, $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');
|
||||
|
||||
$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($project, $commit['id'], $branch, $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(
|
||||
$project,
|
||||
$commit['id'],
|
||||
$branch,
|
||||
$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.'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for creating a new build.
|
||||
*
|
||||
* @param Project $project
|
||||
* @param string $commitId
|
||||
* @param string $branch
|
||||
* @param string $committer
|
||||
* @param string $commitMessage
|
||||
* @param array $extra
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function createBuild(
|
||||
Project $project,
|
||||
$commitId,
|
||||
$branch,
|
||||
$committer,
|
||||
$commitMessage,
|
||||
array $extra = null
|
||||
) {
|
||||
// Check if a build already exists for this commit ID:
|
||||
$builds = $this->buildStore->getByProjectAndCommit($project->getId(), $commitId);
|
||||
|
||||
if ($builds['count']) {
|
||||
return [
|
||||
'status' => 'ignored',
|
||||
'message' => sprintf('Duplicate of build #%d', $builds['items'][0]->getId())
|
||||
];
|
||||
}
|
||||
|
||||
// If not, create a new build job for it:
|
||||
$build = $this->buildService->createBuild($project, $commitId, $branch, $committer, $commitMessage, $extra);
|
||||
|
||||
return ['status' => 'ok', 'buildID' => $build->getID()];
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a project and check its type.
|
||||
*
|
||||
* @param int $projectId
|
||||
* @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)
|
||||
{
|
||||
$project = $this->projectStore->getById($projectId);
|
||||
|
||||
if (empty($projectId)) {
|
||||
throw new Exception('Project does not exist: ' . $projectId);
|
||||
}
|
||||
|
||||
if (is_array($expectedType)
|
||||
? !in_array($project->getType(), $expectedType)
|
||||
: $project->getType() !== $expectedType
|
||||
) {
|
||||
throw new Exception('Wrong project type: ' . $project->getType());
|
||||
}
|
||||
|
||||
return $project;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue