Skip to content

Commit 2022287

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 2577baf commit 2022287

2 files changed

Lines changed: 104 additions & 7 deletions

File tree

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

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ const makeRepositories = Effect.gen(function* () {
8888
yield* git(remote, ["checkout", "--quiet", "main"]);
8989
yield* commit("unrelated.txt", "main moved on");
9090
yield* git(root, ["clone", "--quiet", remote, workspace]);
91-
return { root, workspace, headSha, lowerSha, upperSha };
91+
return { root, remote, workspace, headSha, lowerSha, upperSha };
9292
});
9393

9494
const detail = (workspaceRoot: string, headSha: string): PullRequestDetail =>
@@ -147,6 +147,8 @@ const stack = (repositories: {
147147

148148
const makeLayer = (input: {
149149
readonly root: string;
150+
/** The repository the workspace was cloned from, which the project's identity points at. */
151+
readonly remote: string;
150152
readonly workspace: string;
151153
readonly headSha: string;
152154
readonly lowerSha: string;
@@ -191,7 +193,10 @@ const makeLayer = (input: {
191193
Effect.succeedSome({
192194
id: projectId,
193195
workspaceRoot: input.workspace,
194-
repositoryIdentity: input.identity ?? ACME_APP,
196+
repositoryIdentity: {
197+
...(input.identity ?? ACME_APP),
198+
locator: { ...ACME_APP.locator, remoteUrl: input.remote },
199+
},
195200
}),
196201
} as unknown as ProjectService.ProjectService["Service"]),
197202
),
@@ -655,6 +660,57 @@ it.layer(layerBase, { excludeTestServices: true })("PullRequestReviewGuideServic
655660
),
656661
);
657662

663+
it.effect("measures a fork checkout's change from the upstream base, not the fork's", () =>
664+
Effect.scoped(
665+
Effect.gen(function* () {
666+
const repositories = yield* makeRepositories;
667+
// The checkout becomes a fork's: `origin` is a copy whose base branch never took the
668+
// commit the change is built on, and the pull request's repository is `upstream`. The
669+
// project's identity still names it `origin`, as a cached one does after the rename.
670+
const fork = `${repositories.root}/fork`;
671+
yield* git(repositories.root, ["clone", "--quiet", "--bare", repositories.remote, fork]);
672+
yield* git(fork, ["update-ref", "refs/heads/stack-base", "refs/heads/main~1"]);
673+
yield* git(repositories.workspace, ["remote", "rename", "origin", "upstream"]);
674+
yield* git(repositories.workspace, ["remote", "add", "origin", fork]);
675+
const changed = yield* Deferred.make<string>();
676+
const driver = yield* GitVcsDriver.GitVcsDriver;
677+
const stored = yield* Deferred.make<void>();
678+
const context = yield* Layer.build(
679+
makeLayer({
680+
...repositories,
681+
stored,
682+
generate: (input) =>
683+
git(input.cwd, ["diff", "--name-only", `${input.baseCommit}...HEAD`]).pipe(
684+
Effect.provideService(GitVcsDriver.GitVcsDriver, driver),
685+
Effect.orDie,
686+
Effect.flatMap((names) => Deferred.succeed(changed, names)),
687+
Effect.as({ stops: [] }),
688+
),
689+
}),
690+
);
691+
const service = Context.get(
692+
context,
693+
PullRequestReviewGuideService.PullRequestReviewGuideService,
694+
);
695+
yield* service.generate(reference);
696+
const failedRun = service.get(reference).pipe(
697+
Effect.flatMap((result) =>
698+
result.run?._tag === "Failed"
699+
? Effect.die(`run failed: ${result.run.detail}`)
700+
: Effect.fail("still running" as const),
701+
),
702+
Effect.retry(Schedule.spaced("20 millis")),
703+
);
704+
// Measured from the fork's stale base this would also name lower.txt.
705+
assert.strictEqual(
706+
yield* Effect.raceFirst(Deferred.await(changed), failedRun),
707+
"feature.txt",
708+
);
709+
yield* Effect.raceFirst(Deferred.await(stored), failedRun);
710+
}),
711+
),
712+
);
713+
658714
it.effect("refuses to write a guide while the server has review guides off", () =>
659715
Effect.scoped(
660716
Effect.gen(function* () {

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

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,19 @@ const make = Effect.gen(function* () {
108108
},
109109
identity,
110110
);
111-
if (own === null || own.host !== key.host || own.repository !== key.repository) {
111+
if (
112+
identity == null ||
113+
own === null ||
114+
own.host !== key.host ||
115+
own.repository !== key.repository
116+
) {
112117
return yield* new PullRequestOperationError({
113118
operation: "generateReviewGuide",
114119
detail:
115120
"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.",
116121
});
117122
}
123+
return identity.locator;
118124
},
119125
);
120126

@@ -148,17 +154,50 @@ const make = Effect.gen(function* () {
148154

149155
const checkoutHead = Effect.fn("PullRequestReviewGuideService.checkoutHead")(function* (
150156
workspaceRoot: string,
157+
locator: RepositoryIdentity["locator"],
151158
detail: PullRequestDetail,
152159
headSha: string,
153160
stack: PullRequestStack | null,
154161
runId: string,
155162
) {
156-
const remote = yield* git.resolvePrimaryRemoteName(workspaceRoot);
163+
// The remote for the pull request's own repository, not the checkout's primary one: in a
164+
// fork's checkout that is `upstream`, and the fork's `origin` may have a base branch far
165+
// behind the one the pull request targets. Found by URL among the remotes as they are now,
166+
// since the identity is cached and its remote may have been renamed since. `ls-remote
167+
// --get-url` reports the URL git would fetch from, as the identity's was read, and unlike
168+
// `remote get-url` it also answers for a remote configured outside the repository.
169+
const names = yield* git
170+
.execute({
171+
operation: "PullRequestReviewGuideService.listRemotes",
172+
cwd: workspaceRoot,
173+
args: ["remote"],
174+
})
175+
.pipe(Effect.map((result) => result.stdout.split("\n").filter((name) => name.length > 0)));
176+
const remote = yield* Effect.findFirst(
177+
[locator.remoteName, ...names.filter((name) => name !== locator.remoteName)],
178+
(name) =>
179+
git
180+
.execute({
181+
operation: "PullRequestReviewGuideService.remoteUrl",
182+
cwd: workspaceRoot,
183+
args: ["ls-remote", "--get-url", "--", name],
184+
})
185+
.pipe(
186+
Effect.map((result) => result.stdout.trim() === locator.remoteUrl),
187+
Effect.orElseSucceed(() => false),
188+
),
189+
).pipe(Effect.map(Option.getOrUndefined));
190+
if (remote === undefined) {
191+
return yield* new PullRequestOperationError({
192+
operation: "generateReviewGuide",
193+
detail: "This project's checkout no longer has a remote for the pull request's repository.",
194+
});
195+
}
157196
const fetch = (args: ReadonlyArray<string>) =>
158197
git.execute({
159198
operation: "PullRequestReviewGuideService.fetch",
160199
cwd: workspaceRoot,
161-
args: ["fetch", "--quiet", "--no-tags", remote, ...args],
200+
args: ["fetch", "--quiet", "--no-tags", "--", remote, ...args],
162201
timeoutMs: 5 * 60_000,
163202
});
164203
// The host names the base branch. A name git rejects, or one that would change the refspec
@@ -327,6 +366,7 @@ const make = Effect.gen(function* () {
327366
input: PullRequestRef,
328367
key: ThreadPullRequestKey,
329368
workspaceRoot: string,
369+
locator: RepositoryIdentity["locator"],
330370
detail: PullRequestDetail,
331371
headSha: string,
332372
runId: string,
@@ -349,6 +389,7 @@ const make = Effect.gen(function* () {
349389
);
350390
const { generated, change } = yield* checkoutHead(
351391
workspaceRoot,
392+
locator,
352393
detail,
353394
headSha,
354395
stack,
@@ -412,7 +453,7 @@ const make = Effect.gen(function* () {
412453
});
413454
}
414455
const { key, workspaceRoot, identity } = yield* keyFor(input);
415-
yield* requireOwnRepository(key, identity);
456+
const locator = yield* requireOwnRepository(key, identity);
416457
const runKey = keyString(key);
417458
yield* pullRequests.invalidate({ reference: input });
418459
const [detail, stored] = yield* Effect.all([pullRequests.detail(input), readStored(key)], {
@@ -465,7 +506,7 @@ const make = Effect.gen(function* () {
465506
return updated;
466507
});
467508
const runId = `${runCounter++}`;
468-
yield* write(input, key, workspaceRoot, detail, currentHeadSha, runId, owns).pipe(
509+
yield* write(input, key, workspaceRoot, locator, detail, currentHeadSha, runId, owns).pipe(
469510
Effect.andThen(finish(null)),
470511
Effect.catchCause((cause) =>
471512
Cause.hasInterruptsOnly(cause)

0 commit comments

Comments
 (0)