-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy path[[...page]].tsx
678 lines (637 loc) · 19 KB
/
[[...page]].tsx
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
import CheckBoxSelector from "components/common/CheckBoxSelector";
import CopyLink from "components/common/CopyLink";
import LoadingPage from "components/common/LoadingPage";
import PageSelector from "components/common/PageSelector";
import { LocalTimeHuman } from "components/common/TimeUtils";
import TooltipTarget from "components/common/tooltipTarget/TooltipTarget";
import styles from "components/hud.module.css";
import {
GroupHudTableColumns,
GroupHudTableHeader,
passesGroupFilter,
} from "components/hud/GroupHudTableHeaders";
import HudGroupedCell from "components/job/GroupJobConclusion";
import JobConclusion from "components/job/JobConclusion";
import JobFilterInput from "components/job/JobFilterInput";
import JobTooltip from "components/job/JobTooltip";
import { fetcher } from "lib/GeneralUtils";
import {
getGroupingData,
groups,
isUnstableGroup,
sortGroupNamesForHUD,
} from "lib/JobClassifierUtil";
import {
isFailedJob,
isRerunDisabledTestsJob,
isUnstableJob,
} from "lib/jobUtils";
import { ParamSelector } from "lib/ParamSelector";
import { track } from "lib/track";
import {
formatHudUrlForRoute,
Highlight,
HudParams,
IssueData,
JobData,
packHudParams,
RowData,
} from "lib/types";
import {
useGroupingPreference,
useHideGreenColumnsPreference,
useMonsterFailuresPreference,
usePreference,
} from "lib/useGroupingPreference";
import useHudData from "lib/useHudData";
import useTableFilter from "lib/useTableFilter";
import Head from "next/head";
import { useRouter } from "next/router";
import { IssueLabelApiResponse } from "pages/api/issue/[label]";
import React, {
createContext,
useCallback,
useContext,
useEffect,
useState,
} from "react";
import useSWR from "swr";
export function JobCell({
sha,
job,
unstableIssues,
}: {
sha: string;
job: JobData;
unstableIssues: IssueData[];
}) {
const [pinnedId, setPinnedId] = useContext(PinnedTooltipContext);
const style = pinnedId.name == job.name ? styles.highlight : "";
return (
<td onDoubleClick={() => window.open(job.htmlUrl)}>
<TooltipTarget
pinnedId={pinnedId}
setPinnedId={setPinnedId}
tooltipContent={<JobTooltip job={job} sha={pinnedId.sha || sha} />}
sha={sha as string}
name={job.name as string}
>
<div className={`${styles.center} ${style}`}>
<JobConclusion
conclusion={job.conclusion}
failedPreviousRun={job.failedPreviousRun}
classified={job.failureAnnotation != null}
warningOnly={
isFailedJob(job) &&
(isRerunDisabledTestsJob(job) ||
isUnstableJob(job, unstableIssues))
}
jobData={job}
/>
</div>
</TooltipTarget>
</td>
);
}
function HudRow({
rowData,
names,
unstableIssues,
}: {
rowData: RowData;
names: string[];
unstableIssues: IssueData[];
}) {
const router = useRouter();
const params = packHudParams(router.query);
const sha = rowData.sha;
const [pinnedId, setPinnedId] = useContext(PinnedTooltipContext);
const style = pinnedId.sha == sha ? styles.highlight : "";
function clickCommit(e: React.MouseEvent) {
if (pinnedId.name !== undefined || pinnedId.sha !== undefined) {
return;
}
e.stopPropagation();
setPinnedId({ sha: rowData.sha, name: undefined });
}
return (
<tr className={style} onClick={(e) => clickCommit(e)}>
<td className={styles.jobMetadata}>
<LocalTimeHuman timestamp={rowData.time} />
</td>
<td className={styles.jobMetadata}>
<a href={rowData.commitUrl}>{sha.substring(0, 7)}</a>
</td>
<td className={styles.jobMetadata}>
<div className={styles.jobMetadataTruncated}>
{/* here, we purposefully do not use Link/. The prefetch behavior
(even with prefetch disabled) spams our backend).*/}
<a href={`/${params.repoOwner}/${params.repoName}/commit/${sha}`}>
{rowData.commitTitle}
</a>
</div>
</td>
<td className={styles.jobMetadata}>
{rowData.prNum !== null && (
<a
href={`https://github.com/${params.repoOwner}/${params.repoName}/pull/${rowData.prNum}`}
title={
rowData.isForcedMerge
? rowData.isForcedMergeWithFailures
? "Forced merge with failures that were merge-blocking"
: "Forced merge. Had no merge-blocking failures"
: undefined
}
>
{rowData.isForcedMerge ? (
<mark
className={
rowData.isForcedMergeWithFailures
? styles.forcedMergeWithFailure
: styles.forcedMerge
}
>
#{rowData.prNum}
</mark>
) : (
<div>#{rowData.prNum}</div>
)}
</a>
)}
</td>
<td className={styles.jobMetadata}>
<div className={styles.jobMetadataTruncatedAuthor}>
{rowData.authorUrl !== null ? (
<a href={rowData.authorUrl}>{rowData.author}</a>
) : (
rowData.author
)}
</div>
</td>
<HudJobCells
rowData={rowData}
names={names}
unstableIssues={unstableIssues}
params={params}
/>
</tr>
);
}
function HudJobCells({
rowData,
names,
unstableIssues,
params,
}: {
rowData: RowData;
names: string[];
unstableIssues: IssueData[];
params: HudParams;
}) {
let groupNames = groups.map((group) => group.name).concat("other");
const { expandedGroups, setExpandedGroups, groupNameMapping } =
useContext(GroupingContext);
return (
<>
{names.map((name) => {
if (groupNames.includes(name)) {
let numClassified = 0;
for (const jobName of groupNameMapping.get(name) ?? []) {
if (rowData.nameToJobs.get(jobName)?.failureAnnotation != null) {
numClassified++;
}
}
const failedJobs = groupNameMapping.get(name)?.filter((jobName) => {
const job = rowData.nameToJobs.get(jobName);
return job && isFailedJob(job);
});
return (
<HudGroupedCell
sha={rowData.sha}
key={name}
groupName={name}
repoOwner={params.repoOwner}
repoName={params.repoName}
jobs={
groupNameMapping
.get(name)
?.map((jobName) => {
return rowData.nameToJobs.get(jobName);
})
.filter((job) => job != null && job != undefined) as JobData[]
}
isExpanded={expandedGroups.has(name)}
toggleExpanded={() => {
if (expandedGroups.has(name)) {
expandedGroups.delete(name);
} else {
expandedGroups.add(name);
}
setExpandedGroups(new Set(expandedGroups));
}}
isClassified={
numClassified != 0 && numClassified == failedJobs?.length
}
unstableIssues={unstableIssues}
/>
);
} else {
const job = rowData.nameToJobs.get(name);
return (
<JobCell
sha={rowData.sha}
key={name}
job={job ?? { name: name, conclusion: undefined }}
unstableIssues={unstableIssues}
/>
);
}
})}
</>
);
}
function HudTableBody({
shaGrid,
names,
unstableIssues,
}: {
shaGrid: RowData[];
names: string[];
unstableIssues: IssueData[];
}) {
return (
<tbody>
{shaGrid.map((row: RowData) => (
<HudRow
key={row.sha}
rowData={row}
names={names}
unstableIssues={unstableIssues}
/>
))}
</tbody>
);
}
function GroupFilterableHudTable({
params,
children,
groupNames,
useGrouping,
setUseGrouping,
hideUnstable,
setHideUnstable,
hideGreenColumns,
setHideGreenColumns,
}: {
params: HudParams;
children: React.ReactNode;
groupNames: string[];
useGrouping: boolean;
setUseGrouping: any;
hideUnstable: boolean;
setHideUnstable: any;
hideGreenColumns: boolean;
setHideGreenColumns: any;
}) {
const { jobFilter, handleSubmit } = useTableFilter(params);
const headerNames = groupNames;
const [mergeLF, setMergeLF] = useContext(MergeLFContext);
return (
<>
<div style={{ position: "relative", clear: "both" }}>
<div className={styles.controlsContainer}>
<JobFilterInput
currentFilter={jobFilter}
handleSubmit={handleSubmit}
/>
<CheckBoxSelector
value={useGrouping}
setValue={(value) => setUseGrouping(value)}
checkBoxName="groupView"
labelText={"Use grouped view"}
/>
<CheckBoxSelector
value={hideUnstable}
setValue={(value) => setHideUnstable(value)}
checkBoxName="hideUnstable"
labelText={"Hide unstable jobs"}
/>
<CheckBoxSelector
value={hideGreenColumns}
setValue={(value) => setHideGreenColumns(value)}
checkBoxName="hideGreenColumns"
labelText={"Hide green columns"}
/>
<CheckBoxSelector
value={mergeLF}
setValue={setMergeLF}
checkBoxName="mergeLF"
labelText={"Condense LF jobs"}
/>
<MonsterFailuresCheckbox />
</div>
<table className={styles.hudTable} style={{ overflow: "auto" }}>
<GroupHudTableColumns names={headerNames} />
<GroupHudTableHeader names={headerNames} />
{children}
</table>
</div>
</>
);
}
export const MonsterFailuresContext = createContext<
[boolean, ((_value: boolean) => void) | undefined]
>([false, undefined]);
export const GroupingContext = createContext<{
groupNameMapping: Map<string, Array<string>>;
expandedGroups: Set<string>;
setExpandedGroups: React.Dispatch<React.SetStateAction<Set<string>>>;
}>({
groupNameMapping: new Map(),
expandedGroups: new Set(),
setExpandedGroups: () => {},
});
export function MonsterFailuresProvider({
children,
}: {
children: React.ReactNode;
}) {
const [monsterFailures, setMonsterFailures] = useMonsterFailuresPreference();
return (
<MonsterFailuresContext.Provider
value={[monsterFailures, setMonsterFailures]}
>
{children}
</MonsterFailuresContext.Provider>
);
}
export function MonsterFailuresCheckbox() {
const [monsterFailures, setMonsterFailures] = useContext(
MonsterFailuresContext
);
return (
<CheckBoxSelector
value={monsterFailures}
setValue={(value) => setMonsterFailures && setMonsterFailures(value)}
checkBoxName="monsterFailures"
labelText={"Monsterize failures"}
/>
);
}
function HudTable({ params }: { params: HudParams }) {
const data = useHudData(params);
if (data === undefined) {
return <LoadingPage />;
}
return (
<>
<GroupedHudTable params={params} data={data} />
</>
);
}
function HudHeader({ params }: { params: HudParams }) {
function handleBranchSubmit(branch: string) {
window.location.href = formatHudUrlForRoute("hud", { ...params, branch });
}
function handleRepoSubmit(repoOwnerAndName: string) {
const split = repoOwnerAndName.split("/");
window.location.href = formatHudUrlForRoute("hud", {
...params,
repoOwner: split[0],
repoName: split[1],
});
}
return (
<h1>
<ParamSelector
value={`${params.repoOwner}/${params.repoName}`}
handleSubmit={handleRepoSubmit}
/>
:{" "}
<ParamSelector value={params.branch} handleSubmit={handleBranchSubmit} />
</h1>
);
}
export const PinnedTooltipContext = createContext<[Highlight, any]>([
{ sha: undefined, name: undefined },
null,
]);
export const MergeLFContext = createContext<[boolean, (val: boolean) => void]>([
false,
(_) => {},
]);
export default function Hud() {
const router = useRouter();
const [mergeLF, setMergeLF] = usePreference("mergeLF");
const params = packHudParams({ ...router.query, mergeLF: mergeLF });
// Logic to handle tooltip pinning. The behavior we want is:
// - If the user clicks on a tooltip, it should be pinned.
// - While a tooltip is pinned, we don't show any other tooltips.
// - Clicking outside the tooltip or pressing esc should unpin it.
// This state needs to be set up at this level because we want to capture all
// clicks.
const [pinnedTooltip, setPinnedTooltip] = useState<Highlight>({
sha: undefined,
name: undefined,
});
function handleClick() {
setPinnedTooltip({ sha: undefined, name: undefined });
}
const handleKeyDown = useCallback(
(e: KeyboardEvent) => {
if (e.code === "Escape") {
setPinnedTooltip({ sha: undefined, name: undefined });
}
},
[setPinnedTooltip]
);
useEffect(() => {
document.addEventListener("keydown", handleKeyDown);
return () => {
document.removeEventListener("keydown", handleKeyDown);
};
}, []);
const titleRepoInfo =
params.repoOwner != null && params.repoName != null && params.branch != null
? ` (${params.repoOwner}/${params.repoName}: ${params.branch})`
: "";
return (
<>
<Head>
<title>{`HUD ${titleRepoInfo}`}</title>
</Head>
<PinnedTooltipContext.Provider value={[pinnedTooltip, setPinnedTooltip]}>
<MonsterFailuresProvider>
<MergeLFContext.Provider value={[mergeLF, setMergeLF]}>
{params.branch !== undefined && (
<div onClick={handleClick}>
<div style={{ display: "flex", alignItems: "flex-end" }}>
<HudHeader params={params} />
<CopyPermanentLink
params={params}
style={{ marginLeft: "10px" }}
/>
</div>
<HudTable params={params} />
<PageSelector params={params} baseUrl="hud" />
<br />
<div>
<em>This page automatically updates.</em>
</div>
</div>
)}
</MergeLFContext.Provider>
</MonsterFailuresProvider>
</PinnedTooltipContext.Provider>
</>
);
}
function useLatestCommitSha(params: HudParams) {
const data = useHudData({ ...params, page: 1, per_page: 1 });
if (data === undefined) {
return null;
}
if (data.length === 0) {
return null; // Nothing worth copying
}
if (data[0].sha === undefined) {
return null; // No sha to copy. This should never happen (TM)
}
return data[0].sha;
}
function CopyPermanentLink({
params,
style,
}: {
params: HudParams;
style?: React.CSSProperties;
}) {
// Branch and tag pointers can change over time.
// For a permanent, we take the latest immutable commit as our reference
const latestCommitSha = useLatestCommitSha(params);
if (latestCommitSha === null) {
return <></>;
}
let permaParams = { ...params, branch: latestCommitSha };
const domain = window.location.origin;
const path = formatHudUrlForRoute("hud", permaParams);
const url = `${domain}${path}`;
return <CopyLink textToCopy={url} compressed={false} style={style} />;
}
function GroupedHudTable({
params,
data,
}: {
params: HudParams;
data: RowData[];
}) {
const { data: unstableIssuesData } = useSWR<IssueLabelApiResponse>(
`/api/issue/unstable`,
fetcher,
{
dedupingInterval: 300 * 1000,
refreshInterval: 300 * 1000, // refresh every 5 minutes
}
);
const jobNames = new Set(
data.flatMap((row) => Array.from(row.nameToJobs.keys()))
);
const [hideUnstable, setHideUnstable] = usePreference("hideUnstable");
const [hideGreenColumns, setHideGreenColumns] =
useHideGreenColumnsPreference();
const [useGrouping, setUseGrouping] = useGroupingPreference(
params.nameFilter
);
const { shaGrid, groupNameMapping, jobsWithFailures, groupsWithFailures } =
getGroupingData(
data,
jobNames,
(!useGrouping && hideUnstable) || (useGrouping && !hideUnstable),
unstableIssuesData ?? []
);
const [expandedGroups, setExpandedGroups] = useState(new Set<string>());
const { jobFilter } = useTableFilter(params);
const router = useRouter();
useEffect(() => {
// Only run on component mount, this assumes that the user's preference is
// the value in local storage
track(router, "groupingPreference", { useGrouping: useGrouping });
}, [router, useGrouping]);
const groupNames = Array.from(groupNameMapping.keys());
let names = sortGroupNamesForHUD(groupNames);
if (useGrouping) {
expandedGroups.forEach((group) => {
const nameInd = names.indexOf(group);
names = [
...names.slice(0, nameInd + 1),
...(groupNameMapping.get(group) ?? []),
...names.slice(nameInd + 1),
];
});
if (hideUnstable) {
names = names.filter(
(name) => !isUnstableGroup(name, unstableIssuesData ?? [])
);
}
} else {
names = [...jobNames];
groups.forEach((group) => {
if (
groupNames.includes(group.name) &&
(group.persistent ||
(isUnstableGroup(group.name, unstableIssuesData ?? []) &&
hideUnstable))
) {
// Add group name, take out all the jobs that belong to that group
// unless the group is expanded
names.push(group.name);
names = names.filter(
(name) => !groupNameMapping.get(group.name)?.includes(name)
);
if (expandedGroups.has(group.name)) {
names = names.concat(groupNameMapping.get(group.name) ?? []);
}
}
});
}
names = names.filter((name) => {
// Filter by job filter text first
if (!passesGroupFilter(jobFilter, name, groupNameMapping)) {
return false;
}
// If hiding green columns, filter out names that don't have any failed jobs
if (hideGreenColumns) {
// For group names, check if any job in the group has failures
if (groupNameMapping.has(name)) {
return groupsWithFailures.has(name);
}
// For individual job names, check if this job has failures
else {
return jobsWithFailures.has(name);
}
}
return true;
});
return (
<GroupingContext.Provider
value={{ groupNameMapping, expandedGroups, setExpandedGroups }}
>
<GroupFilterableHudTable
params={params}
groupNames={names}
useGrouping={useGrouping}
setUseGrouping={setUseGrouping}
hideUnstable={hideUnstable}
setHideUnstable={setHideUnstable}
hideGreenColumns={hideGreenColumns}
setHideGreenColumns={setHideGreenColumns}
>
<HudTableBody
shaGrid={shaGrid}
names={names}
unstableIssues={unstableIssuesData ?? []}
/>
</GroupFilterableHudTable>
</GroupingContext.Provider>
);
}