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

threadPullRequests.ts

87 lines4.0 KB

1import type { ThreadLinkedPullRequest, ThreadPullRequestLink } from "@t3tools/contracts";
2​
3import { parseUrl } from "./url.ts";
4​
5/** Tombstones prevent sync from re-adding hidden members. */
6export function visibleThreadPullRequests(
7 links: ReadonlyArray<ThreadPullRequestLink>,
8): ReadonlyArray<ThreadPullRequestLink> {
9 const isVisible = (link: ThreadPullRequestLink) => link.source !== "stack-dismissed";
10 // Keep the caller's array when nothing is dismissed, so memoized consumers stay stable.
11 return links.every(isVisible) ? links : links.filter(isVisible);
13​
14export function threadPullRequestUpdatedAtMs(link: ThreadPullRequestLink): number {
15 const value = link.snapshot?.updatedAt ?? link.linkedAt;
16 const ms = Date.parse(value);
17 return Number.isNaN(ms) ? 0 : ms;
19​
20export interface ThreadSearchPullRequest {
21 readonly number: number;
22 readonly title: string | null;
23 /** `owner/repo#123` and the pull request URL, matched only by a whole query. */
24 readonly references: ReadonlyArray<string>;
26​
27const SEARCHED_PULL_REQUEST_TITLES = 3;
28​
29/** Pull requests thread search matches by number, title, and reference. */
30export function threadSearchPullRequests(thread: {
31 readonly pullRequests?: ReadonlyArray<ThreadPullRequestLink> | undefined;
32 readonly linkedPullRequest?: ThreadLinkedPullRequest | null | undefined;
33 readonly branchPullRequest?: ThreadLinkedPullRequest | null | undefined;
34}): ThreadSearchPullRequest[] {
35 const entry = (
36 link: Pick<ThreadLinkedPullRequest, "number" | "repository" | "url">,
37 title: string | null,
38 ): ThreadSearchPullRequest => ({
39 number: link.number,
40 title,
41 references: [`${link.repository}#${link.number}`, link.url],
42 });
43 const branch = thread.branchPullRequest ? [entry(thread.branchPullRequest, null)] : [];
44 if (thread.pullRequests !== undefined && thread.pullRequests.length > 0) {
45 // Every pull request matches by number and link, but only the newest few
46 // by title: a triage thread linked to dozens would match almost any word.
47 const newestFirst = visibleThreadPullRequests(thread.pullRequests)
48 .map((link, index) => ({ link, index, updatedAt: threadPullRequestUpdatedAtMs(link) }))
49 .sort((left, right) => right.updatedAt - left.updatedAt || left.index - right.index);
50 // The synced link carries the title, so the same PR from the branch would only shadow it.
51 // Match by identity, since a link can be a comment or files-tab URL; only the URL names the
52 // host, and another forge can share the repository path and number.
53 const branchPullRequest = thread.branchPullRequest;
54 const branchHost = branchPullRequest && parseUrl(branchPullRequest.url)?.host;
55 const branchLink =
56 branchPullRequest && branchHost
57 ? thread.pullRequests.find(
58 (link) =>
59 link.number === branchPullRequest.number &&
60 link.repository.toLowerCase() === branchPullRequest.repository.toLowerCase() &&
61 parseUrl(link.url)?.host === branchHost,
62 )
63 : undefined;
64 return (branchLink ? [] : branch).concat(
65 newestFirst.map(({ link }, index) => {
66 const title = index < SEARCHED_PULL_REQUEST_TITLES ? link.snapshot?.title : null;
67 const result = entry(link, title ?? null);
68 return branchPullRequest && link === branchLink && link.url !== branchPullRequest.url
69 ? { ...result, references: [...result.references, branchPullRequest.url] }
70 : result;
71 }),
72 );
73 }
74 return thread.linkedPullRequest ? branch.concat(entry(thread.linkedPullRequest, null)) : branch;
76​
77export function threadPullRequestSearchTerms(thread: {
78 readonly pullRequests?: ReadonlyArray<ThreadPullRequestLink> | undefined;
79 readonly linkedPullRequest?: ThreadLinkedPullRequest | null | undefined;
80 readonly branchPullRequest?: ThreadLinkedPullRequest | null | undefined;
81}): string[] {
82 return threadSearchPullRequests(thread).flatMap((pullRequest) => [
83 `#${pullRequest.number}`,
84 ...pullRequest.references,
85 ...(pullRequest.title ? [pullRequest.title] : []),
86 ]);