-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
347 lines (271 loc) · 10.3 KB
/
index.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
const {
getRepos,
getMilestoneIssues,
lastFiveDaysIso,
getNewestCommentFirst,
isWeeklyUpdateComment,
cleanUpdate,
getOctokit,
getMilestones,
getMilestoneLabel,
getEpics,
formatMilestoneByEpicList,
wasUpdatedInMonth,
formatMonthlyReport, firstDayOfMonth, isMonthlyUpdateComment, getIssues, formatProjectName, getMonday, LB,
compareRepos,
formatIssueTitleWithUrl,
ContributorUpdates,
formatCheckBox,
epicLabels,
formatEpicList,
cleanLabels
} = require("./lib");
const {program} = require('commander');
program.name("milestone")
program.command("weekly")
.description("print weekly update report")
.action(async() => {
await weekly()
})
program.command("epics")
.description("list epics")
.action(async () => {
await listEpics()
})
program.command("milestones")
.description("list all epics by milestones")
.action(async () => {
await listByMilestone()
})
program.command("month <month>")
.description("print monthly report")
.action(async (m) => {
await month(m)
})
program.parse()
async function weekly() {
const octokit = getOctokit();
const org = "waku-org"
// Only care about comments made in the last week
const lastWeek = lastFiveDaysIso()
// Get all repos
const repos = await getRepos(octokit, org);
// Get all issues updates in the last week
const issues = []
for (const repo of repos) {
const _issues = await getIssues(octokit, org, repo.name, {state: "all", since: lastWeek, per_page: 100})
issues.push(..._issues.map(i => {
return {repoName: repo.name, ...i}
}))
}
// Track contributor updates
const contributorUpdates = new ContributorUpdates();
// Map<epicLabel, {issue, text}[]>
const weeklyUpdates = new Map()
// Get all weekly update comments
for (const issue of issues) {
const comments = await getNewestCommentFirst(octokit, issue, issue.repoName, lastWeek);
let _weeklyUpdatesText = []
for (const comment of comments) {
if (isWeeklyUpdateComment(comment)) {
_weeklyUpdatesText.push(cleanUpdate(comment.body))
contributorUpdates.update(comment)
}
}
if (_weeklyUpdatesText.length !== 0) {
let labels = epicLabels(issue).map(l => l.name)
if (!labels || !labels.length) {
labels = cleanLabels(issue)
console.log(labels, issue.html_url)
}
if (!labels || !labels.length) {
console.error("Issue with no labels: " + issue.html_url)
continue;
}
for (const label of labels) {
const _updates = weeklyUpdates.get(label) ?? []
for (const text of _weeklyUpdatesText) {
_updates.push({text, issue})
}
weeklyUpdates.set(label, _updates)
}
}
}
// Check who has done an update
let contributorsCheck = ""
for (const [contributor, comments] of contributorUpdates.updates) {
contributorsCheck += formatCheckBox(comments.length) + contributor + LB
}
contributorsCheck += LB
console.log(contributorsCheck)
// Build the report
let report = ""
let projectName = formatProjectName(org);
report += "# " + getMonday().toISOString().substring(0, 10) + " " + projectName + " weekly" + LB + LB
repos.sort(compareRepos)
const milestoneRepo = "pm"
// Get all milestones
const milestones = await getMilestones(octokit, org, milestoneRepo)
// Get all epics
const epics = await getEpics(octokit, org, milestoneRepo)
// Sort epics by milestone
const epicsByMilestone = new Map()
for (const epic of epics) {
const labels = epicLabels(epic)
epic.epicLabel = labels[0]?.name ?? undefined
const milestoneNumber = epic.milestone?.number ?? 0
const _epics = epicsByMilestone.get(milestoneNumber) ?? []
_epics.push(epic)
epicsByMilestone.set(milestoneNumber, _epics)
}
milestones.push({number: 0, title: "Other Work"})
epicsByMilestone.set(0, [
{epicLabel: "enhancement", title: "Enhancements"},
{epicLabel: "bug", title: "Bugs"},
{epicLabel: "documentation", title: "Documentation"},
{epicLabel: "dependencies", title: "Chores"}
])
const issueMilestones = await getMilestoneIssues(octokit, org, milestoneRepo)
// Format
for (const milestone of milestones) {
const issueMilestone = issueMilestones.find(i => i.html_url === milestone.description)
const title = issueMilestone ? formatIssueTitleWithUrl(issueMilestone) : milestone.title;
let fmtDueDate = ""
if (milestone.due_on) {
const dueDate = new Date(milestone.due_on);
fmtDueDate = " - " + dueDate.toISOString().substring(0, 10)
}
report += "## " + title + fmtDueDate + LB + LB;
const milestoneUpdates = weeklyUpdates.get("milestone");
if (milestoneUpdates) {
const milestoneUpdate = milestoneUpdates.find(({issue}) => issue.id === issueMilestone?.id )
if (milestoneUpdate) {
report += milestoneUpdate.text + LB + LB
} else {
console.error("Milestone update missing", issueMilestone?.html_url?? milestone.description)
}
} else {
console.error("Milestone update missing", issueMilestone?.html_url?? milestone.description)
}
const epics = epicsByMilestone.get(milestone.number);
for (const epic of epics) {
const updates = weeklyUpdates.get(epic.epicLabel)
if (!updates || !updates.length) {
continue
}
report += "### " + formatIssueTitleWithUrl(epic) + LB + LB
// Add updates
for (const {issue, text} of updates) {
if (epic.id !== issue.id) {
const prefix = issue.repoName ? `[${issue.repoName}] ` : ""
report += "**" + prefix + formatIssueTitleWithUrl(issue) + "**" + LB
}
report += text + LB + LB
}
}
}
console.log(report)
}
async function month(m) {
const octokit = getOctokit();
const org = "waku-org"
const milestoneRepo = "pm"
const monthIndex = m - 1
// Get all milestones
const milestones = await getMilestoneIssues(octokit, org, milestoneRepo)
const since = firstDayOfMonth(monthIndex)
// For each milestone, get the monthly update
for (const milestone of milestones) {
// TODO: this really only work when getting most recent month
const comments = await getNewestCommentFirst(octokit, milestone, milestoneRepo, since);
for (const comment of comments) {
if (isMonthlyUpdateComment(comment)) {
milestone.monthlyUpdate = cleanUpdate(comment.body)
break
}
}
}
const milestoneToEpics = new Map()
// Get all repositories
const repos = await getRepos(octokit, org);
for (const repo of repos) {
// Get all milestones from the repository.
const epics = await getEpics(octokit, org, repo.name, {state: "all"})
for (let epic of epics) {
const milestoneLabel = getMilestoneLabel(epic)
epic.repo_name = repo.name
let stateOfEpics = milestoneToEpics.get(milestoneLabel) ?? {open: [], closed: [], updated: []}
if (epic.state === "open") {
stateOfEpics.open.push(epic)
} else {
stateOfEpics.closed.push(epic)
}
if (wasUpdatedInMonth(monthIndex, epic)) {
stateOfEpics.updated.push(epic)
}
milestoneToEpics.set(milestoneLabel, stateOfEpics)
}
}
const text = formatMonthlyReport(milestones, milestoneToEpics);
console.log(text)
}
async function listEpics() {
const octokit = getOctokit();
const org = "waku-org"
const pmRepo = "pm"
// Only get open epics
const epics = await getEpics(octokit, org, pmRepo)
const issuesPerLabel = new Map();
const epicsPerLabel = new Map();
for (const epic of epics) {
const labels = epicLabels(epic)
// should be one label per epic
if (labels.length > 1) throw new Error(`Too many labels on ${epic.html_url}`)
if (labels.length) {
issuesPerLabel.set(labels[0].name, [])
epicsPerLabel.set(labels[0].name, epic)
}
}
const allEpicLabels = Array.from(issuesPerLabel.keys());
console.log(allEpicLabels.length + " labels")
// Get all repositories
const repos = await getRepos(octokit, org);
console.log(repos.length + " repos")
for (const repo of repos) {
if (repo.name === pmRepo) continue
// Seems like the best way is to spam the API to get all issues with label of open epics
for (const label of allEpicLabels) {
const issues = await getIssues(octokit, org, repo.name, {labels: label, state: "all"})
const _issues = issuesPerLabel.get(label)
_issues.push(...issues.map(i => {
return {repoName: repo.name, ...i}
}))
issuesPerLabel.set(label, _issues)
}
}
const text = formatEpicList(epicsPerLabel, issuesPerLabel);
console.log(text)
}
async function listByMilestone() {
const octokit = getOctokit();
const org = "waku-org"
const milestoneRepo = "pm"
// Get all repositories
const repos = await getRepos(octokit, org);
// Get all milestones
const milestones = await getMilestoneIssues(octokit, org, milestoneRepo)
const milestoneToEpics = new Map()
for (const repo of repos) {
// Get all milestones from the repository.
const epics = await getEpics(octokit, org, repo.name, {state: "all"})
for (let epic of epics) {
const milestoneLabel = getMilestoneLabel(epic)
epic.repo_name = repo.name
let _m = milestoneToEpics.get(milestoneLabel) ?? []
_m.push(epic)
milestoneToEpics.set(milestoneLabel, _m)
}
}
const text = formatMilestoneByEpicList(milestones, milestoneToEpics);
console.log(text)
}