Commit 403ac6f1 authored by michael's avatar michael

60: Renaming service and fixing YAML. Change publish controller to use new processor response

parent 58c48bc5
......@@ -10,13 +10,12 @@ services:
arguments:
- %books_dir%
github.hook.processor:
webhook.processor:
class: AppBundle\Utils\WebhookProcessor
arguments:
[
'@github.webhook_handler'
'@gitlab.webbook_handler'
]
-
- '@github.webhook_handler'
- '@gitlab.webbook_handler'
github.webhook_handler:
class: AppBundle\Utils\WebhookAdapters\GithubHandler
......
......@@ -2,6 +2,7 @@
namespace AppBundle\Controller;
use AppBundle\Model\WebhookEvent;
use AppBundle\Utils\Publisher;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
......@@ -56,50 +57,46 @@ class PublishController extends Controller {
* @Route("/admin/listen")
*/
public function ListenAction(Request $request) {
$body = $request->getContent();
$event = $request->headers->get('X-GitHub-Event');
$processor = $this->get('github.hook.processor');
try {
$hookData = $processor->process($event, json_decode($body));
}
catch (\Exception $e) {
$response = "CRITICAL - Skipping the publishing process due to the "
. "following reason: " . $e->getMessage();
return new Response($response, 200);
}
$processor = $this->get('webhook.processor');
$event = $processor->process($request);
$library = $this->get('library');
$identifiers = $library->getIdentifiersByRepo($hookData['repo']);
if ($identifiers) {
$this->publisher = $this->get('publisher');
foreach ($identifiers as $identifier) {
$fullIdentifier = sprintf('%s/%s', $identifier, $hookData['branch']);
$this->publisher->publish($fullIdentifier);
$this->sendEmail($fullIdentifier, $hookData);
}
$response = $this->publisher->getMessagesInPlainText();
$identifiers = $library->getIdentifiersByRepo($event->getRepo());
if (!$identifiers) {
$msg = "CRITICAL - No books found which match " . $event->getRepo();
return new Response($msg);
}
else {
$response = "CRITICAL - No books found which match " . $hookData['repo'];
$this->publisher = $this->get('publisher');
foreach ($identifiers as $identifier) {
$fullIdentifier = sprintf('%s/%s', $identifier, $event->getBranch());
$this->publisher->publish($fullIdentifier);
$this->sendEmail($fullIdentifier, $event);
}
$response = $this->publisher->getMessagesInPlainText();
return new Response($response, 200);
return new Response($response);
}
/**
* Send notification emails after publishing
*
* @param string $identifier
* @param array $hookData
* @param WebhookEvent $event
*/
private function sendEmail(string $identifier, $hookData) {
private function sendEmail(string $identifier, WebhookEvent $event) {
/**
* Array of strings for email addresses that should receive the
* notification email. If none are specified, then the email will be sent to
* all addresses set in the book's yaml configuration
*/
$extraRecipients = $hookData['recipients'];
$commits = $hookData['commits'];
$extraRecipients = $event->getNotificationRecipients();
$commits = $event->getCommits();
$library = $this->get('library');
$messages = $this->get('publisher')->getMessages();
$parts = $library::parseIdentifier($identifier);
......
......@@ -32,8 +32,8 @@ class WebhookEvent {
protected $branch = '';
/**
* @var array[]
* An array of commits in the event, each commit will have keys "ID",
* @var \stdClass[]
* An array of commits in the event, each commit will have properties "ID",
* "author" and "message"
*/
protected $commits = [];
......@@ -114,14 +114,14 @@ class WebhookEvent {
}
/**
* @param array[] $commits
* @param \stdClass[] $commits
*/
public function setCommits(array $commits) {
$this->commits = $commits;
}
/**
* @return array[]
* @return \stdClass[]
*/
public function getCommits() {
return $this->commits;
......
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