feat(pull-requests): link multiple pull requests to threads (#10839)

This commit is contained in:
Julius Marminge authored and GitHub committed 2026-09-09 15:50:58 -07:00
1 parent 383cc40f4d
commit afb84898be
172 files changed
+12063 -914

No files matched your search

+202 -59
View File
@@ -1,3 +1,7 @@
import {
canonicalRepositoryKey,
sourceControlRepositorySelector,
} from "@t3tools/shared/sourceControl";
import * as Cache from "effect/Cache";
import * as Clock from "effect/Clock";
import * as Context from "effect/Context";
@@ -46,6 +50,7 @@ import {
type PullRequestLabelCandidateList,
type PullRequestLabelChangeInput,
type PullRequestSubmitReviewInput,
type PullRequestStack,
type PullRequestSummary,
type PullRequestThreadReplyInput,
type PullRequestThreadResolutionInput,
@@ -144,6 +149,13 @@ export class PullRequestService extends Context.Service<
input: PullRequestRef,
options?: { readonly recoverTransientFailure?: boolean },
) => Effect.Effect<PullRequestSummary, PullRequestError>;
/**
* The host-native stack the pull request belongs to, or null when it is not in one or the
* host keeps no such object. Cached like a summary; a stack changes about as often.
*/
readonly stack: (
input: PullRequestRef,
) => Effect.Effect<PullRequestStack | null, PullRequestError>;
readonly subscribeMerges: Effect.Effect<
Stream.Stream<PullRequestMergeEvent>,
never,
@@ -241,6 +253,7 @@ const LABEL_CHANGE_REFUSAL = "You need triage access on this repository to chang
/** A project this page can read: its remote is on a host with an implementation. */
interface SupportedProject {
readonly cursorKey: string;
readonly project: OrchestrationProjectShell;
readonly api: PullRequestProviderApi;
readonly repository: string;
@@ -453,7 +466,7 @@ function withRateLimitBackoff(
call: (...args: Args) => Effect.Effect<A, PullRequestProviderError>,
) => wrap(operation, call, true);
return {
const wrapped = {
kind: api.kind,
capabilities: api.capabilities,
getViewer: wrap("getViewer", api.getViewer),
@@ -474,6 +487,9 @@ function withRateLimitBackoff(
: {
getChangeRequestSummary: wrap("getChangeRequestSummary", api.getChangeRequestSummary),
}),
...(api.getChangeRequestStack === undefined
? {}
: { getChangeRequestStack: wrap("getChangeRequestStack", api.getChangeRequestStack) }),
getChangeRequestActivity: wrap("getChangeRequestActivity", api.getChangeRequestActivity),
...(api.getReviewThreadComments === undefined
? {}
@@ -506,30 +522,9 @@ function withRateLimitBackoff(
setReaction: interactive("setReaction", api.setReaction),
setThreadResolution: interactive("setThreadResolution", api.setThreadResolution),
};
}
/**
* The provider-native repository selector. `displayName` is the full path below the host, which
* is what nested GitLab groups need; owner/name is the two-segment fallback for identities
* recorded before that field existed.
*
* Azure DevOps is the exception: `az repos pr list --repository` takes a repository name, and
* takes the organisation and project from the checkout it detects — so the recorded
* `org/project/_git/repo` path is refused outright and the whole repository reads as
* unavailable. Its name is the last segment, which is what this hands over.
*
* One function because everything downstream is keyed by what it answers: the rows' own
* `repository`, the per-repository cursors, and the detail and diff reads a row leads to.
*/
export function repositoryIdentityOf(project: OrchestrationProjectShell): string | null {
const identity = project.repositoryIdentity;
if (!identity) return null;
if (identity.provider === "azure-devops") {
const segments = (identity.displayName ?? "").split("/").filter((part) => part !== "_git");
return identity.name || segments.at(-1) || null;
}
if (identity.displayName) return identity.displayName;
return identity.owner && identity.name ? `${identity.owner}/${identity.name}` : null;
// Optional provider methods must be forwarded too; returning the interface alone permits omissions.
return wrapped satisfies PullRequestProviderApi &
Record<Exclude<keyof PullRequestProviderApi, keyof typeof wrapped>, never>;
}
export const make = Effect.gen(function* () {
@@ -554,7 +549,11 @@ export const make = Effect.gen(function* () {
for (const project of projects) {
if (filter.projectId !== undefined && project.id !== filter.projectId) continue;
const identity = project.repositoryIdentity;
if (identity?.provider !== "unknown" || repositoryIdentityOf(project) === null) continue;
if (
identity?.provider !== "unknown" ||
sourceControlRepositorySelector(project.repositoryIdentity) === null
)
continue;
const host = pullRequestHostOf(identity, "unknown");
// A legacy identity has no canonical host until its provider is refined, so it must reach
// the refinement before a host filter can decide whether it belongs in the result.
@@ -628,7 +627,7 @@ export const make = Effect.gen(function* () {
if (filter.projectIds !== undefined && !filter.projectIds.includes(project.id)) continue;
const identity = project.repositoryIdentity;
let kind = identity?.provider as SourceControlProviderKind | undefined;
const repository = repositoryIdentityOf(project);
const repository = sourceControlRepositorySelector(project.repositoryIdentity);
if (!identity || kind === undefined || repository === null) continue;
// Worktrees of one repository are separate projects; reading the remote once keeps
// the page from repeating every change request per local checkout. The host is part
@@ -647,7 +646,10 @@ export const make = Effect.gen(function* () {
if (roots === undefined) viewerRoots.set(host, [project.workspaceRoot]);
else if (!roots.includes(project.workspaceRoot)) roots.push(project.workspaceRoot);
}
const key = listCursorKey(host, repository);
const key = listCursorKey(
host,
kind === "azure-devops" ? identity.canonicalKey : repository,
);
if (seen.has(key)) continue;
seen.add(key);
if (api === null) {
@@ -657,6 +659,7 @@ export const make = Effect.gen(function* () {
continue;
}
supported.push({
cursorKey: key,
project,
api: withRateLimitBackoff(api, host, rateLimits),
repository,
@@ -667,16 +670,30 @@ export const make = Effect.gen(function* () {
}),
);
/**
* The project whose checkout and credentials serve a reference. The project's own
* repository is the default; a reference that names a `host` may instead point at any
* repository on that host. Prefer its own checkout; providers with explicit repository
* targeting can fall back to another checkout on the host. Azure derives its organization
* from the checkout, so it requires a matching repository.
*/
const requireProject = (ref: PullRequestRef): Effect.Effect<SupportedProject, PullRequestError> =>
listWorkspaceProjects({ projectId: ref.projectId }).pipe(
Effect.flatMap(({ supported }): Effect.Effect<SupportedProject, PullRequestError> => {
const match = supported[0];
if (!match) {
return Effect.fail(new PullRequestUnavailableError({ reason: "provider-unsupported" }));
const own = supported[0];
const repository = ref.repository.trim();
const host = ref.host?.trim().toLowerCase();
if (own !== undefined && own.repository.toLowerCase() === repository.toLowerCase()) {
// Hostless references only ever meant the project's own repository, and a hosted one
// naming it still is; either way the project serves itself.
if (host === undefined || host === own.host) return Effect.succeed(own);
}
// The repository travels through the client, so it is checked against the project's
// own remote rather than being handed to a provider verbatim.
if (match.repository.toLowerCase() !== ref.repository.trim().toLowerCase()) {
if (host === undefined) {
if (own === undefined) {
return Effect.fail(new PullRequestUnavailableError({ reason: "provider-unsupported" }));
}
// The repository travels through the client, so it is checked against the project's
// own remote rather than being handed to a provider verbatim.
return Effect.fail(
new PullRequestOperationError({
operation: "resolveRepository",
@@ -684,7 +701,39 @@ export const make = Effect.gen(function* () {
}),
);
}
return Effect.succeed(match);
const repositoryKey = canonicalRepositoryKey(`${host}/${repository}`.toLowerCase());
// Azure SSH and legacy clone hosts differ from the browser URL's host. Compare
// the complete repository identity before narrowing those checkouts by host.
return listWorkspaceProjects(
repositoryKey.startsWith("dev.azure.com/") ? {} : { host },
).pipe(
Effect.flatMap(({ supported }) => {
const onHost = supported.filter((candidate) => candidate.host === host);
const route =
supported.find(
(candidate) =>
candidate.api.kind === "azure-devops" &&
candidate.project.repositoryIdentity != null &&
canonicalRepositoryKey(
candidate.project.repositoryIdentity.canonicalKey.toLowerCase(),
) === repositoryKey,
) ??
onHost.find(
(candidate) =>
candidate.api.kind !== "azure-devops" &&
candidate.repository.toLowerCase() === repository.toLowerCase(),
) ??
onHost.find((candidate) => candidate.api.kind !== "azure-devops");
if (route === undefined) {
return Effect.fail(
new PullRequestUnavailableError({ reason: "provider-unsupported" }),
);
}
return Effect.succeed(
route.api.kind === "azure-devops" ? route : { ...route, repository },
);
}),
);
}),
);
@@ -829,8 +878,8 @@ export const make = Effect.gen(function* () {
viewer: string,
): boolean => {
if (filters === undefined) return true;
const labels = item.labels.map((label) => label.name.trim().toLowerCase());
const holds = (label: string) => labels.includes(label.trim().toLowerCase());
const labels = new Set(item.labels.map((label) => label.name.trim().toLowerCase()));
const holds = (label: string) => labels.has(label.trim().toLowerCase());
return (
(filters.draft === undefined || item.isDraft === (filters.draft === "only")) &&
// Judged on the provider row rather than the entry, because the two absences mean
@@ -949,9 +998,7 @@ export const make = Effect.gen(function* () {
const selected =
continuation === null
? projects
: projects.filter(({ host, repository }) =>
continuation.has(listCursorKey(host, repository)),
);
: projects.filter(({ cursorKey }) => continuation.has(cursorKey));
const readable = selected.filter(({ host }) => viewers[host] !== undefined);
// A host that could not be read still has projects, and they are absent from the list.
// Reporting them keeps "N repositories were unavailable" honest instead of dropping them.
@@ -993,7 +1040,7 @@ export const make = Effect.gen(function* () {
const limit = input.limit ?? DEFAULT_REPOSITORY_LIST_LIMIT;
const cursorOf = (project: SupportedProject): ListCursor | undefined =>
continuation?.get(listCursorKey(project.host, project.repository));
continuation?.get(project.cursorKey);
/**
* One repository asked on its own. What every host without a search across repositories
@@ -1002,7 +1049,7 @@ export const make = Effect.gen(function* () {
const readRepository = (project: SupportedProject): Effect.Effect<RepositoryBatch> => {
{
const viewer = viewers[project.host]!;
const key = listCursorKey(project.host, project.repository);
const key = project.cursorKey;
const cursor = cursorOf(project);
return project.api
.listChangeRequests({
@@ -1161,7 +1208,7 @@ export const make = Effect.gen(function* () {
!cursorHere.seenAt.includes(item.number),
);
return Effect.succeed({
key: listCursorKey(project.host, project.repository),
key: project.cursorKey,
entries: items
.filter((item) => matchesRowFilters(item, input.filters, viewer))
.map((item) => toEntry({ project, item, viewer })),
@@ -1254,17 +1301,67 @@ export const make = Effect.gen(function* () {
title: changeRequest.title,
url: changeRequest.url,
state: changeRequest.state,
...(changeRequest.isDraft === true ? { isDraft: true } : {}),
headBranch: changeRequest.headBranch,
baseBranch: changeRequest.baseBranch,
closedAt: changeRequest.closedAt ?? null,
mergedAt: changeRequest.mergedAt ?? null,
updatedAt: changeRequest.updatedAt,
...(changeRequest.isDraft === undefined ? {} : { isDraft: changeRequest.isDraft }),
...(changeRequest.author === undefined ? {} : { author: changeRequest.author }),
...(changeRequest.additions === undefined
? {}
: { additions: changeRequest.additions }),
...(changeRequest.deletions === undefined
? {}
: { deletions: changeRequest.deletions }),
...(changeRequest.changedFiles === undefined
? {}
: { changedFiles: changeRequest.changedFiles }),
...(changeRequest.reviewDecision === undefined
? {}
: { reviewDecision: changeRequest.reviewDecision }),
...(changeRequest.checksState === undefined
? {}
: { checksState: changeRequest.checksState }),
...(changeRequest.mergeability === undefined
? {}
: { mergeability: changeRequest.mergeability }),
})),
);
}),
);
const stackUncached: PullRequestService["Service"]["stack"] = (input) =>
requireProject(input).pipe(
Effect.flatMap((project) => {
const read = project.api.getChangeRequestStack;
if (read === undefined) return Effect.succeed(null);
return read({
cwd: project.project.workspaceRoot,
repository: project.repository,
host: project.host,
number: input.number,
}).pipe(
Effect.mapError(toPullRequestError("stack")),
Effect.map((stack): PullRequestStack | null =>
stack === null
? null
: {
id: stack.id,
number: stack.number,
url: stack.url,
base: stack.base,
layers: stack.layers.map((layer) => ({
number: layer.number,
headBranch: layer.headBranch,
state: layer.state,
})),
},
),
);
}),
);
const detailUncached: PullRequestService["Service"]["detail"] = (input) =>
requireProject(input).pipe(
Effect.flatMap((project) =>
@@ -1507,7 +1604,11 @@ export const make = Effect.gen(function* () {
})
.pipe(
Effect.mapError(toPullRequestError("runAction")),
Effect.as(project.repository),
Effect.as(
project.api.kind === "azure-devops"
? input.repository.trim()
: project.repository,
),
);
}),
);
@@ -2133,11 +2234,35 @@ export const make = Effect.gen(function* () {
let turnRefreshEpoch = 0;
const refEpochs = new Map<string, number>();
const REF_EPOCH_CAPACITY = 2_048;
const refScope = (ref: PullRequestRef) => `${ref.projectId} ${ref.repository} ${ref.number}`;
const refScope = (ref: PullRequestRef) =>
`${ref.projectId} ${ref.host?.toLowerCase() ?? ""} ${ref.repository.toLowerCase()} ${ref.number}`;
const refEpoch = (ref: PullRequestRef) =>
Math.max(turnRefreshEpoch, refEpochs.get(refScope(ref)) ?? 0);
// Keys carry the reference back out of the cache loader, so the slot layout is shared with
// `refOfCacheKey` rather than read positionally at every loader.
const refCacheKey = (ref: PullRequestRef) =>
JSON.stringify([refEpoch(ref), ref.projectId, ref.repository, ref.number]);
JSON.stringify([
refEpoch(ref),
ref.projectId,
ref.host?.toLowerCase() ?? null,
ref.repository.toLowerCase(),
ref.number,
]);
const refOfCacheKey = (key: string): PullRequestRef => {
const [, projectId, host, repository, number] = JSON.parse(key) as [
number,
string,
string | null,
string,
number,
];
return {
projectId,
...(host === null ? {} : { host }),
repository,
number,
} as PullRequestRef;
};
// Counts belong to a PR, not a filtered page. Background reads and filter changes reuse
// them; explicit refreshes, mutations, and turns strand old and in-flight results.
const statsCacheKey = (key: string) => JSON.stringify([listingsEpoch, key]);
@@ -2181,8 +2306,7 @@ export const make = Effect.gen(function* () {
const summaryCache = yield* Cache.makeWith(
(key: string) => {
const [, projectId, repository, number] = JSON.parse(key) as [number, string, string, number];
return summaryUncached({ projectId, repository, number } as PullRequestRef);
return summaryUncached(refOfCacheKey(key));
},
{
capacity: DETAIL_CACHE_CAPACITY,
@@ -2203,6 +2327,13 @@ export const make = Effect.gen(function* () {
);
};
const stackCache = yield* Cache.makeWith((key: string) => stackUncached(refOfCacheKey(key)), {
capacity: DETAIL_CACHE_CAPACITY,
timeToLive: (exit) => (Exit.isSuccess(exit) ? SUMMARY_CACHE_TTL : Duration.zero),
});
const stack: PullRequestService["Service"]["stack"] = (input) =>
Cache.get(stackCache, refCacheKey(input));
// Keys serialize positionally and parse back in the lookup, so the cache is the only holder
// of in-flight state: concurrent identical reads coalesce on the key into one host request.
// The continuation cursors are part of the key, entries sorted so one continuation is one
@@ -2283,8 +2414,7 @@ export const make = Effect.gen(function* () {
const detailCache = yield* Cache.makeWith(
(key: string) => {
const statsKey = statsCacheKey(key);
const [, projectId, repository, number] = JSON.parse(key) as [number, string, string, number];
return detailUncached({ projectId, repository, number } as PullRequestRef).pipe(
return detailUncached(refOfCacheKey(key)).pipe(
Effect.tap(
Effect.fn("PullRequestService.recordDetailStats")(function* (value: PullRequestDetail) {
recordStats(
@@ -2307,7 +2437,12 @@ export const make = Effect.gen(function* () {
timeToLive: (exit) => (Exit.isSuccess(exit) ? DETAIL_CACHE_TTL : Duration.zero),
},
);
const summaryFromDetail = (detail: PullRequestDetail): PullRequestSummary => ({
const summaryFromDetail = (
detail: PullRequestDetail,
previous: PullRequestSummary | undefined,
): PullRequestSummary => ({
// Detail does not carry review/check summaries. Keep the last summary observation.
...previous,
provider: detail.provider,
projectId: detail.projectId,
repository: detail.repository,
@@ -2315,7 +2450,12 @@ export const make = Effect.gen(function* () {
title: detail.title,
url: detail.url,
state: detail.state,
...(detail.isDraft === true ? { isDraft: true } : {}),
isDraft: detail.isDraft,
author: detail.author,
additions: detail.additions,
deletions: detail.deletions,
changedFiles: detail.changedFiles,
mergeability: detail.mergeability,
headBranch: detail.headBranch,
baseBranch: detail.baseBranch,
closedAt: detail.closedAt,
@@ -2338,7 +2478,7 @@ export const make = Effect.gen(function* () {
key,
Cache.get(detailCache, key).pipe(
Effect.tap((value) => {
const summary = summaryFromDetail(value);
const summary = summaryFromDetail(value, lastGoodSummary.peek(key));
return shouldReplaceHeldSummary(key, summary)
? lastGoodSummary.record(key, summary)
: Effect.void;
@@ -2350,8 +2490,7 @@ export const make = Effect.gen(function* () {
const activityCache = yield* Cache.makeWith(
(key: string) => {
const [, projectId, repository, number] = JSON.parse(key) as [number, string, string, number];
return activityUncached({ projectId, repository, number } as PullRequestRef);
return activityUncached(refOfCacheKey(key));
},
{
capacity: DETAIL_CACHE_CAPACITY,
@@ -2365,9 +2504,10 @@ export const make = Effect.gen(function* () {
const diffCache = yield* Cache.makeWith(
(key: string) => {
const [, projectId, repository, number, cursor, commit] = JSON.parse(key) as [
const [, projectId, host, repository, number, cursor, commit] = JSON.parse(key) as [
number,
string,
string | null,
string,
number,
string | null,
@@ -2375,6 +2515,7 @@ export const make = Effect.gen(function* () {
];
return diffUncached({
projectId,
...(host === null ? {} : { host }),
repository,
number,
...(cursor === null ? {} : { cursor }),
@@ -2385,7 +2526,7 @@ export const make = Effect.gen(function* () {
capacity: DIFF_CACHE_CAPACITY,
timeToLive: (exit, key) => {
if (!Exit.isSuccess(exit)) return Duration.zero;
const commit = (JSON.parse(key) as ReadonlyArray<unknown>)[5];
const commit = (JSON.parse(key) as ReadonlyArray<unknown>)[6];
return commit === null ? DIFF_CACHE_TTL : COMMIT_DIFF_CACHE_TTL;
},
},
@@ -2394,7 +2535,8 @@ export const make = Effect.gen(function* () {
const key = JSON.stringify([
refEpoch(input),
input.projectId,
input.repository,
input.host?.toLowerCase() ?? null,
input.repository.toLowerCase(),
input.number,
input.cursor ?? null,
input.commit ?? null,
@@ -2523,6 +2665,7 @@ export const make = Effect.gen(function* () {
list,
listStats,
summary,
stack,
subscribeMerges: PubSub.subscribe(mergedPullRequests).pipe(
Effect.map((subscription) => Stream.fromSubscription(subscription)),
),