Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

composerPullRequestMatches.ts

112 lines4.2 KB

1import type { ThreadPullRequestKey, ThreadPullRequestLink } from "@t3tools/contracts";
2​
3import { foldPullRequestCompletionSearch } from "./pullRequestComposerTrigger.ts";
4import { visibleThreadPullRequests } from "./threadPullRequests.ts";
5​
6export interface ComposerPullRequestMatch extends ThreadPullRequestKey {
7 readonly projectId: string;
8 readonly updatedAt: string;
9}
10​
11export function composerPullRequestsFromLinks(
12 links: ReadonlyArray<ThreadPullRequestLink>,
13 projectId: string,
14) {
15 return visibleThreadPullRequests(links).flatMap((link) =>
16 link.snapshot === null
17 ? []
18 : [
19 {
20 projectId,
21 host: link.host,
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 );
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 readonly keyOf: (entry: Entry | Linked) => string;
47}): ReadonlyArray<Entry | Linked> {
48 const repository = input.repository.trim().toLowerCase();
49 const { keyOf } = input;
50 // Compare numerically so a zero-padded query like "0042" still finds #42.
51 const queryNumber = /^\d+$/u.test(input.query) ? Number(input.query) : Number.NaN;
52 const isExactMatch = (entry: ComposerPullRequestMatch) => entry.number === queryNumber;
53 const matchesQuery = (entry: ComposerPullRequestMatch) =>
54 isExactMatch(entry) || String(entry.number).includes(input.query);
55 const linked = (input.linked ?? []).filter(matchesQuery);
56 const linkedKeys = new Set(linked.map(keyOf));
57 const matchingEntries = input.entries.filter(
58 (entry) =>
59 entry.projectId === input.projectId &&
60 entry.repository.trim().toLowerCase() === repository &&
61 matchesQuery(entry),
62 );
63 const uniqueEntries = new Map<string, Entry | Linked>();
64 for (const entry of [...matchingEntries, ...linked]) {
65 if (!uniqueEntries.has(keyOf(entry))) {
66 uniqueEntries.set(keyOf(entry), entry);
67 }
68 }
69 const isLinked = (entry: Entry | Linked) => linkedKeys.has(keyOf(entry));
70 const updatedAtMs = (entry: ComposerPullRequestMatch) => Date.parse(entry.updatedAt) || 0;
71 // Hermes has no ES2023 array methods, so sort a fresh array.
72 return [...uniqueEntries.values()]
73 .sort(
74 (left, right) =>
75 Number(isExactMatch(right)) - Number(isExactMatch(left)) ||
76 Number(isLinked(right)) - Number(isLinked(left)) ||
77 updatedAtMs(right) - updatedAtMs(left),
78 )
79 .slice(0, input.limit);
81​
82/** Puts the thread's linked pull requests that match a text query ahead of the searched ones. */
83export function withLinkedComposerPullRequestsFirst<
84 Entry extends ComposerPullRequestMatch,
85 Linked extends ComposerPullRequestMatch & {
86 readonly title: string;
87 readonly headBranch: string;
88 },
89>(input: {
90 readonly matches: ReadonlyArray<Entry>;
91 readonly linked: ReadonlyArray<Linked>;
92 readonly query: string;
93 readonly limit: number;
94 readonly keyOf: (entry: Entry | Linked) => string;
95}): ReadonlyArray<Entry | Linked> {
96 const { keyOf } = input;
97 const matchesByKey = new Map(input.matches.map((entry) => [keyOf(entry), entry]));
98 const words = foldPullRequestCompletionSearch(input.query).split(/\s+/u).filter(Boolean);
99 const linked = input.linked.flatMap((entry): Array<Entry | Linked> => {
100 const match = matchesByKey.get(keyOf(entry));
101 if (match !== undefined) return [match];
102 const text = foldPullRequestCompletionSearch(
103 `#${entry.number} ${entry.title} ${entry.repository} ${entry.headBranch}`,
104 );
105 return words.every((word) => text.includes(word)) ? [entry] : [];
106 });
107 const linkedKeys = new Set(linked.map(keyOf));
108 return [...linked, ...input.matches.filter((entry) => !linkedKeys.has(keyOf(entry)))].slice(
109 0,
110 input.limit,
111 );