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