This repository has been archived by the owner on Nov 4, 2022. It is now read-only.
forked from Lightning-AI/probot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-cc-bot.ts
89 lines (84 loc) · 2.92 KB
/
auto-cc-bot.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
import {parseSubscriptions} from './subscriptions';
import {CachedIssueTracker} from './utils';
import {Probot, Context} from 'probot';
function myBot(app: Probot): void {
const tracker = new CachedIssueTracker(
app,
'tracking_issue',
parseSubscriptions
);
async function loadSubscriptions(context: Context): Promise<object> {
return tracker.loadIssue(context);
}
async function runBotForLabels(
context: Context,
payloadType: string
): Promise<void> {
const subscriptions = await loadSubscriptions(context);
context.log('payload_type=', payloadType);
const labels = context.payload[payloadType]['labels'].map(e => e['name']);
context.log({labels});
const cc = new Set();
// eslint-disable-next-line github/array-foreach
labels.forEach(l => {
if (l in subscriptions) {
// eslint-disable-next-line github/array-foreach
subscriptions[l].forEach(u => cc.add(u));
}
});
context.log({cc: Array.from(cc)}, 'from subscriptions');
if (cc.size) {
const body = context.payload[payloadType]['body'];
const reCC = /cc( +@[a-zA-Z0-9-/]+)+/;
const oldCCMatch = body ? body.match(reCC) : null;
const prevCC = new Set();
if (oldCCMatch) {
const oldCCString = oldCCMatch[0];
context.log({oldCCString}, 'previous cc string');
let m;
const reUsername = /@([a-zA-Z0-9-/]+)/g;
while ((m = reUsername.exec(oldCCString)) !== null) {
prevCC.add(m[1]);
cc.add(m[1]);
}
context.log({prevCC: Array.from(prevCC)}, 'pre-existing ccs');
}
// Invariant: prevCC is a subset of cc
if (prevCC.size !== cc.size) {
let newCCString = 'cc';
// eslint-disable-next-line github/array-foreach
cc.forEach(u => {
newCCString += ` @${u}`;
});
const newBody = body
? oldCCMatch
? body.replace(reCC, newCCString)
: `${body}\n\n${newCCString}`
: newCCString;
context.log({newBody});
if (payloadType === 'issue') {
await context.octokit.issues.update(context.issue({body: newBody}));
} else if (payloadType === 'pull_request') {
await context.octokit.pulls.update(
context.pullRequest({body: newBody})
);
}
} else {
context.log('no action: no change from existing cc list on issue');
}
} else {
context.log('no action: cc list from subscription is empty');
}
}
app.on('issues.labeled', async context => {
await runBotForLabels(context, 'issue');
});
app.on('pull_request.labeled', async context => {
await runBotForLabels(context, 'pull_request');
});
// If the bot is disabled for draft PRs, we want to run it when the PR is marked as ready
app.on('pull_request.ready_for_review', async context => {
await runBotForLabels(context, 'pull_request');
});
}
export default myBot;