Allow to specify a mail template ('short' or 'long') in phpci.yml.

Email plugin: checks if the custom template exists before trying to use it.

Closed #933
This commit is contained in:
Adirelle 2015-04-22 13:24:00 +02:00 committed by Tobias van Beek
parent 590ab5fae5
commit 5cd29ca7fc
7 changed files with 78 additions and 37 deletions

View file

@ -9,11 +9,13 @@
namespace PHPCI\Plugin;
use Exception;
use b8\View;
use PHPCI\Builder;
use PHPCI\Helper\Lang;
use PHPCI\Model\Build;
use PHPCI\Helper\Email as EmailHelper;
use Psr\Log\LogLevel;
/**
* Email Plugin - Provides simple email capability to PHPCI.
@ -70,12 +72,25 @@ class Email implements \PHPCI\Plugin
$buildStatus = $this->build->isSuccessful() ? "Passing Build" : "Failing Build";
$projectName = $this->build->getProject()->getTitle();
$mailTemplate = $this->build->isSuccessful() ? 'Email/success' : 'Email/failed';
$view = new View($mailTemplate);
try {
$view = $this->getMailTemplate();
} catch (Exception $e) {
$this->phpci->log(
sprintf('Unknown mail template "%s", falling back to default.', $this->options['template']),
LogLevel::WARNING
);
$view = $this->getDefaultMailTemplate();
}
$view->build = $this->build;
$view->project = $this->build->getProject();
$body = $view->render();
$layout = new View('Email/layout');
$layout->build = $this->build;
$layout->project = $this->build->getProject();
$layout->content = $view->render();
$body = $layout->render();
$sendFailures = $this->sendSeparateEmails(
$addresses,
@ -97,7 +112,7 @@ class Email implements \PHPCI\Plugin
* @param string $body Email body
* @return array Array of failed addresses
*/
public function sendEmail($toAddress, $ccList, $subject, $body)
protected function sendEmail($toAddress, $ccList, $subject, $body)
{
$email = new EmailHelper();
@ -168,6 +183,7 @@ class Email implements \PHPCI\Plugin
/**
* Get the list of email addresses to CC.
*
* @return array
*/
protected function getCcAddresses()
@ -182,4 +198,30 @@ class Email implements \PHPCI\Plugin
return $ccAddresses;
}
/**
* Get the mail template used to sent the mail.
*
* @return View
*/
protected function getMailTemplate()
{
if (isset($this->options['template'])) {
return new View('Email/' . $this->options['template']);
}
return $this->getDefaultMailTemplate();
}
/**
* Get the default mail template.
*
* @return View
*/
protected function getDefaultMailTemplate()
{
$template = $this->build->isSuccessful() ? 'short' : 'long';
return new View('Email/' . $template);
}
}

View file

@ -1,15 +0,0 @@
<div style="background: #900; padding: 25px;">
<div style="background: #fff; padding: 15px; border-radius: 5px">
<div style="font-family: arial, verdana, sans-serif; font-size: 25px; margin-bottom: 15px">
<?php print $project->getTitle(); ?> - Build #<?php print $build->getId(); ?>
</div>
<div style="font-family: arial, verdana, sans-serif; font-size: 15px">
<p>Your commit <strong><?php print $build->getCommitId(); ?></strong> caused a failed build in project <strong><?php print $project->getTitle(); ?></strong>.</p>
<p style="margin: 10px; background: #fafafa"><?php print $build->getCommitMessage(); ?></p>
<p>Please review <a href="<?php print $build->getCommitLink(); ?>">your commit</a> and the <a href="<?php print PHPCI_URL . 'build/view/' . $build->getId(); ?>">build log</a>.</p>
</div>
</div>
</div>

View file

@ -0,0 +1,28 @@
<html>
<head>
<style>
<?php include(__DIR__ . '/../../../public/assets/css/ansi-colors.css'); ?>
body { font-family: arial, verdana, sans-serif; font-size: 15px; }
header { font-size: 25px; margin-bottom: 15px; }
</style>
</head>
<body>
<div style="background: #<?php print $build->isSuccessful() ? '090' : '900'; ?>; padding: 25px;">
<div style="background: #fff; padding: 15px; border-radius: 5px">
<header><?php print $project->getTitle(); ?> - Build #<?php print $build->getId(); ?></header>
<div>
<p>
Your commit <strong><?php print $build->getCommitId(); ?></strong> generated a
<?php print $build->isSuccessful() ? 'successful' : 'failed'; ?> build in project
<strong><?php print $project->getTitle(); ?></strong>.
</p>
<?php print $content; ?>
</div>
<footer>
You can review <a href="<?php print $build->getCommitLink(); ?>">your commit</a>
and the <a href="<?php print PHPCI_URL . 'build/view/' . $build->getId(); ?>">build log</a>.
</footer>
</div>
</div>
</body>
</html>

View file

@ -0,0 +1,2 @@
<p style="margin: 10px; background: #fafafa"><?php print $build->getCommitMessage(); ?></p>
<pre class="ansi_color_bg_black ansi_color_fg_white" style="padding: 4px"><?php print \PHPCI\Helper\AnsiConverter::convert($build->getLog()); ?></pre>

View file

@ -0,0 +1 @@
<p style="margin: 10px; background: #fafafa"><?php print $build->getCommitMessage(); ?></p>

View file

@ -1,17 +0,0 @@
<?php use PHPCI\Helper\AnsiConverter; ?>
<style><?php include(__DIR__ . '/../../../public/assets/css/ansi-colors.css'); ?></style>
<div style="background: #090; padding: 25px;">
<div style="background: #fff; padding: 15px; border-radius: 5px">
<div style="font-family: arial, verdana, sans-serif; font-size: 25px; margin-bottom: 15px">
<?php print $project->getTitle(); ?> - Build #<?php print $build->getId(); ?>
</div>
<div style="font-family: arial, verdana, sans-serif; font-size: 15px">
<p>Your commit <strong><?php print $build->getCommitId(); ?></strong> genrate a successfull build in project <strong><?php print $project->getTitle(); ?></strong>.</p>
<p style="margin: 10px; background: #fafafa"><?php print $build->getCommitMessage(); ?></p>
<pre class="ansi_color_bg_black ansi_color_fg_white" style="padding: 4px"><?php print AnsiConverter::convert($build->getLog()); ?></pre>
<p>You can review <a href="<?php print $build->getCommitLink(); ?>">your commit</a> and the <a href="<?php print PHPCI_URL . 'build/view/' . $build->getId(); ?>">build log</a>.</p>
</div>
</div>
</div>

View file

@ -349,7 +349,7 @@ class EmailTest extends \PHPUnit_Framework_TestCase
$this->testedEmailPlugin->execute();
$this->assertContains('Passing', $this->message['subject']);
$this->assertContains('successfull', $this->message['body']);
$this->assertContains('successful', $this->message['body']);
}
/**