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

composerPullRequestMatches.ts

106 lines3.9 KB

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;
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 );
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 // Hermes has no ES2023 array methods, so sort a fresh array.
67 return [...uniqueEntries.values()]
68 .sort(
69 (left, right) =>
70 Number(isExactMatch(right)) - Number(isExactMatch(left)) ||
71 Number(isLinked(right)) - Number(isLinked(left)) ||
72 right.updatedAt.localeCompare(left.updatedAt),
73 )
74 .slice(0, input.limit);
76​
77/** Puts the thread's linked pull requests that match a text query ahead of the searched ones. */
78export function withLinkedComposerPullRequestsFirst<
79 Entry extends ComposerPullRequestMatch,
80 Linked extends ComposerPullRequestMatch & {
81 readonly title: string;
82 readonly headBranch: string;
83 },
84>(input: {
85 readonly matches: ReadonlyArray<Entry>;
86 readonly linked: ReadonlyArray<Linked>;
87 readonly query: string;
88 readonly limit: number;
89}): ReadonlyArray<Entry | Linked> {
90 const keyOf = (entry: ComposerPullRequestMatch) =>
91 `${entry.repository.trim().toLowerCase()}#${entry.number}`;
92 const matchesByKey = new Map(input.matches.map((entry) => [keyOf(entry), entry]));
93 const words = input.query.toLowerCase().split(/\s+/u).filter(Boolean);
94 const linked = input.linked.flatMap((entry): Array<Entry | Linked> => {
95 const match = matchesByKey.get(keyOf(entry));
96 if (match !== undefined) return [match];
97 const text =
98 `#${entry.number} ${entry.title} ${entry.repository} ${entry.headBranch}`.toLowerCase();
99 return words.every((word) => text.includes(word)) ? [entry] : [];
100 });
101 const linkedKeys = new Set(linked.map(keyOf));
102 return [...linked, ...input.matches.filter((entry) => !linkedKeys.has(keyOf(entry)))].slice(
103 0,
104 input.limit,
105 );