fix(server): require membership in the pull request own commits

Exclude inherited base commits using forge commit lists. Carry membership evidence in the server snapshot so auto-archive uses the same rule without Git subprocesses or network fetches. Preserve GitHub batching and cache terminal commit reads.
This commit is contained in:
Mohamed Boudra
2026-11-17 04:39:03 +01:00
parent 7e5ea17d6c
commit fedb40d112
15 changed files with 393 additions and 146 deletions
+15 -10
View File
@@ -98,17 +98,22 @@ Register the adapter in `defaultForgeRegistry` with:
- `probeHost` when self-hosted/Enterprise detection is supported
Current change-request lookup prefers an open request for the branch and repository.
Closed and merged requests belong to a checkout only when their head contains the
checkout commit. Display and auto-archive share this ancestry rule. This retains a
request after a server-side branch update while rejecting historical requests on
reused branch names. Auto-archive still requires an observed open-to-merged
transition and fresh workspace safety checks; being behind the remote alone does
not block archive when the merged request contains the checkout commit.
A closed or merged request belongs to a workspace only when its checkout commit
is in the forge's PR/MR commit list. Commits inherited from the base do not count.
Display and auto-archive share this rule, preserving squash-merged requests and
requests updated remotely while rejecting a fresh workspace on their base commit.
A rewritten or newly advanced checkout no longer matches the terminal request.
Containment uses Git objects, fetching a missing PR head from origin without
moving refs or writing FETCH_HEAD. This keeps ancestry reads outside the GitHub
GraphQL budget. If history or remote access cannot establish containment, hide
the terminal request and leave the workspace active.
Commit membership stays in the server snapshot; clients receive the selected
request without its commit list. Archive uses that same evidence after refreshing
the workspace. It still requires an observed open-to-merged transition and the
workspace safety checks; being behind alone does not block it.
Containment spawns no Git commands and fetches no Git objects. GitHub reads commit
IDs in the existing batched query, batches additional pages under the same rate
budget, and remembers terminal pages by PR head. GitLab and Gitea cache commit-list
reads, including failures, for five minutes. Missing membership evidence leaves
the workspace active.
Keep checkout HEAD in adapter cache and poll identities so checkout changes
refresh the facts used by archive eligibility.
@@ -31,6 +31,7 @@ function createPullRequest(
baseRefName: "main",
headRefName: "feature",
headSha: "same-head",
commitOids: ["same-head"],
isMerged: true,
...overrides,
};
@@ -64,7 +64,12 @@ export async function archiveIfSafe(input: {
const cwd = snapshot.cwd;
const pullRequest = snapshot.forge.pullRequest;
if (!pullRequest?.isMerged || !snapshot.git.headSha || snapshot.forge.error) {
if (
!pullRequest?.isMerged ||
!pullRequest.headSha ||
!snapshot.git.headSha ||
snapshot.forge.error
) {
return;
}
if (snapshot.git.isDirty !== false) {
@@ -75,11 +80,10 @@ export async function archiveIfSafe(input: {
}
if (
!(await pullRequestContainsCommit({
cwd,
!pullRequestContainsCommit({
workspaceCommit: snapshot.git.headSha,
pullRequestCommit: pullRequest.headSha,
}))
pullRequestCommits: pullRequest.commitOids,
})
)
return;
@@ -179,6 +179,7 @@ async function createUnobservedWorkspace(journeyOptions?: {
});
let observation: PullRequestObservation | null = null;
let remoteHeadSha = execFileSync("git", ["rev-parse", "HEAD"], { cwd, encoding: "utf8" }).trim();
const workspaceHeadSha = remoteHeadSha;
const reads: Array<string | undefined> = [];
const archives: string[] = [];
let archiveDecisions = 0;
@@ -194,6 +195,7 @@ async function createUnobservedWorkspace(journeyOptions?: {
status: observation && {
...observation,
headSha: remoteHeadSha,
commitOids: [workspaceHeadSha, remoteHeadSha],
title: observation.headRefName,
baseRefName: "main",
isMerged: observation.state === "merged",
@@ -51,6 +51,7 @@ describe("checkout status projection", () => {
const payload = normalizeCheckoutPrStatusPayload(
{
number: 123,
commitOids: ["server-only-membership"],
repoOwner: "internal-owner",
repoName: "internal-repo",
url: "https://github.com/getpaseo/paseo/pull/123",
@@ -76,6 +77,7 @@ describe("checkout status projection", () => {
"github",
);
expect(payload).not.toHaveProperty("commitOids");
expect(payload).toHaveProperty("repoOwner", "internal-owner");
expect(payload).toHaveProperty("repoName", "internal-repo");
expect(payload).toHaveProperty("forge", "github");
+2 -1
View File
@@ -6188,9 +6188,10 @@ export class Session {
private buildWorkspaceGitHubRuntimePayload(
snapshot: WorkspaceGitRuntimeSnapshot,
): NonNullable<WorkspaceDescriptorPayload["githubRuntime"]> {
const { commitOids: _commitOids, ...pullRequest } = snapshot.forge.pullRequest ?? {};
return {
featuresEnabled: snapshot.forge.featuresEnabled,
pullRequest: snapshot.forge.pullRequest,
pullRequest: snapshot.forge.pullRequest ? pullRequest : null,
error: snapshot.forge.error,
};
}
@@ -201,6 +201,8 @@ export interface WorkspaceGitRuntimeSnapshot {
baseRefName: string;
headRefName: string;
headSha?: string;
/** Server-only commit membership evidence; never projected onto the wire. */
commitOids?: string[];
isMerged: boolean;
isDraft?: boolean;
mergeable?: PullRequestMergeable;
@@ -129,6 +129,8 @@ export interface CurrentPullRequestStatus {
baseRefName: string;
headRefName: string;
headSha?: string;
/** Server-only commit membership evidence; never projected onto the wire. */
commitOids?: string[];
isMerged: boolean;
isDraft?: boolean;
mergeable: PullRequestMergeable;
@@ -2050,6 +2050,8 @@ describe("createGiteaService", () => {
if (args[0] === "pr" && args[1] === "list" && argValue(args, "--state") === "open") {
return ok(JSON.stringify([]));
}
if (args[1]?.includes("/commits?"))
return ok(JSON.stringify(args[1].endsWith("page=1") ? [{ sha: headSha }] : []));
if (args[0] === "api" && args[1].includes("/pulls?state=all")) {
return ok(JSON.stringify([closedPr]));
}
@@ -2080,6 +2082,8 @@ describe("createGiteaService", () => {
const { service, calls } = makeService(
(args) => {
if (args[0] === "pr" && args[1] === "list") return ok("[]");
if (args[1]?.includes("/commits?"))
return ok(JSON.stringify(args[1].endsWith("page=1") ? [{ sha: headSha }] : []));
if (args[0] === "api" && args[1].startsWith("repos/{owner}/{repo}/pulls?")) {
return ok(JSON.stringify([closedPr]));
}
@@ -2114,6 +2118,8 @@ describe("createGiteaService", () => {
});
const { service } = makeService((args) => {
if (args[0] === "pr" && args[1] === "list") return ok("[]");
if (args[1]?.includes("/commits?"))
return ok(JSON.stringify(args[1].endsWith("page=1") ? [{ sha: workspaceCommit }] : []));
if (args[0] === "api" && args[1].includes("/pulls?state=all")) {
return ok(JSON.stringify([stale]));
}
+25 -6
View File
@@ -1,4 +1,7 @@
import { pullRequestContainsCommit } from "./pull-request-contains-commit.js";
import {
pullRequestContainsCommit,
createPullRequestCommitReader,
} from "./pull-request-contains-commit.js";
import { z } from "zod";
import { CHECK_TRAIT_ACTION_REQUIRED, CHECK_TRAIT_WARNING } from "@getpaseo/protocol/check-traits";
import { mapGiteaCommitState } from "@getpaseo/protocol/gitea-status";
@@ -164,6 +167,7 @@ const GiteaPrListItemSchema = z
labels: z.string().optional(),
ci: z.string().optional(),
headSha: z.string().optional(),
commitOids: z.array(z.string()).optional(),
})
.passthrough();
@@ -1400,6 +1404,7 @@ function throwFirstNonTeaAuthSearchRejection(results: PromiseSettledResult<unkno
}
export function createGiteaService(options: CreateGiteaServiceOptions = {}): ForgeService {
const readRequestCommits = createPullRequestCommitReader();
const runner = options.runner ?? runTeaCommand;
const resolveTea = createCachedCliPathResolver(options.resolveTeaPath ?? resolveTeaPath);
const resolveRemoteUrl = options.resolveRemoteUrl ?? defaultResolveRemoteUrl;
@@ -1595,7 +1600,7 @@ export function createGiteaService(options: CreateGiteaServiceOptions = {}): For
if (!match) {
return null;
}
const status = toCurrentPullRequestStatus(match);
const status = { ...toCurrentPullRequestStatus(match), commitOids: match.commitOids };
return loadCurrentPullRequestChecks(input.cwd, match, status);
}
@@ -1664,14 +1669,28 @@ export function createGiteaService(options: CreateGiteaServiceOptions = {}): For
Number(right.head.sha === input.headSha) - Number(left.head.sha === input.headSha),
);
for (const item of candidates) {
const commitOids = await readRequestCommits(
`${input.cwd}:${repoPath}/${item.number}@${item.head.sha}`,
async () => {
const result: string[] = [];
for (let page = 1; ; page++) {
const commits = await runJsonArray(
["api", `${repoPath}/pulls/${item.number}/commits?limit=50&page=${page}`],
{ cwd: input.cwd },
z.object({ sha: z.string() }),
);
result.push(...commits.map((commit) => commit.sha));
if (commits.length === 0) return result;
}
},
);
if (
await pullRequestContainsCommit({
cwd: input.cwd,
pullRequestContainsCommit({
workspaceCommit: input.headSha,
pullRequestCommit: item.head.sha,
pullRequestCommits: commitOids,
})
) {
return currentPullRequestApiToListItem(item);
return { ...currentPullRequestApiToListItem(item), commitOids };
}
}
return null;
@@ -1,3 +1,4 @@
import { createPullRequestCommitReader } from "./pull-request-contains-commit.js";
import { createPullRequestHistory } from "../test-utils/pull-request-history.js";
import { onTestFinished } from "vitest";
import { execFileSync } from "node:child_process";
@@ -25,7 +26,7 @@ const EXPECTED_GITHUB_FAST_POLL_MS = 20_000;
const EXPECTED_GITHUB_SLOW_POLL_MS = 120_000;
const EXPECTED_GITHUB_ERROR_BACKOFF_CAP_MS = 300_000;
const CURRENT_PR_STATUS_BASE_FIELDS =
"number,url,title,state,isDraft,baseRefName,headRefName,headRefOid,updatedAt,mergedAt,reviewDecision,mergeable,headRepositoryOwner";
"number,url,title,state,isDraft,baseRefName,headRefName,headRefOid,updatedAt,mergedAt,reviewDecision,mergeable,headRepositoryOwner,commits";
const CURRENT_PR_STATUS_FIELDS = `${CURRENT_PR_STATUS_BASE_FIELDS},statusCheckRollup`;
const FORK_PR_QUERY = "state=all&per_page=100&head=forkOwner%3Afeature%2Ffork";
@@ -213,6 +214,7 @@ function currentPullRequestJson(overrides: Record<string, unknown> = {}): string
baseRefName: "main",
headRefName: "feature/fork",
headRefOid: "1111111111111111111111111111111111111111",
commits: [{ oid: overrides.headRefOid ?? "1111111111111111111111111111111111111111" }],
mergedAt: null,
statusCheckRollup: [],
reviewDecision: "REVIEW_REQUIRED",
@@ -267,6 +269,7 @@ function batchPollPrNodeJson(overrides: Record<string, unknown> = {}): Record<st
viewerCanUpdateBranch: false,
isMergeQueueEnabled: false,
isInMergeQueue: false,
commits: { nodes: [{ commit: { oid: overrides.headRefOid ?? "oid-42" } }] },
...overrides,
};
}
@@ -1113,45 +1116,91 @@ describe("ForgeService", () => {
expect(currentPullRequestStatusCalls(runner.calls)).toHaveLength(1);
});
it("filters terminal PRs by containment in a batched poll without per-target API calls", async () => {
vi.useRealTimers();
const history = createPullRequestHistory();
onTestFinished(history.dispose);
const { cwd, workspaceCommit, pullRequestCommit } = history;
const runner = createScriptedRunner([
batchPollStatusJson({
t0: batchPollRepositoryJson([
batchPollPrNodeJson({ headRefName: "feature", headRefOid: pullRequestCommit }),
]),
}),
batchPollStatusJson({ t0: batchPollChecksAliasJson([]) }),
it("shares terminal commit reads and caches failures until retry", async () => {
let now = 0;
let calls = 0;
const read = createPullRequestCommitReader(() => now);
const load = async () => {
calls++;
throw new Error("offline");
};
await expect(read("request@head", load)).rejects.toThrow("offline");
await expect(read("request@head", load)).rejects.toThrow("offline");
expect(calls).toBe(1);
now = 300_001;
await expect(read("request@head", load)).rejects.toThrow("offline");
expect(calls).toBe(2);
await expect(read("request@new-head", async () => ["own-commit"])).resolves.toEqual([
"own-commit",
]);
const service = createGitHubService({
runner: runner.runner,
resolveGhPath: async () => "/usr/bin/gh",
resolveRepoHost: async () => null,
resolveRepoSlug: async () => "acme/widgets",
});
onTestFinished(() => service.dispose?.());
const statuses: Array<CurrentPullRequestStatus | null> = [];
const subscription = service.retainCurrentPullRequestStatusPoll?.({
cwd,
headRef: "feature",
headSha: workspaceCommit,
onStatus: (status) => statuses.push(status),
});
onTestFinished(() => subscription?.unsubscribe());
await vi.waitFor(() => expect(statuses).toHaveLength(1));
expect(statuses[0]).toMatchObject({ state: "merged", headSha: pullRequestCommit });
expect(runner.calls).toHaveLength(2);
expect(runner.calls.every((call) => call.args[0] === "api" && call.args[1] === "graphql")).toBe(
true,
);
expect(history.git(["rev-parse", "HEAD"])).toBe(workspaceCommit);
expect(history.git(["rev-parse", "@{upstream}"])).toBe(workspaceCommit);
expect(() => history.git(["rev-parse", "--verify", "FETCH_HEAD"])).toThrow();
});
it.each([false, true])(
"filters terminal PRs in a batched poll (pagination: %s)",
async (paginate) => {
vi.useRealTimers();
const history = createPullRequestHistory();
onTestFinished(history.dispose);
const { cwd, workspaceCommit, pullRequestCommit } = history;
const runner = createScriptedRunner([
batchPollStatusJson({
t0: batchPollRepositoryJson([
batchPollPrNodeJson({
headRefName: "feature",
headRefOid: pullRequestCommit,
commits: {
nodes: [
{ commit: { oid: paginate ? "other" : workspaceCommit } },
{ commit: { oid: pullRequestCommit } },
],
pageInfo: { hasNextPage: paginate, endCursor: "cursor-1" },
},
}),
]),
}),
...(paginate
? [
batchPollStatusJson({
t0: {
pullRequest: {
commits: {
nodes: [{ commit: { oid: workspaceCommit } }],
pageInfo: { hasNextPage: false, endCursor: "cursor-2" },
},
},
},
}),
]
: []),
batchPollStatusJson({ t0: batchPollChecksAliasJson([]) }),
]);
const service = createGitHubService({
runner: runner.runner,
resolveGhPath: async () => "/usr/bin/gh",
resolveRepoHost: async () => null,
resolveRepoSlug: async () => "acme/widgets",
});
onTestFinished(() => service.dispose?.());
const statuses: Array<CurrentPullRequestStatus | null> = [];
const subscription = service.retainCurrentPullRequestStatusPoll?.({
cwd,
headRef: "feature",
headSha: workspaceCommit,
onStatus: (status) => statuses.push(status),
});
onTestFinished(() => subscription?.unsubscribe());
await vi.waitFor(() => expect(statuses).toHaveLength(1));
expect(statuses[0]).toMatchObject({ state: "merged", headSha: pullRequestCommit });
expect(runner.calls).toHaveLength(paginate ? 3 : 2);
expect(
runner.calls.every((call) => call.args[0] === "api" && call.args[1] === "graphql"),
).toBe(true);
expect(history.git(["rev-parse", "HEAD"])).toBe(workspaceCommit);
expect(history.git(["rev-parse", "@{upstream}"])).toBe(workspaceCommit);
expect(() => history.git(["rev-parse", "--verify", "FETCH_HEAD"])).toThrow();
},
);
it("batches PR status polls across workspaces into one GraphQL request", async () => {
let now = 0;
const runner = createScriptedRunner([
@@ -3352,6 +3401,7 @@ describe("ForgeService", () => {
state: "MERGED",
mergedAt: "2026-07-17T12:00:00Z",
headRefOid: pullRequestCommit,
commits: [{ oid: workspaceCommit }, { oid: pullRequestCommit }],
});
const runner = createScriptedRunner([
stale,
@@ -3404,6 +3454,35 @@ describe("ForgeService", () => {
},
);
it.each(["CLOSED", "MERGED"])(
"hides a %s PR when the workspace only shares its inherited base",
async (state) => {
const history = createPullRequestHistory();
onTestFinished(history.dispose);
const candidate = JSON.parse(
currentPullRequestJson({
state,
headRefOid: history.pullRequestCommit,
commits: [{ oid: history.pullRequestCommit }],
headRepositoryOwner: { login: "forkOwner" },
}),
);
const runner = createScriptedRunner([JSON.stringify(candidate), JSON.stringify([candidate])]);
const service = createGitHubService({
runner: runner.runner,
resolveGhPath: async () => "/usr/bin/gh",
});
await expect(
service.getCurrentPullRequestStatus({
cwd: history.cwd,
headRef: "feature/fork",
headRepositoryOwner: "forkOwner",
headSha: history.workspaceCommit,
}),
).resolves.toBeNull();
},
);
it("rejects an unrelated terminal PR on a reused branch", async () => {
const owner = { login: "forkOwner" };
const terminal = ({
+159 -32
View File
@@ -1,4 +1,7 @@
import { pullRequestContainsCommit } from "./pull-request-contains-commit.js";
import {
pullRequestContainsCommit,
createPullRequestCommitReader,
} from "./pull-request-contains-commit.js";
import { z } from "zod";
import {
isGitHubHost,
@@ -348,6 +351,7 @@ const CurrentPullRequestStatusSchema = z.object({
baseRefName: z.string().catch(""),
headRefName: z.string().catch(""),
headRefOid: z.string().optional(),
commits: z.array(z.object({ oid: z.string() })).optional(),
updatedAt: z.string().optional(),
mergedAt: z.string().nullable().optional(),
statusCheckRollup: z.unknown().optional(),
@@ -527,7 +531,7 @@ query PullRequestCheckoutTarget($owner: String!, $name: String!, $number: Int!)
}`;
const CURRENT_PR_STATUS_BASE_FIELDS =
"number,url,title,state,isDraft,baseRefName,headRefName,headRefOid,updatedAt,mergedAt,reviewDecision,mergeable,headRepositoryOwner";
"number,url,title,state,isDraft,baseRefName,headRefName,headRefOid,updatedAt,mergedAt,reviewDecision,mergeable,headRepositoryOwner,commits";
const CURRENT_PR_STATUS_FIELDS = `${CURRENT_PR_STATUS_BASE_FIELDS},statusCheckRollup`;
const PULL_REQUEST_STATUS_FACTS_QUERY = `
@@ -643,6 +647,12 @@ const BatchPollPrNodeSchema = z.object({
baseRefName: z.string().catch(""),
headRefName: z.string().catch(""),
headRefOid: z.string().catch(""),
commits: z
.object({
nodes: z.array(z.object({ commit: z.object({ oid: z.string() }) })),
pageInfo: z.object({ hasNextPage: z.boolean(), endCursor: z.string().nullable() }).optional(),
})
.optional(),
updatedAt: z.string().optional(),
mergedAt: z.string().nullable().optional(),
mergeable: PullRequestMergeableSchema.optional().default("UNKNOWN"),
@@ -748,6 +758,7 @@ fragment PaseoPollPullRequest on PullRequest {
baseRefName
headRefName
headRefOid
commits(first: 100) { nodes { commit { oid } } pageInfo { hasNextPage endCursor } }
updatedAt
mergedAt
mergeable
@@ -1174,6 +1185,7 @@ export function createGitHubService(options: CreateGitHubServiceOptions = {}): G
// Rollup nodes for merged/closed PRs keyed by repo#number@headOid: their
// checks can no longer change, so each is fetched at most once per daemon run.
const frozenPullRequestChecksCache = new Map<string, unknown[]>();
const terminalCommitPages = new Map<string, NonNullable<BatchPollPrNode["commits"]>>();
const pollRepositoryRedirects = new Map<
string,
{ owner: string; name: string; headRepositoryOwner: string }
@@ -1662,7 +1674,13 @@ export function createGitHubService(options: CreateGitHubServiceOptions = {}): G
return;
}
const distribution = await distributeGitHubPollBatch({
try {
if (!(await loadBatchCommitPages(addressedEntries, response.aliases))) return;
} catch (error) {
reportGitHubBatchError(addressedEntries, error);
return;
}
const distribution = distributeGitHubPollBatch({
entries: addressedEntries,
aliases: response.aliases,
args,
@@ -1703,12 +1721,87 @@ export function createGitHubService(options: CreateGitHubServiceOptions = {}): G
}
}
async function distributeGitHubPollBatch(input: {
function collectBatchCommitPages(
entries: GitHubBatchPollEntry[],
aliases: Record<string, unknown>,
): Array<{ entry: GitHubBatchPollEntry; node: BatchPollPrNode }> {
const pending: Array<{ entry: GitHubBatchPollEntry; node: BatchPollPrNode }> = [];
for (const [index, entry] of entries.entries()) {
const parsed = BatchPollRepositorySchema.safeParse(aliases[batchAlias(index)]);
if (!parsed.success) continue;
aliases[batchAlias(index)] = parsed.data;
if (parsed.data.isFork && parsed.data.parent) continue;
if (selectBatchPollNode(entry, parsed.data)?.state === "OPEN") continue;
for (const node of parsed.data.pullRequests.nodes) {
const key = `${entry.host}/${entry.owner}/${entry.name}#${node.number}@${node.headRefOid}`;
const cachedCommits = terminalCommitPages.get(key);
if (node.state !== "OPEN" && cachedCommits)
node.commits = { ...cachedCommits, nodes: [...cachedCommits.nodes] };
if (node.state !== "OPEN" && entry.target.headSha && node.commits?.pageInfo?.hasNextPage)
pending.push({ entry, node });
}
}
return pending;
}
async function loadBatchCommitPages(
entries: GitHubBatchPollEntry[],
aliases: Record<string, unknown>,
): Promise<boolean> {
const pending = collectBatchCommitPages(entries, aliases);
while (pending.length) {
const needing = pending.filter(
({ entry, node }) =>
node.commits?.pageInfo?.hasNextPage &&
!node.commits.nodes.some(({ commit }) => commit.oid === entry.target.headSha),
);
if (!needing.length) break;
const fields = needing.map(
({ entry, node }, index) =>
`${batchAlias(index)}: repository(owner: ${graphqlString(entry.owner)}, name: ${graphqlString(entry.name)}) { pullRequest(number: ${node.number}) { commits(first:100, after:${graphqlString(node.commits!.pageInfo!.endCursor!)}) { nodes { commit { oid } } pageInfo { hasNextPage endCursor } } } }`,
);
const args = [
"api",
"graphql",
"-f",
`query=query PaseoBatchCommitPages { rateLimit { limit remaining resetAt } ${fields.join("\n")} }`,
];
const response = await loadGitHubPollBatchResponse({
entries,
firstEntry: entries[0]!,
args,
});
if (!response) return false;
const rateLimit = parseGraphqlBatchRateLimit(response.stdout);
if (rateLimit && pauseGitHubPollsAtReserve(entries[0]!.host, rateLimit)) {
reportGitHubBatchError(entries, new GitHubGraphqlPollPausedError(rateLimit));
return false;
}
for (const [index, { node }] of needing.entries()) {
const page = z
.object({
pullRequest: z.object({ commits: BatchPollPrNodeSchema.shape.commits.unwrap() }),
})
.parse(response.aliases[batchAlias(index)]).pullRequest.commits;
node.commits!.nodes.push(...page.nodes);
node.commits!.pageInfo = page.pageInfo;
const entry = needing[index]!.entry;
const key = `${entry.host}/${entry.owner}/${entry.name}#${node.number}@${node.headRefOid}`;
terminalCommitPages.delete(key);
terminalCommitPages.set(key, node.commits!);
if (terminalCommitPages.size > 512)
terminalCommitPages.delete(terminalCommitPages.keys().next().value!);
}
}
return true;
}
function distributeGitHubPollBatch(input: {
entries: GitHubBatchPollEntry[];
aliases: Record<string, unknown>;
args: string[];
startedAt: number;
}): Promise<GitHubBatchDistribution> {
}): GitHubBatchDistribution {
const distribution: GitHubBatchDistribution = {
pendingChecks: [],
legacyFallback: [],
@@ -1739,7 +1832,7 @@ export function createGitHubService(options: CreateGitHubServiceOptions = {}): G
distribution.redirected.push(entry);
continue;
}
const node = await selectBatchPollNode(entry, repository.data);
const node = selectBatchPollNode(entry, repository.data);
if (!node) {
// Legacy fallback covers the two ways "no match" can be wrong: a fork
// whose PR lives in the parent repository, and a full candidate page —
@@ -1803,10 +1896,10 @@ export function createGitHubService(options: CreateGitHubServiceOptions = {}): G
return `${entry.host}\n${entry.originOwner}/${entry.originName}`;
}
async function selectBatchPollNode(
function selectBatchPollNode(
entry: GitHubBatchPollEntry,
repository: BatchPollRepository,
): Promise<BatchPollPrNode | null> {
): BatchPollPrNode | null {
// Prefer the canonical owner from the response: the origin slug can carry
// stale casing or a pre-transfer owner that repository() redirects from.
const selfOwner = (repository.owner?.login ?? entry.owner).toLowerCase();
@@ -1831,8 +1924,7 @@ export function createGitHubService(options: CreateGitHubServiceOptions = {}): G
nodeByCandidate.set(candidate, node);
candidates.push(candidate);
}
const match = await pickPullRequestCandidate({
cwd: entry.target.cwd,
const match = pickPullRequestCandidate({
candidates,
headRef: entry.target.headRef,
headSha: entry.target.headSha,
@@ -2995,8 +3087,7 @@ async function resolveCurrentPullRequestView(options: {
}): Promise<CurrentPullRequestStatus | null> {
const viewCandidate = await tryCurrentPullRequestView(options);
const viewMatch = viewCandidate
? await pickPullRequestCandidate({
cwd: options.cwd,
? pickPullRequestCandidate({
candidates: [viewCandidate],
headRef: options.headRef,
headSha: options.headSha,
@@ -3033,8 +3124,7 @@ async function resolveCurrentPullRequestView(options: {
headRef: options.headRef,
run: options.run,
});
const match = await pickPullRequestCandidate({
cwd: options.cwd,
const match = pickPullRequestCandidate({
candidates: viewMatch ? [...candidates, viewMatch] : candidates,
headRef: options.headRef,
headSha: options.headSha,
@@ -3071,8 +3161,7 @@ async function resolveForkPullRequestView(options: {
}),
),
);
const match = await pickPullRequestCandidate({
cwd: options.cwd,
const match = pickPullRequestCandidate({
candidates: [
...candidates.filter(
(candidate): candidate is ResolvedPullRequestCandidate =>
@@ -3175,6 +3264,41 @@ async function loadPullRequestGithubFacts(options: {
}
}
const readGitHubCommitList = createPullRequestCommitReader();
async function completeCandidateCommits(
candidate: ResolvedPullRequestCandidate,
options: {
cwd: string;
run: (args: string[], options: GitHubCommandRunnerOptions) => Promise<string>;
},
): Promise<void> {
const status = candidate.status;
if (status.state === "open" || (status.commitOids?.length ?? 0) < 100) return;
status.commitOids = await readGitHubCommitList(`${status.url}@${status.headSha}`, async () => {
const commits: string[] = [];
let cursor: string | null = null;
for (;;) {
const query = `query { repository(owner:${graphqlString(status.repoOwner!)},name:${graphqlString(status.repoName!)}) { pullRequest(number:${status.number}) { commits(first:100${cursor ? `,after:${graphqlString(cursor)}` : ""}) { nodes { commit { oid } } pageInfo { hasNextPage endCursor } } } } }`;
const stdout = await options.run(["api", "graphql", "-f", `query=${query}`], {
cwd: options.cwd,
});
const page = z
.object({
data: z.object({
repository: z.object({
pullRequest: z.object({ commits: BatchPollPrNodeSchema.shape.commits.unwrap() }),
}),
}),
})
.parse(JSON.parse(stdout)).data.repository.pullRequest.commits;
commits.push(...page.nodes.map(({ commit }) => commit.oid));
if (!page.pageInfo?.hasNextPage) return commits;
cursor = page.pageInfo.endCursor;
}
});
}
async function tryCurrentPullRequestView(options: {
cwd: string;
headRef: string;
@@ -3188,10 +3312,12 @@ async function tryCurrentPullRequestView(options: {
run: options.run,
args,
});
return parseCurrentPullRequestCandidate(stdout, options.headRef, {
const candidate = parseCurrentPullRequestCandidate(stdout, options.headRef, {
args: [...args, "--json", CURRENT_PR_STATUS_FIELDS],
cwd: options.cwd,
});
if (candidate) await completeCandidateCommits(candidate, options);
return candidate;
} catch (error) {
if (isNoPullRequestFoundError(error)) {
return null;
@@ -3212,10 +3338,12 @@ async function listCurrentPullRequestCandidates(options: {
run: options.run,
args,
});
return parseCurrentPullRequestCandidateList(stdout, options.headRef, {
const candidates = parseCurrentPullRequestCandidateList(stdout, options.headRef, {
args: [...args, "--json", CURRENT_PR_STATUS_FIELDS],
cwd: options.cwd,
});
await Promise.all(candidates.map((candidate) => completeCandidateCommits(candidate, options)));
return candidates;
} catch (error) {
if (isNoPullRequestFoundError(error)) {
return [];
@@ -3412,6 +3540,7 @@ function toBatchCurrentPullRequestItem(
baseRefName: node.baseRefName,
headRefName: node.headRefName,
...(node.headRefOid ? { headRefOid: node.headRefOid } : {}),
commits: node.commits?.nodes.map(({ commit }) => ({ oid: commit.oid })),
updatedAt: node.updatedAt,
mergedAt: node.mergedAt ?? null,
...(statusCheckRollup ? { statusCheckRollup } : {}),
@@ -3447,13 +3576,12 @@ function hasResolvedRepoIdentity(status: CurrentPullRequestStatus): boolean {
return Boolean(status.repoOwner && status.repoName);
}
async function pickPullRequestCandidate(options: {
cwd: string;
function pickPullRequestCandidate(options: {
candidates: ResolvedPullRequestCandidate[];
headRef: string;
headSha?: string;
headRepositoryOwner?: string;
}): Promise<ResolvedPullRequestCandidate | null> {
}): ResolvedPullRequestCandidate | null {
const matching = options.candidates.filter((candidate) => {
if (!isCandidateForHeadRef(candidate, options.headRef)) {
return false;
@@ -3466,18 +3594,16 @@ async function pickPullRequestCandidate(options: {
matching.sort((left, right) =>
comparePullRequestCandidatePreference(left, right, options.headSha),
);
for (const candidate of matching) {
if (
candidate.status.state === "open" ||
(await pullRequestContainsCommit({
cwd: options.cwd,
const open = matching.find((candidate) => candidate.status.state === "open");
if (open) return open;
return (
matching.find((candidate) =>
pullRequestContainsCommit({
workspaceCommit: options.headSha,
pullRequestCommit: candidate.headSha,
}))
)
return candidate;
}
return null;
pullRequestCommits: candidate.status.commitOids,
}),
) ?? null
);
}
function comparePullRequestCandidatePreference(
@@ -4053,6 +4179,7 @@ function toCurrentPullRequestStatus(
baseRefName: item.baseRefName,
headRefName: item.headRefName || fallbackHeadRefName,
...(item.headRefOid ? { headSha: item.headRefOid } : {}),
commitOids: item.commits?.map((commit) => commit.oid),
isMerged: mergedAt !== null,
isDraft: item.isDraft ?? false,
mergeable: item.mergeable,
@@ -377,6 +377,7 @@ describe("createGitLabService", () => {
const { service, calls } = makeService((args) => {
if (args[1] === "list") return ok(JSON.stringify([newestStale, exactOlder]));
if (args[1] === "view" && args[2] === "270") return ok(JSON.stringify(exactOlder));
if (args[1]?.includes("/commits?")) return ok(JSON.stringify([{ id: checkoutSha }]));
if (args[0] === "api") return ok("{}");
throw new Error(`unexpected call: ${args.join(" ")}`);
});
@@ -388,7 +389,7 @@ describe("createGitLabService", () => {
});
expect(status?.number).toBe(270);
expect(calls[1]).toEqual(["mr", "view", "270", "-F", "json"]);
expect(calls[2]).toEqual(["mr", "view", "270", "-F", "json"]);
});
it("keeps a merged MR when its remote head advances beyond the checkout", async () => {
@@ -401,9 +402,11 @@ describe("createGitLabService", () => {
source_branch: "dev",
sha: pullRequestCommit,
};
const { service, calls } = makeService((args) =>
ok(JSON.stringify(args[1] === "list" ? [stale] : stale)),
);
const { service, calls } = makeService((args) => {
if (args[1]?.includes("/commits?"))
return ok(JSON.stringify([{ id: workspaceCommit }, { id: pullRequestCommit }]));
return ok(JSON.stringify(args[1] === "list" ? [stale] : stale));
});
await expect(
service.getCurrentPullRequestStatus({
+26 -9
View File
@@ -1,4 +1,7 @@
import { pullRequestContainsCommit } from "./pull-request-contains-commit.js";
import {
pullRequestContainsCommit,
createPullRequestCommitReader,
} from "./pull-request-contains-commit.js";
import { z } from "zod";
import { parseGitRemoteLocation } from "@getpaseo/protocol/git-remote";
import { findExecutable } from "../executable-resolution/executable-resolution.js";
@@ -154,6 +157,7 @@ const GitLabMergeRequestSchema = z
source_branch: z.string(),
target_branch: z.string(),
sha: z.string().optional(),
commitOids: z.array(z.string()).optional(),
source_project_id: z.number().nullable().optional(),
target_project_id: z.number().nullable().optional(),
draft: z.boolean().optional(),
@@ -740,6 +744,7 @@ function toCurrentPullRequestStatus(
baseRefName: mr.target_branch,
headRefName: mr.source_branch,
...(mr.sha ? { headSha: mr.sha } : {}),
commitOids: mr.commitOids,
isMerged: mr.state === "merged" || mr.merged_at != null,
isDraft: mr.draft ?? mr.work_in_progress ?? false,
mergeable: mapMergeable(mr),
@@ -920,6 +925,7 @@ function gitLabNumberQuery(query: string | undefined): string | null {
}
export function createGitLabService(options: CreateGitLabServiceOptions = {}): ForgeService {
const readRequestCommits = createPullRequestCommitReader();
const runner = options.runner ?? runGlabCommand;
const resolveGlab = createCachedCliPathResolver(options.resolveGlabPath ?? resolveGlabPath);
const resolveRemoteUrl = options.resolveRemoteUrl ?? defaultResolveRemoteUrl;
@@ -997,14 +1003,25 @@ export function createGitLabService(options: CreateGitLabServiceOptions = {}): F
if (open) return viewMergeRequest(cwd, String(open.iid));
candidates.sort((left, right) => Number(right.sha === headSha) - Number(left.sha === headSha));
for (const mr of candidates) {
if (
await pullRequestContainsCommit({
cwd,
workspaceCommit: headSha,
pullRequestCommit: mr.sha,
})
) {
return viewMergeRequest(cwd, String(mr.iid));
const project = mr.target_project_id ?? extractProjectPath(mr.references?.full);
if (project === undefined) continue;
const commitOids = await readRequestCommits(`${mr.web_url}@${mr.sha}`, async () => {
const result: string[] = [];
for (let page = 1; ; page++) {
const commits = await runJson(
[
"api",
`projects/${encodeURIComponent(project)}/merge_requests/${mr.iid}/commits?per_page=100&page=${page}`,
],
{ cwd },
z.array(z.object({ id: z.string() })),
);
result.push(...commits.map((commit) => commit.id));
if (commits.length < 100) return result;
}
});
if (pullRequestContainsCommit({ workspaceCommit: headSha, pullRequestCommits: commitOids })) {
return { ...(await viewMergeRequest(cwd, String(mr.iid))), commitOids };
}
}
return null;
@@ -1,44 +1,21 @@
import { execCommand } from "../utils/spawn.js";
/** A terminal request belongs to a checkout only when its head contains that commit. */
export async function pullRequestContainsCommit(input: {
cwd: string;
/** Only commits introduced by the request establish workspace ownership. */
export function pullRequestContainsCommit(input: {
workspaceCommit?: string;
pullRequestCommit?: string;
}): Promise<boolean> {
const { cwd, workspaceCommit, pullRequestCommit } = input;
if (!workspaceCommit || !pullRequestCommit) return false;
if (workspaceCommit === pullRequestCommit) return true;
// Only object IDs cross the git command boundary, never remote-supplied revisions/options.
if (![workspaceCommit, pullRequestCommit].every((sha) => /^[a-f0-9]{40,64}$/i.test(sha))) {
return false;
}
const git = (args: string[]) =>
execCommand("git", args, {
cwd,
timeout: 15_000,
envOverlay: { GIT_TERMINAL_PROMPT: "0" },
});
try {
try {
await git(["cat-file", "-e", `${pullRequestCommit}^{commit}`]);
} catch {
// A server-side branch update need not have been fetched by this checkout.
// Fetch objects only: preserve FETCH_HEAD and all local/tracking refs.
await git([
"fetch",
"--no-tags",
"--no-write-fetch-head",
"--no-recurse-submodules",
"--no-auto-maintenance",
"origin",
pullRequestCommit,
]);
}
await git(["merge-base", "--is-ancestor", workspaceCommit, pullRequestCommit]);
return true;
} catch {
// Missing objects, shallow history and unavailable remotes cannot prove ownership.
return false;
}
pullRequestCommits?: readonly string[];
}): boolean {
return !!input.workspaceCommit && !!input.pullRequestCommits?.includes(input.workspaceCommit);
}
/** Cache immutable terminal-request commit lists, including failed reads, across polls. */
export function createPullRequestCommitReader(now: () => number = Date.now) {
const cache = new Map<string, { expiresAt: number; commits: Promise<string[]> }>();
return (key: string, load: () => Promise<string[]>): Promise<string[]> => {
const existing = cache.get(key);
if (existing && existing.expiresAt > now()) return existing.commits;
const entry = { expiresAt: now() + 300_000, commits: load() };
cache.delete(key);
cache.set(key, entry);
if (cache.size > 1024) cache.delete(cache.keys().next().value!);
return entry.commits;
};
}