-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutils.js
More file actions
247 lines (218 loc) · 6.54 KB
/
utils.js
File metadata and controls
247 lines (218 loc) · 6.54 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
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
const { BOT_USERNAMES } = require('./constants');
// const { CLOSE_CONTRIBUTORS, TEAMS_WITH_CLOSE_CONTRIBUTORS } = require('./constants');
const { CLOSE_CONTRIBUTORS } = require('./constants');
/**
* Checks if username belongs to one of our bots.
*/
async function isBot(username, { core }) {
if (!username) {
core.setFailed('Missing username');
return false;
}
return BOT_USERNAMES.includes(username);
}
/**
* Checks if a user is a contributor (= not a core team member or bot).
*/
async function isContributor(username, authorAssociation, { github, context, core }) {
if (!username) {
core.setFailed('Missing username');
return false;
}
if (!authorAssociation) {
core.setFailed('Missing authorAssociation');
return false;
}
if (authorAssociation === 'OWNER') {
return false;
}
if (await isBot(username, { core })) {
return false;
}
const isClose = await isCloseContributor(username, { github, context, core });
// Some close contributors may be 'MEMBER's due to GSoC or other GitHub team
// memberships, so here we need to exclude only team members who are not
// close contributors
if (authorAssociation === 'MEMBER' && !isClose) {
return false;
}
return true;
}
/**
* Checks if a user is a close contributor by checking
* both the constants list and team membership in monitored teams.
*/
async function isCloseContributor(username, { core }) {
if (!username) {
core.setFailed('Missing username');
return false;
}
if (CLOSE_CONTRIBUTORS.map(c => c.toLowerCase().trim()).includes(username.toLowerCase().trim())) {
return true;
} else {
return false;
}
// Detection on GitHub teams below is disabled until we re-think
// how close contributors are managed (see Notion tracker):
// - it was only fallback as explained lower
// - it causes the problem when we receive undesired notification
// to #support-dev when Richard posts issue comment since he is a member
// of GSoC and other GitHub teams with close contributors (they require moderator).
/* const org = context.repo.owner;
// Even though we check on team members here, it's best
// to add everyone to CLOSE_CONTRIBUTORS constant anyway
// for reliable results (e.g. check below won't work
// for people who have their membership set to private,
// and we don't have control over that)
const promises = TEAMS_WITH_CLOSE_CONTRIBUTORS.map(team_slug =>
github.rest.teams.getMembershipForUserInOrg({
org,
team_slug,
username,
})
);
try {
const results = await Promise.allSettled(promises);
let isMember = false;
for (const result of results) {
if (result.status === 'fulfilled' && result.value.data.state === 'active') {
isMember = true;
break;
}
if (result.status === 'rejected' && result.reason.status !== 404) {
throw new Error(`API Error: ${result.reason.message}`);
}
}
return isMember;
} catch (error) {
core.setFailed(error.message);
return false;
} */
}
/**
* Sends a bot message as a comment on an issue. Returns message URL if successful.
*/
async function sendBotMessage(issueNumber, message, { github, context }) {
try {
if (!issueNumber) {
throw new Error('Issue number is required');
}
if (!message) {
throw new Error('Message content is required');
}
const response = await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
body: message,
});
if (!response?.data?.html_url) {
throw new Error('Comment created but no URL returned');
}
return response.data.html_url;
} catch (error) {
throw new Error(error.message);
}
}
function escapeIssueTitleForSlackMessage(issueTitle) {
return issueTitle.replace(/"/g, '\\"').replace(/</g, '<').replace(/>/g, '>');
}
/**
* Checks if a bot sent a message with a given text on an issue
* in the past specified milliseconds.
*/
async function hasRecentBotComment(
issueNumber,
botUsername,
commentText,
msAgo,
{ github, context, core },
) {
const oneHourAgo = new Date(Date.now() - msAgo);
const owner = context.repo.owner;
const repo = context.repo.repo;
try {
const response = await github.rest.issues.listComments({
owner,
repo,
issue_number: issueNumber,
since: oneHourAgo.toISOString(),
});
return (response.data || []).some(
comment =>
comment.user && comment.user.login === botUsername && comment.body.includes(commentText),
);
} catch (error) {
core.warning(`Failed to fetch comments on issue #${issueNumber}: ${error.message}`);
}
}
/**
* Checks if an issue has a label with the given name (case-insensitive).
*/
async function hasLabel(name, owner, repo, issueNumber, github, core) {
let labels = [];
try {
const allLabels = await github.paginate(github.rest.issues.listLabelsOnIssue, {
owner,
repo,
issue_number: issueNumber,
});
labels = allLabels.map(label => label.name);
} catch (error) {
core.warning(`Failed to fetch labels on issue #${issueNumber}: ${error.message}`);
labels = [];
}
return labels.some(label => label.toLowerCase() === name.toLowerCase());
}
/**
* Fetches issues assigned to an assignee in given repositories.
*/
async function getIssues(assignee, state, owner, repos, github, core) {
const promises = repos.map(repo =>
github
.paginate(github.rest.issues.listForRepo, {
owner,
repo,
assignee,
state,
})
.then(issues => issues.filter(issue => !issue.pull_request))
.catch(error => {
core.warning(`Failed to fetch issues from ${repo}: ${error.message}`);
return [];
}),
);
const results = await Promise.all(promises);
return results.flat();
}
/**
* Fetches pull requests by an author in given repositories.
*/
async function getPullRequests(author, state, owner, repos, github, core) {
const promises = repos.map(repo =>
github
.paginate(github.rest.pulls.list, {
owner,
repo,
state,
})
.then(prs => prs.filter(pr => pr.user.login === author))
.catch(error => {
core.warning(`Failed to fetch pull requests from ${repo}: ${error.message}`);
return [];
}),
);
const results = await Promise.all(promises);
return results.flat();
}
module.exports = {
isContributor,
isCloseContributor,
isBot,
sendBotMessage,
escapeIssueTitleForSlackMessage,
hasRecentBotComment,
hasLabel,
getIssues,
getPullRequests,
};