forked from malko/rocketchat-gitlab-hook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitlab-rocketchat.hooks.js
145 lines (137 loc) · 4.39 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
/*jshint esnext:true*/
// see https://gitlab.com/help/web_hooks/web_hooks for full json posted by GitLab
const NOTIF_COLOR = '#6498CC';
const refParser = (ref) => ref.replace(/^refs\/(?:tags|heads)\/(.+)$/,'$1');
class Script {
process_incoming_request({request}) {
try {
switch(request.headers['x-gitlab-event']){
case 'Push Hook':
return this.pushEvent(request.content);
case 'Merge Request Hook':
return this.mergeRequestEvent(request.content);
case 'Note Hook':
return this.commentEvent(request.content);
case 'Issue Hook':
return this.issueEvent(request.content);
case 'Tag Push Hook':
return this.tagEvent(request.content);
}
} catch(e) {
console.log('gitlabevent error', e);
return {
error: {
success: false,
message: e.message || e
}
};
}
}
issueEvent(data) {
return {
content: {
username: data.user.name,
text: `${data.user.username} ${data.object_attributes.state} an issue _${data.object_attributes.title}_ on ${data.project.name}.
*Description:* ${data.object_attributes.description}.
See: ${data.object_attributes.url}`,
icon_url: data.user.avatar_url,
attachments:[]
}
};
}
commentEvent(data) {
const comment = data.object_attributes;
const user = data.user;
let text;
if (data.merge_request) {
let mr = data.merge_request;
text = `${user.name} commented on merge request #${mr.id} [${mr.title}](${comment.url})`;
} else if (data.commit) {
let commit = data.commit;
let message = commit.message.replace(/\n[^\s\S]+/, '...').replace(/\n$/,'');
text = `${user.name} commented on commit [${commit.id.slice(0, 8)} ${message}](${comment.url})`;
} else if (data.issue) {
let issue = data.issue;
text = `${user.name} commented on issue [#${issue.id} ${issue.title}](${comment.url})`;
} else if (data.snippet) {
let snippet = data.snippet;
text = `${user.name} commented on code snippet [#${snippet.id} ${snippet.title}](${comment.url})`;
}
return {
content: {
username: 'gitlab/' + data.project.name,
icon_url: data.project.avatar_url || user.avatar_url || '',
text,
attachments: [
{
text: comment.note,
color: NOTIF_COLOR
}
]
}
};
}
mergeRequestEvent(data) {
const user = data.user;
const mr = data.object_attributes;
return {
content: {
username: `gitlab/${mr.target.name}`,
icon_url: mr.target.avatar_url || mr.source.avatar_url || user.avatar_url || '',
text: `${user.name} ${mr.action} Merge Request [#${mr.iid} ${mr.title}](${mr.url})`,
attachments: [
{
text: `${mr.source_branch} into ${mr.target_branch}`,
color: NOTIF_COLOR
}
]
}
};
}
pushEvent(data) {
const project = data.project;
if (data.checkout_sha === null && !data.commits.length) {
return {
content: {
username: `gitlab/${project.name}`,
text: `${data.user_name} removed branch ${refParser(data.ref)} from [${project.name}](${project.web_url})`,
icon_url: project.avatar_url || data.user_avatar || '',
attachments:[]
}
};
}
if (data.before == 0) {
return {
content: {
username: `gitlab/${project.name}`,
text: `${data.user_name} pushed new branch [${refParser(data.ref)}](${project.web_url}/commits/${refParser(data.ref)}) to [${project.name}](${project.web_url}), which is ${data.total_commits_count} commits ahead of master`,
icon_url: project.avatar_url || data.user_avatar || '',
attachments: []
}
};
}
return {
content: {
username: `gitlab/${project.name}`,
text: `${data.user_name} pushed ${data.total_commits_count} commits to branch [${refParser(data.ref)}](${project.web_url}/commits/${refParser(data.ref)}) in [${project.name}](${project.web_url})`,
icon_url: project.avatar_url || data.user_avatar || '',
attachments: [
{
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) {
let tag = refParser(data.ref);
return {
content: {
username: `gitlab/${data.project.name}`,
icon_url: data.project.avatar_url || data.user_avatar || '',
text: `${data.user_name} push tag [${tag} ${data.checkout_sha.slice(0,8)}](${data.project.web_url}/tags/${tag})`
}
};
}
}