-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.js
362 lines (300 loc) · 9.73 KB
/
lib.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
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
351
352
353
354
355
356
357
358
359
360
361
362
// Foolproof regex
const {Octokit} = require("octokit");
const WEEKLY_UPDATE_RE = /^\*?\*?weekly *update\*?\*?:?/i
const MONTHLY_UPDATE_RE = /^\*?\*?monthly *update\*?\*?/i
const LB = "\n"
const NO_MILESTONE_LABEL = "NO EPIC LABEL"
function getOctokit() {
const TOKEN = process.env.GH_TOKEN
if (!TOKEN) {
throw new Error("GitHub Token needed to access repo comments." +
" Use `repo` scope for public and private repositories," +
"`public_repo` for only public repositories")
}
return new Octokit({
auth: TOKEN
});
}
getEpics = (octokit, org, repoName, options) => getIssues(octokit, org, repoName, {labels: "epic", ...options})
getMilestoneIssues = (octokit, org, repoName, options) => getIssues(octokit, org, repoName, {labels: "milestone", ...options})
async function getIssues(octokit, org, repoName, options) {
const res = await octokit.request(`GET /repos/${org}/${repoName}/issues`, {
owner: org,
repo: repoName,
headers: {
'X-GitHub-Api-Version': '2022-11-28'
},
...options
})
if (!res.data) throw new Error(`Failed to get issues for ${repoName}, ${JSON.stringify(options)}: ${res}`)
return res.data.filter(i => !i.pull_request)
}
async function getIssuesForMonth(octokit, org, repoName, monthIndex, options) {
// TODO: not future proof
const since = new Date(2023, monthIndex, 1, 0, 0, 0, 0).toISOString();
let lastDay = new Date(2023, monthIndex + 1, 1, 0, 0, 0, 0);
lastDay = new Date(lastDay.valueOf() - 1)
let page = 1
const issues = []
let cont = true
while (cont) {
let _issues = await getIssues(octokit, org, repoName, {
page,
since,
sort: "updated",
direction: "asc", ...options
})
if (!_issues.length) break;
for (const issue of _issues) {
const updatedAt = new Date(issue.updated_at)
if (updatedAt.getTime() < lastDay.getTime()) {
issues.push(issue)
} else {
cont = false
}
}
page += 1
}
return issues
}
function wasUpdatedInMonth(monthIndex, issue) {
const firstDay = firstDayOfMonth(monthIndex);
let lastDay = new Date(2023, monthIndex + 1, 1, 0, 0, 0, 0);
lastDay = new Date(lastDay.valueOf() - 1)
// TODO: maybe best to rely on weekly updates (issues comments)
const updatedAt = (new Date(issue.updated_at)).getTime()
return updatedAt > firstDay.getTime() &&
updatedAt < lastDay.getTime()
}
async function getRepos(octokit, owner) {
const res = await octokit.request(`GET /orgs/${owner}/repos`, {
org: 'owner',
headers: {
'X-GitHub-Api-Version': '2022-11-28'
},
type: "public"
})
if (!res.data) throw new Error(`Failed to get repos for ${owner}: ${res}`)
return res.data.filter (r => !r.archived)
}
function isWeeklyUpdateComment(comment) {
return comment.body.search(WEEKLY_UPDATE_RE) !== -1
}
function isMonthlyUpdateComment(comment) {
return comment.body.search(MONTHLY_UPDATE_RE) !== -1
}
function cleanUpdate(update) {
return update.replace(WEEKLY_UPDATE_RE, "")
.replace(MONTHLY_UPDATE_RE, "")
.replace(/^\s*[\r\n]$/gm, "")
}
function formatProjectName(org) {
let projectName = org;
projectName = projectName.replace(/-.*/, "")
return projectName[0].toUpperCase() + projectName.substring(1)
}
function lastFiveDaysIso() {
const lastWeek = new Date()
const lastWeekInt = (lastWeek).getDate() - 5;
lastWeek.setDate(lastWeekInt);
return lastWeek.toISOString()
}
function firstDayOfMonth(monthIndex) {
return new Date(2023, monthIndex, 1, 0, 0, 0, 0)
}
async function getNewestCommentFirst(octokit, milestone, repoName, since) {
const res = await octokit.request(milestone.comments_url, {
owner: milestone.owner,
repo: repoName,
issue_number: milestone.number,
since: since,
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
})
if (!res.data) throw new Error(`Failed to get comments for ${milestone.html_url}: ${res}`)
return res.data.reverse()
}
function formatIssueTitleWithUrl(issue) {
const title = issue.title.replace(/\[?milestone]?:? +/i, "").replace(/\[?epic]?:? +/i, "")
if (issue.html_url) {
return "[" + title + "](" + issue.html_url + ")";
} else {
return title;
}
}
function getMonday( ) {
let date = new Date();
const day = date.getUTCDay() || 7;
if( day !== 1 )
date.setUTCHours(-24 * (day - 1));
return date;
}
function formatEpicList(epicsPerLabel, issuesPerLabel) {
let text = ""
for (const [label, epic] of epicsPerLabel) {
text += "# " + formatIssueTitleWithUrl(epic) + " {" + label + "}" + LB + LB
const issues = issuesPerLabel.get(label)
for (const issue of issues) {
text += formatCheckBoxIssue(issue) + issue.repoName + ": " + issue.html_url + LB
}
text += LB
}
return text;
}
function formatCheckBoxIssue(issue) {
return formatCheckBox(issue.state === 'closed')
}
function formatCheckBox(pred) {
if (pred) {
return "- [x] "
} else {
return "- [ ] "
}
}
function formatMonthlyReport(milestones, milestoneEpics) {
let text = ""
milestones.forEach((milestone) => {
const label = getEpicLabel(milestone);
if (!label) throw new Error(`No label for ${milestone.html_url}`)
text += "# " + formatIssueTitleWithUrl(milestone) + " `" + label + "`" + LB + LB
const {closed, open, updated} = milestoneEpics.get(label) ?? {closed: [], open: [], updated: []}
text += `**Epics: ${closed.length} closed, ${open.length} open**` + LB + LB
text += milestone.monthlyUpdate + LB + LB
text += `## ${updated.length} Epic${updated.length ? "s" : ""} Updated` + LB
for (const epic of updated) {
text += " " + formatCheckBoxIssue(epic) + epic.repo_name + ": " + formatIssueTitleWithUrl(epic) + LB
}
text += LB
})
return text;
}
function formatMilestoneByEpicList(milestones, milestoneEpics) {
let text = ""
milestones.forEach((milestone) => {
const label = getEpicLabel(milestone) ?? NO_MILESTONE_LABEL;
text += "# " + formatIssueTitleWithUrl(milestone) + " `" + label + "`" + LB
const epics = milestoneEpics.get(label) ?? []
for (const epic of epics) {
text += " " + formatCheckBoxIssue(epic) + epic.repo_name + ": " + formatIssueTitleWithUrl(epic) + LB
}
text += LB
})
const epics = milestoneEpics.get(NO_MILESTONE_LABEL)?.filter(m => m.state === "open")
if (epics) {
text += "# Orphan Milestones" + LB
for (const epic of epics) {
text += formatCheckBoxIssue(epic) + epic.repo_name + ": " + formatIssueTitleWithUrl(epic) + LB
}
text += LB
}
return text;
}
const REPOS_IN_ORDER = ["pm", "internal-waku-outreach", "docs.waku.org", "research", "nwaku", "js-waku", "go-waku"]
function compareRepos(repoA, repoB) {
return REPOS_IN_ORDER.indexOf(repoA.name) - REPOS_IN_ORDER.indexOf(repoB.name);
}
function epicLabels(issue) {
return issue.labels.filter(({name}) => name.startsWith("E:"))
}
async function getMilestones(octokit, org, repoName) {
const res = await octokit.request(`GET /repos/${org}/${repoName}/milestones`, {
owner: org,
repo: repoName,
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
})
if (!res.data) throw new Error(`Failed to get milestones for ${repoName}, ${res}`)
return res.data;
}
const LABELS_TO_FILTER_OUT = ["epic", "good first issue", "help wanted", /^track:.*/]
function cleanLabels(issue) {
return issue.labels.map(l => l.name).filter(n => {
return LABELS_TO_FILTER_OUT.find((test) => {
if (typeof test === 'string') {
return test === n
} else {
return test.test(n)
}
}) === undefined
})
}
const REPO_TEAM_MAP = new Map([
["docs.waku.org", "Docs"],
["internal-waku-outreach", "Eco Dev"],
["research", "Research"],
["pm", "Epics"]
])
function mapToTeamName(repo) {
const teamName = REPO_TEAM_MAP.get(repo);
return teamName ?? repo
}
const CONTRIBUTORS = [
"LordGhostX",
"danisharora099",
"jm-clius",
"Ivansete-status",
"harsh-98",
"weboko",
"richard-ramos",
"gabrielmer",
"NagyZoltanPeter",
"vpavlin",
"chaitanyaprem",
"fryorcraken",
"hackyguru",
"SionoiS",
"s-tikhomirov",
"alrevuelta",
"adklempner"
]
class ContributorUpdates {
updates;
constructor() {
this.updates = new Map()
for (const c of CONTRIBUTORS.sort()) {
this.updates.set(c, [])
}
}
update(comment) {
const contributor = comment?.user?.login
if (contributor) {
const comments = this.updates.get(contributor) ?? []
const url = comment.html_url
if (!url) {
console.log(comment)
}
comments.push(url)
this.updates.set(contributor, comments)
}
}
}
module.exports = {
getRepos,
getMilestoneIssues,
getMilestones,
lastFiveDaysIso,
getNewestCommentFirst,
isWeeklyUpdateComment,
cleanUpdate,
formatProjectName,
getMonday,
LB,
cleanLabels,
mapToTeamName,
compareRepos,
formatMonthlyReport,
getEpics,
getIssues,
epicLabels,
ContributorUpdates,
formatCheckBox,
getOctokit,
formatEpicList,
formatIssueTitleWithUrl,
formatMilestoneByEpicList,
firstDayOfMonth,
isMonthlyUpdateComment,
wasUpdatedInMonth
}