Repository navigation
Expand file tree
/
Copy pathrow.js
More file actions
430 lines (383 loc) · 14.2 KB
/
Copy pathrow.js
File metadata and controls
430 lines (383 loc) · 14.2 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/**
* @import { Summary } from "./pull-request.js"
*
* @typedef {object} Row - A PR in the queue and the row that shows it
* @property {object} pr - The PR as the search found it
* @property {object} [detailed] - The copy of `pr` the summary’s details were asked for
* @property {Summary} summary
* @property {HTMLTableRowElement} tr
*/
import { size, sum, withLabel } from "./pull-request.js";
import { href, viewOf } from "./view.js";
import { showTime } from "./time.js";
import { plural } from "./plural.js";
let rowTemplate = document.querySelector("#row");
let reviewerTemplate = document.querySelector("#reviewer");
let labelTemplate = document.querySelector("#label");
/** Why a count with a pattern of ignored files counts every file, keyed by a diff’s `unsplit` */
const unsplitTitles = {
"too-many-files":
"GitHub lists at most 3,000 files of a pull request, so this counts excluded files too",
unlisted: "GitHub didn’t send the list of files, so this counts excluded files too",
commits:
"This adds up the sizes of the commits since the review, so it counts excluded files too",
};
/** Why “Changes since last review” counts the whole PR, keyed by the diff’s `whole` */
const wholeTitles = {
unreviewed: "Not reviewed yet, so this counts the whole pull request",
"force-pushed":
"A force push removed the reviewed commit, so this counts the whole pull request",
older: "The reviewed commit isn’t among the last 100 commits, so this counts the whole pull request",
};
/** Why “Changes since last review” has no changes, keyed by the diff’s `newCommits` */
const newCommitsTitles = {
none: "No new commits since your last review",
merges: "Since your last review, only merges of the base branch were pushed. They bring in changes from other pull requests",
own: "The commits since your last review change no lines in total, for example a change and its revert",
};
/** What each other reviewer did, keyed by their `state` */
const reviewerPhrases = {
awaiting: "has yet to review",
approved: "approved",
"changes-requested": "requested changes",
commented: "commented",
};
/** What each step of an author’s PR says, keyed by its `name`. The author’s steps say what GitHub’s own buttons do, e.g. “Ready for review” */
const stepPhrases = {
draft: () => "Ready for review",
"request-review": () => "Request a review",
"await-review": () => "Waiting for a review",
"resolve-conflicts": () => "Resolve conflicts",
"fix-checks": () => "Fix failing checks",
"make-changes": () => "Make changes",
"re-request": ({ logins }) => (logins.length > 1 ? "Re-request reviews" : "Re-request review"),
reply: ({ count }) => `Reply to ${count} ${plural("conversation", count)}`,
// A request to a team names no one
waiting: ({ logins, count }) =>
count > 1
? `Waiting for ${count} reviews`
: logins.length > 0
? `Waiting for @${logins[0]}`
: "Waiting for a team review",
queued: () => "Queued to merge",
"auto-merge": () => "Auto-merge enabled",
"checks-running": () => "Checks are running",
approved: () => "Merge pull request",
"await-merge": () => "Waiting for a maintainer to merge",
};
/** How many labels a row shows before a button for the rest */
const labelLimit = 5;
/**
* Show a summary in its row, replacing the row only when it shows something new.
* @param {Partial<Row>} row - A new row has no `tr` yet
* @param {Summary} summary
*/
export function fill (row, summary) {
let tr = render(summary);
// Labels the user showed stay shown, so the row only changes when they do
if (row.tr?.querySelector(`.labels > :nth-child(${labelLimit + 1}):not([hidden])`)) {
showAllLabels(tr);
}
// An unchanged row stays, e.g. on a refresh, so a focused link or selected text in it stays too
if (!row.tr?.isEqualNode(tr)) {
row.tr?.replaceWith(tr);
row.tr = tr;
}
row.summary = summary;
}
/**
* Mark what changed since the last load: the PRs it didn’t show, and the cells that differ from it.
* @param {Row[]} rows - The rows on screen
* @param {Map<string, Summary>} last - The summaries it showed, by PR id
* @returns {{ added: number, left: number }} How many PRs it didn’t show, and how many it showed that are gone, e.g. merged or reviewed
*/
export function mark (rows, last) {
let added = 0;
for (let { pr, tr } of rows) {
// Rendered now, so the page’s own changes don’t count, e.g. label links that keep the authors picked since
let old = last.has(pr.id) && render(last.get(pr.id));
let cells = old ? [...tr.cells].filter(cell => !same(cell, old.cells[cell.cellIndex])) : [];
if (old && cells.length === 0) {
continue;
}
let label = tr.querySelector("mark");
label.textContent = old ? "changed" : "new";
label.hidden = false;
if (!old) {
tr.classList.add("new");
added++;
}
for (let cell of cells) {
cell.classList.add("changed");
}
}
let left = new Set(last.keys()).difference(new Set(rows.map(row => row.pr.id))).size;
return { added, left };
}
/**
* What the rows show, by PR id, for {@link mark} to compare with on the next load.
* @param {Row[]} rows
* @returns {Map<string, Summary>}
*/
export function summaries (rows) {
return new Map(rows.map(row => [row.pr.id, row.summary]));
}
/**
* Point the label and author links of the rows at the URL’s view, e.g. after a pick of authors, which renders no row anew.
* @param {Row[]} rows
*/
export function relink (rows) {
for (let { tr } of rows) {
for (let link of tr.querySelectorAll(".labels a")) {
link.href = labelLink(link.textContent);
}
for (let link of tr.querySelectorAll(".author")) {
link.href = authorLink(link.textContent);
}
}
}
/**
* Remove the marks, leaving the row as {@link render} makes it, e.g. no `class=""`, so that it equals its next render.
* @param {HTMLTableRowElement} tr
*/
export function unmark (tr) {
let label = tr.querySelector("mark");
label.hidden = true;
label.textContent = "";
for (let element of [tr, ...tr.cells]) {
element.classList.remove("new", "changed", "stale");
if (element.classList.length === 0) {
element.removeAttribute("class");
}
}
}
/**
* Whether two cells show the same thing. A time compares by its date alone, since its text, tooltip and class change as time passes,
* e.g. “5 min. ago”, or “· 1,036 days” in the tooltip.
* Labels compare all of them, shown or not, since the user may have shown them after the last load.
* A cell that was empty, e.g. of a PR whose details failed, showed nothing to change from. Avatars alone are not empty.
*/
function same (cell, other) {
if (!other.textContent.trim() && !other.querySelector("img")) {
return true;
}
let [x, y] = [cell, other].map(element => {
let clone = element.cloneNode(true);
// The reason for “No changes” alone changes nothing on screen, e.g. a merge of the base branch after a re-request
clone.removeAttribute("title");
for (let time of clone.querySelectorAll("time")) {
time.replaceWith(time.dateTime);
}
showAllLabels(clone);
return clone;
});
return x.isEqualNode(y);
}
function render ({
number,
title,
url,
author,
avatar,
labels,
opened,
draft,
step,
diff,
diffSinceReview,
iterations,
waitingSince,
reviewers,
}) {
let tr = rowTemplate.content.firstElementChild.cloneNode(true);
let link = tr.querySelector(".title");
link.href = url;
link.textContent = title;
showLabels(tr.querySelector(".headline"), labels);
tr.querySelector(".number").textContent = `#${number}`;
// As on GitHub’s pulls page, a click on the author shows only their PRs
let name = tr.querySelector(".author");
name.querySelector("span").textContent = author;
name.href = authorLink(author);
name.ariaLabel = `Filter by author ${author}`;
// An author’s PRs are all theirs, so the byline says when, not by whom
tr.querySelector(opened ? ".by" : ".opened").remove();
if (opened) {
showTime(tr.querySelector(".opened time"), opened);
}
let image = tr.querySelector(".avatar img");
image.src = avatar;
// The link to the profile, named as GitHub names avatars
image.alt = `@${author}`;
// As in GitHub’s list of pull requests, where a draft’s icon differs from an open PR’s
if (draft) {
let kind = tr.querySelector(".kind");
kind.classList.add("draft");
kind.role = "img";
kind.ariaLabel = "Draft";
kind.removeAttribute("aria-hidden");
}
tr.querySelector(".avatar").href = `https://github.com/${author}`;
showStep(tr.querySelector(".step"), step, url);
showDiff(tr.querySelector(".diff"), diff);
showDiff(tr.querySelector(".since-review"), diffSinceReview);
tr.classList.toggle("totals", Boolean(tr.querySelector(".total:not([hidden])")));
tr.querySelector(".iterations").textContent = iterations === null ? "—" : (iterations ?? "");
// For an author, how long the step has waited. For a reviewer, how long ago the request came
showTime(tr.querySelector(".requested time"), waitingSince, { duration: Boolean(step) });
showReviewers(tr.querySelector(".reviewers"), reviewers);
return tr;
}
/** Links to the queue of each label, e.g. `?repo=foo/bar&filter=label:"bug"`, the ones past {@link labelLimit} behind a button */
function showLabels (headline, labels) {
if (!labels.length) {
return;
}
let list = document.createElement("ul");
list.className = "labels";
list.ariaLabel = "Labels";
list.append(
...labels.map(({ name, color, description }, i) => {
let item = labelTemplate.content.firstElementChild.cloneNode(true);
let link = item.querySelector("a");
link.href = labelLink(name);
link.textContent = name;
link.ariaLabel = `Filter by label ${name}`;
link.title = description ?? "";
item.style.setProperty("--color", `#${color}`);
item.hidden = i >= labelLimit;
return item;
}),
);
let rest = labels.length - labelLimit;
if (rest > 0) {
let item = document.createElement("li");
let button = document.createElement("button");
button.className = "more";
button.textContent = `+${rest}`;
button.ariaLabel = `+${rest} more ${plural("label", rest)}`;
button.addEventListener("click", () => {
let first = list.querySelector("li[hidden] a");
showAllLabels(list);
first.focus();
});
item.append(button);
list.append(item);
}
headline.append(" ", list);
}
/** The URL’s view with the label `name` in its filter, e.g. `?repo=foo/bar&filter=label:"bug"`. */
function labelLink (name) {
return href({ filter: withLabel(viewOf().filter, name) });
}
/** The URL’s view with `login` as the only picked author, e.g. `?repo=foo/bar&authors=foo`. */
function authorLink (login) {
return href({ authors: login });
}
function showAllLabels (element) {
for (let item of element.querySelectorAll(".labels li[hidden]")) {
item.hidden = false;
}
element.querySelector(".labels .more")?.parentElement.remove();
}
/**
* Show the step as text when others have the turn. Otherwise show a button to where the author does it on GitHub, the PR’s page unless the step names another.
* A draft’s button is quieter, since a draft waits for no one, e.g. “Ready for review”.
*/
function showStep (cell, step, url) {
if (!step) {
return;
}
let text = stepPhrases[step.name](step);
if (step.whose === "others") {
cell.append(text);
return;
}
let link = Object.assign(document.createElement("a"), {
href: step.url ?? url,
target: "_blank",
className: step.whose === "nobody" ? "action quiet" : "action",
textContent: text,
});
cell.append(link);
}
function showReviewers (cell, reviewers) {
if (!reviewers?.length) {
cell.textContent = reviewers ? "—" : "";
return;
}
let list = document.createElement("ul");
list.append(
...reviewers.map(({ name, avatar, state, url, earlier }) => {
let item = reviewerTemplate.content.firstElementChild.cloneNode(true);
let link = item.querySelector("a");
let image = item.querySelector("img");
item.className = state;
item.classList.toggle("earlier", Boolean(earlier));
link.href = url;
image.src = avatar;
// On the link, so keyboard focus reaches it too
image.alt =
link.title = `${name} ${earlier ? "approved an earlier version" : reviewerPhrases[state]}`;
return item;
}),
);
cell.append(list);
}
/** A diff’s figures, e.g. “+1,200 −30” */
function showFigures (figures, { additions, deletions }) {
figures.querySelector(".added").textContent = `+${additions.toLocaleString("en")}`;
figures.querySelector(".deleted").textContent = `−${deletions.toLocaleString("en")}`;
}
/** Text a screen reader hears, which the figures’ look says to sighted users */
function spoken (text) {
return Object.assign(document.createElement("span"), {
className: "spoken",
textContent: text,
});
}
function showDiff (cell, diff) {
if (!diff) {
cell.textContent = "";
return;
}
let total = diff.ignored && sum([diff, diff.ignored]);
// NOTE: In a row with a total, it stays centered, below the other size’s first line
if (size(total ?? diff) === 0) {
cell.textContent = "No changes";
// The “Changes” column’s diff and a whole PR’s have no `newCommits`
if (diff.newCommits) {
cell.title = newCommitsTitles[diff.newCommits];
}
return;
}
let link = cell.querySelector("a");
link.href = diff.url;
showFigures(link, diff);
cell.querySelector(".diffstat").style =
`--additions: ${diff.additions}; --deletions: ${diff.deletions}`;
// The lines of every file, ignored ones too, under those of the rest, e.g. “+650 −32 total”.
// Without a split, e.g. in a sum of commits, the figures are the total already, marked as a stand-in
if (diff.unsplit) {
cell.classList.add("unsplit");
link.title = unsplitTitles[diff.unsplit];
link.append(spoken(" with excluded files"));
}
else if (total && size(diff.ignored) > 0) {
let all = cell.querySelector(".total");
showFigures(all, total);
all.hidden = false;
cell.querySelector("small").hidden = false;
// Sighted users tell the two lines apart by the word “total” under the first,
// so a screen reader hears the link as “+585 −20 without excluded files”, then “+650 −32 total”
link.append(spoken(" without excluded files"));
}
if (diff.whole) {
cell.classList.add("whole");
// On the link, so keyboard focus reaches it too
link.title = [wholeTitles[diff.whole], unsplitTitles[diff.unsplit]]
.filter(Boolean)
.join(". ");
cell.querySelector(".force-pushed").hidden = diff.whole !== "force-pushed";
}
}