Skip to content

Commit 0f44341

Browse files
saphidclaude
andcommitted
fix(source-control): PR checkout picks the fork protocol from the fetched remote URL
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a6374de commit 0f44341

4 files changed

Lines changed: 74 additions & 2 deletions

File tree

‎packages/source-control-bitbucket/src/server/BitbucketApi.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,37 @@ it.effect.each([false, true])("honors PR URL repositories, context=%s", (withCon
253253
}).pipe(Effect.provide(layer));
254254
});
255255

256+
it.effect("adds a fork remote with the protocol of the URL origin fetches from", () => {
257+
// `readConfigValue` answers `remote.origin.url` with an SSH second URL.
258+
const { git, layer } = makeLayer({
259+
remoteUrl: "https://bitbucket.org/pingdotgg/t3code.git",
260+
response: (request) =>
261+
Response.json(
262+
request.url.endsWith("/pullrequests/42")
263+
? bitbucketPullRequest
264+
: {
265+
...repositoryJson,
266+
full_name: "octocat/t3code",
267+
links: {
268+
clone: [
269+
{ name: "https", href: "https://bitbucket.org/octocat/t3code.git" },
270+
{ name: "ssh", href: "[email protected]:octocat/t3code.git" },
271+
],
272+
},
273+
},
274+
),
275+
});
276+
return Effect.gen(function* () {
277+
const bitbucket = yield* BitbucketApi.BitbucketApi;
278+
yield* bitbucket.checkoutPullRequest({ cwd: "/repo", reference: "#42" });
279+
assert.deepStrictEqual(git.ensureRemote.mock.calls[0]?.[0], {
280+
cwd: "/repo",
281+
preferredName: "octocat",
282+
url: "https://bitbucket.org/octocat/t3code.git",
283+
});
284+
}).pipe(Effect.provide(layer));
285+
});
286+
256287
it.effect.each([false, true])(
257288
"reuses matching checkout remotes for numeric references, context=%s",
258289
(withContext) => {

‎packages/source-control-bitbucket/src/server/BitbucketApi.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,12 @@ export const make = Effect.gen(function* () {
915915
repository: input.sourceRepositoryName,
916916
...(input.context ? { context: input.context } : {}),
917917
}).pipe(Effect.map(normalizeRepositoryCloneUrls));
918-
const originRemoteUrl = yield* readConfigValueNullable(input.cwd, "remote.origin.url");
918+
// Git fetches from a remote's first URL; `config --get` returns the last of several.
919+
const originRemoteUrl = yield* git.remotes(input.cwd).pipe(
920+
Effect.flatten,
921+
Effect.map(({ remotes }) => remotes.find((remote) => remote.name === "origin")?.url ?? null),
922+
Effect.orElseSucceed(() => null),
923+
);
919924
return yield* git.ensureRemote({
920925
cwd: input.cwd,
921926
preferredName: input.isCrossRepository

‎packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,40 @@ describe("GitHubSourceControlProvider.checkoutChangeRequest", () => {
651651
}).pipe(Effect.provide(layer));
652652
});
653653

654+
it.effect("adds a fork remote with the protocol of the URL origin fetches from", () => {
655+
// The harness's `remote.origin.url` config read answers with an SSH second URL.
656+
const { layer, git } = harness({
657+
remotes: [
658+
"origin\thttps://github.com/acme/web.git (fetch)",
659+
"origin\thttps://github.com/acme/web.git (push)",
660+
"origin\[email protected]:acme/web.git (push)",
661+
].join("\n"),
662+
graphql: () => ({ pullRequest: node(7, "feature", "someone") }),
663+
api: {
664+
rest: (input) => {
665+
const fullName = input.path.slice("repos/".length);
666+
return Effect.succeed(
667+
restResponse({
668+
full_name: fullName,
669+
html_url: `https://github.com/${fullName}`,
670+
ssh_url: `[email protected]:${fullName}.git`,
671+
default_branch: "main",
672+
}),
673+
);
674+
},
675+
},
676+
});
677+
return Effect.gen(function* () {
678+
const gh = yield* GitHubSourceControlProvider.make;
679+
yield* gh.checkoutChangeRequest({ cwd: "/repo", reference: "7" });
680+
assert.deepStrictEqual(git.find(([name]) => name === "ensureRemote")?.[1], {
681+
cwd: "/repo",
682+
preferredName: "someone",
683+
url: "https://github.com/someone/web",
684+
});
685+
}).pipe(Effect.provide(layer));
686+
});
687+
654688
it.effect("keeps git's own output out of what a failed checkout reports", () => {
655689
const { layer } = harness({
656690
remotes: remotesOutput(["origin", "[email protected]:acme/web.git"]),

‎packages/source-control-github/src/server/GitHubSourceControlProvider.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,9 @@ export const make = Effect.gen(function* () {
724724
if (known !== null) return known;
725725
const [owner, name] = headNameWithOwner.split("/");
726726
const fork = yield* readRepository({ host: base.host, owner: owner!, name: name! });
727-
const originUrl = yield* git.readConfigValue(input.cwd, "remote.origin.url");
727+
// Git fetches from a remote's first URL; `config --get` returns the last of several.
728+
const originUrl =
729+
parseFetchRemotes(remotes).find((remote) => remote.name === "origin")?.url ?? null;
728730
return yield* git.ensureRemote({
729731
cwd: input.cwd,
730732
preferredName: headOwner ?? "fork",

0 commit comments

Comments
 (0)