Commit f2675cc2 authored by michael's avatar michael

60: Add some classes and interface to handle incoming webhook requests

parent 66ca5cd8
<?php
namespace AppBundle\Utils\WebhookAdapters;
use AppBundle\Model\WebhookEvent;
use Symfony\Component\HttpFoundation\Request;
class GithubHandler implements WebhookHandler {
/**
* @inheritdoc
*/
public function handle(Request $request): WebhookEvent {
$event = new WebhookEvent();
$event->setType($request->headers->get('X-GitHub-Event'));
$event->setSource($event::SOURCE_GITHUB);
$this->getDetailsFromPush($event, $request->getContent());
return $event;
}
/**
* @inheritdoc
*/
public function canHandle(Request $request): bool {
return $request->headers->has('X-Github-Event');
}
/**
* 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 WebhookEvent $event
* @param string $payload
*
* @return WebhookEvent
*
* @throws \Exception
*/
protected function getDetailsFromPush(WebhookEvent $event, $payload) {
$payload = json_decode($payload);
if (empty($payload)) {
throw new \Exception('Could not decode webhook body');
}
$branch = basename($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");
}
$event->setRepo($repo);
$event->setBranch($branch);
$event->setCommits($payload->commits);
return $event;
}
}
<?php
namespace AppBundle\Utils\WebhookAdapters;
use AppBundle\Model\WebhookEvent;
use Symfony\Component\HttpFoundation\Request;
class GitlabHandler implements WebhookHandler {
/**
* @inheritdoc
*/
public function handle(Request $request): WebhookEvent {
$event = new WebhookEvent();
$event->setType($this->getEventType($request));
$event->setSource($event::SOURCE_GITLAB);
$this->getDetailsFromPush($event, $request->getContent());
return $event;
}
/**
* @inheritdoc
*/
public function canHandle(Request $request): bool {
return $request->headers->has('X-Gitlab-Event');
}
/**
* 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 WebhookEvent $event
* @param string $payload
*
* @return WebhookEvent
*
* @throws \Exception
*/
protected function getDetailsFromPush(WebhookEvent $event, $payload) {
$payload = json_decode($payload);
if (empty($payload)) {
throw new \Exception('Could not decode webhook body');
}
$branch = basename($payload->ref);;
if (empty($branch)) {
throw new \Exception("Unable to determine branch from payload data");
}
$repo = $payload->repository->git_http_url;
if (empty($repo)) {
throw new \Exception("Unable to determine repository from payload data");
}
$event->setRepo($repo);
$event->setBranch($branch);
$event->setCommits($payload->commits);
return $event;
}
/**
* @param Request $request
* @return string
* @throws \Exception
*/
protected function getEventType(Request $request) : string {
switch ($request->headers->get('X-Gitlab-Event')) {
case 'Push Hook':
return self::EVENT_PUSH;
default:
throw new \Exception('Unrecognized webhook event type');
}
}
}
<?php
namespace AppBundle\Utils\WebhookAdapters;
use AppBundle\Model\WebhookEvent;
use Symfony\Component\HttpFoundation\Request;
interface WebhookHandler {
const EVENT_PUSH = 'push';
/**
* Turns a request into a webhook event
*
* @param Request $request
* @return WebhookEvent
*/
public function handle(Request $request) : WebhookEvent;
/**
* Decides whether or not this adapter can handle the incoming request
*
* @param Request $request
* @return bool
*/
public function canHandle(Request $request) : bool;
}
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