-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutils.js
More file actions
350 lines (312 loc) · 9.92 KB
/
Copy pathutils.js
File metadata and controls
350 lines (312 loc) · 9.92 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
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
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}`);
}
}
/**
* Fetches all label names for an issue. Returns an array of lowercase label strings.
*/
async function getLabels(owner, repo, issueNumber, github, core) {
try {
const allLabels = await github.paginate(github.rest.issues.listLabelsOnIssue, {
owner,
repo,
issue_number: issueNumber,
});
return allLabels.map(label => label.name.toLowerCase());
} catch (error) {
core.warning(`Failed to fetch labels on issue #${issueNumber}: ${error.message}`);
return [];
}
}
/**
* Checks if an issue has a label with the given name (case-insensitive).
*/
async function hasLabel(name, owner, repo, issueNumber, github, core) {
const labels = await getLabels(owner, repo, issueNumber, github, core);
return labels.includes(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();
}
/**
* Deletes bot comments on an issue that contain a specific marker string.
*/
async function deleteBotComments(issueNumber, botUsername, marker, owner, repo, github, core) {
try {
const comments = await github.paginate(github.rest.issues.listComments, {
owner,
repo,
issue_number: issueNumber,
});
const toDelete = comments.filter(
c => c.user?.login === botUsername && c.body?.includes(marker),
);
await Promise.all(
toDelete.map(async comment => {
await github.rest.issues.deleteComment({ owner, repo, comment_id: comment.id });
core.info(`Deleted bot comment ${comment.id} on issue #${issueNumber}`);
}),
);
} catch (error) {
core.warning(`Failed to delete bot comments on #${issueNumber}: ` + error.message);
}
}
/**
* Finds recent unassignment events for a user across repos.
* Uses the search API to find candidate issues, then checks
* timeline events for unassigned events within the cutoff.
*/
async function getRecentUnassignments(username, daysAgo, owner, repos, github, core) {
const usernameLower = username.toLowerCase();
const cutoff = new Date(Date.now() - daysAgo * 24 * 60 * 60 * 1000);
const since = cutoff.toISOString().split('T')[0];
const promises = repos.map(async repo => {
const repoUnassignments = [];
try {
const q = `involves:${username} repo:${owner}/${repo} is:issue updated:>=${since}`;
const issues = await github.paginate(github.rest.search.issuesAndPullRequests, {
q,
per_page: 100,
});
for (const issue of issues) {
try {
const events = await github.paginate(github.rest.issues.listEventsForTimeline, {
owner,
repo,
issue_number: issue.number,
per_page: 100,
});
for (const event of events) {
if (
event.event === 'unassigned' &&
event.assignee?.login?.toLowerCase() === usernameLower &&
new Date(event.created_at) >= cutoff
) {
repoUnassignments.push({
repo,
issueNumber: issue.number,
issueUrl: issue.html_url,
issueTitle: issue.title,
unassignedAt: event.created_at,
});
}
}
} catch (tlError) {
core.warning(`Failed to fetch timeline for ${repo}#${issue.number}: ${tlError.message}`);
}
}
} catch (error) {
core.warning(`Failed to search issues in ${repo}: ${error.message}`);
}
return repoUnassignments;
});
const results = await Promise.all(promises);
const unassignments = results.flat();
// Deduplicate by issueUrl, keeping the most recent unassignment event.
// A user could be assigned/unassigned multiple times on the same issue
// within the cooldown window, and each event should only count once.
const byIssue = new Map();
for (const u of unassignments) {
const existing = byIssue.get(u.issueUrl);
if (!existing || u.unassignedAt > existing.unassignedAt) {
byIssue.set(u.issueUrl, u);
}
}
return [...byIssue.values()];
}
module.exports = {
isContributor,
isCloseContributor,
isBot,
sendBotMessage,
escapeIssueTitleForSlackMessage,
hasRecentBotComment,
getLabels,
hasLabel,
getIssues,
getPullRequests,
deleteBotComments,
getRecentUnassignments,
};