forked from learningequality/.github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontributor-issue-comment.js
More file actions
175 lines (157 loc) · 5.29 KB
/
contributor-issue-comment.js
File metadata and controls
175 lines (157 loc) · 5.29 KB
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
// See docs/community-automations.md
const {
LE_BOT_USERNAME,
KEYWORDS_DETECT_ASSIGNMENT_REQUEST,
ISSUE_LABEL_HELP_WANTED,
BOT_MESSAGE_ISSUE_NOT_OPEN,
BOT_MESSAGE_ALREADY_ASSIGNED,
COMMUNITY_REPOS,
} = require('./constants');
const {
isCloseContributor,
sendBotMessage,
escapeIssueTitleForSlackMessage,
hasRecentBotComment,
hasLabel,
getIssues,
getPullRequests,
} = require('./utils');
// Format information about author's assigned open issues
// as '(Issues #1 #2 | PRs #3)' and PRs for Slack message
function formatAuthorActivity(issues, pullRequests) {
const parts = [];
if (issues.length > 0) {
const issueLinks = issues.map(issue => `<${issue.html_url}|#${issue.number}>`).join(' ');
parts.push(`Issues ${issueLinks}`);
} else {
parts.push(`Issues none`);
}
if (pullRequests.length > 0) {
const prLinks = pullRequests.map(pr => `<${pr.html_url}|#${pr.number}>`).join(' ');
parts.push(`PRs ${prLinks}`);
} else {
parts.push(`PRs none`);
}
return `(${parts.join(' | ')})`;
}
function shouldSendBotReply(
issueCreator,
commentAuthor,
commentAuthorIsCloseContributor,
isHelpWanted,
isAssignmentRequest,
isIssueAssignedToSomeoneElse,
) {
if (commentAuthorIsCloseContributor) {
return [false, null];
}
if (issueCreator === commentAuthor) {
return [false, null];
}
if (isHelpWanted && isIssueAssignedToSomeoneElse && isAssignmentRequest) {
return [true, BOT_MESSAGE_ALREADY_ASSIGNED];
}
if (!isHelpWanted && isAssignmentRequest) {
return [true, BOT_MESSAGE_ISSUE_NOT_OPEN];
}
return [false, null];
}
function shouldContactSupport(
commentAuthorIsCloseContributor,
isHelpWanted,
isIssueAssignedToSomeoneElse,
) {
if (commentAuthorIsCloseContributor) {
return true;
}
if (!isHelpWanted) {
return false;
}
if (isHelpWanted && isIssueAssignedToSomeoneElse) {
return false;
}
return true;
}
module.exports = async ({ github, context, core }) => {
try {
const issueNumber = context.payload.issue.number;
const issueUrl = context.payload.issue.html_url;
const issueTitle = escapeIssueTitleForSlackMessage(context.payload.issue.title);
const issueCreator = context.payload.issue.user.login;
const issueAssignees = context.payload.issue.assignees?.map(assignee => assignee.login) || [];
const commentId = context.payload.comment.id;
const commentAuthor = context.payload.comment.user.login;
const commentBody = context.payload.comment.body;
const repo = context.repo.repo;
const owner = context.repo.owner;
const keywordRegexes = KEYWORDS_DETECT_ASSIGNMENT_REQUEST.map(k => k.trim().toLowerCase())
.filter(Boolean)
.map(keyword => new RegExp(`\\b${keyword}\\b`, 'i'));
const isAssignmentRequest = keywordRegexes.find(regex => regex.test(commentBody));
const isIssueAssignedToSomeoneElse =
issueAssignees && issueAssignees.length > 0 && !issueAssignees.includes(commentAuthor);
const isHelpWanted = await hasLabel(
ISSUE_LABEL_HELP_WANTED,
owner,
repo,
issueNumber,
github,
core,
);
const commentAuthorIsCloseContributor = await isCloseContributor(commentAuthor, {
github,
context,
core,
});
const [shouldPostBot, botMessage] = shouldSendBotReply(
issueCreator,
commentAuthor,
commentAuthorIsCloseContributor,
isHelpWanted,
isAssignmentRequest,
isIssueAssignedToSomeoneElse,
);
if (shouldPostBot) {
// post bot reply only when there are no same bot comments
// in the past hour to prevent overwhelming issue comment section
const skipBot = await hasRecentBotComment(issueNumber, LE_BOT_USERNAME, botMessage, 3600000, {
github,
context,
core,
});
if (skipBot) {
const slackMessage = `*[${repo}] Bot response skipped on issue: <${issueUrl}|${issueTitle}> (less than 1 hour since last bot message)*`;
core.setOutput('support_dev_notifications_bot', slackMessage);
} else {
const botMessageUrl = await sendBotMessage(issueNumber, botMessage, {
github,
context,
core,
});
if (botMessageUrl) {
const slackMessage = `*[${repo}] <${botMessageUrl}|Bot response sent> on issue: <${issueUrl}|${issueTitle}>*`;
core.setOutput('support_dev_notifications_bot', slackMessage);
}
}
}
const contactSupport = shouldContactSupport(
commentAuthorIsCloseContributor,
isHelpWanted,
isIssueAssignedToSomeoneElse,
);
let slackMessage = `*[${repo}] <${issueUrl}#issuecomment-${commentId}|New comment> on issue: <${issueUrl}|${issueTitle}> by _${commentAuthor}_*`;
if (contactSupport) {
const [assignedOpenIssues, openPRs] = await Promise.all([
getIssues(commentAuthor, 'open', owner, COMMUNITY_REPOS, github, core),
getPullRequests(commentAuthor, 'open', owner, COMMUNITY_REPOS, github, core),
]);
const authorActivity = formatAuthorActivity(assignedOpenIssues, openPRs);
slackMessage += ` _${authorActivity}_`;
core.setOutput('support_dev_message', slackMessage);
} else {
core.setOutput('support_dev_notifications_message', slackMessage);
}
} catch (error) {
core.setFailed(`Action failed with error: ${error.message}`);
}
};