Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2
3import {
4 gitHubApiHostForRemote,
5 parseGitHubRepositorySelector,
6 selectGitHubBaseRepository,
7} from "./gitHubRepositoryResolution.ts";
8
9describe("selectGitHubBaseRepository", () => {
10 const remotes = (...entries: ReadonlyArray<readonly [string, string]>) =>
11 entries
12 .flatMap(([name, url]) => [`${name}\t${url} (fetch)`, `${name}\t${url} (push)`])
13 .join("\n");
14 const select = (input: { remotes: string; resolved?: string }) =>
15 selectGitHubBaseRepository({ resolved: "", host: "github.com", ...input });
16
17 it("picks the repository gh reads without a prompt", () => {
18 assert.deepStrictEqual(select({ remotes: remotes(["fork", "git@github.com:me/web.git"]) }), {
19 owner: "me",
20 name: "web",
21 });
22 const fork = remotes(
23 ["origin", "git@github.com:me/web.git"],
24 ["upstream", "https://github.com/Acme/Web.git"],
25 );
26 assert.deepStrictEqual(select({ remotes: fork }), { owner: "acme", name: "web" });
27 assert.deepStrictEqual(select({ remotes: fork, resolved: "remote.origin.gh-resolved base" }), {
28 owner: "me",
29 name: "web",
30 });
31 assert.deepStrictEqual(
32 select({ remotes: fork, resolved: "remote.origin.gh-resolved acme/other" }),
33 { owner: "acme", name: "other" },
34 );
35 const partialClone = fork.replaceAll("(fetch)", "(fetch) [blob:none]");
36 assert.deepStrictEqual(
37 select({ remotes: partialClone, resolved: "remote.origin.gh-resolved base" }),
38 { owner: "me", name: "web" },
39 );
40 assert.deepStrictEqual(
41 select({
42 remotes: remotes(
43 ["origin", "git@github.com:me/web.git"],
44 ["Upstream", "git@github.com:acme/web.git"],
45 ),
46 }),
47 { owner: "acme", name: "web" },
48 );
49 });
50
51 it("leaves gh to choose when it might weigh the remotes differently", () => {
52 for (const input of [
53 { remotes: "" },
54 {
55 remotes: remotes(["a", "git@github.com:me/web.git"], ["b", "git@github.com:acme/web.git"]),
56 },
57 {
58 remotes: remotes(
59 ["origin", "git@github.com:me/web.git"],
60 ["mirror", "git@gitlab.com:me/web.git"],
61 ),
62 },
63 { remotes: remotes(["origin", "git@github-work:me/web.git"]) },
64 {
65 remotes: remotes(
66 ["origin", "git@github.com:me/web.git"],
67 ["Origin", "git@github.com:acme/web.git"],
68 ),
69 },
70 {
71 remotes: remotes(
72 ["origin", "git@github.com:me/web.git"],
73 ["upstream", "git@github.com:acme/web.git"],
74 ),
75 resolved: "remote.origin.gh-resolved base\nremote.upstream.gh-resolved base",
76 },
77 ]) {
78 assert.strictEqual(select(input), null);
79 }
80 });
81});
82
83describe("gitHubApiHostForRemote", () => {
84 it("reads an SSH alias through github.com and a real host as itself", () => {
85 assert.strictEqual(gitHubApiHostForRemote("git@github:acme/web.git"), "github.com");
86 assert.strictEqual(
87 gitHubApiHostForRemote("git@github.example.com:a/b.git"),
88 "github.example.com",
89 );
90 assert.strictEqual(gitHubApiHostForRemote("git@gitlab.com:a/b.git"), null);
91 });
92});
93
94describe("parseGitHubRepositorySelector", () => {
95 it("reads host/owner/name only when the host is a plain hostname", () => {
96 assert.deepStrictEqual(parseGitHubRepositorySelector("GHE.corp:8443/acme/web", "github.com"), {
97 host: "ghe.corp:8443",
98 owner: "acme",
99 name: "web",
100 });
101 for (const selector of [
102 "evil.com#.ghe.com/acme/web",
103 "evil.com?.ghe.com/acme/web",
104 "token@evil.com/acme/web",
105 "evil.com\\.ghe.com/acme/web",
106 "-evil.com/acme/web",
107 ]) {
108 assert.strictEqual(parseGitHubRepositorySelector(selector, "github.com"), null, selector);
109 }
110 });
111});