From 06685576596783aabb275864dc6dafae1f069baa Mon Sep 17 00:00:00 2001 From: Michael Devery Date: Thu, 19 Oct 2017 23:20:22 +0100 Subject: [PATCH] 60: Rename GitHubHookProcessor to generic WebhookProcessor, use handlers to process incoming requests. --- app/config/services.yml | 13 ++- src/AppBundle/Utils/GitHubHookProcessor.php | 92 --------------------- src/AppBundle/Utils/WebhookProcessor.php | 53 ++++++++++++ 3 files changed, 65 insertions(+), 93 deletions(-) delete mode 100644 src/AppBundle/Utils/GitHubHookProcessor.php create mode 100644 src/AppBundle/Utils/WebhookProcessor.php diff --git a/app/config/services.yml b/app/config/services.yml index ca045c2..6fb5816 100644 --- a/app/config/services.yml +++ b/app/config/services.yml @@ -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 diff --git a/src/AppBundle/Utils/GitHubHookProcessor.php b/src/AppBundle/Utils/GitHubHookProcessor.php deleted file mode 100644 index b80ea5e..0000000 --- a/src/AppBundle/Utils/GitHubHookProcessor.php +++ /dev/null @@ -1,92 +0,0 @@ -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)); - } -} diff --git a/src/AppBundle/Utils/WebhookProcessor.php b/src/AppBundle/Utils/WebhookProcessor.php new file mode 100644 index 0000000..592edf8 --- /dev/null +++ b/src/AppBundle/Utils/WebhookProcessor.php @@ -0,0 +1,53 @@ +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; + } +} -- GitLab