forked from php-censor/php-censor
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSlackNotify.php
More file actions
137 lines (113 loc) · 3.53 KB
/
Copy pathSlackNotify.php
File metadata and controls
137 lines (113 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace PHPCensor\Plugin;
use PHPCensor\Builder;
use PHPCensor\Model\Build;
use PHPCensor\Plugin;
use Maknz\Slack\Client;
use Maknz\Slack\Attachment;
use Maknz\Slack\AttachmentField;
/**
* Slack Plugin
*
* @author Stephen Ball <phpci@stephen.rebelinblue.com>
*/
class SlackNotify extends Plugin
{
private $webHook;
private $room;
private $username;
private $message;
private $icon;
private $show_status;
/**
* @return string
*/
public static function pluginName()
{
return 'slack_notify';
}
/**
* {@inheritdoc}
*/
public function __construct(Builder $builder, Build $build, array $options = [])
{
parent::__construct($builder, $build, $options);
if (is_array($options) && isset($options['webhook_url'])) {
$this->webHook = trim($options['webhook_url']);
if (isset($options['message'])) {
$this->message = $options['message'];
} else {
$this->message = '<%PROJECT_URI%|%PROJECT_TITLE%> - <%BUILD_URI%|Build #%BUILD%> has finished ';
$this->message .= 'for commit <%COMMIT_URI%|%SHORT_COMMIT% (%COMMIT_EMAIL%)> ';
$this->message .= 'on branch <%BRANCH_URI%|%BRANCH%>';
}
if (isset($options['room'])) {
$this->room = $options['room'];
} else {
$this->room = '#php-censor';
}
if (isset($options['username'])) {
$this->username = $options['username'];
} else {
$this->username = 'PHP Censor';
}
if (isset($options['show_status'])) {
$this->show_status = (bool) $options['show_status'];
} else {
$this->show_status = true;
}
if (isset($options['icon'])) {
$this->icon = $options['icon'];
}
} else {
throw new \Exception('Please define the webhook_url for slack_notify plugin!');
}
}
/**
* Run the Slack plugin.
* @return bool
*/
public function execute()
{
$body = $this->builder->interpolate($this->message);
$client = new Client($this->webHook);
$message = $client->createMessage();
if (!empty($this->room)) {
$message->setChannel($this->room);
}
if (!empty($this->username)) {
$message->setUsername($this->username);
}
if (!empty($this->icon)) {
$message->setIcon($this->icon);
}
// Include an attachment which shows the status and hide the message
if ($this->show_status) {
$successfulBuild = $this->build->isSuccessful();
if ($successfulBuild) {
$status = 'Success';
$color = 'good';
} else {
$status = 'Failed';
$color = 'danger';
}
// Build up the attachment data
$attachment = new Attachment([
'fallback' => $body,
'pretext' => $body,
'color' => $color,
'fields' => [
new AttachmentField([
'title' => 'Status',
'value' => $status,
'short' => false
])
]
]);
$message->attach($attachment);
$body = '';
}
$message->send($body);
return true;
}
}