Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { assert, describe, it } from "@effect/vitest";

import {
gitHubApiHostForRemote,
parseGitHubRepositorySelector,
selectGitHubBaseRepository,
} from "./gitHubRepositoryResolution.ts";

Expand Down Expand Up @@ -74,6 +75,26 @@ describe("selectGitHubBaseRepository", () => {
});
});

describe("parseGitHubRepositorySelector", () => {
it("drops a clone URL's .git suffix wherever the selector ends", () => {
for (const [selector, host, name] of [
["https://github.com/acme/web.git", "github.com", "web"],
["https://github.com/acme/web.git/", "github.com", "web"],
["https://github.com/acme/web.git?tab=readme", "github.com", "web"],
["https://github.com/acme/web.git.git", "github.com", "web.git"],
["https://github.com/acme/web.git.git/", "github.com", "web.git"],
["acme/web.git/", "github.com", "web"],
["ghe.example.com/acme/web.git/", "ghe.example.com", "web"],
] as const) {
assert.deepStrictEqual(parseGitHubRepositorySelector(selector, "github.com"), {
host,
owner: "acme",
name,
});
}
});
});

describe("gitHubApiHostForRemote", () => {
it("reads an SSH alias through github.com and a real host as itself", () => {
assert.strictEqual(gitHubApiHostForRemote("git@github:acme/web.git"), "github.com");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,21 @@ export function parseGitHubRepositorySelector(
selector: string,
defaultHost: string,
): GitHubRepositoryLocator | null {
const trimmed = selector.trim().replace(/\.git$/i, "");
// Like gh, a clone URL's `.git` comes off the path, before any trailing slash, query or fragment.
const withoutGitSuffix = (path: string) => path.replace(/\.git\/*$/i, "");
const trimmed = selector.trim();
if (/^https?:\/\//i.test(trimmed)) {
try {
const url = new URL(trimmed);
const [owner, name, ...rest] = url.pathname.split("/").filter(Boolean);
const [owner, name, ...rest] = withoutGitSuffix(url.pathname).split("/").filter(Boolean);
return owner && name && rest.length === 0
? { host: url.host.toLowerCase(), owner, name }
: null;
} catch {
return null;
}
}
const parts = trimmed.split("/").filter(Boolean);
const parts = withoutGitSuffix(trimmed).split("/").filter(Boolean);
if (parts.length === 2) return { host: defaultHost, owner: parts[0]!, name: parts[1]! };
if (parts.length === 3)
return { host: parts[0]!.toLowerCase(), owner: parts[1]!, name: parts[2]! };
Expand Down
Loading