+36
![github-actions[bot]](/assets/img/avatar_default.png)








Julius Marminge
maria-rcks
Bilal Bakr
shivam
Vitalii Yehorov
Jake Leventhal
Claude Opus 5
Alex Southwell
github-actions[bot]
Nicholas Wasmiller
PB
Exotic
Yash Singh
Devin
Theo Browne
Gabriel De Andrade
Dara Adedeji
scratchyone
Dominic Roy
chukfinley
Primož Ajdišek
benthecarman
NaveDanan
aaditagrawal
Aditya Garud
Nick Anisimov
MacKinley Smith
Yordis Prieto
t3-code[bot]
AKolenda
Guillermo Casanova
Otavio Salvador
Shirish Pothi
Ishaan Kothari
Bob Fowler
Anton Bezdenezhnykh
ValeraZSD
Ephraim
Ryan Ilano
Alex
maco
Cursor
Tristan Knight
PR Batch Tester
oliver
kamkm
de34391427
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Bilal Bakr <[email protected]> Co-authored-by: shivam <[email protected]> Co-authored-by: Vitalii Yehorov <[email protected]> Co-authored-by: Jake Leventhal <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-authored-by: Alex Southwell <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Nicholas Wasmiller <[email protected]> Co-authored-by: PB <[email protected]> Co-authored-by: Exotic <[email protected]> Co-authored-by: Yash Singh <[email protected]> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Theo Browne <[email protected]> Co-authored-by: Gabriel De Andrade <[email protected]> Co-authored-by: Dara Adedeji <[email protected]> Co-authored-by: scratchyone <[email protected]> Co-authored-by: Dominic Roy <[email protected]> Co-authored-by: chukfinley <[email protected]> Co-authored-by: Primož Ajdišek <[email protected]> Co-authored-by: benthecarman <[email protected]> Co-authored-by: NaveDanan <[email protected]> Co-authored-by: aaditagrawal <[email protected]> Co-authored-by: Aditya Garud <[email protected]> Co-authored-by: Nick Anisimov <[email protected]> Co-authored-by: MacKinley Smith <[email protected]> Co-authored-by: Yordis Prieto <[email protected]> Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com> Co-authored-by: AKolenda <[email protected]> Co-authored-by: Guillermo Casanova <[email protected]> Co-authored-by: Otavio Salvador <[email protected]> Co-authored-by: Shirish Pothi <[email protected]> Co-authored-by: Ishaan Kothari <[email protected]> Co-authored-by: Bob Fowler <[email protected]> Co-authored-by: Anton Bezdenezhnykh <[email protected]> Co-authored-by: ValeraZSD <[email protected]> Co-authored-by: Ephraim <[email protected]> Co-authored-by: Ryan Ilano <[email protected]> Co-authored-by: Alex <[email protected]> Co-authored-by: maco <[email protected]> Co-authored-by: Cursor <[email protected]> Co-authored-by: Tristan Knight <[email protected]> Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: oliver <[email protected]> Co-authored-by: kamkm <[email protected]> Signed-off-by: Yordis Prieto <[email protected]>
348 lines
13 KiB
TypeScript
348 lines
13 KiB
TypeScript
import type {
|
|
RepositoryIdentity,
|
|
SourceControlProviderKind,
|
|
ThreadLinkedPullRequest,
|
|
ThreadPullRequestKey,
|
|
ThreadPullRequestLink,
|
|
} from "@t3tools/contracts";
|
|
|
|
import { pullRequestHostOf } from "@t3tools/contracts";
|
|
import { parseChangeRequestUrl } from "./changeRequestUrl.ts";
|
|
import { canonicalRepositoryKey, sourceControlRepositorySelector } from "./sourceControl.ts";
|
|
|
|
type ThreadPullRequestKeySource = ThreadPullRequestKey & {
|
|
readonly authority?: string;
|
|
readonly url?: string;
|
|
};
|
|
|
|
/** Normalize stored links, recovering Forgejo HTTP ports from old links' URLs. */
|
|
export function normalizeThreadPullRequestKey(
|
|
key: ThreadPullRequestKeySource,
|
|
): ThreadPullRequestKey {
|
|
const parsed = key.url === undefined ? null : parseChangeRequestUrl(key.url);
|
|
const authority =
|
|
key.authority ??
|
|
(parsed?.repository === key.repository.trim().toLowerCase() && parsed.number === key.number
|
|
? parsed.authority
|
|
: undefined);
|
|
const canonical = canonicalRepositoryKey(
|
|
`${(authority ?? key.host).trim().toLowerCase()}/${key.repository.trim().toLowerCase()}`,
|
|
);
|
|
const separator = canonical.indexOf("/");
|
|
return {
|
|
host: canonical.slice(0, separator),
|
|
repository: canonical.slice(separator + 1),
|
|
number: key.number,
|
|
};
|
|
}
|
|
|
|
/** Legacy Azure selectors omit the organization and project; recover those from the PR URL. */
|
|
export function legacyThreadPullRequestKey(
|
|
linked: Pick<ThreadLinkedPullRequest, "repository" | "number" | "url">,
|
|
fallbackHost?: string,
|
|
): ThreadPullRequestKey {
|
|
const parsed = parseChangeRequestUrl(linked.url);
|
|
if (parsed !== null && parsed.number === linked.number) {
|
|
const canonical = canonicalRepositoryKey(`${parsed.host}/${parsed.repository}`);
|
|
if (parsed.authority !== undefined || canonical.startsWith("dev.azure.com/")) {
|
|
return normalizeThreadPullRequestKey(parsed);
|
|
}
|
|
}
|
|
let host = fallbackHost;
|
|
if (host === undefined) {
|
|
try {
|
|
host = new URL(linked.url).hostname;
|
|
} catch {
|
|
host = "unknown";
|
|
}
|
|
}
|
|
return {
|
|
host: host.trim().toLowerCase() || "unknown",
|
|
repository: linked.repository.trim().toLowerCase(),
|
|
number: linked.number,
|
|
};
|
|
}
|
|
|
|
/** Identity comparison for links: host-level, case-insensitive on host and repository. */
|
|
export function threadPullRequestKeysEqual(
|
|
left: ThreadPullRequestKeySource,
|
|
right: ThreadPullRequestKeySource,
|
|
): boolean {
|
|
return threadPullRequestKeyOf(left) === threadPullRequestKeyOf(right);
|
|
}
|
|
|
|
export function threadPullRequestKeyOf(key: ThreadPullRequestKeySource): string {
|
|
const normalized = normalizeThreadPullRequestKey(key);
|
|
return `${normalized.host}/${normalized.repository}#${normalized.number}`;
|
|
}
|
|
|
|
/** Links a user should see. Tombstoned stack members stay in the array only so the
|
|
* sync reactor does not re-add them. */
|
|
export function visibleThreadPullRequests(
|
|
links: ReadonlyArray<ThreadPullRequestLink>,
|
|
): ReadonlyArray<ThreadPullRequestLink> {
|
|
return links.filter((link) => link.source !== "stack-dismissed");
|
|
}
|
|
|
|
function isOpen(link: ThreadPullRequestLink): boolean {
|
|
// Unsynced links are treated as open: they were just linked, and hiding them
|
|
// behind a terminal PR until the first sync would make the link look lost.
|
|
return link.snapshot === null || link.snapshot.state === "open";
|
|
}
|
|
|
|
function latestUpdatedAt(link: ThreadPullRequestLink): number {
|
|
const value = link.snapshot?.updatedAt ?? link.linkedAt;
|
|
const ms = Date.parse(value);
|
|
return Number.isNaN(ms) ? 0 : ms;
|
|
}
|
|
|
|
/** The single pull request a one-slot surface (sidebar badge, tab icon, copy link) shows. */
|
|
export type ThreadCurrentPullRequest =
|
|
| { readonly kind: "single"; readonly link: ThreadPullRequestLink }
|
|
| {
|
|
readonly kind: "stack";
|
|
readonly open: ReadonlyArray<ThreadPullRequestLink>;
|
|
/** Highest layer of the open set; the one "View PR" and copy-link target. */
|
|
readonly top: ThreadPullRequestLink;
|
|
};
|
|
|
|
/**
|
|
* Prefer open work and the highest open layer within a chain. A completed chain still
|
|
* points at its top; unrelated terminal links use the most recently updated request.
|
|
*/
|
|
export function resolveThreadCurrentPullRequest(
|
|
links: ReadonlyArray<ThreadPullRequestLink>,
|
|
): ThreadCurrentPullRequest | null {
|
|
const visible = visibleThreadPullRequests(links);
|
|
if (visible.length === 0) return null;
|
|
const open = visible.filter(isOpen);
|
|
if (open.length === 1) return { kind: "single", link: open[0]! };
|
|
const chains = resolveThreadPullRequestChains(visible);
|
|
if (open.length > 1) {
|
|
// `.reverse()` on a copy, not `.toReversed()`: this runs on Hermes, which has no ES2023
|
|
// array methods, and a TypeError here is fatal on every mobile launch that renders a stack.
|
|
const openChains = chains
|
|
.map((chain) => [...chain.layers].reverse().filter(isOpen))
|
|
.filter((layers) => layers.length > 0)
|
|
.sort(
|
|
(left, right) =>
|
|
Math.max(...right.map((link) => Date.parse(link.linkedAt))) -
|
|
Math.max(...left.map((link) => Date.parse(link.linkedAt))),
|
|
);
|
|
const ordered = openChains.flat();
|
|
return { kind: "stack", open: ordered, top: ordered[0]! };
|
|
}
|
|
if (chains.length === 1) {
|
|
return { kind: "single", link: chains[0]!.layers.at(-1)! };
|
|
}
|
|
const terminal = [...visible].sort(
|
|
(left, right) => latestUpdatedAt(right) - latestUpdatedAt(left),
|
|
);
|
|
return { kind: "single", link: terminal[0]! };
|
|
}
|
|
|
|
/** The one link a legacy `linkedPullRequest` consumer should see, or null. */
|
|
export function resolveThreadCurrentPullRequestLink(
|
|
links: ReadonlyArray<ThreadPullRequestLink>,
|
|
): ThreadPullRequestLink | null {
|
|
const current = resolveThreadCurrentPullRequest(links);
|
|
if (current === null) return null;
|
|
return current.kind === "single" ? current.link : current.top;
|
|
}
|
|
|
|
/** Legacy clients can only route links belonging to the thread's own repository. */
|
|
export function legacyLinkedPullRequestOf(
|
|
links: ReadonlyArray<ThreadPullRequestLink>,
|
|
projectId: ThreadLinkedPullRequest["projectId"],
|
|
identity: RepositoryIdentity | null | undefined,
|
|
): ThreadLinkedPullRequest | null {
|
|
if (!identity) return null;
|
|
// A local-path remote has no host segment and no provider, so there is no host to match.
|
|
const host = pullRequestHostOf(identity, identity.provider as SourceControlProviderKind);
|
|
if (typeof host !== "string") return null;
|
|
const repository = sourceControlRepositorySelector(identity);
|
|
if (repository === null) return null;
|
|
const azureKey =
|
|
identity.provider === "azure-devops"
|
|
? canonicalRepositoryKey(identity.canonicalKey.toLowerCase())
|
|
: null;
|
|
const link = resolveThreadCurrentPullRequestLink(
|
|
links.filter((link) => {
|
|
if (azureKey !== null) {
|
|
const key = legacyThreadPullRequestKey(link, link.host);
|
|
return canonicalRepositoryKey(`${key.host}/${key.repository}`) === azureKey;
|
|
}
|
|
const parsed = parseChangeRequestUrl(link.url);
|
|
if (parsed?.authority !== undefined) {
|
|
try {
|
|
const remote = new URL(identity.locator.remoteUrl);
|
|
if (remote.protocol === "http:" || remote.protocol === "https:") {
|
|
return (
|
|
parsed.authority === remote.host && parsed.repository === repository.toLowerCase()
|
|
);
|
|
}
|
|
} catch {
|
|
// SSH web ports are resolved by the provider's configured login.
|
|
}
|
|
return parsed.host === host && parsed.repository === repository.toLowerCase();
|
|
}
|
|
return (
|
|
link.host.toLowerCase() === host.toLowerCase() &&
|
|
link.repository.toLowerCase() === repository.toLowerCase()
|
|
);
|
|
}),
|
|
);
|
|
if (link === null) return null;
|
|
return {
|
|
projectId,
|
|
repository: azureKey === null ? link.repository : repository,
|
|
number: link.number,
|
|
url: link.url,
|
|
};
|
|
}
|
|
|
|
export interface ThreadPullRequestChain {
|
|
readonly kind: "native" | "derived";
|
|
/** Bottom to top. */
|
|
readonly layers: ReadonlyArray<ThreadPullRequestLink>;
|
|
}
|
|
|
|
/**
|
|
* Groups a thread's links into stacks. Native stacks come from the host and win; the rest
|
|
* are chained by matching one link's base branch to another's head branch within the same
|
|
* repository. A link that chains to nothing is a one-layer chain.
|
|
*/
|
|
export function resolveThreadPullRequestChains(
|
|
links: ReadonlyArray<ThreadPullRequestLink>,
|
|
): ReadonlyArray<ThreadPullRequestChain> {
|
|
const visible = visibleThreadPullRequests(links);
|
|
const chains: Array<ThreadPullRequestChain> = [];
|
|
const placed = new Set<string>();
|
|
|
|
const nativeStacks = new Map<string, Array<ThreadPullRequestLink>>();
|
|
for (const link of visible) {
|
|
if (link.stack === null) continue;
|
|
const key = normalizeThreadPullRequestKey(link);
|
|
const stackKey = `${key.host}/${key.repository}#stack:${link.stack.id}`;
|
|
const members = nativeStacks.get(stackKey) ?? [];
|
|
members.push(link);
|
|
nativeStacks.set(stackKey, members);
|
|
}
|
|
for (const members of nativeStacks.values()) {
|
|
const order = new Map(members[0]!.stack!.layers.map((layer, index) => [layer.number, index]));
|
|
members.sort((left, right) => (order.get(left.number) ?? 0) - (order.get(right.number) ?? 0));
|
|
for (const member of members) placed.add(threadPullRequestKeyOf(member));
|
|
chains.push({ kind: "native", layers: members });
|
|
}
|
|
|
|
const remaining = visible.filter((link) => !placed.has(threadPullRequestKeyOf(link)));
|
|
const branchKey = (link: ThreadPullRequestLink, branch: string) => {
|
|
const key = normalizeThreadPullRequestKey(link);
|
|
return `${key.host}/${key.repository}:${branch}`;
|
|
};
|
|
// Reused head names cannot identify a parent unambiguously.
|
|
const byHead = new Map<string, ThreadPullRequestLink | null>();
|
|
for (const link of remaining) {
|
|
if (link.snapshot === null) continue;
|
|
const key = branchKey(link, link.snapshot.headBranch);
|
|
byHead.set(key, byHead.has(key) ? null : link);
|
|
}
|
|
const hasChild = new Set<string>();
|
|
for (const link of remaining) {
|
|
if (link.snapshot === null) continue;
|
|
const parent = byHead.get(branchKey(link, link.snapshot.baseBranch));
|
|
if (parent != null && parent !== link) hasChild.add(threadPullRequestKeyOf(parent));
|
|
}
|
|
// Walk from each top (a link nothing builds on) down its base chain.
|
|
for (const top of remaining) {
|
|
if (hasChild.has(threadPullRequestKeyOf(top))) continue;
|
|
const layers: Array<ThreadPullRequestLink> = [];
|
|
let cursor: ThreadPullRequestLink | undefined = top;
|
|
while (cursor !== undefined && !placed.has(threadPullRequestKeyOf(cursor))) {
|
|
placed.add(threadPullRequestKeyOf(cursor));
|
|
layers.unshift(cursor);
|
|
cursor =
|
|
cursor.snapshot === null
|
|
? undefined
|
|
: (byHead.get(branchKey(cursor, cursor.snapshot.baseBranch)) ?? undefined);
|
|
}
|
|
if (layers.length > 0) chains.push({ kind: "derived", layers });
|
|
}
|
|
// Cycles have no top. Keep those links visible without inventing a stack order.
|
|
for (const link of remaining) {
|
|
if (!placed.has(threadPullRequestKeyOf(link))) {
|
|
chains.push({ kind: "derived", layers: [link] });
|
|
}
|
|
}
|
|
return chains;
|
|
}
|
|
|
|
export type ThreadPullRequestBadge = {
|
|
readonly state: "open" | "closed" | "merged" | "draft";
|
|
} & (
|
|
| {
|
|
readonly kind: "stack";
|
|
readonly layers: number;
|
|
}
|
|
| { readonly kind: "pull-request"; readonly others: number }
|
|
);
|
|
|
|
/** Aggregate visible links' state for both stacks and unrelated linked counts. */
|
|
export function resolveThreadPullRequestBadge(
|
|
pullRequests: ReadonlyArray<ThreadPullRequestLink> | undefined,
|
|
): ThreadPullRequestBadge | null {
|
|
const visible = visibleThreadPullRequests(pullRequests ?? []);
|
|
if (visible.length === 0) return null;
|
|
const states = visible.map((link) => link.snapshot?.state ?? "open");
|
|
const state = visible.every((link) => link.snapshot?.state === "open" && link.snapshot.isDraft)
|
|
? "draft"
|
|
: states.includes("open")
|
|
? "open"
|
|
: states.every((entry) => entry === "merged")
|
|
? "merged"
|
|
: "closed";
|
|
const chains = resolveThreadPullRequestChains(visible);
|
|
if (visible.length > 1 && chains.length === 1) {
|
|
return { kind: "stack", layers: visible.length, state };
|
|
}
|
|
return { kind: "pull-request", others: visible.length - 1, state };
|
|
}
|
|
|
|
/** Search terms for visible PR links, including the legacy single-link projection. */
|
|
export function threadPullRequestSearchTerms(thread: {
|
|
readonly pullRequests?: ReadonlyArray<ThreadPullRequestLink> | undefined;
|
|
readonly linkedPullRequest?: ThreadLinkedPullRequest | null | undefined;
|
|
}): string[] {
|
|
if (thread.pullRequests !== undefined && thread.pullRequests.length > 0) {
|
|
return visibleThreadPullRequests(thread.pullRequests).flatMap((link) => [
|
|
`#${link.number}`,
|
|
`${link.repository}#${link.number}`,
|
|
link.url,
|
|
link.snapshot?.title ?? "",
|
|
]);
|
|
}
|
|
const legacy = thread.linkedPullRequest;
|
|
return legacy ? [`#${legacy.number}`, `${legacy.repository}#${legacy.number}`, legacy.url] : [];
|
|
}
|
|
|
|
/** Older V2 event payloads stored one link; an explicit empty array means it was unlinked. */
|
|
export function threadPullRequestsOf(thread: {
|
|
readonly pullRequests?: ReadonlyArray<ThreadPullRequestLink> | undefined;
|
|
readonly linkedPullRequest?: ThreadLinkedPullRequest | null | undefined;
|
|
}): ReadonlyArray<ThreadPullRequestLink> {
|
|
if (thread.pullRequests !== undefined) return thread.pullRequests;
|
|
const linked = thread.linkedPullRequest;
|
|
return linked == null
|
|
? []
|
|
: [
|
|
{
|
|
...legacyThreadPullRequestKey(linked),
|
|
url: linked.url,
|
|
source: "manual",
|
|
linkedAt: "1970-01-01T00:00:00.000Z",
|
|
snapshot: null,
|
|
stack: null,
|
|
},
|
|
];
|
|
}
|