forked from deployphp/recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.php
149 lines (129 loc) · 4.66 KB
/
slack.php
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
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/* (c) Tomas Majer <tomasmajer@gmail.com>
/* (c) Elan Ruusamäe <glen@delfi.ee>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Deployer;
/*
* Get local username
*/
set('local_user', function () {
return trim(run('whoami'));
});
// Do not skip slack notifications by default
set('slack_skip_notification', false);
desc('Notifying Slack channel of deployment');
task('deploy:slack', function () {
if (true === get('slack_skip_notification')) {
return;
}
global $php_errormsg;
$user = trim(runLocally('git config --get user.name'));
$revision = trim(runLocally('git log -n 1 --format="%h"'));
$stage = get('stages')[0];
$branch = get('branch');
if (input()->hasOption('branch')) {
$inputBranch = input()->getOption('branch');
if (!empty($inputBranch)) {
$branch = $inputBranch;
}
}
$defaultConfig = [
'channel' => '#general',
'icon' => ':sunny:',
'username' => 'Deploy',
'message' => "Deployment to `{{host}}` on *{{stage}}* was successful\n({{release_path}})",
'app' => 'app-name',
'unset_text' => true,
'attachments' => [
[
'text' => sprintf(
'Revision %s deployed to %s by %s',
substr($revision, 0, 6),
$stage,
$user
),
'title' => 'Deployment Complete',
'fallback' => sprintf('Deployment to %s complete.', $stage),
'color' => '#7CD197',
'fields' => [
[
'title' => 'User',
'value' => $user,
'short' => true,
],
[
'title' => 'Stage',
'value' => $stage,
'short' => true,
],
[
'title' => 'Branch',
'value' => $branch,
'short' => true,
],
[
'title' => 'Host',
'value' => get('server.name'),
'short' => true,
],
],
],
],
];
$newConfig = get('slack');
if (is_callable($newConfig)) {
$newConfig = $newConfig();
}
$config = array_merge($defaultConfig, (array) $newConfig);
if (!is_array($config) || !isset($config['token']) || !isset($config['team']) || !isset($config['channel'])) {
throw new \RuntimeException("Please configure new slack: set('slack', ['token' => 'xoxp...', 'team' => 'team', 'channel' => '#channel', 'messsage' => 'message to send']);");
}
$server = \Deployer\Task\Context::get()->getServer();
if ($server instanceof \Deployer\Server\Local) {
$user = get('local_user');
} else {
$user = $server->getConfiguration()->getUser() ?: null;
}
$messagePlaceHolders = [
'{{release_path}}' => get('release_path'),
'{{host}}' => get('server.host'),
'{{stage}}' => $stage,
'{{user}}' => $user,
'{{branch}}' => $branch,
'{{app_name}}' => $config['app'] ?? 'app-name',
];
$config['message'] = strtr($config['message'], $messagePlaceHolders);
$urlParams = [
'channel' => $config['channel'],
'token' => $config['token'],
'text' => $config['message'],
'username' => $config['username'],
'icon_emoji' => $config['icon'],
'pretty' => true,
];
foreach (['unset_text' => 'text', 'icon_url' => 'icon_emoji'] as $set => $unset) {
if (isset($config[$set])) {
unset($urlParams[$unset]);
}
}
foreach (['parse', 'link_names', 'icon_url', 'unfurl_links', 'unfurl_media', 'as_user'] as $option) {
if (isset($config[$option])) {
$urlParams[$option] = $config[$option];
}
}
if (isset($config['attachments'])) {
$urlParams['attachments'] = json_encode($config['attachments']);
}
$url = 'https://slack.com/api/chat.postMessage?' . http_build_query($urlParams);
$result = @file_get_contents($url);
if (!$result) {
throw new \RuntimeException($php_errormsg);
}
$response = @json_decode($result);
if (!$response || isset($response->error)) {
throw new \RuntimeException($response->error);
}
})->desc('Notifying Slack channel of deployment');