Commit cbbf3243 authored by Sean Madsen's avatar Sean Madsen

Merge branch '24_commit-messages-in-emails' into 'master'

Add commit messages to emails

Closes #24

See merge request !78
parents 69d21fd3 22b6b6fd
......@@ -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,15 +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;
$extraRecipients = $hookData['recipients'];
$commits = $hookData['commits'];
$library = $this->get('library');
$messages = $this->get('publisher')->getMessages();
$parts = $library::parseIdentifier($identifier);
......@@ -111,8 +113,9 @@ class PublishController extends Controller {
$renderParams = [
'publishURLBase' => $webPath,
'status' => $subject,
'messages' => $messages,
'status' => $subject,
'messages' => $messages,
'commits' => $commits,
];
$body = $this->renderView('AppBundle:Emails:notify.html.twig', $renderParams);
$mail = \Swift_Message::newInstance()
......
<p><strong>Note from
<a href="{{ publishURLBase }}">{{ publishURLBase }}</a>:<br />
{{ status }}</strong>
<p>
<strong>
Note from <a href="{{ publishURLBase }}">{{ publishURLBase }}</a>:
<br/>
{{ status }}
</strong>
</p>
<p>Please check the publish log for any errors:</p>
<h4>Messages:</h4>
<ul>
{% for message in messages %}
<li><strong>{{ message.label }}:</strong> {{ message.content|raw }}</li>
{% endfor %}
</ul>
<p>You are receiving this message because either (a) you have made commits
included in the changes currently being published, or (b) your email address
is set for notification in this book's repository.</p>
<p>If you have questions, please contact the CiviCRM documentation working
group here:<br />
<h4>Commits:</h4>
<ul>
{% for commit in commits %}
<li>
<strong><a href="{{ commit.url }}">{{ commit.id }}</a></strong> |
{{ commit.message }} | {{ commit.author.username }}
</li>
{% endfor %}
</ul>
<p>
You are receiving this message because either (a) you have made commits
included in the changes currently being published, or (b) your email address
is set for notification in this book's repository.
</p>
<p>If you have questions, please contact the CiviCRM documentation working group
here:
<br/>
<a href='https://chat.civicrm.org/civicrm/channels/documentation'>
https://chat.civicrm.org/civicrm/channels/documentation</a></p>
https://chat.civicrm.org/civicrm/channels/documentation
</a>
</p>
......@@ -4,11 +4,6 @@ namespace AppBundle\Utils;
class GitHubHookProcessor {
/**
* @var array of strings for email addresses of people to notify
*/
public $recipients = array();
/**
* @var string the URL for the repository
*/
......@@ -19,13 +14,6 @@ class GitHubHookProcessor {
*/
public $branch;
/**
* Constructor
*/
public function __construct() {
}
/**
* Process a GitHub webhook
*
......@@ -35,6 +23,8 @@ class GitHubHookProcessor {
* @param mixed $payload
* An object given by json_decode()
*
* @return array
*
* @throws \Exception
*/
public function process($event, $payload) {
......@@ -48,7 +38,8 @@ class GitHubHookProcessor {
if ($event != 'push') {
throw new \Exception("Webhook event type is not 'push'");
}
$this->getDetailsFromPush($payload);
return $this->getDetailsFromPush($payload);
}
/**
......@@ -58,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");
......@@ -69,27 +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);
}
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));
}
}
......@@ -15,11 +15,11 @@ class PublishControllerTest extends WebTestCase {
$client = static::createClient();
$client->enableProfiler();
$body = $this->getGithubRequestBody();
$hookBody = $this->getGithubRequestBody();
$headers = $this->getHeaders();
$endpoint = '/admin/listen';
$client->request('POST', $endpoint, [], [], $headers, $body);
$client->request('POST', $endpoint, [], [], $headers, $hookBody);
$statusCode = $client->getResponse()->getStatusCode();
$this->assertEquals(Response::HTTP_OK, $statusCode);
......@@ -30,23 +30,19 @@ class PublishControllerTest extends WebTestCase {
$mails = $mailCollector->getMessages();
$this->assertCount(1, $mails);
$hookData = json_decode($hookBody, true);
$sampleCommitHash = current($hookData['commits'])['id'];
$sentMessage = array_shift($mails);
$this->assertContains('Publishing Successful', $sentMessage->getBody());
$this->assertContains($sampleCommitHash, $sentMessage->getBody());
}
/**
* @return string
*/
private function getGithubRequestBody(): string {
$body = [
'ref' => 'refs/heads/master',
'commits' => [],
'repository' => [
'html_url' => 'https://github.com/civicrm/civicrm-dev-docs',
],
];
return json_encode($body);
return file_get_contents(__DIR__ . '/../Files/webhook-push-sample.json');
}
/**
......
This diff is collapsed.
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