Open History to see the last commit
1import type { ThreadPullRequestLink } from "@t3tools/contracts";
2
3import { visibleThreadPullRequests } from "./threadPullRequests.ts";
4
5export interface ComposerPullRequestMatch {
6 readonly number: number;
7 readonly projectId: string;
8 readonly repository: string;
9 readonly updatedAt: string;
10}
11
12export function composerPullRequestsFromLinks(
13 links: ReadonlyArray<ThreadPullRequestLink>,
14 projectId: string,
15) {
16 return visibleThreadPullRequests(links).flatMap((link) =>
17 link.snapshot === null
18 ? []
19 : [
20 {
21 projectId,
22 repository: link.repository,
23 number: link.number,
24 url: link.url,
25 title: link.snapshot.title,
26 headBranch: link.snapshot.headBranch,
27 baseBranch: link.snapshot.baseBranch,
28 state: link.snapshot.state,
29 isDraft: link.snapshot.isDraft,
30 updatedAt: link.snapshot.updatedAt ?? link.linkedAt,
31 },
32 ],
33 );
34}
35
36export function filterComposerPullRequestMatches<
37 Entry extends ComposerPullRequestMatch,
38 Linked extends ComposerPullRequestMatch = never,
39>(input: {
40 readonly entries: ReadonlyArray<Entry>;
41 readonly linked?: ReadonlyArray<Linked>;
42 readonly projectId: string;
43 readonly repository: string;
44 readonly query: string;
45 readonly limit: number;
46}): ReadonlyArray<Entry | Linked> {
47 const repository = input.repository.trim().toLowerCase();
48 const keyOf = (entry: ComposerPullRequestMatch) =>
49 `${entry.repository.trim().toLowerCase()}#${entry.number}`;
50 const linked = (input.linked ?? []).filter((entry) => String(entry.number).includes(input.query));
51 const linkedKeys = new Set(linked.map(keyOf));
52 const matchingEntries = input.entries.filter(
53 (entry) =>
54 entry.projectId === input.projectId &&
55 entry.repository.trim().toLowerCase() === repository &&
56 String(entry.number).includes(input.query),
57 );
58 const uniqueEntries = new Map<string, Entry | Linked>();
59 for (const entry of [...matchingEntries, ...linked]) {
60 if (!uniqueEntries.has(keyOf(entry))) {
61 uniqueEntries.set(keyOf(entry), entry);
62 }
63 }
64 const isExactMatch = (entry: ComposerPullRequestMatch) => String(entry.number) === input.query;
65 const isLinked = (entry: ComposerPullRequestMatch) => linkedKeys.has(keyOf(entry));
66 const updatedAtMs = (entry: ComposerPullRequestMatch) => Date.parse(entry.updatedAt) || 0;
67 // Hermes has no ES2023 array methods, so sort a fresh array.
68 return [...uniqueEntries.values()]
69 .sort(
70 (left, right) =>
71 Number(isExactMatch(right)) - Number(isExactMatch(left)) ||
72 Number(isLinked(right)) - Number(isLinked(left)) ||
73 updatedAtMs(right) - updatedAtMs(left),
74 )
75 .slice(0, input.limit);
76}
77
78/** Puts the thread's linked pull requests that match a text query ahead of the searched ones. */
79export function withLinkedComposerPullRequestsFirst<
80 Entry extends ComposerPullRequestMatch,
81 Linked extends ComposerPullRequestMatch & {
82 readonly title: string;
83 readonly headBranch: string;
84 },
85>(input: {
86 readonly matches: ReadonlyArray<Entry>;
87 readonly linked: ReadonlyArray<Linked>;
88 readonly query: string;
89 readonly limit: number;
90}): ReadonlyArray<Entry | Linked> {
91 const keyOf = (entry: ComposerPullRequestMatch) =>
92 `${entry.repository.trim().toLowerCase()}#${entry.number}`;
93 const matchesByKey = new Map(input.matches.map((entry) => [keyOf(entry), entry]));
94 const words = input.query.toLowerCase().split(/\s+/u).filter(Boolean);
95 const linked = input.linked.flatMap((entry): Array<Entry | Linked> => {
96 const match = matchesByKey.get(keyOf(entry));
97 if (match !== undefined) return [match];
98 const text =
99 `#${entry.number} ${entry.title} ${entry.repository} ${entry.headBranch}`.toLowerCase();
100 return words.every((word) => text.includes(word)) ? [entry] : [];
101 });
102 const linkedKeys = new Set(linked.map(keyOf));
103 return [...linked, ...input.matches.filter((entry) => !linkedKeys.has(keyOf(entry)))].slice(
104 0,
105 input.limit,
106 );
107}