Skip to content

Commit fc1eecd

Browse files
committed
fix(server): detect existing PR when remote URL uses a renamed repository slug
1 parent ede3f2d commit fc1eecd

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4903,6 +4903,51 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
49034903
}),
49044904
);
49054905

4906+
it.effect(
4907+
"matches same-repo PR metadata when the remote URL uses a renamed repository slug",
4908+
() =>
4909+
Effect.sync(() => {
4910+
const pr = {
4911+
number: 142,
4912+
title: "Renamed repository PR",
4913+
url: "https://github.com/octo/new-name/pull/142",
4914+
baseRefName: "main",
4915+
headRefName: "feature/rename",
4916+
state: "open" as const,
4917+
updatedAt: Option.none(),
4918+
isCrossRepository: false,
4919+
headRepositoryNameWithOwner: "octo/new-name",
4920+
headRepositoryOwnerLogin: "octo",
4921+
};
4922+
const headContext = {
4923+
headBranch: "feature/rename",
4924+
headRepositoryNameWithOwner: "octo/old-name",
4925+
headRepositoryOwnerLogin: "octo",
4926+
isCrossRepository: false,
4927+
};
4928+
4929+
expect(GitManager.matchesBranchHeadContext(pr, headContext)).toBe(true);
4930+
// A different owner is never a rename.
4931+
expect(
4932+
GitManager.matchesBranchHeadContext(pr, {
4933+
...headContext,
4934+
headRepositoryNameWithOwner: "someone-else/old-name",
4935+
headRepositoryOwnerLogin: "someone-else",
4936+
}),
4937+
).toBe(false);
4938+
// Cross-repository heads keep the strict repository comparison.
4939+
expect(
4940+
GitManager.matchesBranchHeadContext(
4941+
{ ...pr, isCrossRepository: true },
4942+
{ ...headContext, isCrossRepository: true },
4943+
),
4944+
).toBe(false);
4945+
expect(
4946+
GitManager.matchesBranchHeadContext({ ...pr, isCrossRepository: true }, headContext),
4947+
).toBe(false);
4948+
}),
4949+
);
4950+
49064951
it.effect("creates PR when one does not already exist", () =>
49074952
Effect.gen(function* () {
49084953
const repoDir = yield* makeTempDir("t3code-git-manager-");

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,17 @@ export function matchesBranchHeadContext(
435435
if (expectedHead.repositoryNameWithOwner) {
436436
if (pullRequestHead.repositoryNameWithOwner) {
437437
if (expectedHead.repositoryNameWithOwner !== pullRequestHead.repositoryNameWithOwner) {
438-
return false;
438+
// A remote URL can keep a stale repository slug after a rename or transfer (the host
439+
// redirects it), while the provider reports the canonical name. When neither side is
440+
// cross-repository and the owner is unchanged, that is the same repository.
441+
const sameOwner =
442+
expectedHead.ownerLogin !== null &&
443+
expectedHead.ownerLogin === pullRequestHead.ownerLogin;
444+
const sameRepositoryUnderRename =
445+
sameOwner && !headContext.isCrossRepository && pr.isCrossRepository === false;
446+
if (!sameRepositoryUnderRename) {
447+
return false;
448+
}
439449
}
440450
}
441451
if (expectedHead.ownerLogin && pullRequestHead.ownerLogin) {

0 commit comments

Comments
 (0)