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

threadPullRequests.ts

66 lines2.7 KB

1import type { ThreadLinkedPullRequest, ThreadPullRequestLink } from "@t3tools/contracts";
2​
3/** Tombstoned stack members stay in the array only so the
4 * sync reactor does not re-add them. */
5export function visibleThreadPullRequests(
6 links: ReadonlyArray<ThreadPullRequestLink>,
7): ReadonlyArray<ThreadPullRequestLink> {
8 return links.filter((link) => link.source !== "stack-dismissed");
9}
10​
11function latestUpdatedAt(link: ThreadPullRequestLink): number {
12 const value = link.snapshot?.updatedAt ?? link.linkedAt;
13 const ms = Date.parse(value);
14 return Number.isNaN(ms) ? 0 : ms;
16​
17export interface ThreadSearchPullRequest {
18 readonly number: number;
19 readonly title: string | null;
20 /** `owner/repo#123` and the pull request URL, matched only by a whole query. */
21 readonly references: ReadonlyArray<string>;
23​
24const SEARCHED_PULL_REQUEST_TITLES = 3;
25​
26/** Pull requests thread search matches by number, title, and reference. */
27export function threadSearchPullRequests(thread: {
28 readonly pullRequests?: ReadonlyArray<ThreadPullRequestLink> | undefined;
29 readonly linkedPullRequest?: ThreadLinkedPullRequest | null | undefined;
30 readonly branchPullRequest?: ThreadLinkedPullRequest | null | undefined;
31}): ThreadSearchPullRequest[] {
32 const entry = (
33 link: Pick<ThreadLinkedPullRequest, "number" | "repository" | "url">,
34 title: string | null,
35 ): ThreadSearchPullRequest => ({
36 number: link.number,
37 title,
38 references: [`${link.repository}#${link.number}`, link.url],
39 });
40 const branch = thread.branchPullRequest ? [entry(thread.branchPullRequest, null)] : [];
41 if (thread.pullRequests !== undefined && thread.pullRequests.length > 0) {
42 // Every pull request matches by number and link, but only the newest few
43 // by title: a triage thread linked to dozens would match almost any word.
44 const newestFirst = [...visibleThreadPullRequests(thread.pullRequests)].sort(
45 (left, right) => latestUpdatedAt(right) - latestUpdatedAt(left),
46 );
47 return branch.concat(
48 newestFirst.map((link, index) =>
49 entry(link, index < SEARCHED_PULL_REQUEST_TITLES ? (link.snapshot?.title ?? null) : null),
50 ),
51 );
52 }
53 return thread.linkedPullRequest ? branch.concat(entry(thread.linkedPullRequest, null)) : branch;
55​
56export function threadPullRequestSearchTerms(thread: {
57 readonly pullRequests?: ReadonlyArray<ThreadPullRequestLink> | undefined;
58 readonly linkedPullRequest?: ThreadLinkedPullRequest | null | undefined;
59 readonly branchPullRequest?: ThreadLinkedPullRequest | null | undefined;
60}): string[] {
61 return threadSearchPullRequests(thread).flatMap((pullRequest) => [
62 `#${pullRequest.number}`,
63 ...pullRequest.references,
64 ...(pullRequest.title ? [pullRequest.title] : []),
65 ]);