Skip to content

Commit

Permalink
Merge pull request #3 from ngraf/feature/channels-on-fail
Browse files Browse the repository at this point in the history
-- feature: "channelOnFail" - with this feature you can specify an alternative list for failure
  • Loading branch information
ngraf authored Dec 21, 2017
2 parents e872d29 + 391de71 commit bfdf4da
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ Enable and configure the extension in your `codeception.yaml`
config:
Codeception\Extension\SlackExtension:
webhook: https://hooks.slack.com/services/...
channel: '#any-channel,@any-user'
username: CI
icon: :ghost:
messagePrefix: '*Smoke-Test*'
messageSuffix: <http://my-ci/my-job|Link>
messageSuffixOnFail: <!channel>
# possible notification strategies: always|successonly|failonly|failandrecover|statuschange
strategy: always
Expand All @@ -52,6 +46,19 @@ Enable and configure the extension in your `codeception.yaml`
# Limit the size of error messages in extended mode. 0 = unlimited. Default value: 80
extendedMaxLength: 80
# customize your message with additional prefix and/or suffix
messagePrefix: '*Smoke-Test*'
messageSuffix: <http://my-ci/my-job|Link>
messageSuffixOnFail: <!channel>
# optional config keys that will overwrite the default configuration of the webhook
channel: '#any-channel,@any-user'
channelOnFail: '#any-channel,@any-user'
username: CI
icon: :ghost:
Example
-----

Expand Down
30 changes: 27 additions & 3 deletions src/Extension/SlackExtension.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
namespace Codeception\Extension;

use Codeception\Events;
use Codeception\Exception\ExtensionException;
use Codeception\Extension;
use Maknz\Slack\Client;
use Maknz\Slack\Message;

Expand All @@ -19,7 +21,7 @@
* SlackExtension:
* webhook: 'https://hooks.slack.com/services/...'
*/
class SlackExtension extends \Codeception\Extension
class SlackExtension extends Extension
{
const STRATEGY_ALWAYS = 'always';
const STRATEGY_FAIL_ONLY = 'failonly';
Expand All @@ -30,7 +32,7 @@ class SlackExtension extends \Codeception\Extension
* @var array list events to listen to
*/
public static $events = array(
'result.print.after' => 'sendTestResults',
Events::RESULT_PRINT_AFTER => 'sendTestResults',
);

/**
Expand Down Expand Up @@ -58,6 +60,13 @@ class SlackExtension extends \Codeception\Extension
*/
protected $channels;

/**
* @var array Array of slack channels for the special case of failure.
* Defined by Codeception config "channelOnFail".
* This Codeception configuration is optional, otherwise "channel" will be used.
*/
protected $channelOnFail;

/**
* @var boolean If set to true notifications will be send only when at least one test fails.
*/
Expand Down Expand Up @@ -100,6 +109,11 @@ public function _initialize()
$client = new Client($this->config['webhook']);

if (isset($this->config['channel'])) {
if (true === empty($this->config['channel'])) {
throw new ExtensionException(
$this, "SlackExtension: The specified value for key \"channel\" must not be empty."
);
}
$this->channels = explode(',', $this->config['channel']);
}

Expand Down Expand Up @@ -132,6 +146,14 @@ public function _initialize()
if (isset($this->config['messageSuffixOnFail'])) {
$this->messageSuffixOnFail = ' ' . $this->config['messageSuffixOnFail'];
}
if (isset($this->config['channelOnFail'])) {
if (true === empty($this->config['channelOnFail'])) {
throw new ExtensionException(
$this, "SlackExtension: The specified value for key \"channelOnFail\" must not be empty."
);
}
$this->channelOnFail = explode(',', $this->config['channelOnFail']);
}

if (isset($this->config['strategy'])) {
if (false === in_array(
Expand Down Expand Up @@ -230,7 +252,9 @@ private function sendFailMessage(\PHPUnit_Framework_TestResult $result)
$this->attachExtendedInformation($this->message, $result);
}

foreach ($this->channels as $channel) {
$targetChannels = isset($this->channelOnFail) ? $this->channelOnFail : $this->channels;

foreach ($targetChannels as $channel) {
$this->message->setChannel(trim($channel));

$this->message->send(
Expand Down

0 comments on commit bfdf4da

Please sign in to comment.