-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatter.js
288 lines (225 loc) · 10.1 KB
/
formatter.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
// SPDX-License-Identifier: AGPL-3.0-only
import { MessageBuilder } from 'discord-webhook-node';
const BASE = 'https://github.com';
function truncateString(string, length) {
if (!string) {
return '';
}
if (string.length <= length) {
return string;
}
string = string.substr(0, length - 4);
return string.substr(0, string.lastIndexOf(' ')) + ' ...';
}
export function formatPushEvent(eventObj) {
const message = new MessageBuilder()
.setColor('#7289da');
// expecting eventObj.repo.name === "TerryCavanagh/VVVVVV"
const repoName = eventObj.repo.name.split('/')[1];
// expecting eventObj.payload.ref === "refs/heads/master"
const branchName = eventObj.payload.ref.split('/')[2];
const newCommits = eventObj.payload.commits.length === 1 ?
"1 new commit" :
`${eventObj.payload.commits.length} new commits`;
message.setTitle(`[${repoName}:${branchName}] ${newCommits}`)
.setAuthor(eventObj.actor.login, eventObj.actor.avatar_url, `${BASE}/${eventObj.actor.login}`);
var embedUrl = '';
var lines = [];
for (const commit of eventObj.payload.commits) {
if (lines.length >= 5) {
break;
}
const shortSha = commit.sha.substr(0, 7);
const url = `${BASE}/${eventObj.repo.name}/commit/${commit.sha}`;
const title = truncateString(commit.message.split('\n')[0], 50);
lines.push(`[\`${shortSha}\`](${url}) ${title} - ${commit.author.name}`);
if (eventObj.payload.commits.length === 1) {
embedUrl = url;
}
}
if (eventObj.payload.commits.length === 1) {
message.setURL(embedUrl);
} else {
message.setURL(`${BASE}/${eventObj.repo.name}/compare/${eventObj.payload.before.substr(0, 12)}...${eventObj.payload.head.substr(0, 12)}`);
}
message.setDescription(lines.join('\n'));
return message;
}
export function formatIssuesEvent(eventObj) {
switch (eventObj.payload.action) {
case 'opened':
return formatIssueOpenedEvent(eventObj);
case 'closed':
return formatIssueClosedEvent(eventObj);
case 'reopened':
return formatIssueReopenedEvent(eventObj);
}
console.log(`Ignoring issue event ${eventObj.id} (${eventObj.payload.action})`);
}
function formatIssueOpenedEvent(eventObj) {
const title = truncateString(eventObj.payload.issue.title, 150);
const message = new MessageBuilder()
.setColor('#eb6420')
.setTitle(`[${eventObj.repo.name}] Issue opened: #${eventObj.payload.issue.number} ${title}`)
.setAuthor(eventObj.actor.login, eventObj.actor.avatar_url, `${BASE}/${eventObj.actor.login}`)
.setURL(eventObj.payload.issue.html_url)
.setDescription(truncateString(eventObj.payload.issue.body, 500));
return message;
}
function formatIssueClosedEvent(eventObj) {
const title = truncateString(eventObj.payload.issue.title, 150);
const message = new MessageBuilder()
.setTitle(`[${eventObj.repo.name}] Issue closed: #${eventObj.payload.issue.number} ${title}`)
.setAuthor(eventObj.actor.login, eventObj.actor.avatar_url, `${BASE}/${eventObj.actor.login}`)
.setURL(eventObj.payload.issue.html_url);
return message;
}
function formatIssueReopenedEvent(eventObj) {
const title = truncateString(eventObj.payload.issue.title, 150);
const message = new MessageBuilder()
.setTitle(`[${eventObj.repo.name}] Issue reopened: #${eventObj.payload.issue.number} ${title}`)
.setAuthor(eventObj.actor.login, eventObj.actor.avatar_url, `${BASE}/${eventObj.actor.login}`)
.setURL(eventObj.payload.issue.html_url);
return message;
}
export function formatPullRequestEvent(eventObj) {
switch (eventObj.payload.action) {
case 'opened':
return formatPullRequestOpenedEvent(eventObj);
case 'closed':
return formatPullRequestClosedEvent(eventObj);
case 'reopened':
return formatPullRequestReopenedEvent(eventObj);
}
console.log(`Ignoring pull request event ${eventObj.id} (${eventObj.payload.action})`);
}
function formatPullRequestOpenedEvent(eventObj) {
const title = truncateString(eventObj.payload.pull_request.title, 150);
const message = new MessageBuilder()
.setColor('#009800')
.setTitle(`[${eventObj.repo.name}] Pull request opened: #${eventObj.payload.pull_request.number} ${title}`)
.setAuthor(eventObj.actor.login, eventObj.actor.avatar_url, `${BASE}/${eventObj.actor.login}`)
.setURL(eventObj.payload.pull_request.html_url)
.setDescription(truncateString(eventObj.payload.pull_request.body, 500));
return message;
}
function formatPullRequestClosedEvent(eventObj) {
const title = truncateString(eventObj.payload.pull_request.title, 150);
const message = new MessageBuilder()
.setTitle(`[${eventObj.repo.name}] Pull request closed: #${eventObj.payload.pull_request.number} ${title}`)
.setAuthor(eventObj.actor.login, eventObj.actor.avatar_url, `${BASE}/${eventObj.actor.login}`)
.setURL(eventObj.payload.pull_request.html_url);
return message;
}
function formatPullRequestReopenedEvent(eventObj) {
const title = truncateString(eventObj.payload.pull_request.title, 150);
const message = new MessageBuilder()
.setTitle(`[${eventObj.repo.name}] Pull request reopened: #${eventObj.payload.pull_request.number} ${title}`)
.setAuthor(eventObj.actor.login, eventObj.actor.avatar_url, `${BASE}/${eventObj.actor.login}`)
.setURL(eventObj.payload.pull_request.html_url);
return message;
}
export function formatIssueCommentEvent(eventObj) {
// GitHub considers issues to be the same as pull requests in this case
if (eventObj.payload.issue.pull_request) {
return formatPullRequestComment(eventObj);
}
return formatIssueComment(eventObj);
}
function formatPullRequestComment(eventObj) {
const title = truncateString(eventObj.payload.issue.title, 150);
const message = new MessageBuilder()
.setColor('#bfe5bf')
.setTitle(`[${eventObj.repo.name}] New comment on pull request #${eventObj.payload.issue.number}: ${title}`)
.setAuthor(eventObj.actor.login, eventObj.actor.avatar_url, `${BASE}/${eventObj.actor.login}`)
.setURL(eventObj.payload.comment.html_url)
.setDescription(truncateString(eventObj.payload.comment.body, 500));
return message;
}
function formatIssueComment(eventObj) {
const title = truncateString(eventObj.payload.issue.title, 150);
const message = new MessageBuilder()
.setColor('#e68d60')
.setTitle(`[${eventObj.repo.name}] New comment on issue #${eventObj.payload.issue.number}: ${title}`)
.setAuthor(eventObj.actor.login, eventObj.actor.avatar_url, `${BASE}/${eventObj.actor.login}`)
.setURL(eventObj.payload.comment.html_url)
.setDescription(truncateString(eventObj.payload.comment.body, 500));
return message;
}
export function formatPullRequestReviewEvent(eventObj) {
if (eventObj.payload.action !== 'created') {
console.log(`Ignoring pull request review event ${eventObj.id} (${eventObj.payload.action})`);
return;
}
const title = truncateString(eventObj.payload.pull_request.title, 150);
const message = new MessageBuilder()
.setTitle(`[${eventObj.repo.name}] Pull request review submitted: #${eventObj.payload.pull_request.number} ${title}`)
.setAuthor(eventObj.actor.login, eventObj.actor.avatar_url, `${BASE}/${eventObj.actor.login}`)
.setURL(eventObj.payload.review.html_url)
.setDescription(truncateString(eventObj.payload.review.body, 500));
return message;
}
export function formatPullRequestReviewCommentEvent(eventObj) {
if (eventObj.payload.action !== 'created') {
console.log(`Ignoring pull request review comment event ${eventObj.id} (${eventObj.payload.action})`);
return;
}
const title = truncateString(eventObj.payload.pull_request.title, 150);
const message = new MessageBuilder()
.setTitle(`[${eventObj.repo.name}] New review comment on pull request #${eventObj.payload.pull_request.number} ${title}`)
.setAuthor(eventObj.actor.login, eventObj.actor.avatar_url, `${BASE}/${eventObj.actor.login}`)
.setURL(eventObj.payload.comment.html_url)
.setDescription(truncateString(eventObj.payload.comment.body, 500));
return message;
}
export function formatWatchEvent(eventObj) {
// apparently these are people starring, not watching the repository
if (eventObj.payload.action !== 'started') {
console.log(`Ignoring watch event ${eventObj.id} (${eventObj.payload.action})`);
return;
}
const message = new MessageBuilder()
.setTitle(`[${eventObj.repo.name}] New star added`)
.setAuthor(eventObj.actor.login, eventObj.actor.avatar_url, `${BASE}/${eventObj.actor.login}`)
.setURL(`${BASE}/${eventObj.repo.name}`);
return message;
}
export function formatForkEvent(eventObj) {
const message = new MessageBuilder()
.setTitle(`[${eventObj.repo.name}] Fork created: ${eventObj.payload.forkee.full_name}`)
.setAuthor(eventObj.actor.login, eventObj.actor.avatar_url, `${BASE}/${eventObj.actor.login}`)
.setURL(eventObj.payload.forkee.html_url);
return message;
}
export function formatReleaseEvent(eventObj) {
if (eventObj.payload.action !== 'published') {
console.log(`Ignoring release event ${eventObj.id} (${eventObj.payload.action}`);
return;
}
const message = new MessageBuilder()
.setTitle(`[${eventObj.repo.name}] New release published: ${eventObj.payload.release.tag_name}`)
.setAuthor(eventObj.actor.login, eventObj.actor.avatar_url, `${BASE}/${eventObj.actor.login}`)
.setURL(eventObj.payload.release.html_url);
return message;
}
export function formatCreateEvent(eventObj) {
switch (eventObj.payload.ref_type) {
case 'tag':
return formatTagCreatedEvent(eventObj);
case 'branch':
return formatBranchCreatedEvent(eventObj);
}
console.log(`Ignoring create event ${eventObj.id} (${eventObj.payload.ref_type})`);
}
function formatTagCreatedEvent(eventObj) {
const message = new MessageBuilder()
.setTitle(`[${eventObj.repo.name}] New tag created: ${eventObj.payload.ref}`)
.setAuthor(eventObj.actor.login, eventObj.actor.avatar_url, `${BASE}/${eventObj.actor.login}`);
return message;
}
function formatBranchCreatedEvent(eventObj) {
const message = new MessageBuilder()
.setTitle(`[${eventObj.repo.name}] New branch created: ${eventObj.payload.ref}`)
.setAuthor(eventObj.actor.login, eventObj.actor.avatar_url, `${BASE}/${eventObj.actor.login}`);
return message;
}