Skip to content

Commit cbd3458

Browse files
fix(server): use the verified remote for review guides
1 parent 731aeee commit cbd3458

2 files changed

Lines changed: 65 additions & 25 deletions

File tree

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

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { assert, it } from "@effect/vitest";
33
import * as Context from "effect/Context";
44
import * as Deferred from "effect/Deferred";
55
import * as Effect from "effect/Effect";
6+
import * as Fiber from "effect/Fiber";
67
import * as FileSystem from "effect/FileSystem";
78
import * as Layer from "effect/Layer";
89
import * as Ref from "effect/Ref";
@@ -147,7 +148,6 @@ const stack = (repositories: {
147148

148149
const makeLayer = (input: {
149150
readonly root: string;
150-
/** The repository the workspace was cloned from, which the project's identity points at. */
151151
readonly remote: string;
152152
readonly workspace: string;
153153
readonly headSha: string;
@@ -660,18 +660,22 @@ it.layer(layerBase, { excludeTestServices: true })("PullRequestReviewGuideServic
660660
),
661661
);
662662

663-
it.effect("measures a fork checkout's change from the upstream base, not the fork's", () =>
663+
it.effect("measures only the PR change using the renamed verified remote's fetch URL", () =>
664664
Effect.scoped(
665665
Effect.gen(function* () {
666666
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.
670667
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"]);
668+
yield* git(repositories.root, ["clone", "--quiet", repositories.remote, fork]);
669+
yield* git(fork, ["branch", "stack-base", "main~1"]);
670+
// The cached identity still names origin, which now belongs to the stale fork.
673671
yield* git(repositories.workspace, ["remote", "rename", "origin", "upstream"]);
674672
yield* git(repositories.workspace, ["remote", "add", "origin", fork]);
673+
yield* git(repositories.workspace, [
674+
"config",
675+
`url.${repositories.remote}.insteadOf`,
676+
"review-upstream:",
677+
]);
678+
yield* git(repositories.workspace, ["remote", "set-url", "upstream", "review-upstream:"]);
675679
const changed = yield* Deferred.make<string>();
676680
const driver = yield* GitVcsDriver.GitVcsDriver;
677681
const stored = yield* Deferred.make<void>();
@@ -693,20 +697,61 @@ it.layer(layerBase, { excludeTestServices: true })("PullRequestReviewGuideServic
693697
PullRequestReviewGuideService.PullRequestReviewGuideService,
694698
);
695699
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),
700+
assert.strictEqual(yield* Deferred.await(changed), "feature.txt");
701+
yield* Deferred.await(stored);
702+
const result = yield* service.get(reference);
703+
assert.strictEqual(result.guide?.headSha, repositories.headSha);
704+
assert.deepStrictEqual(result.guide?.stops[0]?.files, [{ path: "feature.txt" }]);
705+
}),
706+
),
707+
);
708+
709+
it.effect("refuses generation when the verified remote disappeared despite a usable origin", () =>
710+
Effect.scoped(
711+
Effect.gen(function* () {
712+
const repositories = yield* makeRepositories;
713+
const fork = `${repositories.root}/fork`;
714+
yield* git(repositories.root, ["clone", "--quiet", repositories.remote, fork]);
715+
yield* git(fork, ["branch", "stack-base", repositories.lowerSha]);
716+
yield* git(repositories.workspace, ["remote", "set-url", "origin", fork]);
717+
const started = yield* Deferred.make<Fiber.Fiber<unknown, unknown>>();
718+
const stored = yield* Deferred.make<void>();
719+
const generated = yield* Ref.make(false);
720+
const driver = yield* GitVcsDriver.GitVcsDriver;
721+
const context = yield* Layer.build(
722+
makeLayer({
723+
...repositories,
724+
stored,
725+
generate: () => Ref.set(generated, true).pipe(Effect.as({ stops: [] })),
726+
}).pipe(
727+
Layer.provide(
728+
Layer.succeed(GitVcsDriver.GitVcsDriver, {
729+
...driver,
730+
execute: (call) =>
731+
Effect.withFiber((fiber) =>
732+
Deferred.succeed(started, fiber).pipe(Effect.andThen(driver.execute(call))),
733+
),
734+
}),
735+
),
701736
),
702-
Effect.retry(Schedule.spaced("20 millis")),
703737
);
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",
738+
const service = Context.get(
739+
context,
740+
PullRequestReviewGuideService.PullRequestReviewGuideService,
741+
);
742+
const before = yield* service.get(reference);
743+
yield* service.generate(reference);
744+
// Join the background run so its failure is visible without polling service state.
745+
yield* Fiber.await(yield* Deferred.await(started));
746+
const result = yield* service.get(reference);
747+
assert.strictEqual(result.run?._tag, "Failed");
748+
assert.include(
749+
result.run?._tag === "Failed" ? result.run.detail : "",
750+
"no longer has a remote for the pull request's repository",
708751
);
709-
yield* Effect.raceFirst(Deferred.await(stored), failedRun);
752+
assert.deepStrictEqual(result.guide, before.guide);
753+
assert.isFalse(yield* Deferred.isDone(stored));
754+
assert.isFalse(yield* Ref.get(generated));
710755
}),
711756
),
712757
);

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,7 @@ const make = Effect.gen(function* () {
160160
stack: PullRequestStack | null,
161161
runId: string,
162162
) {
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.
163+
// Identity names can be stale; match effective fetch URLs, including insteadOf rewrites.
169164
const names = yield* git
170165
.execute({
171166
operation: "PullRequestReviewGuideService.listRemotes",
@@ -180,7 +175,7 @@ const make = Effect.gen(function* () {
180175
.execute({
181176
operation: "PullRequestReviewGuideService.remoteUrl",
182177
cwd: workspaceRoot,
183-
args: ["ls-remote", "--get-url", "--", name],
178+
args: ["remote", "get-url", "--", name],
184179
})
185180
.pipe(
186181
Effect.map((result) => result.stdout.trim() === locator.remoteUrl),

0 commit comments

Comments
 (0)