forked from malko/rocketchat-gitlab-hook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitlab-rocketchat.hooks.js
475 lines (451 loc) · 14.8 KB
/
gitlab-rocketchat.hooks.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/* eslint no-console:0, max-len:0, complexity:0 */
// see https://gitlab.com/help/web_hooks/web_hooks for full json posted by GitLab
const MENTION_ALL_ALLOWED = false; // <- check that bot permission 'mention-all' are activated in rocketchat before passing this to true.
const NOTIF_COLOR = '#6498CC';
const IGNORE_CONFIDENTIAL = true;
const IGNORE_UNKNOWN_EVENTS = false;
const IGNORE_ERROR_MESSAGES = false;
const USE_ROCKETCHAT_AVATAR = false;
const DEFAULT_AVATAR = null; // <- null means use the avatar from settings if no other is available
const STATUSES_COLORS = {
success: '#2faa60',
pending: '#e75e40',
failed: '#d22852',
canceled: '#5c5c5c',
created: '#ffc107',
running: '#607d8b',
};
const ACTION_VERBS = {
create: 'created',
destroy: 'removed',
update: 'updated',
rename: 'renamed',
transfer: 'transferred',
add: 'added',
remove: 'removed',
};
const ATTACHMENT_TITLE_SIZE = 10; // Put 0 here to have not title as in previous versions
const refParser = (ref) => ref.replace(/^refs\/(?:tags|heads)\/(.+)$/, '$1');
const displayName = (name) => (name && name.toLowerCase().replace(/\s+/g, '.').normalize('NFD').replace(/[\u0300-\u036f]/g, ''));
const atName = (user) => (user && user.name ? '@' + displayName(user.name) : '');
const makeAttachment = (author, text, timestamp, color) => {
const currentTime = (new Date()).toISOString();
const attachment = {
author_name: author ? displayName(author.name) : '',
author_icon: author ? author.avatar_url : '',
ts: timestamp || currentTime,
text,
color: color || NOTIF_COLOR
};
if (ATTACHMENT_TITLE_SIZE > 0) {
attachment.title = text.substring(0, ATTACHMENT_TITLE_SIZE) + '...';
}
return attachment;
};
const pushUniq = (array, val) => ~array.indexOf(val) || array.push(val); // eslint-disable-line
class Script { // eslint-disable-line
process_incoming_request({ request }) {
try {
let result = null;
const channel = request.url.query.channel;
const event = request.headers['x-gitlab-event'];
switch (event) {
case 'Push Hook':
result = this.pushEvent(request.content);
break;
case 'Merge Request Hook':
result = this.mergeRequestEvent(request.content);
break;
case 'Note Hook':
result = this.commentEvent(request.content);
break;
case 'Confidential Issue Hook':
case 'Issue Hook':
result = this.issueEvent(request.content, event);
break;
case 'Tag Push Hook':
result = this.tagEvent(request.content);
break;
case 'Pipeline Hook':
case 'Pipeline Event':
result = this.pipelineEvent(request.content);
break;
case 'Build Hook': // GitLab < 9.3.0
case 'Job Hook': // GitLab >= 9.3.0
result = this.buildEvent(request.content);
break;
case 'Wiki Page Hook':
result = this.wikiEvent(request.content);
break;
case 'System Hook':
result = this.systemEvent(request.content);
break;
case "Release Hook":
result = this.releaseEvent(request.content);
break;
default:
if (IGNORE_UNKNOWN_EVENTS) {
console.log('gitlabevent unknown', event);
return { error: { success: false, message: `unknonwn event ${event}` } };
}
result = IGNORE_UNKNOWN_EVENTS ? null : this.unknownEvent(request, event);
break;
}
if (result && result.content && channel) {
result.content.channel = '#' + channel;
}
return result;
} catch (e) {
console.log('gitlabevent error', e);
return this.createErrorChatMessage(e);
}
}
createErrorChatMessage(error) {
if (IGNORE_ERROR_MESSAGES) {
return { error: { success: false, message: `gitlabevent error: ${error.message}` } };
}
return {
content: {
username: 'Rocket.Cat ErrorHandler',
text: 'Error occured while parsing an incoming webhook request. Details attached.',
icon_url: USE_ROCKETCHAT_AVATAR ? null : DEFAULT_AVATAR,
attachments: [
{
text: `Error: '${error}', \n Message: '${error.message}', \n Stack: '${error.stack}'`,
color: NOTIF_COLOR
}
]
}
};
}
unknownEvent(data, event) {
const user_avatar = data.user ? data.user.avatar_url : (data.user_avatar || DEFAULT_AVATAR);
return {
content: {
username: data.user ? data.user.name : (data.user_name || 'Unknown user'),
text: `Unknown event '${event}' occured. Data attached.`,
icon_url: USE_ROCKETCHAT_AVATAR ? null : user_avatar,
attachments: [
{
text: `${JSON.stringify(data, null, 4)}`,
color: NOTIF_COLOR
}
]
}
};
}
issueEvent(data, event) {
if (event === 'Confidential Issue Hook' && IGNORE_CONFIDENTIAL) {
return false;
}
const project = data.project || data.repository;
const state = data.object_attributes.state;
const action = data.object_attributes.action;
const time = data.object_attributes.updated_at;
const project_avatar = project.avatar_url || data.user.avatar_url || DEFAULT_AVATAR;
let user_action = state;
let assigned = '';
if (action === 'update') {
user_action = 'updated';
}
if (data.assignee) {
assigned = `*Assigned to*: @${data.assignee.username}\n`;
}
return {
content: {
username: 'gitlab/' + project.name,
icon_url: USE_ROCKETCHAT_AVATAR ? null : project_avatar,
text: (data.assignee && data.assignee.name !== data.user.name) ? atName(data.assignee) : '',
attachments: [
makeAttachment(
data.user,
`${user_action} an issue _${data.object_attributes.title}_ on ${project.name}.
*Description:* ${data.object_attributes.description}.
${assigned}
See: ${data.object_attributes.url}`,
time
)
]
}
};
}
commentEvent(data) {
const project = data.project || data.repository;
const comment = data.object_attributes;
const user = data.user;
const avatar = project.avatar_url || user.avatar_url || DEFAULT_AVATAR;
const at = [];
let text;
if (data.merge_request) {
const mr = data.merge_request;
const lastCommitAuthor = mr.last_commit && mr.last_commit.author;
if (mr.assignee && mr.assignee.name !== user.name) {
at.push(atName(mr.assignee));
}
if (lastCommitAuthor && lastCommitAuthor.name !== user.name) {
pushUniq(at, atName(lastCommitAuthor));
}
text = `commented on MR [#${mr.id} ${mr.title}](${comment.url})`;
} else if (data.commit) {
const commit = data.commit;
const message = commit.message.replace(/\n[^\s\S]+/, '...').replace(/\n$/, '');
if (commit.author && commit.author.name !== user.name) {
at.push(atName(commit.author));
}
text = `commented on commit [${commit.id.slice(0, 8)} ${message}](${comment.url})`;
} else if (data.issue) {
const issue = data.issue;
text = `commented on issue [#${issue.id} ${issue.title}](${comment.url})`;
} else if (data.snippet) {
const snippet = data.snippet;
text = `commented on code snippet [#${snippet.id} ${snippet.title}](${comment.url})`;
}
return {
content: {
username: 'gitlab/' + project.name,
icon_url: USE_ROCKETCHAT_AVATAR ? null : avatar,
text: at.join(' '),
attachments: [
makeAttachment(user, `${text}\n${comment.note}`, comment.updated_at)
]
}
};
}
mergeRequestEvent(data) {
const user = data.user;
const mr = data.object_attributes;
const assignee = data.assignee;
const avatar = mr.target.avatar_url || mr.source.avatar_url || user.avatar_url || DEFAULT_AVATAR;
let at = [];
if (mr.action === 'open' && assignee) {
at.push(atName(assignee));
} else if (mr.action === 'merge') {
const lastCommitAuthor = mr.last_commit && mr.last_commit.author;
if (assignee && assignee.username !== user.username) {
at.push(atName(assignee));
}
if (lastCommitAuthor && lastCommitAuthor.username !== user.username) {
pushUniq(at, atName(lastCommitAuthor));
}
} else if (mr.action === 'update' && assignee) {
at.push(atName(assignee));
}
return {
content: {
username: `gitlab/${mr.target.name}`,
icon_url: USE_ROCKETCHAT_AVATAR ? null : avatar,
text: at.join(' '),
attachments: [
makeAttachment(user, `${mr.action} MR [#${mr.iid} ${mr.title}](${mr.url})\n${mr.source_branch} into ${mr.target_branch}`, mr.updated_at)
]
}
};
}
pushEvent(data) {
const project = data.project || data.repository;
const web_url = project.web_url || project.homepage;
const user = {
name: data.user_name,
avatar_url: data.user_avatar
};
const avatar = project.avatar_url || data.user_avatar || DEFAULT_AVATAR;
// branch removal
if (data.checkout_sha === null && !data.commits.length) {
return {
content: {
username: `gitlab/${project.name}`,
icon_url: USE_ROCKETCHAT_AVATAR ? null : avatar,
attachments: [
makeAttachment(user, `removed branch ${refParser(data.ref)} from [${project.name}](${web_url})`)
]
}
};
}
// new branch
if (data.before == 0) { // eslint-disable-line
return {
content: {
username: `gitlab/${project.name}`,
icon_url: USE_ROCKETCHAT_AVATAR ? null : avatar,
attachments: [
makeAttachment(user, `pushed new branch [${refParser(data.ref)}](${web_url}/commits/${refParser(data.ref)}) to [${project.name}](${web_url}), which is ${data.total_commits_count} commits ahead of master`)
]
}
};
}
return {
content: {
username: `gitlab/${project.name}`,
icon_url: USE_ROCKETCHAT_AVATAR ? null : avatar,
attachments: [
makeAttachment(user, `pushed ${data.total_commits_count} commits to branch [${refParser(data.ref)}](${web_url}/commits/${refParser(data.ref)}) in [${project.name}](${web_url})`),
{
text: data.commits.map((commit) => ` - ${new Date(commit.timestamp).toUTCString()} [${commit.id.slice(0, 8)}](${commit.url}) by ${commit.author.name}: ${commit.message.replace(/\s*$/, '')}`).join('\n'),
color: NOTIF_COLOR
}
]
}
};
}
tagEvent(data) {
const project = data.project || data.repository;
const web_url = project.web_url || project.homepage;
const tag = refParser(data.ref);
const user = {
name: data.user_name,
avatar_url: data.user_avatar
};
const avatar = project.avatar_url || data.user_avatar || DEFAULT_AVATAR;
let message;
if (data.checkout_sha === null) {
message = `deleted tag [${tag}](${web_url}/tags/)`;
} else {
message = `pushed tag [${tag} ${data.checkout_sha.slice(0, 8)}](${web_url}/tags/${tag})`;
}
return {
content: {
username: `gitlab/${project.name}`,
icon_url: USE_ROCKETCHAT_AVATAR ? null : avatar,
text: MENTION_ALL_ALLOWED ? '@all' : '',
attachments: [
makeAttachment(user, message)
]
}
};
}
pipelineEvent(data) {
const project = data.project || data.repository;
const commit = data.commit;
const user = {
name: data.user_name,
avatar_url: data.user_avatar
};
const pipeline = data.object_attributes;
const pipeline_time = pipeline.finished_at || pipeline.created_at;
const avatar = project.avatar_url || data.user_avatar || DEFAULT_AVATAR;
return {
content: {
username: `gitlab/${project.name}`,
icon_url: USE_ROCKETCHAT_AVATAR ? null : avatar,
attachments: [
makeAttachment(user, `pipeline returned *${pipeline.status}* for commit [${commit.id.slice(0, 8)}](${commit.url}) made by *${commit.author.name}*`, pipeline_time, STATUSES_COLORS[pipeline.status])
]
}
};
}
buildEvent(data) {
const user = {
name: data.user_name,
avatar_url: data.user_avatar
};
return {
content: {
username: `gitlab/${data.repository.name}`,
icon_url: USE_ROCKETCHAT_AVATAR ? null : DEFAULT_AVATAR,
attachments: [
makeAttachment(user, `build named *${data.build_name}* returned *${data.build_status}* for [${data.project_name}](${data.repository.homepage})`, null, STATUSES_COLORS[data.build_status])
]
}
};
}
wikiPageTitle(wiki_page) {
if (wiki_page.action === 'delete') {
return wiki_page.title;
}
return `[${wiki_page.title}](${wiki_page.url})`;
}
wikiEvent(data) {
const user_name = data.user.name;
const project = data.project;
const project_path = project.path_with_namespace;
const wiki_page = data.object_attributes;
const wiki_page_title = this.wikiPageTitle(wiki_page);
const user_action = ACTION_VERBS[wiki_page.action] || 'modified';
const avatar = project.avatar_url || data.user.avatar_url || DEFAULT_AVATAR;
return {
content: {
username: project_path,
icon_url: USE_ROCKETCHAT_AVATAR ? null : avatar,
text: `The wiki page ${wiki_page_title} was ${user_action} by ${user_name}`
}
};
}
releaseEvent(data) {
const project = data.project;
const project_name = project.name;
const avatar = project.avatar_url || DEFAULT_AVATAR;
const user = {
name: data.commit.author.name,
email: data.commit.author.email,
avatar_url: avatar
};
const tag = data.tag;
const user_action = ACTION_VERBS[data.action] || "modified";
const release_description = data.description;
return {
content: {
username: `gitlab/${project_name}`,
icon_url: USE_ROCKETCHAT_AVATAR ? null : DEFAULT_AVATAR,
text: `${user_action} release for ${project_name} with tag ${tag} by ${user.name}`,
attachments: [makeAttachment(user, release_description)],
},
};
}
systemEvent(data) {
const event_name = data.event_name;
const [, eventType] = data.event_name.split('_');
const action = eventType in ACTION_VERBS ? ACTION_VERBS[eventType] : '';
let text = '';
switch (event_name) {
case 'project_create':
case 'project_destroy':
case 'project_update':
text = `Project \`${data.path_with_namespace}\` ${action}.`;
break;
case 'project_rename':
case 'project_transfer':
text = `Project \`${data.old_path_with_namespace}\` ${action} to \`${data.path_with_namespace}\`.`;
break;
case 'user_add_to_team':
case 'user_remove_from_team':
text = `User \`${data.user_username}\` was ${action} to project \`${data.project_path_with_namespace}\` with \`${data.project_access}\` access.`;
break;
case 'user_add_to_group':
case 'user_remove_from_group':
text = `User \`${data.user_username}\` was ${action} to group \`${data.group_path}\` with \`${data.group_access}\` access.`;
break;
case 'user_create':
case 'user_destroy':
text = `User \`${data.username}\` was ${action}.`;
break;
case 'user_rename':
text = `User \`${data.old_username}\` was ${action} to \`${data.username}\`.`;
break;
case 'key_create':
case 'key_destroy':
text = `Key \`${data.username}\` was ${action}.`;
break;
case 'group_create':
case 'group_destroy':
text = `Group \`${data.path}\` was ${action}.`;
break;
case 'group_rename':
text = `Group \`${data.old_full_path}\` was ${action} to \`${data.full_path}\`.`;
break;
default:
text = 'Unknown system event';
break;
}
return {
content: {
text: `${text}`,
attachments: [
{
text: `${JSON.stringify(data, null, 4)}`,
color: NOTIF_COLOR
}
]
}
};
}
}