Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

gitHubRepositoryResolution.test.ts

287 lines9.5 KB

1import { assert, describe, it } from "@effect/vitest";
2​
3import {
4 gitHubApiHostForRemote,
5 resolveGitHubRepository,
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("honors a marked SSH alias origin over upstream", () => {
52 for (const url of [
53 "git@github.com-scale:me/web.git",
54 "ssh://git@github.com-scale:22/me/web.git",
55 "git@github-work:me/web.git",
56 ]) {
57 const input = {
58 remotes: remotes(["origin", url], ["upstream", "https://github.com/acme/web.git"]),
59 resolved: "remote.origin.gh-resolved base",
60 };
61 assert.deepStrictEqual(select(input), { owner: "me", name: "web" }, url);
62 assert.deepStrictEqual(
63 resolveGitHubRepository({ ...input, hostHint: undefined, defaultHost: "github.com" }),
64 { host: "github.com", locator: { host: "github.com", owner: "me", name: "web" } },
65 url,
66 );
67 }
68 });
69​
70 it("does not honor a github.com mark on a different or unrecognized host", () => {
71 for (const url of [
72 "git@github.com-evil.example:me/web.git",
73 "git@github.com.evil.example:me/web.git",
74 "git@evil-github.com:me/web.git",
75 "https://github.com-scale/me/web.git",
76 "git@work:me/web.git",
77 ]) {
78 const input = {
79 remotes: remotes(["origin", url], ["upstream", "https://github.com/acme/web.git"]),
80 resolved: "remote.origin.gh-resolved base",
81 };
82 assert.strictEqual(select(input), null, url);
83 assert.deepStrictEqual(
84 resolveGitHubRepository({ ...input, hostHint: "github.com", defaultHost: "github.com" }),
85 { host: "github.com", locator: { host: "github.com", owner: "acme", name: "web" } },
86 url,
87 );
88 }
89 });
90​
91 it("preserves selection behavior for real hosts with explicit ports", () => {
92 const input = {
93 remotes: remotes(["origin", "https://github.example.com:8443/me/web.git"]),
94 resolved: "remote.origin.gh-resolved acme/other",
95 };
96 assert.strictEqual(
97 selectGitHubBaseRepository({ ...input, host: "github.example.com:8443" }),
98 null,
99 );
100 assert.deepStrictEqual(
101 resolveGitHubRepository({ ...input, hostHint: undefined, defaultHost: "github.com" }),
102 {
103 host: "github.example.com:8443",
104 locator: { host: "github.example.com:8443", owner: "me", name: "web" },
105 },
106 );
107 });
108​
109 it("leaves gh to choose when it might weigh the remotes differently", () => {
110 for (const input of [
111 { remotes: "" },
112 {
113 remotes: remotes(["a", "git@github.com:me/web.git"], ["b", "git@github.com:acme/web.git"]),
114 },
115 {
116 remotes: remotes(
117 ["origin", "git@github.com:me/web.git"],
118 ["mirror", "git@gitlab.com:me/web.git"],
119 ),
120 },
121 { remotes: remotes(["origin", "git@work:me/web.git"]) },
122 {
123 remotes: remotes(
124 ["origin", "git@github.com:me/web.git"],
125 ["Origin", "git@github.com:acme/web.git"],
126 ),
127 },
128 {
129 remotes: remotes(
130 ["origin", "git@github.com:me/web.git"],
131 ["upstream", "git@github.com:acme/web.git"],
132 ),
133 resolved: "remote.origin.gh-resolved base\nremote.upstream.gh-resolved base",
134 },
135 ]) {
136 assert.strictEqual(select(input), null);
137 }
138 });
139});
140​
141describe("gitHubApiHostForRemote", () => {
142 it("reads an SSH alias through github.com and a real host as itself", () => {
143 assert.strictEqual(gitHubApiHostForRemote("git@github:acme/web.git"), "github.com");
144 assert.strictEqual(
145 gitHubApiHostForRemote("git@github.example.com:a/b.git"),
146 "github.example.com",
147 );
148 assert.strictEqual(gitHubApiHostForRemote("git@gitlab.com:a/b.git"), null);
149 });
150});
151​
152describe("github.com-<name> SSH aliases", () => {
153 it("reads a per-account alias through github.com", () => {
154 for (const url of [
155 "git@github.com-scale:owner/repo.git",
156 "git@GitHub.com-Work:acme/web.git",
157 "ssh://git@github.com-personal/acme/web.git",
158 "ssh://git@github.com-scale:22/acme/web.git",
159 ]) {
160 assert.strictEqual(gitHubApiHostForRemote(url), "github.com", url);
161 }
162 });
163​
164 it("never maps a hostile or real DNS host onto github.com", () => {
165 for (const [url, host] of [
166 ["git@github.com-evil.example:acme/web.git", "github.com-evil.example"],
167 ["ssh://git@github.com-evil.example/acme/web.git", "github.com-evil.example"],
168 ["git@github.com.evil.example:acme/web.git", "github.com.evil.example"],
169 ["git@evil-github.com:acme/web.git", null],
170 ["https://github.com-scale/acme/web.git", "github.com-scale"],
171 ] as const) {
172 assert.strictEqual(gitHubApiHostForRemote(url), host, url);
173 }
174 });
175​
176 it("resolves the API host and repository from an alias remote and its host hint", () => {
177 const remotes = [
178 "origin\tgit@github.com-scale:owner/repo.git (fetch)",
179 "origin\tgit@github.com-scale:owner/repo.git (push)",
180 ].join("\n");
181 const expected = {
182 host: "github.com",
183 locator: { host: "github.com", owner: "owner", name: "repo" },
184 };
185 assert.deepStrictEqual(
186 resolveGitHubRepository({
187 remotes,
188 resolved: "",
189 hostHint: "github.com-scale",
190 defaultHost: "github.com-scale",
191 }),
192 expected,
193 );
194 assert.deepStrictEqual(
195 resolveGitHubRepository({
196 remotes,
197 resolved: "",
198 hostHint: undefined,
199 defaultHost: "github.com",
200 }),
201 expected,
202 );
203 });
204​
205 it.each([false, true])("preserves an HTTPS alias-shaped host hint (upstream: %s)", (upstream) => {
206 const remotes = [
207 "origin\thttps://github.com-scale/me/web.git (fetch)",
208 ...(upstream ? ["upstream\thttps://github.com/acme/web.git (fetch)"] : []),
209 ].join("\n");
210 assert.deepStrictEqual(
211 resolveGitHubRepository({
212 remotes,
213 resolved: "",
214 hostHint: "github.com-scale",
215 defaultHost: "github.com",
216 }),
217 {
218 host: "github.com-scale",
219 locator: { host: "github.com-scale", owner: "me", name: "web" },
220 },
221 );
222 });
223​
224 it("requires matching SSH evidence to normalize a dotted hint", () => {
225 for (const remotes of [
226 "",
227 "origin\tgit@github.com-work:me/web.git (fetch)",
228 "origin\tgit@github.com-scale:me/web.git (push)",
229 ]) {
230 assert.deepStrictEqual(
231 resolveGitHubRepository({
232 remotes,
233 resolved: "",
234 hostHint: "GitHub.com-Scale",
235 defaultHost: "github.com",
236 }),
237 { host: "github.com-scale", locator: null },
238 );
239 }
240 });
241​
242 it("preserves a dotted hint when matching remotes use both transports", () => {
243 const entries = [
244 "origin\tgit@github.com-scale:me/web.git (fetch)",
245 "mirror\thttps://github.com-scale/me/web.git (fetch)",
246 ];
247 for (const remotes of [entries.join("\n"), entries.toReversed().join("\n")]) {
248 assert.deepStrictEqual(
249 resolveGitHubRepository({
250 remotes,
251 resolved: "",
252 hostHint: "github.com-scale",
253 defaultHost: "github.com",
254 }),
255 {
256 host: "github.com-scale",
257 locator: { host: "github.com-scale", owner: "me", name: "web" },
258 },
259 );
260 }
261 });
262​
263 it("retains dotless hint compatibility without remote evidence", () => {
264 for (const hostHint of ["github", "github-work", "workgithub"]) {
265 assert.deepStrictEqual(
266 resolveGitHubRepository({ remotes: "", resolved: "", hostHint, defaultHost: hostHint }),
267 { host: "github.com", locator: null },
268 );
269 }
270 });
271​
272 it("keeps a hostile host hint and its remote off github.com", () => {
273 const remotes = "origin\tgit@github.com-evil.example:acme/web.git (fetch)";
274 assert.deepStrictEqual(
275 resolveGitHubRepository({
276 remotes,
277 resolved: "",
278 hostHint: "github.com-evil.example",
279 defaultHost: "github.com-evil.example",
280 }),
281 {
282 host: "github.com-evil.example",
283 locator: { host: "github.com-evil.example", owner: "acme", name: "web" },
284 },
285 );
286 });
287});