Added new build source: Webhook (Pull request).

This commit is contained in:
Dmitry Khomutov 2017-12-27 23:05:04 +07:00
parent dc79c499f4
commit eecf3ea3ce
No known key found for this signature in database
GPG key ID: EC19426474B37AAC
6 changed files with 79 additions and 35 deletions

View file

@ -129,6 +129,7 @@ class WebhookController extends Controller
}
$results[$commit['new']['target']['hash']] = $this->createBuild(
Build::SOURCE_WEBHOOK,
$project,
$commit['new']['target']['hash'],
$commit['new']['name'],
@ -211,7 +212,16 @@ class WebhookController extends Controller
'remote_reference' => $payload['pullrequest']['source']['repository']['full_name'],
];
$results[$id] = $this->createBuild($project, $id, $branch, null, $committer, $message, $extra);
$results[$id] = $this->createBuild(
Build::SOURCE_WEBHOOK_PULL_REQUEST,
$project,
$id,
$branch,
null,
$committer,
$message,
$extra
);
$status = 'ok';
} catch (Exception $ex) {
$results[$id] = ['status' => 'failed', 'error' => $ex->getMessage()];
@ -221,16 +231,6 @@ class WebhookController extends Controller
return ['status' => $status, 'commits' => $results];
}
/**
* Bitbucket webhooks.
*
* @deprecated, for BC purpose
*/
protected function bitbucketWebhook($payload, $project)
{
return $this->bitbucketCommitRequest($project, $payload);
}
/**
* Bitbucket POST service.
*/
@ -247,6 +247,7 @@ class WebhookController extends Controller
$email = substr($email, strpos($email, '<') + 1);
$results[$commit['raw_node']] = $this->createBuild(
Build::SOURCE_WEBHOOK,
$project,
$commit['raw_node'],
$commit['branch'],
@ -272,13 +273,21 @@ class WebhookController extends Controller
*/
public function git($projectId)
{
$project = $this->fetchProject($projectId, ['local', 'remote']);
$branch = $this->getParam('branch', $project->getBranch());
$commit = $this->getParam('commit');
$project = $this->fetchProject($projectId, ['local', 'remote']);
$branch = $this->getParam('branch', $project->getBranch());
$commit = $this->getParam('commit');
$commitMessage = $this->getParam('message');
$committer = $this->getParam('committer');
$committer = $this->getParam('committer');
return $this->createBuild($project, $commit, $branch, null, $committer, $commitMessage);
return $this->createBuild(
Build::SOURCE_WEBHOOK,
$project,
$commit,
$branch,
null,
$committer,
$commitMessage
);
}
/**
@ -339,7 +348,7 @@ class WebhookController extends Controller
$commit = $payload['head_commit'];
$results = [];
$status = 'failed';
if (!$commit['distinct']) {
$results[$commit['id']] = ['status' => 'ignored'];
} else {
@ -355,6 +364,7 @@ class WebhookController extends Controller
}
$results[$commit['id']] = $this->createBuild(
Build::SOURCE_WEBHOOK,
$project,
$commit['id'],
$branch,
@ -446,7 +456,16 @@ class WebhookController extends Controller
'remote_url' => $payload['pull_request']['head']['repo'][$remoteUrlKey],
];
$results[$id] = $this->createBuild($project, $id, $branch, null, $committer, $message, $extra);
$results[$id] = $this->createBuild(
Build::SOURCE_WEBHOOK_PULL_REQUEST,
$project,
$id,
$branch,
null,
$committer,
$message,
$extra
);
$status = 'ok';
} catch (Exception $ex) {
$results[$id] = ['status' => 'failed', 'error' => $ex->getMessage()];
@ -470,11 +489,19 @@ class WebhookController extends Controller
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'];
$branch = $attributes['source_branch'];
$commit = $attributes['last_commit'];
$committer = $commit['author']['email'];
return $this->createBuild($project, $commit['id'], $branch, null, $committer, $commit['message']);
return $this->createBuild(
Build::SOURCE_WEBHOOK_PULL_REQUEST,
$project,
$commit['id'],
$branch,
null,
$committer,
$commit['message']
);
}
}
@ -489,6 +516,7 @@ class WebhookController extends Controller
$branch = str_replace('refs/heads/', '', $payload['ref']);
$committer = $commit['author']['email'];
$results[$commit['id']] = $this->createBuild(
Build::SOURCE_WEBHOOK,
$project,
$commit['id'],
$branch,
@ -525,7 +553,15 @@ class WebhookController extends Controller
$commitMessage = $this->getParam('message');
$committer = $this->getParam('committer');
return $this->createBuild($project, $commit, $branch, null, $committer, $commitMessage);
return $this->createBuild(
Build::SOURCE_WEBHOOK,
$project,
$commit,
$branch,
null,
$committer,
$commitMessage
);
}
/**
@ -576,6 +612,7 @@ class WebhookController extends Controller
$branch = str_replace('refs/heads/', '', $payload['ref']);
$committer = $commit['author']['email'];
$results[$commit['id']] = $this->createBuild(
Build::SOURCE_WEBHOOK,
$project,
$commit['id'],
$branch,
@ -598,6 +635,7 @@ class WebhookController extends Controller
/**
* Wrapper for creating a new build.
*
* @param integer $source
* @param Project $project
* @param string $commitId
* @param string $branch
@ -611,6 +649,7 @@ class WebhookController extends Controller
* @throws Exception
*/
protected function createBuild(
$source,
Project $project,
$commitId,
$branch,
@ -665,7 +704,7 @@ class WebhookController extends Controller
$tag,
$committer,
$commitMessage,
Build::SOURCE_WEBHOOK,
(integer)$source,
0,
$extra
);
@ -704,7 +743,7 @@ class WebhookController extends Controller
$tag,
$committer,
$commitMessage,
Build::SOURCE_WEBHOOK,
(integer)$source,
0,
$extra
);

View file

@ -154,6 +154,7 @@ PHP Censor',
'source_manual_console' => 'Manual (from CLI)',
'source_periodical' => 'Periodical',
'source_webhook' => 'Webhook',
'source_webhook_pull_request' => 'Webhook (Pull request)',
'webhooks' => 'Webhooks',
'webhooks_help_github' => 'To automatically build this project when new commits are pushed, add the URL below

View file

@ -150,6 +150,7 @@ PHP Censor',
'source_manual_console' => 'Вручную (Через CLI)',
'source_periodical' => 'Переодический',
'source_webhook' => 'Webhook',
'source_webhook_pull_request' => 'Webhook (Pull request)',
'webhooks' => 'Webhooks',
'webhooks_help_github' => 'Чтобы Автоматически собирать этот проект при публикации новых коммитов, добавьте URL ниже в качестве нового хука в разделе настроек <a href="https://github.com/%s/settings/hooks">Webhooks

View file

@ -27,11 +27,12 @@ class Build extends Model
const STATUS_SUCCESS = 2;
const STATUS_FAILED = 3;
const SOURCE_UNKNOWN = 0;
const SOURCE_MANUAL_WEB = 1;
const SOURCE_MANUAL_CONSOLE = 2;
const SOURCE_PERIODICAL = 3;
const SOURCE_WEBHOOK = 4;
const SOURCE_UNKNOWN = 0;
const SOURCE_MANUAL_WEB = 1;
const SOURCE_MANUAL_CONSOLE = 2;
const SOURCE_PERIODICAL = 3;
const SOURCE_WEBHOOK = 4;
const SOURCE_WEBHOOK_PULL_REQUEST = 5;
/**
* @var array
@ -1037,6 +1038,8 @@ OUT;
switch ($this->getSource()) {
case Build::SOURCE_WEBHOOK:
return 'source_webhook';
case Build::SOURCE_WEBHOOK_PULL_REQUEST:
return 'source_webhook_pull_request';
case Build::SOURCE_MANUAL_WEB:
return 'source_manual_web';
case Build::SOURCE_MANUAL_CONSOLE:

View file

@ -12,7 +12,7 @@ use PHPCensor\Model\BuildError;
/**
* BitBucket Build Model
*
*
* @author Dan Cryer <dan@block8.co.uk>
*/
class BitbucketBuild extends RemoteGitBuild
@ -48,7 +48,7 @@ class BitbucketBuild extends RemoteGitBuild
*/
public function sendStatusPostback()
{
if (Build::SOURCE_WEBHOOK !== $this->getSource()) {
if (!in_array($this->getSource(), [Build::SOURCE_WEBHOOK, Build::SOURCE_WEBHOOK_PULL_REQUEST], true)) {
return false;
}

View file

@ -12,7 +12,7 @@ use PHPCensor\Model\BuildError;
/**
* Github Build Model
*
*
* @author Dan Cryer <dan@block8.co.uk>
*/
class GithubBuild extends RemoteGitBuild
@ -46,7 +46,7 @@ class GithubBuild extends RemoteGitBuild
*/
public function sendStatusPostback()
{
if (Build::SOURCE_WEBHOOK !== $this->getSource()) {
if (!in_array($this->getSource(), [Build::SOURCE_WEBHOOK, Build::SOURCE_WEBHOOK_PULL_REQUEST], true)) {
return false;
}
@ -54,7 +54,7 @@ class GithubBuild extends RemoteGitBuild
if (empty($project)) {
return false;
}
$token = Config::getInstance()->get('php-censor.github.token');
if (empty($token) || empty($this->data['id'])) {
@ -97,7 +97,7 @@ class GithubBuild extends RemoteGitBuild
'context' => 'PHP Censor',
]
]);
return true;
}