Skip to content

Commit c75430d

Browse files
committed
fix(server): review guides in a fork checkout cover only the pull request's change
In a checkout with both `origin` (a fork) and `upstream`, the project's repository identity is read from `upstream`, but the guide fetched the base branch from the checkout's primary remote, `origin`. When the fork's base branch was behind upstream's, the guide was measured from that stale tip and covered every upstream commit since, not the pull request's own change. The guide now fetches from the remote whose URL is the one the identity was read from, looked up among the checkout's current remotes so a cached remote name that has since been renamed still resolves.
1 parent ac2cc78 commit c75430d

2 files changed

Lines changed: 105 additions & 8 deletions

File tree

‎apps/server/src/pullRequest/PullRequestReviewGuideService.test.ts‎

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ const makeRepositories = Effect.gen(function* () {
9494
yield* git(remote, ["checkout", "--quiet", "main"]);
9595
yield* commit("unrelated.txt", "main moved on");
9696
yield* git(root, ["clone", "--quiet", remote, workspace]);
97-
return { root, workspace, headSha, lowerSha, upperSha };
97+
return { root, remote, workspace, headSha, lowerSha, upperSha };
9898
});
9999

100100
const detail = (workspaceRoot: string, headSha: string): PullRequestDetail =>
@@ -154,6 +154,8 @@ const stack = (repositories: {
154154

155155
const makeLayer = (input: {
156156
readonly root: string;
157+
/** The repository the workspace was cloned from, which the project's identity points at. */
158+
readonly remote: string;
157159
readonly workspace: string;
158160
readonly headSha: string;
159161
readonly lowerSha: string;
@@ -202,7 +204,10 @@ const makeLayer = (input: {
202204
Effect.succeedSome({
203205
id: projectId,
204206
workspaceRoot: input.workspace,
205-
repositoryIdentity: input.identity ?? ACME_APP,
207+
repositoryIdentity: {
208+
...(input.identity ?? ACME_APP),
209+
locator: { ...ACME_APP.locator, remoteUrl: input.remote },
210+
},
206211
}),
207212
} as unknown as ProjectService.ProjectService["Service"]),
208213
),
@@ -358,6 +363,57 @@ it.layer(layerBase, { excludeTestServices: true })("PullRequestReviewGuideServic
358363
),
359364
);
360365

366+
it.effect("measures a fork checkout's change from the upstream base, not the fork's", () =>
367+
Effect.scoped(
368+
Effect.gen(function* () {
369+
const repositories = yield* makeRepositories;
370+
// The checkout becomes a fork's: `origin` is a copy whose base branch never took the
371+
// commit the change is built on, and the pull request's repository is `upstream`. The
372+
// project's identity still names it `origin`, as a cached one does after the rename.
373+
const fork = `${repositories.root}/fork`;
374+
yield* git(repositories.root, ["clone", "--quiet", "--bare", repositories.remote, fork]);
375+
yield* git(fork, ["update-ref", "refs/heads/stack-base", "refs/heads/main~1"]);
376+
yield* git(repositories.workspace, ["remote", "rename", "origin", "upstream"]);
377+
yield* git(repositories.workspace, ["remote", "add", "origin", fork]);
378+
const changed = yield* Deferred.make<string>();
379+
const driver = yield* GitVcsDriver.GitVcsDriver;
380+
const stored = yield* Deferred.make<void>();
381+
const context = yield* Layer.build(
382+
makeLayer({
383+
...repositories,
384+
stored,
385+
generate: (input) =>
386+
git(input.cwd, ["diff", "--name-only", `${input.baseCommit}...HEAD`]).pipe(
387+
Effect.provideService(GitVcsDriver.GitVcsDriver, driver),
388+
Effect.orDie,
389+
Effect.flatMap((names) => Deferred.succeed(changed, names)),
390+
Effect.as({ stops: [] }),
391+
),
392+
}),
393+
);
394+
const service = Context.get(
395+
context,
396+
PullRequestReviewGuideService.PullRequestReviewGuideService,
397+
);
398+
yield* service.generate(reference);
399+
const failedRun = service.get(reference).pipe(
400+
Effect.flatMap((result) =>
401+
result.run?._tag === "Failed"
402+
? Effect.die(`run failed: ${result.run.detail}`)
403+
: Effect.fail("still running" as const),
404+
),
405+
Effect.retry(Schedule.spaced("20 millis")),
406+
);
407+
// Measured from the fork's stale base this would also name lower.txt.
408+
assert.strictEqual(
409+
yield* Effect.raceFirst(Deferred.await(changed), failedRun),
410+
"feature.txt",
411+
);
412+
yield* Effect.raceFirst(Deferred.await(stored), failedRun);
413+
}),
414+
),
415+
);
416+
361417
it.effect("refuses to write a guide while the server has review guides off", () =>
362418
Effect.scoped(
363419
Effect.gen(function* () {

‎apps/server/src/pullRequest/PullRequestReviewGuideService.ts‎

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ const make = Effect.gen(function* () {
119119
/**
120120
* A guide reads the change in a checkout fetched from the project's own remote, so the pull
121121
* request has to be in that repository. One from another repository is refused rather than
122-
* measured against commits that are not its own.
122+
* measured against commits that are not its own. Returns where that repository was read from.
123123
*/
124124
const requireOwnRepository = Effect.fn("PullRequestReviewGuideService.requireOwnRepository")(
125125
function* (key: ThreadPullRequestKey, identity: RepositoryIdentity | null | undefined) {
@@ -134,13 +134,19 @@ const make = Effect.gen(function* () {
134134
},
135135
identity,
136136
);
137-
if (own === null || own.host !== key.host || own.repository !== key.repository) {
137+
if (
138+
identity == null ||
139+
own === null ||
140+
own.host !== key.host ||
141+
own.repository !== key.repository
142+
) {
138143
return yield* new PullRequestOperationError({
139144
operation: "generateReviewGuide",
140145
detail:
141146
"Guides are written from this project's checkout, and this pull request is in another repository. Write it from a project that is a checkout of that repository.",
142147
});
143148
}
149+
return identity.locator;
144150
},
145151
);
146152

@@ -179,17 +185,50 @@ const make = Effect.gen(function* () {
179185
*/
180186
const checkoutHead = Effect.fn("PullRequestReviewGuideService.checkoutHead")(function* (
181187
workspaceRoot: string,
188+
locator: RepositoryIdentity["locator"],
182189
detail: PullRequestDetail,
183190
headSha: string,
184191
stack: PullRequestStack | null,
185192
runId: string,
186193
) {
187-
const remote = yield* git.resolvePrimaryRemoteName(workspaceRoot);
194+
// The remote for the pull request's own repository, not the checkout's primary one: in a
195+
// fork's checkout that is `upstream`, and the fork's `origin` may have a base branch far
196+
// behind the one the pull request targets. Found by URL among the remotes as they are now,
197+
// since the identity is cached and its remote may have been renamed since. `ls-remote
198+
// --get-url` reports the URL git would fetch from, as the identity's was read, and unlike
199+
// `remote get-url` it also answers for a remote configured outside the repository.
200+
const names = yield* git
201+
.execute({
202+
operation: "PullRequestReviewGuideService.listRemotes",
203+
cwd: workspaceRoot,
204+
args: ["remote"],
205+
})
206+
.pipe(Effect.map((result) => result.stdout.split("\n").filter((name) => name.length > 0)));
207+
const remote = yield* Effect.findFirst(
208+
[locator.remoteName, ...names.filter((name) => name !== locator.remoteName)],
209+
(name) =>
210+
git
211+
.execute({
212+
operation: "PullRequestReviewGuideService.remoteUrl",
213+
cwd: workspaceRoot,
214+
args: ["ls-remote", "--get-url", "--", name],
215+
})
216+
.pipe(
217+
Effect.map((result) => result.stdout.trim() === locator.remoteUrl),
218+
Effect.orElseSucceed(() => false),
219+
),
220+
).pipe(Effect.map(Option.getOrUndefined));
221+
if (remote === undefined) {
222+
return yield* new PullRequestOperationError({
223+
operation: "generateReviewGuide",
224+
detail: "This project's checkout no longer has a remote for the pull request's repository.",
225+
});
226+
}
188227
const fetch = (args: ReadonlyArray<string>) =>
189228
git.execute({
190229
operation: "PullRequestReviewGuideService.fetch",
191230
cwd: workspaceRoot,
192-
args: ["fetch", "--quiet", "--no-tags", remote, ...args],
231+
args: ["fetch", "--quiet", "--no-tags", "--", remote, ...args],
193232
timeoutMs: 5 * 60_000,
194233
});
195234
// The base branch is what the change is measured from; the head commit is the change. Hosts
@@ -309,6 +348,7 @@ const make = Effect.gen(function* () {
309348
input: PullRequestRef,
310349
key: ThreadPullRequestKey,
311350
workspaceRoot: string,
351+
locator: RepositoryIdentity["locator"],
312352
detail: PullRequestDetail,
313353
headSha: string,
314354
runId: string,
@@ -334,6 +374,7 @@ const make = Effect.gen(function* () {
334374
// from it too, so the guide can be held to the lines the change actually touched.
335375
const { generated, change } = yield* checkoutHead(
336376
workspaceRoot,
377+
locator,
337378
detail,
338379
headSha,
339380
stack,
@@ -401,7 +442,7 @@ const make = Effect.gen(function* () {
401442
});
402443
}
403444
const { key, workspaceRoot, identity } = yield* keyFor(input);
404-
yield* requireOwnRepository(key, identity);
445+
const locator = yield* requireOwnRepository(key, identity);
405446
const runKey = keyString(key);
406447
// A press asks for the head as it is now, not a read shared from the last minute, so a guide
407448
// is never started for a commit the pull request has already moved past.
@@ -458,7 +499,7 @@ const make = Effect.gen(function* () {
458499
return updated;
459500
});
460501
const runId = `${runCounter++}`;
461-
yield* write(input, key, workspaceRoot, detail, currentHeadSha, runId, owns).pipe(
502+
yield* write(input, key, workspaceRoot, locator, detail, currentHeadSha, runId, owns).pipe(
462503
Effect.andThen(finish(null)),
463504
Effect.catchCause((cause) =>
464505
Cause.hasInterruptsOnly(cause)

0 commit comments

Comments
 (0)