Open History to see the last commit
1import type { ThreadPullRequestLink } from "@t3tools/contracts";
2import type { ThreadPullRequestChain } from "@t3tools/shared/threadPullRequests";
3
4export interface PullRequestListLine {
5 readonly link: ThreadPullRequestLink;
6 readonly depth: number;
7 readonly chainKey: string;
8 readonly stack: { readonly kind: ThreadPullRequestChain["kind"]; readonly size: number } | null;
9}
10
11function activityAt(link: ThreadPullRequestLink): number {
12 const ms = Date.parse(link.snapshot?.updatedAt ?? link.linkedAt);
13 return Number.isNaN(ms) ? 0 : ms;
14}
15
16function chainKeyOf(chain: ThreadPullRequestChain): string {
17 const bottom = chain.layers[0]!;
18 return `${bottom.host}/${bottom.repository}#${bottom.number}`;
19}
20
21export function pullRequestListLines(
22 chains: ReadonlyArray<ThreadPullRequestChain>,
23): ReadonlyArray<PullRequestListLine> {
24 const ordered = [...chains].sort(
25 (left, right) =>
26 Math.max(...right.layers.map(activityAt)) - Math.max(...left.layers.map(activityAt)),
27 );
28 return ordered.flatMap((chain) => {
29 const chainKey = chainKeyOf(chain);
30 return chain.layers.map((link, index) => ({
31 link,
32 depth: chain.depths[index]!,
33 chainKey,
34 stack:
35 index === 0 && chain.layers.length > 1
36 ? { kind: chain.kind, size: chain.layers.length }
37 : null,
38 }));
39 });
40}