Skip to content

Commit daa2022

Browse files
committed
fix(github): normalize GH_REPO hosts and ported github.com subdomains
Read a github.com subdomain with an explicit port through github.com, and give a GH_REPO owner/name the same host normalization as remote host hints.
1 parent b214216 commit daa2022

6 files changed

Lines changed: 41 additions & 3 deletions

File tree

‎packages/contracts/src/pullRequest.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
PullRequestListResult,
1111
PullRequestReviewerRequestInput,
1212
PullRequestSetFilesViewedInput,
13+
gitHubApiHostOf,
1314
pullRequestHostOf,
1415
resolvePullRequestAuthorFilter,
1516
} from "./pullRequest.ts";
@@ -104,6 +105,11 @@ describe("PullRequestListResult", () => {
104105
),
105106
).toBe("github.example.com");
106107
});
108+
109+
it("reads a github.com subdomain with a port through github.com", () => {
110+
expect(gitHubApiHostOf("work.github.com:8443")).toBe("github.com");
111+
expect(gitHubApiHostOf("github.example.com:8443")).toBe("github.example.com:8443");
112+
});
107113
/**
108114
* The RPC builds this codec at call time, so a shape it cannot lower — an open-keyed record
109115
* with an optional value, for one — fails as an interrupted request rather than as a schema

‎packages/contracts/src/pullRequest.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,7 @@ export function pullRequestHostOf(
13961396
*/
13971397
export function gitHubApiHostOf(host: string): string {
13981398
const normalized = host.toLowerCase();
1399-
return normalized.endsWith(".github.com") ? "github.com" : normalized;
1399+
return normalized.replace(/:\d+$/, "").endsWith(".github.com") ? "github.com" : normalized;
14001400
}
14011401

14021402
/**

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,33 @@ describe("GitHubSourceControlProvider repository resolution", () => {
201201
}).pipe(Effect.provide(layer));
202202
});
203203

204+
it.effect("reads a GH_REPO owner/name behind a github.com subdomain through github.com", () => {
205+
const hosts: string[] = [];
206+
const { layer } = harness({
207+
remotes: remotesOutput(["origin", "[email protected]:acme/web.git"]),
208+
api: {
209+
rest: (input) =>
210+
Effect.sync(() => {
211+
hosts.push(`${input.host} ${input.path}`);
212+
return restResponse({
213+
full_name: "chosen/repo",
214+
html_url: "https://github.com/chosen/repo",
215+
ssh_url: "[email protected]:chosen/repo.git",
216+
default_branch: "main",
217+
});
218+
}),
219+
},
220+
});
221+
return Effect.gen(function* () {
222+
const gh = yield* GitHubSourceControlProvider.make;
223+
yield* gh.getDefaultBranch({ cwd: "/repo", context: githubContext("work.github.com") });
224+
assert.deepStrictEqual(hosts, ["github.com repos/chosen/repo"]);
225+
}).pipe(
226+
Effect.provide(layer),
227+
Effect.provideService(HostProcess.Environment, { GH_REPO: "chosen/repo" }),
228+
);
229+
});
230+
204231
it.effect("names a missing repository rather than a pull request", () => {
205232
const { layer } = harness({
206233
remotes: remotesOutput(["origin", "[email protected]:acme/gone.git"]),

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
type NormalizedGitHubPullRequestRecord,
2929
} from "./gitHubPullRequests.ts";
3030
import {
31+
apiHostForHint,
3132
gitHubApiHostForRemote,
3233
parseFetchRemotes,
3334
parseGitHubRepositorySelector,
@@ -513,7 +514,7 @@ export const make = Effect.gen(function* () {
513514
}) {
514515
const envRepository = environment.GH_REPO?.trim();
515516
const hostHint = input.host ?? contextHost(input.context);
516-
const defaultHost = (hostHint ?? environment.GH_HOST ?? "github.com").toLowerCase();
517+
const defaultHost = apiHostForHint(hostHint ?? environment.GH_HOST ?? "github.com");
517518
if (envRepository) {
518519
const locator = parseGitHubRepositorySelector(envRepository, defaultHost);
519520
if (locator !== null) return locator;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ describe("gitHubApiHostForRemote", () => {
9292
"github.com",
9393
);
9494
assert.strictEqual(gitHubApiHostForRemote("[email protected]:acme/web.git"), "github.com");
95+
assert.strictEqual(
96+
gitHubApiHostForRemote("https://work.github.com:8443/acme/web.git"),
97+
"github.com",
98+
);
9599
});
96100
});
97101

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export function gitHubApiHostForRemote(remoteUrl: string): string | null {
189189
}
190190

191191
/** A caller's host hint, read the way a remote's host is: an alias is not an API host. */
192-
function apiHostForHint(host: string): string {
192+
export function apiHostForHint(host: string): string {
193193
const normalized = host.toLowerCase();
194194
return !normalized.includes(".") && normalized.includes("github")
195195
? "github.com"

0 commit comments

Comments
 (0)