2014-02-24 16:30:44 +01:00
|
|
|
<?php
|
2016-12-30 17:40:14 +01:00
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
namespace PHPCensor\Controller;
|
2014-02-24 16:30:44 +01:00
|
|
|
|
|
|
|
use b8;
|
|
|
|
use b8\Store;
|
2015-03-25 15:15:53 +01:00
|
|
|
use Exception;
|
2017-06-21 17:26:15 +02:00
|
|
|
use GuzzleHttp\Client;
|
2017-01-15 16:35:42 +01:00
|
|
|
use PHPCensor\Helper\Lang;
|
2017-03-23 13:53:24 +01:00
|
|
|
use PHPCensor\Model\Build;
|
2016-07-19 20:28:11 +02:00
|
|
|
use PHPCensor\Model\Project;
|
|
|
|
use PHPCensor\Service\BuildService;
|
|
|
|
use PHPCensor\Store\BuildStore;
|
|
|
|
use PHPCensor\Store\ProjectStore;
|
2016-05-09 08:20:26 +02:00
|
|
|
use b8\Controller;
|
|
|
|
use b8\Config;
|
2017-01-15 16:35:42 +01:00
|
|
|
use b8\Exception\HttpException\NotFoundException;
|
2017-12-26 08:44:45 +01:00
|
|
|
use b8\Store\Factory;
|
2014-02-24 16:30:44 +01:00
|
|
|
|
|
|
|
/**
|
2017-02-06 13:38:09 +01:00
|
|
|
* Webhook Controller - Processes webhook pings from BitBucket, Github, Gitlab, Gogs, etc.
|
2015-03-25 15:15:53 +01:00
|
|
|
*
|
2017-02-06 13:54:44 +01:00
|
|
|
* @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>
|
2016-04-27 16:45:24 +02:00
|
|
|
*
|
2014-02-24 16:30:44 +01:00
|
|
|
*/
|
2016-05-09 08:20:26 +02:00
|
|
|
class WebhookController extends 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);
|
2016-04-20 17:39:48 +02:00
|
|
|
$response->setContent(['status' => 'failed', 'error' => $ex->getMessage()]);
|
2015-03-25 15:15:53 +01:00
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
2015-02-12 13:37:56 +01:00
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
/**
|
2015-07-24 12:36:02 +02:00
|
|
|
* Called by Bitbucket.
|
2015-03-25 15:15:53 +01:00
|
|
|
*/
|
|
|
|
public function bitbucket($projectId)
|
|
|
|
{
|
2017-03-19 05:17:10 +01:00
|
|
|
$project = $this->fetchProject($projectId, ['bitbucket', 'bitbuckethg', 'remote']);
|
2017-02-06 13:38:09 +01:00
|
|
|
|
2015-10-23 22:55:04 +02:00
|
|
|
// Support both old services and new webhooks
|
2015-07-24 12:36:02 +02:00
|
|
|
if ($payload = $this->getParam('payload')) {
|
|
|
|
return $this->bitbucketService(json_decode($payload, true), $project);
|
|
|
|
}
|
2015-10-23 22:55:04 +02:00
|
|
|
|
2015-07-24 12:36:02 +02:00
|
|
|
$payload = json_decode(file_get_contents("php://input"), true);
|
2015-10-23 22:55:04 +02:00
|
|
|
|
2017-08-20 10:06:51 +02:00
|
|
|
// Handle Pull Request webhooks:
|
|
|
|
if (!empty($payload['pullrequest'])) {
|
|
|
|
return $this->bitbucketPullRequest($project, $payload);
|
2015-07-24 12:36:02 +02:00
|
|
|
}
|
2015-10-23 22:55:04 +02:00
|
|
|
|
2017-08-20 10:06:51 +02:00
|
|
|
// Handle Push (and Tag) webhooks:
|
|
|
|
if (!empty($payload['push']['changes'])) {
|
|
|
|
return $this->bitbucketCommitRequest($project, $payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invalid event from bitbucket
|
|
|
|
return [
|
|
|
|
'status' => 'failed',
|
|
|
|
'commits' => []
|
|
|
|
];
|
2015-07-24 12:36:02 +02:00
|
|
|
}
|
2015-10-23 22:55:04 +02:00
|
|
|
|
2015-07-24 12:36:02 +02:00
|
|
|
/**
|
2017-08-20 10:06:51 +02:00
|
|
|
* Handle the payload when Bitbucket sends a commit webhook.
|
|
|
|
*
|
|
|
|
* @param Project $project
|
|
|
|
* @param array $payload
|
|
|
|
*
|
|
|
|
* @return array
|
2015-07-24 12:36:02 +02:00
|
|
|
*/
|
2017-08-20 10:06:51 +02:00
|
|
|
protected function bitbucketCommitRequest(Project $project, array $payload)
|
2015-07-24 12:36:02 +02:00
|
|
|
{
|
2016-08-25 18:13:19 +02:00
|
|
|
$results = [];
|
|
|
|
$status = 'failed';
|
2015-07-24 12:36:02 +02:00
|
|
|
foreach ($payload['push']['changes'] as $commit) {
|
|
|
|
try {
|
|
|
|
$email = $commit['new']['target']['author']['raw'];
|
2017-03-19 04:13:58 +01:00
|
|
|
if (strpos($email, '>') !== false) {
|
|
|
|
// In order not to loose email if it is RAW, w/o "<>" symbols
|
|
|
|
$email = substr($email, 0, strpos($email, '>'));
|
|
|
|
$email = substr($email, strpos($email, '<') + 1);
|
|
|
|
}
|
2015-07-24 12:36:02 +02:00
|
|
|
|
|
|
|
$results[$commit['new']['target']['hash']] = $this->createBuild(
|
2017-12-27 17:05:04 +01:00
|
|
|
Build::SOURCE_WEBHOOK,
|
2015-07-24 12:36:02 +02:00
|
|
|
$project,
|
|
|
|
$commit['new']['target']['hash'],
|
|
|
|
$commit['new']['name'],
|
2017-04-22 15:02:24 +02:00
|
|
|
null,
|
2015-07-24 12:36:02 +02:00
|
|
|
$email,
|
|
|
|
$commit['new']['target']['message']
|
|
|
|
);
|
|
|
|
$status = 'ok';
|
|
|
|
} catch (Exception $ex) {
|
2016-08-25 18:13:19 +02:00
|
|
|
$results[$commit['new']['target']['hash']] = ['status' => 'failed', 'error' => $ex->getMessage()];
|
2015-07-24 12:36:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-25 18:13:19 +02:00
|
|
|
return ['status' => $status, 'commits' => $results];
|
2015-07-24 12:36:02 +02:00
|
|
|
}
|
2015-10-23 22:55:04 +02:00
|
|
|
|
2017-08-20 10:06:51 +02:00
|
|
|
/**
|
|
|
|
* Handle the payload when Bitbucket sends a Pull Request webhook.
|
|
|
|
*
|
|
|
|
* @param Project $project
|
|
|
|
* @param array $payload
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
protected function bitbucketPullRequest(Project $project, array $payload)
|
|
|
|
{
|
|
|
|
// We only want to know about open pull requests:
|
|
|
|
if (!in_array($_SERVER['HTTP_X_EVENT_KEY'], ['pullrequest:created', 'pullrequest:updated'])) {
|
|
|
|
return ['status' => 'ok'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$headers = [];
|
|
|
|
$username = Config::getInstance()->get('php-censor.bitbucket.username');
|
|
|
|
$appPassword = Config::getInstance()->get('php-censor.bitbucket.app_password');
|
|
|
|
|
|
|
|
if (empty($username) || empty($appPassword)) {
|
|
|
|
throw new Exception('Please provide Username and App Password of your Bitbucket account.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$commitsUrl = $payload['pullrequest']['links']['commits']['href'];
|
|
|
|
|
|
|
|
$client = new Client();
|
|
|
|
$commitsResponse = $client->get($commitsUrl, [
|
|
|
|
'auth' => [$username, $appPassword],
|
|
|
|
]);
|
|
|
|
$httpStatus = (integer)$commitsResponse->getStatusCode();
|
|
|
|
|
|
|
|
// Check we got a success response:
|
|
|
|
if ($httpStatus < 200 || $httpStatus >= 300) {
|
|
|
|
throw new Exception('Could not get commits, failed API request.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$results = [];
|
|
|
|
$status = 'failed';
|
|
|
|
$commits = json_decode($commitsResponse->getBody(), true)['values'];
|
|
|
|
foreach ($commits as $commit) {
|
|
|
|
// Skip all but the current HEAD commit ID:
|
|
|
|
$id = $commit['hash'];
|
|
|
|
if (strpos($id, $payload['pullrequest']['source']['commit']['hash']) !== 0) {
|
|
|
|
$results[$id] = ['status' => 'ignored', 'message' => 'not branch head'];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$branch = $payload['pullrequest']['destination']['branch']['name'];
|
|
|
|
$committer = $commit['author']['raw'];
|
|
|
|
if (strpos($committer, '>') !== false) {
|
|
|
|
// In order not to loose email if it is RAW, w/o "<>" symbols
|
|
|
|
$committer = substr($committer, 0, strpos($committer, '>'));
|
|
|
|
$committer = substr($committer, strpos($committer, '<') + 1);
|
|
|
|
}
|
|
|
|
$message = $commit['message'];
|
|
|
|
|
|
|
|
$extra = [
|
|
|
|
'build_type' => 'pull_request',
|
|
|
|
'pull_request_number' => $payload['pullrequest']['id'],
|
|
|
|
'remote_branch' => $payload['pullrequest']['source']['branch']['name'],
|
|
|
|
'remote_reference' => $payload['pullrequest']['source']['repository']['full_name'],
|
|
|
|
];
|
|
|
|
|
2017-12-27 17:05:04 +01:00
|
|
|
$results[$id] = $this->createBuild(
|
|
|
|
Build::SOURCE_WEBHOOK_PULL_REQUEST,
|
|
|
|
$project,
|
|
|
|
$id,
|
|
|
|
$branch,
|
|
|
|
null,
|
|
|
|
$committer,
|
|
|
|
$message,
|
|
|
|
$extra
|
|
|
|
);
|
2017-08-20 10:06:51 +02:00
|
|
|
$status = 'ok';
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
$results[$id] = ['status' => 'failed', 'error' => $ex->getMessage()];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ['status' => $status, 'commits' => $results];
|
|
|
|
}
|
|
|
|
|
2015-07-24 12:36:02 +02:00
|
|
|
/**
|
|
|
|
* Bitbucket POST service.
|
|
|
|
*/
|
|
|
|
protected function bitbucketService($payload, $project)
|
|
|
|
{
|
2014-12-01 18:30:44 +01:00
|
|
|
$payload = json_decode($this->getParam('payload'), true);
|
2014-02-24 16:30:44 +01:00
|
|
|
|
2016-04-20 17:39:48 +02:00
|
|
|
$results = [];
|
|
|
|
$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(
|
2017-12-27 17:05:04 +01:00
|
|
|
Build::SOURCE_WEBHOOK,
|
2015-03-25 15:15:53 +01:00
|
|
|
$project,
|
|
|
|
$commit['raw_node'],
|
|
|
|
$commit['branch'],
|
2017-04-22 15:02:24 +02:00
|
|
|
null,
|
2015-03-25 15:15:53 +01:00
|
|
|
$email,
|
|
|
|
$commit['message']
|
|
|
|
);
|
|
|
|
$status = 'ok';
|
|
|
|
} catch (Exception $ex) {
|
2016-04-20 17:39:48 +02:00
|
|
|
$results[$commit['raw_node']] = ['status' => 'failed', 'error' => $ex->getMessage()];
|
2014-02-24 16:30:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-20 17:39:48 +02:00
|
|
|
return ['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
|
2017-11-05 15:48:36 +01:00
|
|
|
*
|
2016-05-09 08:20:26 +02:00
|
|
|
* @return array
|
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
|
|
|
{
|
2017-12-27 17:05:04 +01:00
|
|
|
$project = $this->fetchProject($projectId, ['local', 'remote']);
|
|
|
|
$branch = $this->getParam('branch', $project->getBranch());
|
|
|
|
$commit = $this->getParam('commit');
|
2014-08-14 13:15:22 +02:00
|
|
|
$commitMessage = $this->getParam('message');
|
2017-12-27 17:05:04 +01:00
|
|
|
$committer = $this->getParam('committer');
|
|
|
|
|
|
|
|
return $this->createBuild(
|
|
|
|
Build::SOURCE_WEBHOOK,
|
|
|
|
$project,
|
|
|
|
$commit,
|
|
|
|
$branch,
|
|
|
|
null,
|
|
|
|
$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
|
|
|
{
|
2017-02-07 13:50:09 +01:00
|
|
|
$project = $this->fetchProject($projectId, ['github', 'remote']);
|
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:
|
2017-04-20 14:35:26 +02:00
|
|
|
return [
|
|
|
|
'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
|
|
|
|
2017-04-20 15:24:53 +02:00
|
|
|
// Handle Pull Request webhooks:
|
2014-05-12 15:46:45 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-20 15:24:53 +02:00
|
|
|
// Handle Push (and Tag) webhooks:
|
2017-04-20 14:35:26 +02:00
|
|
|
if (array_key_exists('head_commit', $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
|
|
|
|
2016-04-20 17:39:48 +02:00
|
|
|
return ['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-03-25 15:15:53 +01:00
|
|
|
*
|
2017-02-06 13:54:44 +01:00
|
|
|
* @return array
|
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
|
|
|
{
|
2017-04-20 15:24:53 +02:00
|
|
|
// 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'
|
|
|
|
) {
|
2016-04-20 17:39:48 +02:00
|
|
|
return ['status' => 'ignored'];
|
2014-02-24 16:30:44 +01:00
|
|
|
}
|
|
|
|
|
2017-04-20 14:35:26 +02:00
|
|
|
if (isset($payload['head_commit']) && $payload['head_commit']) {
|
2017-04-20 15:24:53 +02:00
|
|
|
$isTag = (substr($payload['ref'], 0, 10) == 'refs/tags/') ? true : false;
|
|
|
|
$commit = $payload['head_commit'];
|
2016-04-20 17:39:48 +02:00
|
|
|
$results = [];
|
|
|
|
$status = 'failed';
|
2017-12-27 17:05:04 +01:00
|
|
|
|
2017-04-20 15:24:53 +02:00
|
|
|
if (!$commit['distinct']) {
|
|
|
|
$results[$commit['id']] = ['status' => 'ignored'];
|
|
|
|
} else {
|
|
|
|
try {
|
2017-04-22 15:02:24 +02:00
|
|
|
$tag = null;
|
2017-04-20 15:24:53 +02:00
|
|
|
if ($isTag) {
|
2017-04-22 15:02:24 +02:00
|
|
|
$tag = str_replace('refs/tags/', '', $payload['ref']);
|
2017-04-20 16:14:48 +02:00
|
|
|
$branch = str_replace('refs/heads/', '', $payload['base_ref']);
|
2017-04-20 15:24:53 +02:00
|
|
|
$committer = $payload['pusher']['email'];
|
|
|
|
} else {
|
|
|
|
$branch = str_replace('refs/heads/', '', $payload['ref']);
|
|
|
|
$committer = $commit['committer']['email'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$results[$commit['id']] = $this->createBuild(
|
2017-12-27 17:05:04 +01:00
|
|
|
Build::SOURCE_WEBHOOK,
|
2017-04-20 15:24:53 +02:00
|
|
|
$project,
|
|
|
|
$commit['id'],
|
|
|
|
$branch,
|
2017-04-22 15:02:24 +02:00
|
|
|
$tag,
|
2017-04-20 15:24:53 +02:00
|
|
|
$committer,
|
|
|
|
$commit['message']
|
|
|
|
);
|
|
|
|
|
|
|
|
$status = 'ok';
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
$results[$commit['id']] = ['status' => 'failed', 'error' => $ex->getMessage()];
|
|
|
|
}
|
2014-02-24 16:30:44 +01:00
|
|
|
}
|
2017-04-20 14:35:26 +02:00
|
|
|
|
2016-04-20 17:39:48 +02:00
|
|
|
return ['status' => $status, 'commits' => $results];
|
2015-03-25 15:15:53 +01:00
|
|
|
}
|
2014-02-24 16:30:44 +01:00
|
|
|
|
2016-04-20 17:39:48 +02:00
|
|
|
return ['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.
|
2017-11-05 15:48:36 +01:00
|
|
|
*
|
2015-03-25 15:15:53 +01:00
|
|
|
* @param Project $project
|
2016-05-09 08:20:26 +02:00
|
|
|
* @param array $payload
|
2017-11-05 15:48:36 +01:00
|
|
|
*
|
2016-05-09 08:20:26 +02:00
|
|
|
* @return array
|
2017-11-05 15:48:36 +01:00
|
|
|
*
|
2016-05-09 08:20:26 +02:00
|
|
|
* @throws Exception
|
2014-12-08 12:25:33 +01:00
|
|
|
*/
|
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:
|
2016-04-20 17:39:48 +02:00
|
|
|
if (!in_array($payload['action'], ['opened', 'synchronize', 'reopened'])) {
|
|
|
|
return ['status' => 'ok'];
|
2014-05-12 15:46:45 +02:00
|
|
|
}
|
|
|
|
|
2016-04-20 17:39:48 +02:00
|
|
|
$headers = [];
|
2016-07-21 19:02:11 +02:00
|
|
|
$token = Config::getInstance()->get('php-censor.github.token');
|
2014-05-12 15:46:45 +02:00
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
if (!empty($token)) {
|
2017-06-21 17:26:15 +02:00
|
|
|
$headers['Authorization'] = 'token ' . $token;
|
2015-03-25 15:15:53 +01:00
|
|
|
}
|
2014-05-12 15:46:45 +02:00
|
|
|
|
2017-06-21 17:26:15 +02:00
|
|
|
$url = $payload['pull_request']['commits_url'];
|
2016-04-25 19:30:23 +02:00
|
|
|
|
|
|
|
//for large pull requests, allow grabbing more then the default number of commits
|
2016-07-21 19:02:11 +02:00
|
|
|
$custom_per_page = Config::getInstance()->get('php-censor.github.per_page');
|
2016-04-25 19:30:23 +02:00
|
|
|
$params = [];
|
|
|
|
if ($custom_per_page) {
|
2017-06-21 17:26:15 +02:00
|
|
|
$params['per_page'] = $custom_per_page;
|
2016-04-25 19:30:23 +02:00
|
|
|
}
|
2017-06-21 17:26:15 +02:00
|
|
|
|
|
|
|
$client = new Client();
|
|
|
|
$response = $client->get($url, [
|
|
|
|
'headers' => $headers,
|
|
|
|
'query' => $params,
|
|
|
|
]);
|
|
|
|
$status = (integer)$response->getStatusCode();
|
2014-05-12 15:58:30 +02:00
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
// Check we got a success response:
|
2017-06-21 17:26:15 +02:00
|
|
|
if ($status < 200 || $status >= 300) {
|
2015-03-25 15:15:53 +01:00
|
|
|
throw new Exception('Could not get commits, failed API request.');
|
|
|
|
}
|
|
|
|
|
2016-04-20 17:39:48 +02:00
|
|
|
$results = [];
|
|
|
|
$status = 'failed';
|
2017-06-21 17:26:15 +02:00
|
|
|
$commits = json_decode($response->getBody(), true);
|
|
|
|
foreach ($commits as $commit) {
|
2015-03-25 15:15:53 +01:00
|
|
|
// Skip all but the current HEAD commit ID:
|
|
|
|
$id = $commit['sha'];
|
|
|
|
if ($id != $payload['pull_request']['head']['sha']) {
|
2016-04-20 17:39:48 +02:00
|
|
|
$results[$id] = ['status' => 'ignored', 'message' => 'not branch head'];
|
2015-03-25 15:15:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-03-10 21:20:54 +01:00
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
try {
|
2016-04-20 17:39:48 +02:00
|
|
|
$branch = str_replace('refs/heads/', '', $payload['pull_request']['base']['ref']);
|
2014-05-12 16:53:29 +02:00
|
|
|
$committer = $commit['commit']['author']['email'];
|
2016-04-20 17:39:48 +02:00
|
|
|
$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';
|
|
|
|
|
2016-04-20 17:39:48 +02:00
|
|
|
$extra = [
|
|
|
|
'build_type' => 'pull_request',
|
|
|
|
'pull_request_id' => $payload['pull_request']['id'],
|
2014-05-12 15:46:45 +02:00
|
|
|
'pull_request_number' => $payload['number'],
|
2016-04-20 17:39:48 +02:00
|
|
|
'remote_branch' => $payload['pull_request']['head']['ref'],
|
|
|
|
'remote_url' => $payload['pull_request']['head']['repo'][$remoteUrlKey],
|
|
|
|
];
|
2014-05-12 15:46:45 +02:00
|
|
|
|
2017-12-27 17:05:04 +01:00
|
|
|
$results[$id] = $this->createBuild(
|
|
|
|
Build::SOURCE_WEBHOOK_PULL_REQUEST,
|
|
|
|
$project,
|
|
|
|
$id,
|
|
|
|
$branch,
|
|
|
|
null,
|
|
|
|
$committer,
|
|
|
|
$message,
|
|
|
|
$extra
|
|
|
|
);
|
2015-03-25 15:15:53 +01:00
|
|
|
$status = 'ok';
|
|
|
|
} catch (Exception $ex) {
|
2016-04-20 17:39:48 +02:00
|
|
|
$results[$id] = ['status' => 'failed', 'error' => $ex->getMessage()];
|
2014-05-12 15:46:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-20 17:39:48 +02:00
|
|
|
return ['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
|
|
|
{
|
2017-02-07 13:50:09 +01:00
|
|
|
$project = $this->fetchProject($projectId, ['gitlab', 'remote']);
|
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') {
|
2017-12-27 17:05:04 +01:00
|
|
|
$branch = $attributes['source_branch'];
|
|
|
|
$commit = $attributes['last_commit'];
|
2015-03-25 15:15:53 +01:00
|
|
|
$committer = $commit['author']['email'];
|
2014-12-11 14:56:50 +01:00
|
|
|
|
2017-12-27 17:05:04 +01:00
|
|
|
return $this->createBuild(
|
|
|
|
Build::SOURCE_WEBHOOK_PULL_REQUEST,
|
|
|
|
$project,
|
|
|
|
$commit['id'],
|
|
|
|
$branch,
|
|
|
|
null,
|
|
|
|
$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
|
|
|
|
2016-04-20 17:39:48 +02:00
|
|
|
$results = [];
|
|
|
|
$status = 'failed';
|
2015-03-25 15:15:53 +01:00
|
|
|
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(
|
2017-12-27 17:05:04 +01:00
|
|
|
Build::SOURCE_WEBHOOK,
|
2015-03-25 15:15:53 +01:00
|
|
|
$project,
|
|
|
|
$commit['id'],
|
|
|
|
$branch,
|
2017-04-22 15:02:24 +02:00
|
|
|
null,
|
2015-03-25 15:15:53 +01:00
|
|
|
$committer,
|
|
|
|
$commit['message']
|
|
|
|
);
|
|
|
|
$status = 'ok';
|
|
|
|
} catch (Exception $ex) {
|
2016-04-20 17:39:48 +02:00
|
|
|
$results[$commit['id']] = ['status' => 'failed', 'error' => $ex->getMessage()];
|
2014-02-25 10:09:38 +01:00
|
|
|
}
|
|
|
|
}
|
2016-04-20 17:39:48 +02:00
|
|
|
return ['status' => $status, 'commits' => $results];
|
2014-02-24 16:30:44 +01:00
|
|
|
}
|
|
|
|
|
2016-04-20 17:39:48 +02:00
|
|
|
return ['status' => 'ignored', 'message' => 'Unusable payload.'];
|
2014-02-24 16:30:44 +01:00
|
|
|
}
|
2014-05-12 16:53:29 +02:00
|
|
|
|
2016-12-24 10:54:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by POSTing to /webhook/svn/<project_id>?branch=<branch>&commit=<commit>
|
|
|
|
*
|
|
|
|
* @author Sylvain Lévesque <slevesque@gezere.com>
|
2017-11-05 15:48:36 +01:00
|
|
|
*
|
2016-12-24 10:54:39 +01:00
|
|
|
* @param string $projectId
|
2017-11-05 15:48:36 +01:00
|
|
|
*
|
2016-12-24 10:54:39 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function svn($projectId)
|
|
|
|
{
|
|
|
|
$project = $this->fetchProject($projectId, 'svn');
|
|
|
|
$branch = $this->getParam('branch', $project->getBranch());
|
|
|
|
$commit = $this->getParam('commit');
|
|
|
|
$commitMessage = $this->getParam('message');
|
|
|
|
$committer = $this->getParam('committer');
|
|
|
|
|
2017-12-27 17:05:04 +01:00
|
|
|
return $this->createBuild(
|
|
|
|
Build::SOURCE_WEBHOOK,
|
|
|
|
$project,
|
|
|
|
$commit,
|
|
|
|
$branch,
|
|
|
|
null,
|
|
|
|
$committer,
|
|
|
|
$commitMessage
|
|
|
|
);
|
2016-12-24 10:54:39 +01:00
|
|
|
}
|
|
|
|
|
2017-02-06 13:38:09 +01:00
|
|
|
/**
|
|
|
|
* Called by Gogs Webhooks:
|
2017-11-05 15:48:36 +01:00
|
|
|
*
|
2017-02-06 13:54:44 +01:00
|
|
|
* @param string $projectId
|
2017-11-05 15:48:36 +01:00
|
|
|
*
|
2017-02-06 13:54:44 +01:00
|
|
|
* @return array
|
2017-02-06 13:38:09 +01:00
|
|
|
*/
|
|
|
|
public function gogs($projectId)
|
|
|
|
{
|
2017-02-07 13:50:09 +01:00
|
|
|
$project = $this->fetchProject($projectId, ['gogs', 'remote']);
|
2017-02-06 13:38:09 +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:
|
2017-02-06 13:54:44 +01:00
|
|
|
return ['status' => 'failed', 'error' => 'Content type not supported.', 'responseCode' => 401];
|
2017-02-06 13:38:09 +01:00
|
|
|
}
|
2017-02-06 13:54:44 +01:00
|
|
|
|
2017-02-06 13:38:09 +01:00
|
|
|
// Handle Push web hooks:
|
|
|
|
if (array_key_exists('commits', $payload)) {
|
|
|
|
return $this->gogsCommitRequest($project, $payload);
|
|
|
|
}
|
2017-02-06 13:54:44 +01:00
|
|
|
|
2017-12-26 08:44:45 +01:00
|
|
|
if (array_key_exists('pull_request', $payload)) {
|
|
|
|
return $this->gogsPullRequest($project, $payload);
|
|
|
|
}
|
|
|
|
|
2017-02-06 13:54:44 +01:00
|
|
|
return ['status' => 'ignored', 'message' => 'Unusable payload.'];
|
2017-02-06 13:38:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the payload when Gogs sends a commit webhook.
|
|
|
|
*
|
|
|
|
* @param Project $project
|
2017-02-06 13:54:44 +01:00
|
|
|
* @param array $payload
|
2017-02-06 13:38:09 +01:00
|
|
|
*
|
2017-02-06 13:54:44 +01:00
|
|
|
* @return array
|
2017-02-06 13:38:09 +01:00
|
|
|
*/
|
|
|
|
protected function gogsCommitRequest(Project $project, array $payload)
|
|
|
|
{
|
|
|
|
if (isset($payload['commits']) && is_array($payload['commits'])) {
|
|
|
|
// If we have a list of commits, then add them all as builds to be tested:
|
2017-02-06 13:54:44 +01:00
|
|
|
$results = [];
|
|
|
|
$status = 'failed';
|
2017-02-06 13:38:09 +01:00
|
|
|
foreach ($payload['commits'] as $commit) {
|
|
|
|
try {
|
|
|
|
$branch = str_replace('refs/heads/', '', $payload['ref']);
|
|
|
|
$committer = $commit['author']['email'];
|
|
|
|
$results[$commit['id']] = $this->createBuild(
|
2017-12-27 17:05:04 +01:00
|
|
|
Build::SOURCE_WEBHOOK,
|
2017-02-06 13:38:09 +01:00
|
|
|
$project,
|
|
|
|
$commit['id'],
|
|
|
|
$branch,
|
2017-04-22 15:02:24 +02:00
|
|
|
null,
|
2017-02-06 13:38:09 +01:00
|
|
|
$committer,
|
|
|
|
$commit['message']
|
|
|
|
);
|
|
|
|
$status = 'ok';
|
|
|
|
} catch (Exception $ex) {
|
2017-02-06 13:54:44 +01:00
|
|
|
$results[$commit['id']] = ['status' => 'failed', 'error' => $ex->getMessage()];
|
2017-02-06 13:38:09 +01:00
|
|
|
}
|
|
|
|
}
|
2017-02-06 13:54:44 +01:00
|
|
|
|
|
|
|
return ['status' => $status, 'commits' => $results];
|
2017-02-06 13:38:09 +01:00
|
|
|
}
|
2017-02-06 13:54:44 +01:00
|
|
|
|
|
|
|
return ['status' => 'ignored', 'message' => 'Unusable payload.'];
|
2017-02-06 13:38:09 +01:00
|
|
|
}
|
|
|
|
|
2017-12-26 08:44:45 +01:00
|
|
|
/**
|
|
|
|
* Handle the payload when Gogs sends a pull request webhook.
|
|
|
|
*
|
|
|
|
* @param Project $project
|
|
|
|
* @param array $payload
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function gogsPullRequest(Project $project, array $payload)
|
|
|
|
{
|
|
|
|
$pull_request = $payload['pull_request'];
|
|
|
|
$head_branch = $pull_request['head_branch'];
|
|
|
|
|
|
|
|
$action = $payload['action'];
|
|
|
|
$active_actions = ['opened', 'reopened', 'label_updated', 'label_cleared'];
|
|
|
|
$inactive_actions = ['closed'];
|
|
|
|
|
|
|
|
$state = $pull_request['state'];
|
|
|
|
$active_states = ['open'];
|
|
|
|
$inactive_states = ['closed'];
|
|
|
|
|
|
|
|
if (!in_array($action, $active_actions) and !in_array($action, $inactive_actions)) {
|
|
|
|
return ['status' => 'ignored', 'message' => 'Action ' . $action . ' ignored'];
|
|
|
|
}
|
|
|
|
if (!in_array($state, $active_states) and !in_array($state, $inactive_states)) {
|
|
|
|
return ['status' => 'ignored', 'message' => 'State ' . $state . ' ignored'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$envs = [];
|
|
|
|
|
|
|
|
// Get environment form labels
|
|
|
|
if (in_array($action, $active_actions) and in_array($state, $active_states)) {
|
|
|
|
if (isset($pull_request['labels']) && is_array($pull_request['labels'])) {
|
|
|
|
foreach ($pull_request['labels'] as $label) {
|
|
|
|
if (strpos($label['name'], 'env:') === 0) {
|
|
|
|
$envs[] = substr($label['name'], 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$envs_updated = [];
|
|
|
|
$env_objs = $project->getEnvironmentsObjects();
|
|
|
|
$store = Factory::getStore('Environment', 'PHPCensor');;
|
|
|
|
foreach ($env_objs['items'] as $environment) {
|
|
|
|
$branches = $environment->getBranches();
|
|
|
|
if (in_array($environment->getName(), $envs)) {
|
|
|
|
if (!in_array($head_branch, $branches)) {
|
|
|
|
// Add branch to environment
|
|
|
|
$branches[] = $head_branch;
|
|
|
|
$environment->setBranches($branches);
|
|
|
|
$store->save($environment);
|
|
|
|
$envs_updated[] = $environment->getName();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (in_array($head_branch, $branches)) {
|
|
|
|
// Remove branch from environment
|
|
|
|
$branches = array_diff($branches, [$head_branch]);
|
|
|
|
$environment->setBranches($branches);
|
|
|
|
$store->save($environment);
|
|
|
|
$envs_updated[] = $environment->getName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($state == 'closed') and $pull_request['merged']) {
|
|
|
|
// update base branch enviroments
|
|
|
|
$environment_names = $project->getEnvironmentsNamesByBranch($pull_request['base_branch']);
|
|
|
|
$envs_updated = array_merge($envs_updated, $environment_names);
|
|
|
|
}
|
|
|
|
|
|
|
|
$envs_updated = array_unique($envs_updated);
|
|
|
|
if (!empty($envs_updated)) {
|
|
|
|
foreach ($envs_updated as $environment_name) {
|
|
|
|
$this->buildService->createBuild(
|
|
|
|
$project,
|
|
|
|
$environment_name,
|
|
|
|
'',
|
|
|
|
$project->getBranch(),
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
Build::SOURCE_WEBHOOK,
|
|
|
|
0,
|
|
|
|
null
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ['status' => 'ok', 'message' => 'Branch environments updated ' . join(', ', $envs_updated)];
|
|
|
|
}
|
|
|
|
|
|
|
|
return ['status' => 'ignored', 'message' => 'Branch environments not changed'];
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Wrapper for creating a new build.
|
2015-03-25 15:15:53 +01:00
|
|
|
*
|
2017-12-27 17:05:04 +01:00
|
|
|
* @param integer $source
|
2015-03-25 15:15:53 +01:00
|
|
|
* @param Project $project
|
2017-04-22 15:02:24 +02:00
|
|
|
* @param string $commitId
|
|
|
|
* @param string $branch
|
|
|
|
* @param string $tag
|
|
|
|
* @param string $committer
|
|
|
|
* @param string $commitMessage
|
|
|
|
* @param array $extra
|
2015-03-25 15:15:53 +01:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @throws Exception
|
2014-12-08 12:25:33 +01:00
|
|
|
*/
|
2015-03-25 15:15:53 +01:00
|
|
|
protected function createBuild(
|
2017-12-27 17:05:04 +01:00
|
|
|
$source,
|
2015-03-25 15:15:53 +01:00
|
|
|
Project $project,
|
|
|
|
$commitId,
|
|
|
|
$branch,
|
2017-04-22 15:02:24 +02:00
|
|
|
$tag,
|
2015-03-25 15:15:53 +01:00
|
|
|
$committer,
|
|
|
|
$commitMessage,
|
|
|
|
array $extra = null
|
|
|
|
) {
|
2017-01-15 16:35:42 +01:00
|
|
|
if ($project->getArchived()) {
|
|
|
|
throw new NotFoundException(Lang::get('project_x_not_found', $project->getId()));
|
|
|
|
}
|
|
|
|
|
2014-05-12 16:53:29 +02:00
|
|
|
// Check if a build already exists for this commit ID:
|
2017-04-20 16:14:48 +02:00
|
|
|
$builds = $this->buildStore->getByProjectAndCommit($project->getId(), $commitId);
|
2014-05-12 16:53:29 +02:00
|
|
|
|
2017-03-23 13:53:24 +01:00
|
|
|
$ignore_environments = [];
|
2017-04-22 16:06:53 +02:00
|
|
|
$ignore_tags = [];
|
2014-05-12 16:53:29 +02:00
|
|
|
if ($builds['count']) {
|
2017-03-23 13:53:24 +01:00
|
|
|
foreach($builds['items'] as $build) {
|
|
|
|
/** @var Build $build */
|
|
|
|
$ignore_environments[$build->getId()] = $build->getEnvironment();
|
2017-04-22 16:06:53 +02:00
|
|
|
$ignore_tags[$build->getId()] = $build->getTag();
|
2017-03-23 13:53:24 +01:00
|
|
|
}
|
2014-05-12 17:06:58 +02:00
|
|
|
}
|
2014-05-12 17:28:48 +02:00
|
|
|
|
2017-09-13 16:21:49 +02:00
|
|
|
// Check if this branch is to be built.
|
|
|
|
if ($project->getDefaultBranchOnly() && ($branch !== $project->getBranch())) {
|
|
|
|
return [
|
|
|
|
'status' => 'ignored',
|
|
|
|
'message' => 'The branch is not a branch by default. Build is allowed only for the branch by default.'
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-03-23 13:53:24 +01:00
|
|
|
$environments = $project->getEnvironmentsObjects();
|
|
|
|
if ($environments['count']) {
|
2017-04-22 16:06:53 +02:00
|
|
|
$created_builds = [];
|
2017-03-23 13:53:24 +01:00
|
|
|
$environment_names = $project->getEnvironmentsNamesByBranch($branch);
|
|
|
|
// use base branch from project
|
|
|
|
if (!empty($environment_names)) {
|
|
|
|
$duplicates = [];
|
|
|
|
foreach ($environment_names as $environment_name) {
|
2017-04-22 16:06:53 +02:00
|
|
|
if (
|
|
|
|
!in_array($environment_name, $ignore_environments) ||
|
|
|
|
($tag && !in_array($tag, $ignore_tags, true))
|
|
|
|
) {
|
2017-03-23 13:53:24 +01:00
|
|
|
// If not, create a new build job for it:
|
2017-04-22 15:02:24 +02:00
|
|
|
$build = $this->buildService->createBuild(
|
|
|
|
$project,
|
|
|
|
$environment_name,
|
|
|
|
$commitId,
|
|
|
|
$project->getBranch(),
|
|
|
|
$tag,
|
|
|
|
$committer,
|
|
|
|
$commitMessage,
|
2017-12-27 17:05:04 +01:00
|
|
|
(integer)$source,
|
2017-10-15 16:58:36 +02:00
|
|
|
0,
|
2017-04-22 15:02:24 +02:00
|
|
|
$extra
|
2017-03-23 13:53:24 +01:00
|
|
|
);
|
2017-04-22 15:02:24 +02:00
|
|
|
|
|
|
|
$created_builds[] = [
|
|
|
|
'id' => $build->getID(),
|
|
|
|
'environment' => $environment_name,
|
|
|
|
];
|
2017-03-23 13:53:24 +01:00
|
|
|
} else {
|
|
|
|
$duplicates[] = array_search($environment_name, $ignore_environments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($created_builds)) {
|
|
|
|
if (empty($duplicates)) {
|
|
|
|
return ['status' => 'ok', 'builds' => $created_builds];
|
|
|
|
} else {
|
|
|
|
return ['status' => 'ok', 'builds' => $created_builds, 'message' => sprintf('For this commit some builds already exists (%s)', implode(', ', $duplicates))];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return ['status' => 'ignored', 'message' => sprintf('For this commit already created builds (%s)', implode(', ', $duplicates))];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return ['status' => 'ignored', 'message' => 'Branch not assigned to any environment'];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$environment_name = null;
|
2017-04-22 16:06:53 +02:00
|
|
|
if (
|
|
|
|
!in_array($environment_name, $ignore_environments, true) ||
|
|
|
|
($tag && !in_array($tag, $ignore_tags, true))
|
|
|
|
) {
|
2017-04-22 15:02:24 +02:00
|
|
|
$build = $this->buildService->createBuild(
|
|
|
|
$project,
|
|
|
|
null,
|
|
|
|
$commitId,
|
|
|
|
$branch,
|
|
|
|
$tag,
|
|
|
|
$committer,
|
|
|
|
$commitMessage,
|
2017-12-27 17:05:04 +01:00
|
|
|
(integer)$source,
|
2017-10-15 16:58:36 +02:00
|
|
|
0,
|
2017-04-22 15:02:24 +02:00
|
|
|
$extra
|
|
|
|
);
|
|
|
|
|
2017-03-23 13:53:24 +01:00
|
|
|
return ['status' => 'ok', 'buildID' => $build->getID()];
|
|
|
|
} else {
|
|
|
|
return [
|
|
|
|
'status' => 'ignored',
|
|
|
|
'message' => sprintf('Duplicate of build #%d', array_search($environment_name, $ignore_environments)),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
2015-03-25 15:15:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch a project and check its type.
|
|
|
|
*
|
2017-11-09 14:51:07 +01:00
|
|
|
* @param int|string $projectId id or title of project
|
2015-03-25 15:15:53 +01:00
|
|
|
* @param array|string $expectedType
|
|
|
|
*
|
|
|
|
* @return Project
|
|
|
|
*
|
|
|
|
* @throws Exception If the project does not exist or is not of the expected type.
|
|
|
|
*/
|
|
|
|
protected function fetchProject($projectId, $expectedType)
|
|
|
|
{
|
|
|
|
if (empty($projectId)) {
|
|
|
|
throw new Exception('Project does not exist: ' . $projectId);
|
|
|
|
}
|
|
|
|
|
2017-11-08 09:50:32 +01:00
|
|
|
if (is_numeric($projectId)) {
|
2017-11-09 14:51:07 +01:00
|
|
|
$project = $this->projectStore->getById((integer)$projectId);
|
2017-11-08 09:50:32 +01:00
|
|
|
} else {
|
|
|
|
$projects = $this->projectStore->getByTitle($projectId, 2);
|
|
|
|
if ($projects['count'] < 1) {
|
|
|
|
throw new Exception('Project does not found: ' . $projectId);
|
|
|
|
}
|
|
|
|
if ($projects['count'] > 1) {
|
|
|
|
throw new Exception('Project id is ambiguous: ' . $projectId);
|
|
|
|
}
|
|
|
|
$project = reset($projects['items']);
|
|
|
|
}
|
|
|
|
|
2015-03-25 15:15:53 +01:00
|
|
|
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
|
|
|
}
|