-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.ts
495 lines (393 loc) · 16.6 KB
/
Server.ts
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import { PullRequest, PullRequestEvent, PullRequestReviewEvent } from './GitHubApi/PullRequest';
import { Card, Project, Column, CardEvent } from './GitHubApi/Project';
import { createServer, IncomingMessage, ServerResponse } from 'http';
import { CheckSuiteEvent, CheckSuite } from './GitHubApi/Check';
import { Issue, IssueEvent } from './GitHubApi/Issue';
import { PushEvent, Mention } from './GitHubApi/Push';
import { Repository } from './GitHubApi/Repository';
import { Milestone } from './GitHubApi/Milestone';
import { Validator } from './Services/Azure';
import { Octokit } from './Services/Octokit';
import { Label } from './GitHubApi/Label';
import './Extensions/Arrays';
createServer((request: IncomingMessage, response: ServerResponse) => {
// Only accept POST requests
if (request.method === 'POST') {
let body: string = '';
request.on('data', (chunk: string | Buffer) => { body += chunk.toString(); });
request.on('end', async () => {
// Validates webhook secret and reject if invalid
if (await Validator.ValidateSecretAsync(body, request.headers['x-hub-signature'].toString())) {
// Handle issue events
// https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads#issues
if (request.headers['x-github-event'].toString() === 'issues') {
// Parse request body as json and set aliases
let jsonPayload: any = JSON.parse(body);
let event: IssueEvent = new IssueEvent(jsonPayload);
let issue: Issue = event.issue;
// Create Octokit client with the current installation id
await Octokit.SetClientAsync(event.installation.id);
// Event is related to the 'Average CRM' repo
if (event.repository.name === 'Average-CRM') {
// New issue opened event
if (event.action === 'opened') {
// Add myself as assignee
await issue.AddAssigneeAsync('BrunoBlanes');
// No milestone set
if (issue.milestone == null) {
await issue.AddLabelAsync('Triage');
}
response.writeHead(202);
}
// New label added event
else if (event.action === 'labeled') {
if (await event.repository.GetProjectAsync(event.label.name) != null) {
await issue.CreateProjectCardAsync();
}
response.writeHead(202);
}
// Issue closed event
else if (event.action === 'closed') {
let labels: string[] = [];
// Choose appropriate label if issue was a bug
if (issue.labels.some(x => x.name === 'Bug')) {
labels.push('Fixed');
}
else {
labels.push('Complete');
}
// Look for the specified label in the array
let label: Label = issue.labels.find(x => x.name === 'Awaiting Pull Request');
if (label != null) {
issue.labels.splice(issue.labels.indexOf(label), 1);
}
// Push the remaining labels to the label name array
issue.labels.forEach(x => { labels.push(x.name); });
await issue.UpdateAsync(labels);
response.writeHead(202);
}
}
}
// Handle project cards events
// https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads#project_card
else if (request.headers['x-github-event'].toString() === 'project_card') {
// Parse request body as json and set aliases
let jsonPayload: any = JSON.parse(body);
let event: CardEvent = new CardEvent(jsonPayload);
let card: Card = event.project_card;
// Create Octokit client with the current installation id
await Octokit.SetClientAsync(event.installation.id);
// Event is related to the 'Average CRM' repo
if (event.repository.name === 'Average-CRM') {
// Card is not a note
if (card.content_url != null) {
// Card created event
if (event.action === 'created') {
// Content is an issue
if (card.IsContentAnIssue()) {
let issue: Issue = await event.repository.GetIssueAsync(card.GetContentId());
let column: Column = await card.GetCurrentColumnAsync();
let project: Project = await card.GetProjectAsync();
let milestoneNumber = null;
let labels: string[] = [];
// Remove project labels
for (let project of await event.repository.ListProjectsAsync()) {
for (let label of issue.labels) {
if (project.name === label.name) {
issue.labels.splice(issue.labels.indexOf(label), 1);
break;
}
}
}
// Copy non project related labels
for (let label of issue.labels) {
labels.push(label.name);
}
// Add current project label
labels.push(project.name);
// Look for a milestone with the same name as the current column
for (let milestone of await event.repository.ListMilestonesAsync()) {
if (milestone.title === column.name) {
milestoneNumber = milestone.number;
break;
}
}
await issue.UpdateAsync(labels, milestoneNumber);
response.writeHead(202);
}
}
// Card moved event
else if (event.action === 'moved') {
// Content is an issue
if (card.IsContentAnIssue()) {
let issue: Issue = await event.repository.GetIssueAsync(card.GetContentId());
let columnName: string = (await card.GetCurrentColumnAsync()).name;
let labels: string[] = [];
// Moved to 'Triage'
if (columnName === 'Triage') {
for (let label of issue.labels) {
if (label.name !== 'Working'
&& label.name !== 'Fixed'
&& label.name !== 'Complete'
&& label.name !== 'Awaiting Pull Request') {
labels.push(label.name);
}
}
if (issue.labels.some((label: Label) => label.name === 'Triage') === false) {
labels.push('Triage');
}
await issue.UpdateAsync(labels, -1);
}
// Moved to 'In progess'
else if (columnName === 'In progress') {
for (let label of issue.labels) {
if (label.name !== 'Triage'
&& label.name !== 'Fixed'
&& label.name !== 'Complete'
&& label.name !== 'Awaiting Pull Request') {
labels.push(label.name);
}
}
if (issue.labels.some((label: Label) => label.name === 'Working') === false) {
labels.push('Working');
}
await issue.UpdateAsync(labels);
}
// Moved to 'Done'
else if (columnName === 'Done') {
for (let label of issue.labels) {
if (label.name !== 'Triage'
&& label.name !== 'Fixed'
&& label.name !== 'Working'
&& label.name !== 'Complete') {
labels.push(label.name);
}
}
if (issue.labels.some((label: Label) => label.name === 'Awaiting Pull Request') === false) {
labels.push('Awaiting Pull Request');
}
await issue.UpdateAsync(labels);
}
// Moved to a milestone column
else {
let milestones: Milestone[] = await event.repository.ListMilestonesAsync();
for (let label of issue.labels) {
if (label.name !== 'Triage'
&& label.name !== 'Fixed'
&& label.name !== 'Working'
&& label.name !== 'Complete'
&& label.name !== 'Awaiting Pull Request') {
labels.push(label.name);
}
}
// Add milestone to issue
for (let milestone of milestones) {
if (milestone.title === columnName) {
await issue.UpdateAsync(labels, milestone.number);
break;
}
}
}
response.writeHead(202);
}
}
}
}
}
// Handle push events
// https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads#push
else if (request.headers['x-github-event'].toString() === 'push') {
let jsonPayload: any = JSON.parse(body);
// Parse request body as json and set aliases
let event: PushEvent = new PushEvent(jsonPayload);
let repo: Repository = event.repository;
// Create Octokit client with the current installation id
await Octokit.SetClientAsync(event.installation.id);
// Event is related to the 'Average CRM' repo
if (event.repository.name === 'Average-CRM') {
// Ignore commits to the master branch
if (event.ref.split('/').last() !== 'master') {
for (let commit of event.commits) {
let mention: Mention | null = commit.GetMention();
if (mention != null) {
let issue: Issue = await repo.GetIssueAsync(mention.content_id);
let project: Project = await issue.GetProjectAsync();
let card: Card = await issue.GetProjectCardAsync();
let column: Column = mention.resolved
? await project.GetColumnAsync('Done')
: await project.GetColumnAsync('In progress');
// Move project card
await card.MoveAsync(column);
let pullRequests: PullRequest[] = await repo.ListPullRequestsAsync(`${event.sender.login}:${event.ref}`);
if (pullRequests.length === 0) {
// Creates a pull request if one don't aleady exists
let pullRequest: PullRequest = await repo.CreatePullRequestAsync(
event.ref,
issue.title,
`Resolves #${issue.number}`);
if (mention.resolved === true) {
// Request review from me since Creeper-bot is the one opening it
await pullRequest.RequestReviewAsync('BrunoBlanes');
}
}
else {
let pullRequest: PullRequest = pullRequests.first();
let mentions: Mention[] = pullRequest.GetMentions();
let body: string | null = null;
// This issue was not mentioned before on this pull request
if (mentions.some((mention: Mention) => mention.content_id === issue.number) === false) {
body = pullRequest.body;
body += ` and resolves #${issue.number}`;
}
// All mentions were resolved, convert from draft to final
if (mentions.some((mention: Mention) => mention.resolved === false) === false) {
// Request review from me since Creeper-bot is the one opening it
await pullRequest.RequestReviewAsync('BrunoBlanes');
}
await pullRequest.UpdateAsync(body);
}
}
}
response.writeHead(202);
}
}
}
// Handle pull request events
// https://docs.github.com/en/free-pro-team@latest/developers/webhooks-and-events/webhook-events-and-payloads#pull_request
else if (request.headers['x-github-event'].toString() === 'pull_request') {
let jsonPayload: any = JSON.parse(body);
// Parse request body as json and set aliases
let event: PullRequestEvent = new PullRequestEvent(jsonPayload);
let pullRequest: PullRequest = event.pull_request;
let repo: Repository = event.repository;
// Create Octokit client with the current installation id
await Octokit.SetClientAsync(event.installation.id);
// Event is related to the 'Average CRM' repo
if (event.repository.name === 'Average-CRM') {
// Pull request closed event
if (event.action === 'closed') {
// Pull request has been merged
if (pullRequest.merged) {
for (let commit of await pullRequest.GetCommitsAsync()) {
let mention: Mention = commit.GetMention();
if (mention != null) {
// Move issue to 'Done' column
let issue: Issue = await repo.GetIssueAsync(mention.content_id);
// Commits to master reference pull requests
if (pullRequest.base.ref === 'master') {
// Close the issue
await issue.UpdateAsync(undefined, undefined, 'closed');
// Add nice comment
await issue.CreateCommentAsync(`Thank you for your contribution! This issue was ${issue.labels.some((label: Label) => label.name === 'Bug') ? 'fixed' : 'resolved'} and will be implemented in the next release.`);
}
}
}
response.writeHead(202);
}
}
}
}
// Handle pull request review events
// https://docs.github.com/en/free-pro-team@latest/developers/webhooks-and-events/webhook-events-and-payloads#pull_request_review
else if (request.headers['x-github-event'].toString() === 'pull_request_review') {
let jsonPayload: any = JSON.parse(body);
// Parse request body as json and set aliases
let event: PullRequestReviewEvent = new PullRequestReviewEvent(jsonPayload);
let repo: Repository = event.repository;
// Create Octokit client with the current installation id
await Octokit.SetClientAsync(event.installation.id);
// Event is related to the 'Average CRM' repo
if (event.repository.name === 'Average-CRM') {
// Handle pull request review approved event
if (event.action === 'submitted' && event.review.state === 'approved') {
// Pull request approved by me
if (event.review.user.login === 'BrunoBlanes') {
let pullRequest: PullRequest = await repo.GetPullRequestAsync(event.pull_request.number);
let mention: Mention = pullRequest.GetMentions().first();
let issue: Issue = await repo.GetIssueAsync(mention.content_id);
let method: 'merge' | 'squash' | 'rebase';
// Merges to 'master' must be via squash
if (pullRequest.base.ref === 'master') {
method = 'squash';
}
// Merges from 'master' are rebased
else if (pullRequest.head.ref === 'master') {
method = 'rebase';
}
else {
method = 'merge';
}
// Merge pull request
if (pullRequest.mergeable) {
await pullRequest.MergeAsync(`${issue.title} (#${pullRequest.number})`, method);
await repo.DeleteRerenceAsync(`heads/${pullRequest.head.ref}`);
response.writeHead(202);
}
}
}
}
}
// Handle check suite events
// https://docs.github.com/en/free-pro-team@latest/developers/webhooks-and-events/webhook-events-and-payloads#check_suite
else if (request.headers['x-github-event'].toString() === 'check_suite') {
let jsonPayload: any = JSON.parse(body);
// Parse request body as json and set aliases
let event: CheckSuiteEvent = new CheckSuiteEvent(jsonPayload);
let checkSuite: CheckSuite = event.check_suite;
let repo: Repository = event.repository;
// Create Octokit client with the current installation id
await Octokit.SetClientAsync(event.installation.id);
// Event is related to the 'Average CRM' repo
if (event.repository.name === 'Average-CRM') {
// All check runs in a check suite have completed.
if (event.action === 'completed' && checkSuite.conclusion === 'success') {
if (checkSuite.pull_requests != null) {
let pullRequest: PullRequest = await repo.GetPullRequestAsync(checkSuite.pull_requests.first().number);
let mention: Mention = pullRequest.GetMentions().first();
let issue: Issue = await repo.GetIssueAsync(mention.content_id);
let method: 'merge' | 'squash' | 'rebase';
// Merges to 'master' must be via squash
if (pullRequest.base.ref === 'master') {
method = 'squash';
}
// Merges from 'master' are rebased
else if (pullRequest.head.ref === 'master') {
method = 'rebase';
}
else {
method = 'merge';
}
// Merge pull request
if (pullRequest.mergeable) {
await pullRequest.MergeAsync(`${issue.title} (#${pullRequest.number})`, method);
await repo.DeleteRerenceAsync(`heads/${pullRequest.head.ref}`);
response.writeHead(202);
}
}
}
}
}
if (response.statusCode !== 202) {
response.writeHead(404);
}
response.end();
}
else {
// GitHub Signature validation failed
response.writeHead(401, { 'Content-Type': 'text/plain' });
response.write('Failed GitHub Signature validation.');
response.end();
}
});
}
else if (request.method === 'GET') {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.write('<p>Creeper-bot is a bot created by Bruno Blanes to automate his personal GitHub account.<p>You can find more about him at <a href="https://github.com/BrunoBlanes/Creeper-bot/">https://github.com/BrunoBlanes/Creeper-bot/</a>.<p>v1.3.0');
response.end();
}
else {
response.writeHead(401, { 'Content-Type': 'text/plain' });
response.write('This server only accepts "POST" requests from GitHub.');
response.end();
}
}).listen(process.env.port);