Commit 152d6c20 authored by michael's avatar michael

24: Return data from hook processor service instead of storing it as property

parent a2def7ee
...@@ -60,7 +60,7 @@ class PublishController extends Controller { ...@@ -60,7 +60,7 @@ class PublishController extends Controller {
$event = $request->headers->get('X-GitHub-Event'); $event = $request->headers->get('X-GitHub-Event');
$processor = $this->get('github.hook.processor'); $processor = $this->get('github.hook.processor');
try { try {
$processor->process($event, json_decode($body)); $hookData = $processor->process($event, json_decode($body));
} }
catch (\Exception $e) { catch (\Exception $e) {
$response = "CRITICAL - Skipping the publishing process due to the " $response = "CRITICAL - Skipping the publishing process due to the "
...@@ -74,7 +74,7 @@ class PublishController extends Controller { ...@@ -74,7 +74,7 @@ class PublishController extends Controller {
foreach ($identifiers as $identifier) { foreach ($identifiers as $identifier) {
$fullIdentifier = "{$identifier}/{$processor->branch}"; $fullIdentifier = "{$identifier}/{$processor->branch}";
$this->publisher->publish($fullIdentifier); $this->publisher->publish($fullIdentifier);
$this->sendEmail($fullIdentifier); $this->sendEmail($fullIdentifier, $hookData);
} }
$response = $this->publisher->getMessagesInPlainText(); $response = $this->publisher->getMessagesInPlainText();
} }
...@@ -89,16 +89,17 @@ class PublishController extends Controller { ...@@ -89,16 +89,17 @@ class PublishController extends Controller {
* Send notification emails after publishing * Send notification emails after publishing
* *
* @param string $identifier * @param string $identifier
* @param array $hookData
*/ */
private function sendEmail(string $identifier) { private function sendEmail(string $identifier, $hookData) {
/** /**
* Array of strings for email addresses that should receive the * Array of strings for email addresses that should receive the
* notification email. If none are specified, then the email will be sent to * notification email. If none are specified, then the email will be sent to
* all addresses set in the book's yaml configuration * all addresses set in the book's yaml configuration
*/ */
$extraRecipients = $this->get('github.hook.processor')->recipients; $extraRecipients = $hookData['recipients'];
$commits = $this->get('github.hook.processor')->commits; $commits = $hookData['commits'];
$library = $this->get('library'); $library = $this->get('library');
$messages = $this->get('publisher')->getMessages(); $messages = $this->get('publisher')->getMessages();
$parts = $library::parseIdentifier($identifier); $parts = $library::parseIdentifier($identifier);
......
...@@ -4,16 +4,6 @@ namespace AppBundle\Utils; ...@@ -4,16 +4,6 @@ namespace AppBundle\Utils;
class GitHubHookProcessor { class GitHubHookProcessor {
/**
* @var array of strings for email addresses of people to notify
*/
public $recipients = [];
/**
* @var array
*/
public $commits = [];
/** /**
* @var string the URL for the repository * @var string the URL for the repository
*/ */
...@@ -24,13 +14,6 @@ class GitHubHookProcessor { ...@@ -24,13 +14,6 @@ class GitHubHookProcessor {
*/ */
public $branch; public $branch;
/**
* Constructor
*/
public function __construct() {
}
/** /**
* Process a GitHub webhook * Process a GitHub webhook
* *
...@@ -40,6 +23,8 @@ class GitHubHookProcessor { ...@@ -40,6 +23,8 @@ class GitHubHookProcessor {
* @param mixed $payload * @param mixed $payload
* An object given by json_decode() * An object given by json_decode()
* *
* @return array
*
* @throws \Exception * @throws \Exception
*/ */
public function process($event, $payload) { public function process($event, $payload) {
...@@ -53,7 +38,8 @@ class GitHubHookProcessor { ...@@ -53,7 +38,8 @@ class GitHubHookProcessor {
if ($event != 'push') { if ($event != 'push') {
throw new \Exception("Webhook event type is not 'push'"); throw new \Exception("Webhook event type is not 'push'");
} }
$this->getDetailsFromPush($payload);
return $this->getDetailsFromPush($payload);
} }
/** /**
...@@ -63,9 +49,11 @@ class GitHubHookProcessor { ...@@ -63,9 +49,11 @@ class GitHubHookProcessor {
* @param mixed $payload * @param mixed $payload
* An object given by json_decode() * An object given by json_decode()
* *
* @return array
*
* @throws \Exception * @throws \Exception
*/ */
public function getDetailsFromPush($payload) { protected function getDetailsFromPush($payload) {
$this->branch = preg_replace("#.*/(.*)#", "$1", $payload->ref); $this->branch = preg_replace("#.*/(.*)#", "$1", $payload->ref);
if (empty($this->branch)) { if (empty($this->branch)) {
throw new \Exception("Unable to determine branch from payload data"); throw new \Exception("Unable to determine branch from payload data");
...@@ -74,28 +62,37 @@ class GitHubHookProcessor { ...@@ -74,28 +62,37 @@ class GitHubHookProcessor {
if (empty($this->repo)) { if (empty($this->repo)) {
throw new \Exception("Unable to determine repository from payload data"); throw new \Exception("Unable to determine repository from payload data");
} }
$recipients = [];
foreach ($payload->commits as $commit) { foreach ($payload->commits as $commit) {
$this->addRecipients($commit->author->email); $this->addRecipients($recipients, $commit->author->email);
$this->addRecipients($commit->committer->email); $this->addRecipients($recipients, $commit->committer->email);
} }
$this->commits = $payload->commits; return [
'commits' => $payload->commits,
'recipients' => $recipients
];
} }
/** /**
* Adds one or more email recipients, and makes sure all recipients are * Adds one or more email recipients, and makes sure all recipients are
* kept unique * kept unique
* *
* @param array $recipients * @param array $new
* Array of strings for emails of people to notify * Array of strings for emails of people to notify
* @param array $existing
* Existing recipients so far
* @return array
* Unique array of recipients
*/ */
public function addRecipients($recipients) { protected function addRecipients($existing, $new) {
if (!is_array($recipients)) { if (!is_array($new)) {
$recipients = array($recipients); $new = array($new);
} }
// remove any email addresses begins with "donotreply@" or "noreply" // remove any email addresses begins with "donotreply@" or "noreply"
$recipients = preg_grep('/^(donot|no)reply@/', $recipients, PREG_GREP_INVERT); $new = preg_grep('/^(donot|no)reply@/', $new, PREG_GREP_INVERT);
$this->recipients = array_unique(array_merge($this->recipients, $recipients)); return array_unique(array_merge($existing, $new));
} }
} }
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