Skip to content

Commit efc861d

Browse files
fix(server): PR checkout guard asks the host about renamed checkouts
Before refusing, look the PR number up in the checkout's own repository and accept when the host answers with the same pull request, so a remote that still names a renamed repository is recognised. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent fe06601 commit efc861d

2 files changed

Lines changed: 82 additions & 1 deletion

File tree

‎apps/server/src/git/GitManager.test.ts‎

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ interface FakeProviderScenario {
9191
prListOtherwise?: ReadonlyArray<FakeChangeRequest>;
9292
defaultBranch?: string;
9393
pullRequest?: FakeChangeRequest;
94+
/** The answer to a bare-number lookup in the checkout's own repository; null is not found. */
95+
pullRequestInCheckout?: FakeChangeRequest | null;
9496
repositoryCloneUrls?: Record<string, { url: string; sshUrl: string }>;
9597
failWith?: string;
9698
failCause?: unknown;
@@ -492,7 +494,22 @@ function createFakeSourceControlProvider(scenario: FakeProviderScenario = {}): {
492494
),
493495
getChangeRequest: (input) =>
494496
record(input.cwd, { operation: "getChangeRequest", reference: input.reference }).pipe(
495-
Effect.map(() => toChangeRequest(pullRequest())),
497+
Effect.flatMap(() => {
498+
const inCheckout =
499+
/^\d+$/.test(input.reference) && scenario.pullRequestInCheckout !== undefined
500+
? scenario.pullRequestInCheckout
501+
: pullRequest();
502+
return inCheckout === null
503+
? Effect.fail(
504+
new SourceControlProviderFailure({
505+
provider: kind,
506+
operation: "getChangeRequest",
507+
cwd: input.cwd,
508+
detail: "Not found.",
509+
}),
510+
)
511+
: Effect.succeed(toChangeRequest(inCheckout));
512+
}),
496513
),
497514
getRepositoryCloneUrls: (input) =>
498515
record(input.cwd, {
@@ -5304,6 +5321,7 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
53045321
state: "open",
53055322
isCrossRepository: false,
53065323
},
5324+
pullRequestInCheckout: null,
53075325
},
53085326
});
53095327

@@ -5344,6 +5362,7 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
53445362
state: "open",
53455363
isCrossRepository: false,
53465364
},
5365+
pullRequestInCheckout: null,
53475366
},
53485367
});
53495368

@@ -5360,6 +5379,57 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
53605379
}),
53615380
);
53625381

5382+
it.effect.each([
5383+
"https://github.com/acme/old-name/pull/7",
5384+
"https://github.com/acme/new-name/pull/7",
5385+
"feature/renamed",
5386+
])("prepares a PR thread from %s in a checkout under a repository's old name", (reference) =>
5387+
Effect.gen(function* () {
5388+
// The host redirects the old name, so only it knows the checkout is of the renamed repository.
5389+
const repoDir = yield* makeTempDir("t3code-git-manager-");
5390+
yield* initRepo(repoDir);
5391+
const remoteDir = yield* createBareRemote();
5392+
yield* runGit(repoDir, ["remote", "add", "origin", remoteDir]);
5393+
yield* configureVisibleRemoteUrlWithLocalRewrite(
5394+
repoDir,
5395+
"origin",
5396+
"https://github.com/acme/old-name.git",
5397+
remoteDir,
5398+
);
5399+
yield* runGit(repoDir, ["push", "-u", "origin", "main"]);
5400+
yield* runGit(repoDir, ["checkout", "-b", "feature/renamed"]);
5401+
NodeFS.writeFileSync(NodePath.join(repoDir, "renamed.txt"), "renamed\n");
5402+
yield* runGit(repoDir, ["add", "renamed.txt"]);
5403+
yield* runGit(repoDir, ["commit", "-m", "Renamed repository PR branch"]);
5404+
yield* runGit(repoDir, ["push", "origin", "feature/renamed"]);
5405+
yield* runGit(repoDir, ["checkout", "main"]);
5406+
yield* runGit(repoDir, ["branch", "-D", "feature/renamed"]);
5407+
5408+
const { manager } = yield* makeManager({
5409+
providerScenario: {
5410+
pullRequest: {
5411+
number: 7,
5412+
title: "Renamed repository PR",
5413+
url: "https://github.com/acme/new-name/pull/7",
5414+
baseRefName: "main",
5415+
headRefName: "feature/renamed",
5416+
state: "open",
5417+
isCrossRepository: false,
5418+
headRepositoryNameWithOwner: "acme/new-name",
5419+
},
5420+
},
5421+
});
5422+
5423+
const result = yield* preparePullRequestThread(manager, {
5424+
cwd: repoDir,
5425+
reference,
5426+
mode: "local",
5427+
});
5428+
5429+
expect(result.branch).toBe("feature/renamed");
5430+
}),
5431+
);
5432+
53635433
it.effect("prepares a worktree PR thread through an insteadOf shorthand remote", () =>
53645434
Effect.gen(function* () {
53655435
// `gh:` only names the repository once `insteadOf` rewrites it; the SSH command serves the
@@ -5734,6 +5804,7 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
57345804
headRepositoryNameWithOwner: "octocat/codething-mvp",
57355805
headRepositoryOwnerLogin: "octocat",
57365806
},
5807+
pullRequestInCheckout: null,
57375808
};
57385809
const gitCommands: string[] = [];
57395810
const { manager } = yield* makeManager({

‎apps/server/src/git/GitManager.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2987,6 +2987,16 @@ export const make = Effect.gen(function* () {
29872987
for (const [remoteUrl, remoteName] of remotes) {
29882988
if (yield* servedFromWeb(remoteUrl, remoteName)) return;
29892989
}
2990+
// The host knows this checkout's repository under any spelling, such as a pre-rename name.
2991+
const inCheckout = yield* provider
2992+
.getChangeRequest({ cwd, reference: String(pullRequest.number) })
2993+
.pipe(Effect.option);
2994+
if (
2995+
Option.isSome(inCheckout) &&
2996+
sourceControlDefinitions.isSameChangeRequest(inCheckout.value.url, pullRequest.url)
2997+
) {
2998+
return;
2999+
}
29903000
return yield* new GitManagerError({
29913001
operation: "preparePullRequestThread",
29923002
cwd,

0 commit comments

Comments
 (0)