-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack.js
309 lines (297 loc) · 9.35 KB
/
slack.js
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
module.exports = function (gh) {
var events = {
'PR opened': {
conditions: [
{
exists: 'pull_request'
},
{
exists: 'action',
value: 'opened'
}
],
variables: {
'title': 'pull_request.title',
'number': 'number',
'url': 'pull_request.html_url'
},
message: 'PR #%{number} opened: %{title} (%{url})'
},
'PR closed': {
conditions: [
{
exists: 'pull_request'
},
{
exists: 'action',
value: 'closed'
}
],
variables: {
'title': 'pull_request.title',
'number': 'number',
'url': 'pull_request.html_url'
},
message: 'PR #%{number} closed: %{title} (%{url})'
},
'PR reopened': {
conditions: [
{
exists: 'pull_request'
},
{
exists: 'action',
value: 'reopened'
}
],
variables: {
'title': 'pull_request.title',
'number': 'number',
'url': 'pull_request.html_url'
},
message: 'PR #%{number} re-opened: %{title} (%{url})'
},
'PR edited': {
conditions: [
{
exists: 'pull_request'
},
{
exists: 'action',
value: 'edited'
}
],
variables: {
'title': 'pull_request.title',
'number': 'number',
'url': 'pull_request.html_url'
},
message: 'PR #%{number} edited: %{title} (%{url})'
},
'PR assigned': {
conditions: [
{
exists: 'pull_request'
},
{
exists: 'action',
value: 'assigned'
}
],
variables: {
'title': 'pull_request.title',
'number': 'number',
'url': 'pull_request.html_url',
'assignee': 'pull_request.user.login' // @TODO
},
message: 'PR #%{number} assigned to %{assignee}: %{title} (%{url})'
},
'PR/Issue comment': {
conditions: [
{
exists: 'issue'
},
{
exists: 'action',
value: 'created'
}
],
variables: {
'title': 'issue.title',
'number': 'issue.number',
'url': 'issue.comment.html_url',
'author': 'issue.comment.user.login',
'comment': 'issue.comment.body'
},
message: 'New comment on #%{number} (%{title}) by %{author}: %{url}'
},
'Issue opened': {
conditions: [
{
exists: 'issue'
},
{
exists: 'action',
value: 'opened'
}
],
variables: {
'title': 'issue.title',
'number': 'issue.number',
'url': 'issue.html_url'
},
message: 'Issue #%{number} opened: %{title} (%{url})'
},
'Issue closed': {
conditions: [
{
exists: 'issue'
},
{
exists: 'action',
value: 'closed'
}
],
variables: {
'title': 'issue.title',
'number': 'issue.number',
'url': 'issue.html_url'
},
message: 'Issue #%{number} closed: %{title} (%{url})'
},
'Issue reopened': {
conditions: [
{
exists: 'issue'
},
{
exists: 'action',
value: 'reopened'
}
],
variables: {
'title': 'issue.title',
'number': 'issue.number',
'url': 'issue.html_url'
},
message: 'Issue #%{number} re-opened: %{title} (%{url})'
},
'Issue edited': {
conditions: [
{
exists: 'issue'
},
{
exists: 'action',
value: 'edited'
}
],
variables: {
'title': 'issue.title',
'number': 'issue.number',
'url': 'issue.html_url'
},
message: 'Issue #%{number} edited: %{title} (%{url})'
},
'Issue assigned': {
conditions: [
{
exists: 'issue'
},
{
exists: 'action',
value: 'assigned'
}
],
variables: {
'title': 'issue.title',
'number': 'issue.number',
'url': 'issue.html_url',
'assignee': 'issue.user.login' // @TODO
},
message: 'Issue #%{number} assigned to %{assignee}: %{title} (%{url})'
},
'Fork': {
conditions: [
{
exists: 'forkee'
}
],
variables: {
'project': 'forkee.repository.full_name',
'url': 'forkee.html_url',
'author': 'forkee.owner.login'
},
message: '%{project} was forked by %{author}: %{url}'
},
'Tag': {
conditions: [
{
exists: 'release'
},
{
exists: 'action',
value: 'published'
}
],
variables: {
'project': 'repository.full_name',
'version': 'release.tag_name'
},
message: 'New version of %{project} published: %{version}'
}
};
function isThisEvent(eventName) {
var evt = events[eventName];
for (var i = 0; i < evt.conditions.length; i++) {
var propertyToExist = evt.conditions[i].exists,
valueToHave = evt.conditions[i].value;
if (!gh.data.payload.hasOwnProperty(propertyToExist)) {
return false;
}
else if (valueToHave && valueToHave !== gh.data.payload[propertyToExist]) {
return false;
}
}
return evt;
}
var eventName = gh.data.parameters.event; // e.g. 'Issue assigned'
var event; // we'll define this later, but need access to it globally
if (!events[eventName] && eventName !== 'All events') {
gh.process.fail('Invalid event: "' + eventName + '"');
}
else if (eventName === 'All events') {
// find the corresponding event information for this payload
for (var evtName in events) { if (events.hasOwnProperty(evtName)) {
event = isThisEvent(evtName);
if (event) {
break;
}
else {
event = false;
}
}}
if (!event) {
gh.process.succeed('This appears to be one of the few events we are not interested in.');
}
}
else {
// this block checks if this is the event we want to subscribe to
event = isThisEvent(eventName);
if (!event) {
gh.process.succeed('Event not identified as a "' + eventName + '" event. Skipping.');
}
}
// if we get this far, then this IS the event we've subscribed to, so we want to send the Slack message
// just need to replace any template variables first.
var message = gh.data.parameters.text;
// override with default message if blank
if (message === '') {
message = event.message;
}
// now replace the template variables
for (var name in event.variables) {
var nestedProperties = event.variables[name]; // e.g. 'foo.bar'
var getter = new Function('obj', 'return obj.' + nestedProperties + ';');
var value = getter(gh.data.payload);
message = message.replace('%{' + name + '}', value);
}
// update the parameter before we pass to Slack
gh.data.parameters.text = message;
var options = {
url: gh.data.parameters.url,
headers: {
'Content-Type': 'application/json',
'User-Agent': 'githook-slack',
},
json: gh.data.parameters
};
gh.modules.request.post(options, function (err, httpResponse, body) {
if (err) {
gh.process.fail('Fail! Response: ' + err);
}
else {
gh.process.succeed('Successfully posted to Slack.');
}
});
};