Commit 06685576 authored by michael's avatar michael

60: Rename GitHubHookProcessor to generic WebhookProcessor, use handlers to...

60: Rename GitHubHookProcessor to generic WebhookProcessor, use handlers to process incoming requests.
parent 2af5a802
......@@ -11,7 +11,18 @@ services:
- %books_dir%
github.hook.processor:
class: AppBundle\Utils\GitHubHookProcessor
class: AppBundle\Utils\WebhookProcessor
arguments:
[
'@github.webhook_handler'
'@gitlab.webbook_handler'
]
github.webhook_handler:
class: AppBundle\Utils\WebhookAdapters\GithubHandler
gitlab.webbook_handler:
class: AppBundle\Utils\WebhookAdapters\GitlabHandler
mkdocs:
class: AppBundle\Utils\MkDocs
......
<?php
namespace AppBundle\Utils;
class GitHubHookProcessor {
/**
* Process a GitHub webhook
*
* @param string $event
* e.g. 'pull_request', 'push'
*
* @param mixed $payload
* An object given by json_decode()
*
* @return array
*
* @throws \Exception
*/
public function process($event, $payload) {
if (empty($payload)) {
throw new \Exception("No payload data supplied");
}
if (empty($event)) {
throw new \Exception("Unable to determine webhook event type from "
. "request headers");
}
if ($event != 'push') {
throw new \Exception("Webhook event type is not 'push'");
}
return $this->getDetailsFromPush($payload);
}
/**
* Use a "push" event to figure out what branch and repo we are talking
* about, and the also work out what emails we should send.
*
* @param mixed $payload
* An object given by json_decode()
*
* @return array
*
* @throws \Exception
*/
protected function getDetailsFromPush($payload) {
$branch = preg_replace("#.*/(.*)#", "$1", $payload->ref);
if (empty($branch)) {
throw new \Exception("Unable to determine branch from payload data");
}
$repo = $payload->repository->html_url;
if (empty($repo)) {
throw new \Exception("Unable to determine repository from payload data");
}
$recipients = [];
foreach ($payload->commits as $commit) {
$this->addRecipients($recipients, $commit->author->email);
$this->addRecipients($recipients, $commit->committer->email);
}
return [
'commits' => $payload->commits,
'recipients' => $recipients,
'branch' => $branch,
'repo' => $repo
];
}
/**
* Adds one or more email recipients, and makes sure all recipients are
* kept unique
*
* @param array $new
* Array of strings for emails of people to notify
* @param array $existing
* Existing recipients so far
* @return array
* Unique array of recipients
*/
protected function addRecipients($existing, $new) {
if (!is_array($new)) {
$new = array($new);
}
// remove any email addresses begins with "donotreply@" or "noreply"
$new = preg_grep('/^(donot|no)reply@/', $new, PREG_GREP_INVERT);
return array_unique(array_merge($existing, $new));
}
}
<?php
namespace AppBundle\Utils;
use AppBundle\Model\WebhookEvent;
use AppBundle\Utils\WebhookAdapters\WebhookHandler;
use Symfony\Component\HttpFoundation\Request;
class WebhookProcessor {
/**
* @var WebhookHandler[]
*/
protected $handlers = [];
/**
* @param WebhookHandler[] $handlers
*/
public function __construct(array $handlers) {
$this->handlers = $handlers;
}
/**
* Process a webhook
*
* @param Request $request
*
* @return WebhookEvent
*
* @throws \Exception
*/
public function process(Request $request) {
$event = null;
foreach ($this->handlers as $handler) {
if ($handler->canHandle($request)) {
$event = $handler->handle($request);
break;
}
}
if (!$event) {
throw new \Exception('Could not handle webhook event');
}
if ($event->getType() != 'push') {
throw new \Exception("Webhook event type is not 'push'");
}
return $event;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment