-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
688 lines (586 loc) · 19.7 KB
/
app.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
"use strict";
const { Octokit } = require("@octokit/rest");
const axios = require("axios");
require("dotenv").config();
const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL;
/**
* This is the main entrypoint to your Probot app
* @param {import('probot').Probot} app
*/
module.exports = (app) => {
app.log.info("Yay, the app was loaded!");
/**
* On an issue being opened
* @param {import('probot').Context} context
*/
app.on("issues.opened", async (context) => {
console.log("issue opened");
if (
context.payload.repository.owner.login !== "fairdataihub" &&
context.payload.repository.owner.login !== "misanlab"
) {
return;
}
// Don't respond to the status messages
if (
context.payload.repository.name === "uptime" ||
context.payload.repository.name === "upptime"
) {
return;
}
// Don't respond to the issues opened by our bots or renovate
if (context.payload.issue.user.type === "Bot") {
if (
context.payload.issue.user.login === "renovate[bot]" ||
context.payload.issue.user.login === "doi-checker-app[bot]" ||
context.payload.issue.user.login === "license-check-bot[bot]" ||
context.payload.issue.user.login === "codefair-app[bot]"
) {
return;
}
}
const issueComment = context.issue({
body: "Hello! Thank you for opening this issue. Your input is valuable and helps improve the project. Can you please provide a detailed description of the problem you're encountering? Any additional information such as steps to reproduce the issue would be greatly appreciated. Thank you!",
});
await axios.post(SLACK_WEBHOOK_URL, {
blocks: [
{
type: "header",
text: {
type: "plain_text",
text: `New issue opened in ${context.payload.repository.full_name}`,
emoji: true,
},
},
{
type: "divider",
},
{
type: "section",
text: {
type: "mrkdwn",
text: `*Issue title:* ${context.payload.issue.title}`,
},
},
{
type: "section",
text: {
type: "mrkdwn",
text: `*Issue Link:* ${context.payload.issue.html_url}`,
},
},
],
});
console.log("sending issue comment");
return context.octokit.issues.createComment(issueComment);
});
/**
* On an issue being closed
* @param {import('probot').Context} context
*/
app.on("issues.closed", async (context) => {
console.log("issue closed");
if (
context.payload.repository.owner.login !== "fairdataihub" &&
context.payload.repository.owner.login !== "misanlab"
) {
return;
}
/**
* * Don't respond to the status messages
*/
if (
context.payload.repository.name === "uptime" ||
context.payload.repository.name === "upptime"
) {
return;
}
// Don't respond to the issues closed by our bots or renovate
if (context.payload.issue.user.type === "Bot") {
if (
context.payload.issue.user.login === "renovate[bot]" ||
context.payload.issue.user.login === "doi-checker-app[bot]" ||
context.payload.issue.user.login === "license-check-bot[bot]" ||
context.payload.issue.user.login === "codefair-app[bot]"
) {
return;
}
}
const issueComment = context.issue({
body: "If you're still experiencing any problems, please don't hesitate to open a new issue. Have a great day!",
});
console.log("sending issue comment");
return context.octokit.issues.createComment(issueComment);
});
/**
* On a pull request being opened
* @param {import('probot').Context} context
*/
app.on("pull_request.opened", async (context) => {
console.log("pull request opened");
if (
context.payload.repository.owner.login !== "fairdataihub" &&
context.payload.repository.owner.login !== "misanlab"
) {
return;
}
// Don't respond to the pull requests opened by renovate
if (context.payload.pull_request.user.type === "Bot") {
if (context.payload.pull_request.user.login === "renovate[bot]") {
return;
}
}
// Get the pull request number
const prNumber = context.payload.pull_request.number;
const issueComment = context.issue({
body: "Thank you for submitting this pull request! We appreciate your contribution to the project. Before we can merge it, we need to review the changes you've made to ensure they align with our code standards and meet the requirements of the project. We'll get back to you as soon as we can with feedback. Thanks again!",
issue_number: prNumber,
});
console.log("sending pr comment");
return context.octokit.issues.createComment(issueComment);
});
/**
* On a pull request being closed/merged
* @param {import('probot').Context} context
*/
app.on("pull_request.closed", async (context) => {
console.log("pull request closed");
if (
context.payload.repository.owner.login !== "fairdataihub" &&
context.payload.repository.owner.login !== "misanlab"
) {
return;
}
// Don't respond to the pull requests closed by renovate
if (context.payload.pull_request.user.type === "Bot") {
if (context.payload.pull_request.user.login === "renovate[bot]") {
return;
}
}
// Get the pull request number
const prNumber = context.payload.pull_request.number;
const issueComment = context.issue({
body: "Thanks for closing this pull request! If you have any further questions, please feel free to open a new issue. We are always happy to help!",
issue_number: prNumber,
});
console.log("sending pr comment");
return context.octokit.issues.createComment(issueComment);
});
/**
* On a pull request being edited
* @param {import('probot').Context} context
*/
app.on("pull_request.edited", async (context) => {
console.log("pull request edited");
if (
context.payload.repository.owner.login !== "fairdataihub" &&
context.payload.repository.owner.login !== "misanlab"
) {
return;
}
// Don't respond to the pull requests opened by renovate
if (context.payload.pull_request.user.type === "Bot") {
if (context.payload.pull_request.user.login === "renovate[bot]") {
return;
}
}
// Get the pull request number
const prNumber = context.payload.pull_request.number;
const issueComment = context.issue({
body: "Thanks for making updates to your pull request. Our team will take a look and provide feedback as soon as possible. Please wait for any GitHub Actions to complete before editing your pull request. If you have any additional questions or concerns, feel free to let us know. Thank you for your contributions!",
issue_number: prNumber,
});
console.log("sending pr comment");
return context.octokit.issues.createComment(issueComment);
});
/**
* On a pull request being marked as ready for review
* @param {import('probot').Context} context
*/
app.on("pull_request.ready_for_review", async (context) => {
console.log("pull request ready for review");
if (
context.payload.repository.owner.login !== "fairdataihub" &&
context.payload.repository.owner.login !== "misanlab"
) {
return;
}
// Get the pull request number
const prNumber = context.payload.pull_request.number;
const issueComment = context.issue({
body: "Thanks for making your pull request ready for review! Our team will take a look and provide feedback as soon as possible.",
issue_number: prNumber,
});
console.log("sending pr comment");
return context.octokit.issues.createComment(issueComment);
});
/**
* On a new star being added to a repository
* @param {import('probot').Context} context
*/
app.on("star.created", async (context) => {
console.log("repo starred");
if (
context.payload.repository.owner.login !== "fairdataihub" &&
context.payload.repository.owner.login !== "misanlab"
) {
console.log("repo not fairdataihub or misanlab");
return;
}
await axios.post(SLACK_WEBHOOK_URL, {
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `New star created! :star: \n The <${context.payload.repository.html_url}|${context.payload.repository.name}> repository in the <${context.payload.repository.owner.html_url}|${context.payload.repository.owner.login}> organization was just starred by <${context.payload.sender.html_url}|${context.payload.sender.login}>! :tada: `,
},
accessory: {
type: "image",
image_url: `https://api.dicebear.com/5.x/thumbs/png?seed=${context.id}`,
alt_text: "image",
},
},
{
type: "divider",
},
],
});
return;
});
/**
* On repository being unstarred
* @param {import('probot').Context} context
*/
app.on("star.deleted", async (context) => {
console.log("repo unstarred");
if (
context.payload.repository.owner.login !== "fairdataihub" &&
context.payload.repository.owner.login !== "misanlab"
) {
return;
}
await axios.post(SLACK_WEBHOOK_URL, {
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `Star removed! :star: \n The <${context.payload.repository.html_url}|${context.payload.repository.name}> repository in the <${context.payload.repository.owner.html_url}|${context.payload.repository.owner.login}> organization lost a star from <${context.payload.sender.html_url}|${context.payload.sender.login}>! :cry: `,
},
accessory: {
type: "image",
image_url: `https://api.dicebear.com/5.x/micah/png?seed=${context.id}&mouth=frown,nervous,sad,surprised`,
alt_text: "image",
},
},
{
type: "divider",
},
],
});
return;
});
/**
* On repository being forked
* @param {import('probot').Context} context
*/
app.on("fork", async (context) => {
console.log("repo forked");
await axios.post(SLACK_WEBHOOK_URL, {
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `New fork created! :fork_and_knife: \n The <${context.payload.repository.html_url}|${context.payload.repository.name}> repository in the <${context.payload.repository.owner.html_url}|${context.payload.repository.owner.login}> organization was just forked by <${context.payload.forkee.owner.html_url}|${context.payload.forkee.owner.login}>! :monocle_face: `,
},
accessory: {
type: "image",
image_url: `https://api.dicebear.com/5.x/shapes/png?seed=${context.id}`,
alt_text: "image",
},
},
{
type: "divider",
},
],
});
return;
});
/**
* On a release being published
* @param {import('probot').Context} context
*/
app.on("release.published", async (context) => {
console.log("release published");
if (
context.payload.repository.owner.login !== "fairdataihub" &&
context.payload.repository.owner.login !== "misanlab"
) {
return;
}
// if the repository name is SODA-for-SPARC and the release is a beta release, return
if (
context.payload.repository.name === "SODA-for-SPARC" &&
context.payload.release.name.includes("beta")
) {
return;
}
// Get the release
const release = context.payload.release;
// Get repo owner and name
const owner = context.payload.repository.owner.login;
const repo = context.payload.repository.name;
// Add reactions to the release
await addReactionsToRelease(owner, repo, release.id);
await axios.post(SLACK_WEBHOOK_URL, {
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `New release published! :rocket: \n The <${context.payload.repository.html_url}|${context.payload.repository.name}> repository in the <${context.payload.repository.owner.html_url}|${context.payload.repository.owner.login}> organization just published a new release! :tada: `,
},
accessory: {
type: "image",
image_url: `https://api.dicebear.com/5.x/big-smile/png?seed=${context.id}`,
alt_text: "image",
},
},
{
type: "divider",
},
],
});
return;
});
/**
* On a label being added to an issue
* @param {import('probot').Context} context
*/
app.on("label.created", async (context) => {
console.log("label added");
let issueComment = "";
// Get the label name
const labelName = context.payload.label.name;
if (labelName === "bug" || labelName === "needs-more-info") {
issueComment = context.issue({
body: "We appreciate your contribution to the project. Can you please provide more details, such as steps to reproduce the problem, and any relevant information to help us understand the issue better? This will help us in resolving the issue as soon as possible.",
});
} else if (labelName === "enhancement") {
issueComment = context.issue({
body: "We appreciate your contribution to the project. Can you please provide more details, such as the use case for the enhancement, and any relevant information to help us understand the issue better? This will help us in resolving the issue as soon as possible.",
});
} else {
return;
}
console.log("sending issue comment");
return context.octokit.issues.createComment(issueComment);
});
/**
* On adding the GitHub App to an organization
* @param {import('probot').Context} context
*/
app.on("installation.created", async (context) => {
console.log("app installed");
const owner = context.payload.installation.account.login;
// Check if the repo is in the fairdataihub or misanlab org
if (owner !== "fairdataihub" && owner !== "misanlab") {
return;
}
for (const repo of context.payload.repositories) {
const repoName = repo.name;
// Star the repo
await starRepository(owner, repoName);
// Get the repo's releases
const releases = await context.octokit.repos.listReleases({
owner: owner,
repo: repoName,
});
// loop through the releases
for (const release of releases.data) {
// Check if the release is a draft
if (release.draft) {
continue;
}
await addReactionsToRelease(owner, repoName, release.id);
}
// Send a slack message
await axios.post(SLACK_WEBHOOK_URL, {
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `New installation created! :rocket: \n The app was installed to the ${repoName} repository in the <${context.payload.installation.account.html_url}|${owner}> organization! :tada:`,
},
accessory: {
type: "image",
image_url: `https://api.dicebear.com/5.x/fun-emoji/png?seed=${context.id}`,
alt_text: "image",
},
},
{
type: "divider",
},
],
});
}
return;
});
/**
* On adding a repository to the app
* @param {import('probot').Context} context
*/
app.on("installation_repositories.added", async (context) => {
console.log("repo added");
const owner = context.payload.installation.account.login;
for (const repo of context.payload.repositories_added) {
const repoName = repo.name;
// Check if the repo is in the fairdataihub or misanlab org
if (owner !== "fairdataihub" && owner !== "misanlab") {
continue;
}
// Check if the repo is a fork
if (repo.fork) {
continue;
}
// Star the repo
await starRepository(owner, repoName);
// Get the repo's releases
const releases = await context.octokit.repos.listReleases({
owner: owner,
repo: repoName,
});
// loop through the releases
for (const release of releases.data) {
// Check if the release is a draft
if (release.draft) {
continue;
}
await addReactionsToRelease(owner, repoName, release.id);
}
await axios.post(SLACK_WEBHOOK_URL, {
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `New repository added! ➕ \n The app was installed to the ${repoName} repository in the <${context.payload.installation.account.html_url}|${owner}> organization! :tada:`,
},
accessory: {
type: "image",
image_url: `https://api.dicebear.com/5.x/fun-emoji/png?seed=${context.id}`,
alt_text: "image",
},
},
{
type: "divider",
},
],
});
}
return;
});
/**
* On creating a repository
* @param {import('probot').Context} context
*/
app.on("repository.created", async (context) => {
console.log("repo created");
const owner = context.payload.repository.owner.login;
const repoName = context.payload.repository.name;
// Check if the repo is in the fairdataihub or misanlab org
if (owner !== "fairdataihub" && owner !== "misanlab") {
return;
}
// Check if the repo is a fork
if (repo.fork) {
return;
}
// Star the repo
await context.octokit.activity.starRepo({
owner: owner,
repo: repoName,
});
// Get the repo's releases
const releases = await context.octokit.repos.listReleases({
owner: owner,
repo: repoName,
});
// loop through the releases
for (const release of releases.data) {
// Check if the release is a draft
if (release.draft) {
continue;
}
await addReactionsToRelease(owner, repoName, release.id);
}
await axios.post(SLACK_WEBHOOK_URL, {
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `New repository created! ✨ \n The app was installed to the ${repoName} repository in the <${context.payload.installation.account.html_url}|${owner}> organization! :tada:`,
},
accessory: {
type: "image",
image_url: `https://api.dicebear.com/5.x/fun-emoji/png?seed=${context.id}`,
alt_text: "image",
},
},
{
type: "divider",
},
],
});
return;
});
/**
* On push to master
* @param {import("probot").Context} context
*/
app.on("push", async (context) => {
console.log("push to master");
});
};
/**
* Star a repository using an authenticated user
* @param {string} owner
* @param {string} repo
* @returns
*/
const starRepository = async (owner, repo) => {
const octokit = new Octokit({
auth: process.env.GITHUB_PAT,
});
await octokit.activity.starRepoForAuthenticatedUser({
owner,
repo,
});
return;
};
/**
* Add reactions to a release using an authenticated user
* @param {string} owner
* @param {string} repo
* @param {string} releaseId
* @returns {Promise<void>}
*/
const addReactionsToRelease = async (owner, repo, releaseId) => {
const octokit = new Octokit({
auth: process.env.GITHUB_PAT,
});
const reactions = ["+1", "heart", "hooray", "rocket"];
for (const reaction of reactions) {
await octokit.reactions.createForRelease({
owner,
repo,
release_id: releaseId,
content: reaction,
});
}
return;
};