-
Notifications
You must be signed in to change notification settings - Fork 6
/
automention.js
73 lines (67 loc) · 1.72 KB
/
automention.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
const { isAutomention, formatAutomention } = require('./comments');
const { usersToNotify } = require('./usersToNotify');
async function automention({
issue,
fullIssue,
labels,
issueComments,
issuesApi,
config,
log
}) {
const matchingUsers = [];
const labelToUsers = config;
labels.forEach(label => {
const users = labelToUsers[label];
if (users) {
users.forEach(u => matchingUsers.push(u));
}
});
const users = labels.includes('help wanted')
? usersToNotify({ matchingUsers, fullIssue, issueComments })
: [];
const body = users.length ? formatAutomention(users) : null;
const automentionComments = issueComments.filter(c => isAutomention(c.body));
const [existingComment, ...duplicateComments] = automentionComments;
if (!existingComment) {
log.debug('No automention comments');
if (!body) {
log.info('No users to notify');
} else {
log.info(`Creating comment: '${body}'`);
await issuesApi.createComment({
...issue,
body
});
}
} else {
const existing = automentionComments[0];
if (!body) {
log.info('Removing automention comment');
await issuesApi.deleteComment({
...issue,
comment_id: existingComment.id
});
} else if (existing.body === body) {
log.info('Nothing to update');
} else {
await issuesApi.updateComment({
...issue,
comment_id: existingComment.id,
body
});
}
}
await Promise.all(
duplicateComments.map(async dupe => {
log.warn(`Removing duplicate comment: ${dupe.id}`);
await issuesApi.deleteComment({
...issue,
comment_id: dupe.id
});
})
);
}
module.exports = {
automention
};