Skip to content

Commit 2872482

Browse files
saphidgithub-actions[bot]claudemaria-rcks
authored
fix(vcs): look up pull request clone URLs on the pull request's host (#15000)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: SAPHID <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 7115a0b commit 2872482

6 files changed

Lines changed: 278 additions & 5 deletions

File tree

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

Lines changed: 163 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,12 @@ function createFakeSourceControlProvider(scenario: FakeProviderScenario = {}): {
490490
repository: input.repository,
491491
}).pipe(
492492
Effect.flatMap(() => {
493-
const cloneUrls = scenario.repositoryCloneUrls?.[input.repository];
493+
const repository = input.host ? `${input.host}/${input.repository}` : input.repository;
494+
const cloneUrls =
495+
scenario.repositoryCloneUrls?.[repository] ??
496+
(input.host === "github.com"
497+
? scenario.repositoryCloneUrls?.[input.repository]
498+
: undefined);
494499
return cloneUrls
495500
? Effect.succeed({ nameWithOwner: input.repository, ...cloneUrls })
496501
: Effect.fail(
@@ -3906,6 +3911,163 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
39063911
}),
39073912
);
39083913

3914+
it.effect.each([
3915+
{ mode: "local", repositoryType: "same repo" },
3916+
{ mode: "local", repositoryType: "fork" },
3917+
{ mode: "worktree", repositoryType: "same repo" },
3918+
{ mode: "worktree", repositoryType: "fork" },
3919+
] as const)(
3920+
"checks out same-named repositories on the pull request host ($mode, $repositoryType)",
3921+
({ mode, repositoryType }) =>
3922+
Effect.gen(function* () {
3923+
const isCrossRepository = repositoryType === "fork";
3924+
const repoDir = yield* makeTempDir("t3code-git-manager-");
3925+
yield* initRepo(repoDir);
3926+
// The checkout is on github.com, which also has a same-named repository,
3927+
// and the pull request is on another host. Clone lookups that fall back
3928+
// to the checkout's host find the github.com repository.
3929+
const enterpriseRemote = yield* createBareRemote();
3930+
const defaultHostRemote = yield* createBareRemote();
3931+
const host = "github.enterprise.test";
3932+
const repository = isCrossRepository ? "fork/repo" : "owner/base";
3933+
const cloneUrl = `https://${host}/${repository}.git`;
3934+
yield* runGit(repoDir, ["remote", "add", "origin", defaultHostRemote]);
3935+
yield* runGit(repoDir, ["push", "origin", "main", "main:feature/host"]);
3936+
yield* runGit(repoDir, ["push", enterpriseRemote, "main"]);
3937+
yield* runGit(repoDir, ["checkout", "-b", "feature/host"]);
3938+
NodeFS.writeFileSync(NodePath.join(repoDir, "host.txt"), "enterprise head\n");
3939+
yield* runGit(repoDir, ["add", "host.txt"]);
3940+
yield* runGit(repoDir, ["commit", "-m", "Enterprise head"]);
3941+
const expectedHead = (yield* runGit(repoDir, ["rev-parse", "HEAD"])).stdout.trim();
3942+
yield* runGit(repoDir, ["push", enterpriseRemote, "feature/host"]);
3943+
yield* runGit(repoDir, ["checkout", "main"]);
3944+
if (mode === "worktree") {
3945+
yield* runGit(repoDir, ["branch", "-D", "feature/host"]);
3946+
}
3947+
yield* configureVisibleRemoteUrlWithLocalRewrite(
3948+
repoDir,
3949+
"origin",
3950+
"https://github.com/owner/base.git",
3951+
defaultHostRemote,
3952+
);
3953+
yield* runGit(repoDir, ["config", `url.${enterpriseRemote}.insteadOf`, cloneUrl]);
3954+
const defaultCloneUrl = `https://github.com/${repository}.git`;
3955+
yield* runGit(repoDir, [
3956+
"config",
3957+
"--add",
3958+
`url.${defaultHostRemote}.insteadOf`,
3959+
defaultCloneUrl,
3960+
]);
3961+
3962+
const { manager } = yield* makeManager({
3963+
providerScenario: {
3964+
pullRequest: {
3965+
number: 42,
3966+
title: "Enterprise head",
3967+
url: `https://${host}/owner/base/pull/42`,
3968+
baseRefName: "main",
3969+
headRefName: "feature/host",
3970+
state: "open",
3971+
isCrossRepository,
3972+
headRepositoryNameWithOwner: repository,
3973+
headRepositoryOwnerLogin: isCrossRepository ? "fork" : "owner",
3974+
},
3975+
repositoryCloneUrls: {
3976+
[repository]: { url: defaultCloneUrl, sshUrl: defaultCloneUrl },
3977+
[`${host}/${repository}`]: { url: cloneUrl, sshUrl: cloneUrl },
3978+
},
3979+
},
3980+
});
3981+
const result = yield* preparePullRequestThread(manager, {
3982+
cwd: repoDir,
3983+
reference: "42",
3984+
mode,
3985+
});
3986+
const checkoutDir = result.worktreePath ?? repoDir;
3987+
const upstreamRemote = (yield* runGit(checkoutDir, [
3988+
"config",
3989+
`branch.${result.branch}.remote`,
3990+
])).stdout.trim();
3991+
const upstreamUrl = (yield* runGit(checkoutDir, [
3992+
"config",
3993+
`remote.${upstreamRemote}.url`,
3994+
])).stdout.trim();
3995+
const head = (yield* runGit(checkoutDir, ["rev-parse", "HEAD"])).stdout.trim();
3996+
expect({ upstreamUrl, head }).toEqual({ upstreamUrl: cloneUrl, head: expectedHead });
3997+
expect(NodeFS.readFileSync(NodePath.join(checkoutDir, "host.txt"), "utf8")).toBe(
3998+
"enterprise head\n",
3999+
);
4000+
}),
4001+
);
4002+
4003+
it.effect.each([
4004+
{ mode: "local", url: "malformed pull request URL" },
4005+
{ mode: "worktree", url: "malformed pull request URL" },
4006+
{ mode: "local", url: "file:///owner/base/pull/42" },
4007+
{ mode: "worktree", url: "file:///owner/base/pull/42" },
4008+
] as const)(
4009+
"checks out pull requests with hostless URLs using the default host ($mode, $url)",
4010+
({ mode, url }) =>
4011+
Effect.gen(function* () {
4012+
const repoDir = yield* makeTempDir("t3code-git-manager-");
4013+
yield* initRepo(repoDir);
4014+
const originDir = yield* createBareRemote();
4015+
const forkDir = yield* createBareRemote();
4016+
const cloneUrl = "https://github.com/fork/repo.git";
4017+
yield* runGit(repoDir, ["remote", "add", "origin", originDir]);
4018+
yield* runGit(repoDir, ["push", "origin", "main"]);
4019+
yield* runGit(repoDir, ["checkout", "-b", "feature/malformed-url"]);
4020+
NodeFS.writeFileSync(NodePath.join(repoDir, "fork.txt"), "fork head\n");
4021+
yield* runGit(repoDir, ["add", "fork.txt"]);
4022+
yield* runGit(repoDir, ["commit", "-m", "Fork head"]);
4023+
const expectedHead = (yield* runGit(repoDir, ["rev-parse", "HEAD"])).stdout.trim();
4024+
yield* runGit(repoDir, ["push", forkDir, "feature/malformed-url"]);
4025+
yield* runGit(repoDir, ["checkout", "main"]);
4026+
if (mode === "worktree") {
4027+
yield* runGit(repoDir, ["branch", "-D", "feature/malformed-url"]);
4028+
}
4029+
yield* runGit(repoDir, ["config", `url.${forkDir}.insteadOf`, cloneUrl]);
4030+
4031+
const { manager } = yield* makeManager({
4032+
providerScenario: {
4033+
pullRequest: {
4034+
number: 42,
4035+
title: "Fork head",
4036+
url,
4037+
baseRefName: "main",
4038+
headRefName: "feature/malformed-url",
4039+
state: "open",
4040+
isCrossRepository: true,
4041+
headRepositoryNameWithOwner: "fork/repo",
4042+
headRepositoryOwnerLogin: "fork",
4043+
},
4044+
repositoryCloneUrls: {
4045+
"fork/repo": { url: cloneUrl, sshUrl: cloneUrl },
4046+
},
4047+
},
4048+
});
4049+
const result = yield* preparePullRequestThread(manager, {
4050+
cwd: repoDir,
4051+
reference: "42",
4052+
mode,
4053+
});
4054+
const checkoutDir = result.worktreePath ?? repoDir;
4055+
const upstreamRemote = (yield* runGit(checkoutDir, [
4056+
"config",
4057+
`branch.${result.branch}.remote`,
4058+
])).stdout.trim();
4059+
const upstreamUrl = (yield* runGit(checkoutDir, [
4060+
"config",
4061+
`remote.${upstreamRemote}.url`,
4062+
])).stdout.trim();
4063+
const head = (yield* runGit(checkoutDir, ["rev-parse", "HEAD"])).stdout.trim();
4064+
expect({ upstreamUrl, head }).toEqual({ upstreamUrl: cloneUrl, head: expectedHead });
4065+
expect(NodeFS.readFileSync(NodePath.join(checkoutDir, "fork.txt"), "utf8")).toBe(
4066+
"fork head\n",
4067+
);
4068+
}),
4069+
);
4070+
39094071
const setUpLocalCheckoutPullRequest = Effect.fnUntraced(function* (
39104072
reportedHead: (pullHead: string) => string | undefined = (pullHead) => pullHead,
39114073
syncCheckoutWithPullHead = true,

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,11 @@ export const make = Effect.gen(function* () {
893893
}),
894894
);
895895

896+
const pullRequestHostOptions = (url: string) => {
897+
const host = URL.parse(url)?.host;
898+
return host ? { host } : {};
899+
};
900+
896901
const configurePullRequestHeadUpstreamBase = Effect.fn("configurePullRequestHeadUpstream")(
897902
function* (
898903
cwd: string,
@@ -923,6 +928,7 @@ export const make = Effect.gen(function* () {
923928
const cloneUrls = yield* (yield* sourceControlProvider(cwd)).getRepositoryCloneUrls({
924929
cwd,
925930
repository: repositoryNameWithOwner,
931+
...pullRequestHostOptions(pullRequest.url),
926932
});
927933
const originRemoteUrl = yield* gitCore.readConfigValue(cwd, "remote.origin.url");
928934
const remoteUrl = shouldPreferSshRemote(originRemoteUrl) ? cloneUrls.sshUrl : cloneUrls.url;
@@ -1036,6 +1042,7 @@ export const make = Effect.gen(function* () {
10361042
const cloneUrls = yield* (yield* sourceControlProvider(cwd)).getRepositoryCloneUrls({
10371043
cwd,
10381044
repository: repositoryNameWithOwner,
1045+
...pullRequestHostOptions(pullRequest.url),
10391046
});
10401047
const originRemoteUrl = yield* gitCore.readConfigValue(cwd, "remote.origin.url");
10411048
const remoteUrl = shouldPreferSshRemote(originRemoteUrl) ? cloneUrls.sshUrl : cloneUrls.url;

‎packages/source-control-core/src/server/SourceControlProvider.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ export class SourceControlProvider extends Context.Service<
195195
readonly cwd: string;
196196
readonly context?: SourceControlProviderContext;
197197
readonly repository: string;
198+
/** The host the repository is on. Without it, the checkout's host. */
199+
readonly host?: string;
198200
}) => Effect.Effect<SourceControlRepositoryCloneUrls, SourceControlProviderError>;
199201
readonly createRepository: (input: {
200202
readonly cwd: string;

‎packages/source-control-forgejo/src/server/ForgejoSourceControlProvider.test.ts‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,73 @@ const requestJson = (request: HttpClientRequest.HttpClientRequest): unknown => {
3333
};
3434
const encodeJsonEffect = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
3535

36+
it.effect("reads clone URLs on the given host rather than the checkout's", () => {
37+
const urls: string[] = [];
38+
const client = HttpClient.make((request) => {
39+
urls.push(request.url);
40+
return Effect.succeed(
41+
HttpClientResponse.fromWeb(
42+
request,
43+
new Response(
44+
encodeJson({
45+
full_name: "maria/project",
46+
clone_url: "https://forgejo.test/maria/project.git",
47+
ssh_url: "[email protected]:maria/project.git",
48+
}),
49+
),
50+
),
51+
);
52+
});
53+
return Effect.gen(function* () {
54+
const cli = yield* ForgejoCli.make;
55+
const provider = yield* ForgejoSourceControlProvider.make.pipe(
56+
Effect.provideService(ForgejoCli.ForgejoCli, cli),
57+
);
58+
const cloneUrls = yield* provider.getRepositoryCloneUrls({
59+
cwd: "/repo",
60+
repository: "maria/project",
61+
host: "forgejo.test",
62+
});
63+
assert.strictEqual(cloneUrls.url, "https://forgejo.test/maria/project.git");
64+
assert.deepStrictEqual(urls, ["https://forgejo.test/api/v1/repos/maria/project"]);
65+
}).pipe(
66+
Effect.provideService(HttpClient.HttpClient, client),
67+
Effect.provideService(
68+
FileSystem.FileSystem,
69+
FileSystem.makeNoop({
70+
exists: () => Effect.succeed(true),
71+
readFileString: () =>
72+
Effect.succeed(
73+
encodeJson({
74+
hosts: {
75+
"forgejo.test": { type: "Application", token: "test-token" },
76+
"code.example": { type: "Application", token: "other-token" },
77+
},
78+
}),
79+
),
80+
}),
81+
),
82+
Effect.provide(
83+
TestSourceControlHost.layer({
84+
process: {
85+
run: (input) => {
86+
if (input.command === "git") {
87+
assert.deepStrictEqual(input.args, ["remote", "-v"]);
88+
// The checkout is on another Forgejo server with a same-named repository.
89+
return Effect.succeed(
90+
processOutput("origin\thttps://code.example/maria/project.git (fetch)"),
91+
);
92+
}
93+
assert.strictEqual(input.command, "fj");
94+
assert.deepStrictEqual(input.args, ["--host", "https://forgejo.test", "whoami"]);
95+
return Effect.succeed(processOutput(""));
96+
},
97+
},
98+
}),
99+
),
100+
);
101+
});
102+
36103
const requiredChecksPullRequest = {
37104
number: 7,
38105
title: "Required checks",

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,38 @@ describe("GitHubSourceControlProvider repository resolution", () => {
289289
}).pipe(Effect.provide(layer));
290290
});
291291

292+
it.effect("reads clone URLs on the given host before the checkout's", () => {
293+
const paths: string[] = [];
294+
const { layer } = harness({
295+
remotes: remotesOutput(["origin", "https://github.com/acme/web.git"]),
296+
api: {
297+
rest: (input) =>
298+
Effect.sync(() => {
299+
paths.push(`${input.host} ${input.path}`);
300+
return restResponse({
301+
full_name: "acme/web",
302+
html_url: `https://${input.host}/acme/web`,
303+
ssh_url: `git@${input.host}:acme/web.git`,
304+
});
305+
}),
306+
},
307+
});
308+
return Effect.gen(function* () {
309+
const gh = yield* GitHubSourceControlProvider.make;
310+
const cloneUrls = yield* gh.getRepositoryCloneUrls({
311+
cwd: "/repo",
312+
repository: "acme/web",
313+
host: "GitHub.Enterprise.test",
314+
});
315+
yield* gh.getRepositoryCloneUrls({ cwd: "/repo", repository: "acme/web" });
316+
assert.strictEqual(cloneUrls.url, "https://github.enterprise.test/acme/web");
317+
assert.deepStrictEqual(paths, [
318+
"github.enterprise.test repos/acme/web",
319+
"github.com repos/acme/web",
320+
]);
321+
}).pipe(Effect.provide(layer));
322+
});
323+
292324
it.effect.each([
293325
["github", "git@github:acme/web.git"],
294326
["github.com-scale", "[email protected]:acme/web.git"],

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -996,10 +996,13 @@ export const make = Effect.gen(function* () {
996996
),
997997
getRepositoryCloneUrls: Effect.fn("GitHubSourceControlProvider.getRepositoryCloneUrls")(
998998
function* (input) {
999-
const fallbackHost = (yield* resolveRepository(input).pipe(
1000-
Effect.map((locator) => locator.host),
1001-
Effect.orElseSucceed(() => environment.GH_HOST ?? "github.com"),
1002-
)).toLowerCase();
999+
const fallbackHost = (
1000+
input.host ??
1001+
(yield* resolveRepository(input).pipe(
1002+
Effect.map((locator) => locator.host),
1003+
Effect.orElseSucceed(() => environment.GH_HOST ?? "github.com"),
1004+
))
1005+
).toLowerCase();
10031006
const bareName = input.repository.trim().replace(/\.git$/i, "");
10041007
const locator =
10051008
parseGitHubRepositorySelector(input.repository, fallbackHost) ??

0 commit comments

Comments
 (0)