2014-02-24 16:30:44 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PHPCI - Continuous Integration for PHP
|
|
|
|
*
|
2015-03-25 15:15:53 +01:00
|
|
|
* @copyright Copyright 2014-2015, Block 8 Limited.
|
2014-02-24 16:30:44 +01:00
|
|
|
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
2014-05-12 18:26:17 +02:00
|
|
|
* @link https://www.phptesting.org/
|
2014-02-24 16:30:44 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace PHPCI\Controller;
|
|
|
|
|
|
|
|
use b8;
|
|
|
|
use b8\Store;
|
2015-03-25 15:15:53 +01:00
|
|
|
use Exception;
|
2014-05-12 16:53:29 +02:00
|
|
|
use PHPCI\BuildFactory;
|
2015-03-25 15:15:53 +01:00
|
|
|
use PHPCI\Model\Project;
|
2014-07-14 17:08:19 +02:00
|
|
|
use PHPCI\Service\BuildService;
|
2015-03-25 15:15:53 +01:00
|
|
|
use PHPCI\Store\BuildStore;
|
|
|
|
use PHPCI\Store\ProjectStore;
|
2014-02-24 16:30:44 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Webhook Controller - Processes webhook pings from BitBucket, Github, Gitlab, etc.
|
2015-03-25 15:15:53 +01:00
|
|
|
*
|
2014-02-24 16:30:44 +01:00
|
|
|
* @author Dan Cryer <dan@block8.co.uk>
|
|
|
|
* @author Sami Tikka <stikka@iki.fi>
|
|
|
|
* @author Alex Russell <alex@clevercherry.com>
|
2015-03-25 15:15:53 +01:00
|
|
|
* @author Guillaume Perréal <adirelle@gmail.com>
|
2014-02-24 16:30:44 +01:00
|
|
|
* @package PHPCI
|
|
|
|
* @subpackage Web
|
|
|
|
*/
|
2015-03-25 15:15:53 +01:00
|
|
|
class WebhookController extends \b8\Controller
|
2014-02-24 16:30:44 +01:00
|
|
|
{
|
|
|
|
/**
|
2015-03-25 15:15:53 +01:00
|
|
|
* @var BuildStore
|
2014-02-24 16:30:44 +01:00
|
|
|
*/
|
|
|
|
protected $buildStore;
|
|
|
|
|
2014-07-14 17:08:19 +02:00
|
|
|
/**
|
2015-03-25 15:15:53 +01:00
|
|
|
* @var ProjectStore
|
2014-07-14 17:08:19 +02:00
|
|
|
*/
|
|
|
|
protected $projectStore;
|
|
|
|
|
|
|
|
/**
|
2015-03-25 15:15:53 +01:00
|
|
|
* @var BuildService
|
2014-07-14 17:08:19 +02:00
|
|
|
*/
|
|
|
|
protected $buildService;
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Initialise the controller, set up stores and services.
|
|
|
|
*/
|
2014-02-24 16:30:44 +01:00
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
$this->buildStore = Store\Factory::getStore('Build');
|
2014-07-14 17:08:19 +02:00
|
|
|
$this->projectStore = Store\Factory::getStore('Project');
|
|
|
|
$this->buildService = new BuildService($this->buildStore);
|
2014-02-24 16:30:44 +01:00
|
|
|
}
|
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
/** Handle the action, Ensuring to return a JsonResponse.
|
|
|
|
*
|
|
|
|
* @param string $action
|
|
|
|
* @param mixed $actionParams
|
|
|
|
*
|
|
|
|
* @return \b8\Http\Response
|
2014-02-24 16:30:44 +01:00
|
|
|
*/
|
2015-03-25 15:15:53 +01:00
|
|
|
public function handleAction($action, $actionParams)
|
2014-02-24 16:30:44 +01:00
|
|
|
{
|
2015-02-12 13:37:56 +01:00
|
|
|
$response = new b8\Http\Response\JsonResponse();
|
2015-03-25 15:15:53 +01:00
|
|
|
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(array('status' => 'failed', 'error' => $ex->getMessage()));
|
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
2015-02-12 13:37:56 +01:00
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
/**
|
|
|
|
* Called by Bitbucket POST service.
|
|
|
|
*/
|
|
|
|
public function bitbucket($projectId)
|
|
|
|
{
|
|
|
|
$project = $this->fetchProject($projectId, 'bitbucket');
|
2014-12-01 18:30:44 +01:00
|
|
|
$payload = json_decode($this->getParam('payload'), true);
|
2014-02-24 16:30:44 +01:00
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
$results = array();
|
|
|
|
$status = 'failed';
|
2014-02-24 16:30:44 +01:00
|
|
|
foreach ($payload['commits'] as $commit) {
|
|
|
|
try {
|
2014-02-24 22:13:40 +01:00
|
|
|
$email = $commit['raw_author'];
|
|
|
|
$email = substr($email, 0, strpos($email, '>'));
|
|
|
|
$email = substr($email, strpos($email, '<') + 1);
|
2014-02-24 16:30:44 +01:00
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
$results[$commit['raw_node']] = $this->createBuild(
|
|
|
|
$project,
|
|
|
|
$commit['raw_node'],
|
|
|
|
$commit['branch'],
|
|
|
|
$email,
|
|
|
|
$commit['message']
|
|
|
|
);
|
|
|
|
$status = 'ok';
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
$results[$commit['raw_node']] = array('status' => 'failed', 'error' => $ex->getMessage());
|
2014-02-24 16:30:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
return array('status' => $status, 'commits' => $results);
|
2014-02-24 16:30:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-21 22:18:01 +02:00
|
|
|
* Called by POSTing to /webhook/git/<project_id>?branch=<branch>&commit=<commit>
|
2014-02-24 16:30:44 +01:00
|
|
|
*
|
2015-03-25 15:15:53 +01:00
|
|
|
* @param string $projectId
|
2014-02-24 16:30:44 +01:00
|
|
|
*/
|
2015-03-25 15:15:53 +01:00
|
|
|
public function git($projectId)
|
2014-12-01 22:42:52 +01:00
|
|
|
{
|
2015-03-25 15:15:53 +01:00
|
|
|
$project = $this->fetchProject($projectId, array('local', 'remote'));
|
|
|
|
$branch = $this->getParam('branch', $project->getBranch());
|
2014-02-24 16:30:44 +01:00
|
|
|
$commit = $this->getParam('commit');
|
2014-08-14 13:15:22 +02:00
|
|
|
$commitMessage = $this->getParam('message');
|
|
|
|
$committer = $this->getParam('committer');
|
2014-02-24 16:30:44 +01:00
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
return $this->createBuild($project, $commit, $branch, $committer, $commitMessage);
|
2014-12-01 22:42:52 +01:00
|
|
|
}
|
2014-02-24 16:30:44 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by Github Webhooks:
|
|
|
|
*/
|
2015-03-25 15:15:53 +01:00
|
|
|
public function github($projectId)
|
2014-02-24 16:30:44 +01:00
|
|
|
{
|
2015-03-25 15:15:53 +01:00
|
|
|
$project = $this->fetchProject($projectId, 'github');
|
2015-02-12 13:37:56 +01:00
|
|
|
|
2014-12-01 18:30:44 +01:00
|
|
|
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:
|
2015-03-25 15:15:53 +01:00
|
|
|
return array('status' => 'failed', 'error' => 'Content type not supported.', 'responseCode' => 401);
|
2014-12-01 18:30:44 +01:00
|
|
|
}
|
2014-05-12 15:46:45 +02:00
|
|
|
|
|
|
|
// Handle Pull Request web hooks:
|
|
|
|
if (array_key_exists('pull_request', $payload)) {
|
2015-03-25 15:15:53 +01:00
|
|
|
return $this->githubPullRequest($project, $payload);
|
2014-05-12 15:46:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle Push web hooks:
|
|
|
|
if (array_key_exists('commits', $payload)) {
|
2015-03-25 15:15:53 +01:00
|
|
|
return $this->githubCommitRequest($project, $payload);
|
2014-05-12 15:46:45 +02:00
|
|
|
}
|
2014-02-24 16:30:44 +01:00
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
return array('status' => 'ignored', 'message' => 'Unusable payload.');
|
2014-05-12 15:46:45 +02:00
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Handle the payload when Github sends a commit webhook.
|
2015-03-25 15:15:53 +01:00
|
|
|
*
|
|
|
|
* @param Project $project
|
2014-12-08 12:25:33 +01:00
|
|
|
* @param array $payload
|
2015-02-26 09:45:42 +01:00
|
|
|
* @param b8\Http\Response\JsonResponse $response
|
2015-03-25 15:15:53 +01:00
|
|
|
*
|
2015-02-26 09:45:42 +01:00
|
|
|
* @return b8\Http\Response\JsonResponse
|
2014-12-08 12:25:33 +01:00
|
|
|
*/
|
2015-03-25 15:15:53 +01:00
|
|
|
protected function githubCommitRequest(Project $project, array $payload)
|
2014-05-12 15:46:45 +02:00
|
|
|
{
|
2014-02-24 16:30:44 +01:00
|
|
|
// Github sends a payload when you close a pull request with a
|
|
|
|
// non-existant commit. We don't want this.
|
2014-05-12 15:46:45 +02:00
|
|
|
if (array_key_exists('after', $payload) && $payload['after'] === '0000000000000000000000000000000000000000') {
|
2015-03-25 15:15:53 +01:00
|
|
|
return array('status' => 'ignored');
|
2014-02-24 16:30:44 +01:00
|
|
|
}
|
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
if (isset($payload['commits']) && is_array($payload['commits'])) {
|
|
|
|
// If we have a list of commits, then add them all as builds to be tested:
|
2014-02-24 21:14:57 +01:00
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
$results = array();
|
|
|
|
$status = 'failed';
|
|
|
|
foreach ($payload['commits'] as $commit) {
|
|
|
|
if (!$commit['distinct']) {
|
|
|
|
$results[$commit['id']] = array('status' => 'ignored');
|
|
|
|
continue;
|
|
|
|
}
|
2014-02-24 21:14:57 +01:00
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
try {
|
2014-05-12 16:53:29 +02:00
|
|
|
$branch = str_replace('refs/heads/', '', $payload['ref']);
|
|
|
|
$committer = $commit['committer']['email'];
|
2015-03-25 15:15:53 +01:00
|
|
|
$results[$commit['id']] = $this->createBuild(
|
|
|
|
$project,
|
|
|
|
$commit['id'],
|
|
|
|
$branch,
|
|
|
|
$committer,
|
|
|
|
$commit['message']
|
|
|
|
);
|
|
|
|
$status = 'ok';
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
$results[$commit['id']] = array('status' => 'failed', 'error' => $ex->getMessage());
|
2014-02-24 21:14:57 +01:00
|
|
|
}
|
2014-02-24 16:30:44 +01:00
|
|
|
}
|
2015-03-25 15:15:53 +01:00
|
|
|
return array('status' => $status, 'commits' => $results);
|
|
|
|
}
|
2014-02-24 16:30:44 +01:00
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
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);
|
2014-02-24 16:30:44 +01:00
|
|
|
}
|
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
return array('status' => 'ignored', 'message' => 'Unusable payload.');
|
2014-02-24 16:30:44 +01:00
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Handle the payload when Github sends a Pull Request webhook.
|
2015-03-25 15:15:53 +01:00
|
|
|
*
|
|
|
|
* @param Project $project
|
2014-12-08 12:25:33 +01:00
|
|
|
* @param array $payload
|
|
|
|
*/
|
2015-03-25 15:15:53 +01:00
|
|
|
protected function githubPullRequest(Project $project, array $payload)
|
2014-05-12 15:46:45 +02:00
|
|
|
{
|
|
|
|
// We only want to know about open pull requests:
|
|
|
|
if (!in_array($payload['action'], array('opened', 'synchronize', 'reopened'))) {
|
2015-03-25 15:15:53 +01:00
|
|
|
return array('status' => 'ok');
|
2014-05-12 15:46:45 +02:00
|
|
|
}
|
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
$headers = array();
|
|
|
|
$token = \b8\Config::getInstance()->get('phpci.github.token');
|
2014-05-12 15:46:45 +02:00
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
if (!empty($token)) {
|
|
|
|
$headers[] = 'Authorization: token ' . $token;
|
|
|
|
}
|
2014-05-12 15:46:45 +02:00
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
$url = $payload['pull_request']['commits_url'];
|
|
|
|
$http = new \b8\HttpClient();
|
|
|
|
$http->setHeaders($headers);
|
|
|
|
$response = $http->get($url);
|
2014-05-12 15:58:30 +02:00
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
// Check we got a success response:
|
|
|
|
if (!$response['success']) {
|
|
|
|
throw new Exception('Could not get commits, failed API request.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$results = array();
|
|
|
|
$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] = array('status' => 'ignored', 'message' => 'not branch head');
|
|
|
|
continue;
|
|
|
|
}
|
2015-03-10 21:20:54 +01:00
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
try {
|
2014-05-12 16:53:29 +02:00
|
|
|
$branch = str_replace('refs/heads/', '', $payload['pull_request']['base']['ref']);
|
|
|
|
$committer = $commit['commit']['author']['email'];
|
|
|
|
$message = $commit['commit']['message'];
|
2014-05-12 15:46:45 +02:00
|
|
|
|
2015-02-26 19:59:49 +01:00
|
|
|
$remoteUrlKey = $payload['pull_request']['head']['repo']['private'] ? 'ssh_url' : 'clone_url';
|
|
|
|
|
2014-05-12 15:46:45 +02:00
|
|
|
$extra = array(
|
|
|
|
'build_type' => 'pull_request',
|
|
|
|
'pull_request_id' => $payload['pull_request']['id'],
|
|
|
|
'pull_request_number' => $payload['number'],
|
|
|
|
'remote_branch' => $payload['pull_request']['head']['ref'],
|
2015-02-26 19:59:49 +01:00
|
|
|
'remote_url' => $payload['pull_request']['head']['repo'][$remoteUrlKey],
|
2014-05-12 15:46:45 +02:00
|
|
|
);
|
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
$results[$id] = $this->createBuild($project, $id, $branch, $committer, $message, $extra);
|
|
|
|
$status = 'ok';
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
$results[$id] = array('status' => 'failed', 'error' => $ex->getMessage());
|
2014-05-12 15:46:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
return array('status' => $status, 'commits' => $results);
|
2014-05-12 15:46:45 +02:00
|
|
|
}
|
|
|
|
|
2014-02-24 16:30:44 +01:00
|
|
|
/**
|
|
|
|
* Called by Gitlab Webhooks:
|
|
|
|
*/
|
2015-03-25 15:15:53 +01:00
|
|
|
public function gitlab($projectId)
|
2014-02-24 16:30:44 +01:00
|
|
|
{
|
2015-03-25 15:15:53 +01:00
|
|
|
$project = $this->fetchProject($projectId, 'gitlab');
|
2015-02-12 13:37:56 +01:00
|
|
|
|
2014-02-25 10:47:09 +01:00
|
|
|
$payloadString = file_get_contents("php://input");
|
|
|
|
$payload = json_decode($payloadString, true);
|
2014-02-24 16:30:44 +01:00
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
// 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'];
|
2014-12-11 14:56:50 +01:00
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
return $this->createBuild($project, $commit['id'], $branch, $committer, $commit['message']);
|
2014-12-11 14:56:50 +01:00
|
|
|
}
|
2015-03-25 15:15:53 +01:00
|
|
|
}
|
2014-12-11 14:56:50 +01:00
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
// 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:
|
2014-02-25 10:09:38 +01:00
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
$results = array();
|
|
|
|
$status = 'failed';
|
|
|
|
foreach ($payload['commits'] as $commit) {
|
|
|
|
try {
|
2014-05-12 16:53:29 +02:00
|
|
|
$branch = str_replace('refs/heads/', '', $payload['ref']);
|
|
|
|
$committer = $commit['author']['email'];
|
2015-03-25 15:15:53 +01:00
|
|
|
$results[$commit['id']] = $this->createBuild(
|
|
|
|
$project,
|
|
|
|
$commit['id'],
|
|
|
|
$branch,
|
|
|
|
$committer,
|
|
|
|
$commit['message']
|
|
|
|
);
|
|
|
|
$status = 'ok';
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
$results[$commit['id']] = array('status' => 'failed', 'error' => $ex->getMessage());
|
2014-02-25 10:09:38 +01:00
|
|
|
}
|
|
|
|
}
|
2015-03-25 15:15:53 +01:00
|
|
|
return array('status' => $status, 'commits' => $results);
|
2014-02-24 16:30:44 +01:00
|
|
|
}
|
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
return array('status' => 'ignored', 'message' => 'Unusable payload.');
|
2014-02-24 16:30:44 +01:00
|
|
|
}
|
2014-05-12 16:53:29 +02:00
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Wrapper for creating a new build.
|
2015-03-25 15:15:53 +01:00
|
|
|
*
|
|
|
|
* @param Project $project
|
|
|
|
* @param string $commitId
|
|
|
|
* @param string $branch
|
|
|
|
* @param string $committer
|
|
|
|
* @param string $commitMessage
|
|
|
|
* @param array $extra
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @throws Exception
|
2014-12-08 12:25:33 +01:00
|
|
|
*/
|
2015-03-25 15:15:53 +01:00
|
|
|
protected function createBuild(
|
|
|
|
Project $project,
|
|
|
|
$commitId,
|
|
|
|
$branch,
|
|
|
|
$committer,
|
|
|
|
$commitMessage,
|
|
|
|
array $extra = null
|
|
|
|
) {
|
2014-05-12 16:53:29 +02:00
|
|
|
// Check if a build already exists for this commit ID:
|
2015-03-25 15:15:53 +01:00
|
|
|
$builds = $this->buildStore->getByProjectAndCommit($project->getId(), $commitId);
|
2014-05-12 16:53:29 +02:00
|
|
|
|
|
|
|
if ($builds['count']) {
|
2015-03-25 15:15:53 +01:00
|
|
|
return array(
|
|
|
|
'status' => 'ignored',
|
|
|
|
'message' => sprintf('Duplicate of build #%d', $builds['items'][0]->getId())
|
|
|
|
);
|
2014-05-12 17:06:58 +02:00
|
|
|
}
|
2014-05-12 17:28:48 +02:00
|
|
|
|
2014-07-14 17:08:19 +02:00
|
|
|
// If not, create a new build job for it:
|
|
|
|
$build = $this->buildService->createBuild($project, $commitId, $branch, $committer, $commitMessage, $extra);
|
|
|
|
$build = BuildFactory::getBuild($build);
|
2014-05-12 16:53:29 +02:00
|
|
|
|
|
|
|
// Send a status postback if the build type provides one:
|
|
|
|
$build->sendStatusPostback();
|
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
return array('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;
|
2014-05-12 16:53:29 +02:00
|
|
|
}
|
2014-02-24 16:30:44 +01:00
|
|
|
}
|