1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Behatch\Notifier; |
4
|
|
|
|
5
|
|
|
class Slack extends Notifier |
6
|
|
|
{ |
7
|
|
|
static private $url; |
8
|
|
|
static private $settings; |
9
|
|
|
static private $prefix; |
10
|
|
|
static private $attachment; |
11
|
|
|
|
12
|
|
|
public function __construct($url, $settings = [], $prefix = null, $attachment = [], $spamTimeout = 60) |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
if (!class_exists('\\Maknz\\Slack\\Client')) { |
16
|
|
|
$message = 'Class \Maknz\Slack\Client does not exist. Are you sure you have installed it? i.e. composer require "maknz/slack"'; |
17
|
|
|
throw new \Exception( $message ); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
parent::__construct($spamTimeout); |
21
|
|
|
|
22
|
|
|
self::$url = $url; |
23
|
|
|
self::$prefix = $prefix; |
24
|
|
|
|
25
|
|
|
self::$settings = $settings + [ |
26
|
|
|
'username' => 'Behat', |
27
|
|
|
'channel' => '#general', |
28
|
|
|
'link_names' => true, |
29
|
|
|
'icon' => ':fire:', |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
self::$attachment = $attachment + [ |
33
|
|
|
'fallback' => 'Behat test failed', |
34
|
|
|
'text' => '', |
35
|
|
|
'pretext' => '', |
36
|
|
|
'color' => 'danger', |
37
|
|
|
'fields' => array(), |
38
|
|
|
]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected static function notify($status, $title, $message) |
42
|
|
|
{ |
43
|
|
|
|
44
|
|
|
// We don't need the second notification about the suite failing |
45
|
|
|
if (strpos($title, 'suite ended') !== false) { |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// If there's a prefix, prepend it to the title of the message |
50
|
|
View Code Duplication |
if (self::$prefix !== null) { |
51
|
|
|
$title = '[' . self::$prefix . '] ' . $title; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Fire up the Slack Client with the given webhook url and settings |
55
|
|
|
$client = new \Maknz\Slack\Client(self::$url, self::$settings); |
56
|
|
|
|
57
|
|
|
// Split the message on > as the behat step failures give the regex then the message |
58
|
|
|
// We just want the piece after the final '>'. @TODO: Better option here? As this will |
|
|
|
|
59
|
|
|
// fail horribly if there is a '>' character in the Exception |
60
|
|
|
$splitMessage = explode( '>', $message ); |
61
|
|
|
|
62
|
|
|
// Build the attachment |
63
|
|
|
self::$attachment['pretext'] = $title; |
64
|
|
|
|
65
|
|
|
self::$attachment['fields'] = array( |
66
|
|
|
array( |
67
|
|
|
'title' => 'Step', |
68
|
|
|
'value' => end($splitMessage), |
69
|
|
|
'short' => false, |
70
|
|
|
), |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
// Send! |
74
|
|
|
$client->attach(self::$attachment)->enableMarkdown()->send(); |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|