2014-02-24 16:30:44 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PHPCI - Continuous Integration for PHP
|
|
|
|
*
|
2014-05-12 18:26:17 +02:00
|
|
|
* @copyright Copyright 2014, 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;
|
2014-05-12 16:53:29 +02:00
|
|
|
use PHPCI\BuildFactory;
|
2014-02-24 16:30:44 +01:00
|
|
|
use PHPCI\Model\Build;
|
2014-07-14 17:08:19 +02:00
|
|
|
use PHPCI\Service\BuildService;
|
2014-02-24 16:30:44 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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>
|
|
|
|
* @package PHPCI
|
|
|
|
* @subpackage Web
|
|
|
|
*/
|
|
|
|
class WebhookController extends \PHPCI\Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \PHPCI\Store\BuildStore
|
|
|
|
*/
|
|
|
|
protected $buildStore;
|
|
|
|
|
2014-07-14 17:08:19 +02:00
|
|
|
/**
|
|
|
|
* @var \PHPCI\Store\ProjectStore
|
|
|
|
*/
|
|
|
|
protected $projectStore;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \PHPCI\Service\BuildService
|
|
|
|
*/
|
|
|
|
protected $buildService;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by Bitbucket POST service.
|
|
|
|
*/
|
|
|
|
public function bitbucket($project)
|
|
|
|
{
|
2014-12-01 18:30:44 +01:00
|
|
|
$payload = json_decode($this->getParam('payload'), true);
|
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
|
|
|
|
2014-05-12 16:53:29 +02:00
|
|
|
$this->createBuild($project, $commit['raw_node'], $commit['branch'], $email, $commit['message']);
|
2014-02-24 16:30:44 +01:00
|
|
|
} catch (\Exception $ex) {
|
2014-05-12 16:53:29 +02:00
|
|
|
header('HTTP/1.1 500 Internal Server Error');
|
|
|
|
header('Ex: ' . $ex->getMessage());
|
|
|
|
die('FAIL');
|
2014-02-24 16:30:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
die('OK');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*
|
|
|
|
* @param string $project
|
|
|
|
*/
|
|
|
|
public function git($project)
|
|
|
|
{
|
|
|
|
$branch = $this->getParam('branch');
|
|
|
|
$commit = $this->getParam('commit');
|
|
|
|
|
|
|
|
try {
|
2014-05-12 16:53:29 +02:00
|
|
|
if (empty($branch)) {
|
|
|
|
$branch = 'master';
|
2014-02-24 16:30:44 +01:00
|
|
|
}
|
|
|
|
|
2014-05-12 16:53:29 +02:00
|
|
|
if (empty($commit)) {
|
|
|
|
$commit = null;
|
2014-02-24 16:30:44 +01:00
|
|
|
}
|
|
|
|
|
2014-05-12 16:53:29 +02:00
|
|
|
$this->createBuild($project, $commit, $branch, null, null);
|
2014-02-24 16:30:44 +01:00
|
|
|
|
|
|
|
} catch (\Exception $ex) {
|
2014-05-12 16:53:29 +02:00
|
|
|
header('HTTP/1.1 400 Bad Request');
|
2014-02-24 16:30:44 +01:00
|
|
|
header('Ex: ' . $ex->getMessage());
|
|
|
|
die('FAIL');
|
|
|
|
}
|
|
|
|
|
|
|
|
die('OK');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by Github Webhooks:
|
|
|
|
*/
|
|
|
|
public function github($project)
|
|
|
|
{
|
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:
|
|
|
|
header('HTTP/1.1 400 Bad Request');
|
|
|
|
die('Request content type not supported');
|
|
|
|
}
|
2014-05-12 15:46:45 +02:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
2014-02-24 16:30:44 +01:00
|
|
|
|
2014-05-12 15:46:45 +02:00
|
|
|
header('HTTP/1.1 200 OK');
|
|
|
|
die('This request type is not supported, this is not an error.');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function githubCommitRequest($project, array $payload)
|
|
|
|
{
|
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') {
|
2014-02-24 16:30:44 +01:00
|
|
|
die('OK');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
2014-02-24 21:14:57 +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:
|
|
|
|
|
|
|
|
foreach ($payload['commits'] as $commit) {
|
|
|
|
if (!$commit['distinct']) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-05-12 16:53:29 +02:00
|
|
|
$branch = str_replace('refs/heads/', '', $payload['ref']);
|
|
|
|
$committer = $commit['committer']['email'];
|
|
|
|
$this->createBuild($project, $commit['id'], $branch, $committer, $commit['message']);
|
2014-02-24 21:14:57 +01:00
|
|
|
}
|
|
|
|
} elseif (substr($payload['ref'], 0, 10) == 'refs/tags/') {
|
|
|
|
// If we don't, but we're dealing with a tag, add that instead:
|
2014-05-12 16:53:29 +02:00
|
|
|
$branch = str_replace('refs/tags/', 'Tag: ', $payload['ref']);
|
|
|
|
$committer = $payload['pusher']['email'];
|
|
|
|
$message = $payload['head_commit']['message'];
|
|
|
|
$this->createBuild($project, $payload['after'], $branch, $committer, $message);
|
2014-02-24 16:30:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
header('HTTP/1.1 500 Internal Server Error');
|
|
|
|
header('Ex: ' . $ex->getMessage());
|
|
|
|
die('FAIL');
|
|
|
|
}
|
|
|
|
|
|
|
|
die('OK');
|
|
|
|
}
|
|
|
|
|
2014-05-12 15:46:45 +02:00
|
|
|
protected function githubPullRequest($projectId, array $payload)
|
|
|
|
{
|
|
|
|
// We only want to know about open pull requests:
|
|
|
|
if (!in_array($payload['action'], array('opened', 'synchronize', 'reopened'))) {
|
|
|
|
die('OK');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$headers = array();
|
|
|
|
$token = \b8\Config::getInstance()->get('phpci.github.token');
|
|
|
|
|
|
|
|
if (!empty($token)) {
|
|
|
|
$headers[] = 'Authorization: token ' . $token;
|
|
|
|
}
|
|
|
|
|
|
|
|
$url = $payload['pull_request']['commits_url'];
|
|
|
|
$http = new \b8\HttpClient();
|
2014-05-12 15:58:30 +02:00
|
|
|
$http->setHeaders($headers);
|
2014-05-12 15:46:45 +02:00
|
|
|
$response = $http->get($url);
|
|
|
|
|
2014-05-12 15:58:30 +02:00
|
|
|
// Check we got a success response:
|
|
|
|
if (!$response['success']) {
|
|
|
|
header('HTTP/1.1 500 Internal Server Error');
|
|
|
|
header('Ex: Could not get commits, failed API request.');
|
|
|
|
die('FAIL');
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($response['body'] as $commit) {
|
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
|
|
|
|
|
|
|
$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'],
|
|
|
|
'remote_url' => $payload['pull_request']['head']['repo']['clone_url'],
|
|
|
|
);
|
|
|
|
|
2014-05-12 16:53:29 +02:00
|
|
|
$this->createBuild($projectId, $commit['sha'], $branch, $committer, $message, $extra);
|
2014-05-12 15:46:45 +02:00
|
|
|
}
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
header('HTTP/1.1 500 Internal Server Error');
|
|
|
|
header('Ex: ' . $ex->getMessage());
|
|
|
|
die('FAIL');
|
|
|
|
}
|
|
|
|
|
|
|
|
die('OK');
|
|
|
|
}
|
|
|
|
|
2014-02-24 16:30:44 +01:00
|
|
|
/**
|
|
|
|
* Called by Gitlab Webhooks:
|
|
|
|
*/
|
|
|
|
public function gitlab($project)
|
|
|
|
{
|
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
|
|
|
|
|
|
|
try {
|
|
|
|
|
2014-02-25 10:09:38 +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:
|
|
|
|
|
|
|
|
foreach ($payload['commits'] as $commit) {
|
2014-05-12 16:53:29 +02:00
|
|
|
$branch = str_replace('refs/heads/', '', $payload['ref']);
|
|
|
|
$committer = $commit['author']['email'];
|
|
|
|
$this->createBuild($project, $commit['id'], $branch, $committer, $commit['message']);
|
2014-02-25 10:09:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-24 16:30:44 +01:00
|
|
|
} catch (\Exception $ex) {
|
|
|
|
header('HTTP/1.1 500 Internal Server Error');
|
|
|
|
header('Ex: ' . $ex->getMessage());
|
|
|
|
die('FAIL');
|
|
|
|
}
|
|
|
|
|
|
|
|
die('OK');
|
|
|
|
}
|
2014-05-12 16:53:29 +02:00
|
|
|
|
2014-05-12 17:06:58 +02:00
|
|
|
protected function createBuild($projectId, $commitId, $branch, $committer, $commitMessage, $extra = null)
|
2014-05-12 16:53:29 +02:00
|
|
|
{
|
|
|
|
// Check if a build already exists for this commit ID:
|
|
|
|
$builds = $this->buildStore->getByProjectAndCommit($projectId, $commitId);
|
|
|
|
|
|
|
|
if ($builds['count']) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-14 17:08:19 +02:00
|
|
|
$project = $this->projectStore->getById($projectId);
|
|
|
|
|
|
|
|
if (empty($project)) {
|
|
|
|
throw new \Exception('Project does not exist:' . $projectId);
|
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();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-24 16:30:44 +01:00
|
|
|
}
|