Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2
3import {
4 gitHubApiHostForRemote,
5 resolveGitHubRepository,
6 parseGitHubRepositorySelector,
7 selectGitHubBaseRepository,
8} from "./gitHubRepositoryResolution.ts";
9
10describe("selectGitHubBaseRepository", () => {
11 const remotes = (...entries: ReadonlyArray<readonly [string, string]>) =>
12 entries
13 .flatMap(([name, url]) => [`${name}\t${url} (fetch)`, `${name}\t${url} (push)`])
14 .join("\n");
15 const select = (input: { remotes: string; resolved?: string }) =>
16 selectGitHubBaseRepository({ resolved: "", host: "github.com", ...input });
17
18 it("picks the repository gh reads without a prompt", () => {
19 assert.deepStrictEqual(select({ remotes: remotes(["fork", "git@github.com:me/web.git"]) }), {
20 owner: "me",
21 name: "web",
22 });
23 const fork = remotes(
24 ["origin", "git@github.com:me/web.git"],
25 ["upstream", "https://github.com/Acme/Web.git"],
26 );
27 assert.deepStrictEqual(select({ remotes: fork }), { owner: "acme", name: "web" });
28 assert.deepStrictEqual(select({ remotes: fork, resolved: "remote.origin.gh-resolved base" }), {
29 owner: "me",
30 name: "web",
31 });
32 assert.deepStrictEqual(
33 select({ remotes: fork, resolved: "remote.origin.gh-resolved acme/other" }),
34 { owner: "acme", name: "other" },
35 );
36 const partialClone = fork.replaceAll("(fetch)", "(fetch) [blob:none]");
37 assert.deepStrictEqual(
38 select({ remotes: partialClone, resolved: "remote.origin.gh-resolved base" }),
39 { owner: "me", name: "web" },
40 );
41 assert.deepStrictEqual(
42 select({
43 remotes: remotes(
44 ["origin", "git@github.com:me/web.git"],
45 ["Upstream", "git@github.com:acme/web.git"],
46 ),
47 }),
48 { owner: "acme", name: "web" },
49 );
50 });
51
52 it("honors a marked SSH alias origin over upstream", () => {
53 for (const url of [
54 "git@github.com-scale:me/web.git",
55 "ssh://git@github.com-scale:22/me/web.git",
56 "git@github-work:me/web.git",
57 ]) {
58 const input = {
59 remotes: remotes(["origin", url], ["upstream", "https://github.com/acme/web.git"]),
60 resolved: "remote.origin.gh-resolved base",
61 };
62 assert.deepStrictEqual(select(input), { owner: "me", name: "web" }, url);
63 assert.deepStrictEqual(
64 resolveGitHubRepository({ ...input, hostHint: undefined, defaultHost: "github.com" }),
65 { host: "github.com", locator: { host: "github.com", owner: "me", name: "web" } },
66 url,
67 );
68 }
69 });
70
71 it("does not honor a github.com mark on a different or unrecognized host", () => {
72 for (const url of [
73 "git@github.com-evil.example:me/web.git",
74 "git@github.com.evil.example:me/web.git",
75 "git@evil-github.com:me/web.git",
76 "https://github.com-scale/me/web.git",
77 "git@work:me/web.git",
78 ]) {
79 const input = {
80 remotes: remotes(["origin", url], ["upstream", "https://github.com/acme/web.git"]),
81 resolved: "remote.origin.gh-resolved base",
82 };
83 assert.strictEqual(select(input), null, url);
84 assert.deepStrictEqual(
85 resolveGitHubRepository({ ...input, hostHint: "github.com", defaultHost: "github.com" }),
86 { host: "github.com", locator: { host: "github.com", owner: "acme", name: "web" } },
87 url,
88 );
89 }
90 });
91
92 it("preserves selection behavior for real hosts with explicit ports", () => {
93 const input = {
94 remotes: remotes(["origin", "https://github.example.com:8443/me/web.git"]),
95 resolved: "remote.origin.gh-resolved acme/other",
96 };
97 assert.strictEqual(
98 selectGitHubBaseRepository({ ...input, host: "github.example.com:8443" }),
99 null,
100 );
101 assert.deepStrictEqual(
102 resolveGitHubRepository({ ...input, hostHint: undefined, defaultHost: "github.com" }),
103 {
104 host: "github.example.com:8443",
105 locator: { host: "github.example.com:8443", owner: "me", name: "web" },
106 },
107 );
108 });
109
110 it("leaves gh to choose when it might weigh the remotes differently", () => {
111 for (const input of [
112 { remotes: "" },
113 {
114 remotes: remotes(["a", "git@github.com:me/web.git"], ["b", "git@github.com:acme/web.git"]),
115 },
116 {
117 remotes: remotes(
118 ["origin", "git@github.com:me/web.git"],
119 ["mirror", "git@gitlab.com:me/web.git"],
120 ),
121 },
122 { remotes: remotes(["origin", "git@work:me/web.git"]) },
123 {
124 remotes: remotes(
125 ["origin", "git@github.com:me/web.git"],
126 ["Origin", "git@github.com:acme/web.git"],
127 ),
128 },
129 {
130 remotes: remotes(
131 ["origin", "git@github.com:me/web.git"],
132 ["upstream", "git@github.com:acme/web.git"],
133 ),
134 resolved: "remote.origin.gh-resolved base\nremote.upstream.gh-resolved base",
135 },
136 ]) {
137 assert.strictEqual(select(input), null);
138 }
139 });
140});
141
142describe("parseGitHubRepositorySelector", () => {
143 it("drops a clone URL's .git suffix wherever the selector ends", () => {
144 for (const [selector, host, name] of [
145 ["https://github.com/acme/web.git", "github.com", "web"],
146 ["https://github.com/acme/web.git/", "github.com", "web"],
147 ["https://github.com/acme/web.git?tab=readme", "github.com", "web"],
148 ["https://github.com/acme/web.git.git", "github.com", "web.git"],
149 ["https://github.com/acme/web.git.git/", "github.com", "web.git"],
150 ["acme/web.git/", "github.com", "web"],
151 ["ghe.example.com/acme/web.git/", "ghe.example.com", "web"],
152 ] as const) {
153 assert.deepStrictEqual(parseGitHubRepositorySelector(selector, "github.com"), {
154 host,
155 owner: "acme",
156 name,
157 });
158 }
159 });
160});
161
162describe("gitHubApiHostForRemote", () => {
163 it("reads an SSH alias through github.com and a real host as itself", () => {
164 assert.strictEqual(gitHubApiHostForRemote("git@github:acme/web.git"), "github.com");
165 assert.strictEqual(
166 gitHubApiHostForRemote("git@github.example.com:a/b.git"),
167 "github.example.com",
168 );
169 assert.strictEqual(gitHubApiHostForRemote("git@gitlab.com:a/b.git"), null);
170 });
171
172 it("ignores a secondary remote whose host is not a valid URL host", () => {
173 for (const url of [
174 "git@nas.08:team/repo.git",
175 "git@08:team/repo.git",
176 "git@node.256:team/repo.git",
177 "git@[2001:db8::1]:team/repo.git",
178 ]) {
179 const remotes = [
180 "origin\tgit@github.com:pingdotgg/t3code.git (fetch)",
181 `backup\t${url} (fetch)`,
182 ].join("\n");
183 assert.deepStrictEqual(
184 resolveGitHubRepository({
185 remotes,
186 resolved: "",
187 hostHint: undefined,
188 defaultHost: "github.com",
189 }),
190 { host: "github.com", locator: { host: "github.com", owner: "pingdotgg", name: "t3code" } },
191 url,
192 );
193 }
194 });
195});
196
197describe("github.com-<name> SSH aliases", () => {
198 it("reads a per-account alias through github.com", () => {
199 for (const url of [
200 "git@github.com-scale:scalable-so/scalablev2.git",
201 "git@GitHub.com-Work:acme/web.git",
202 "ssh://git@github.com-personal/acme/web.git",
203 "ssh://git@github.com-scale:22/acme/web.git",
204 ]) {
205 assert.strictEqual(gitHubApiHostForRemote(url), "github.com", url);
206 }
207 });
208
209 it("never maps a hostile or real DNS host onto github.com", () => {
210 for (const [url, host] of [
211 ["git@github.com-evil.example:acme/web.git", "github.com-evil.example"],
212 ["ssh://git@github.com-evil.example/acme/web.git", "github.com-evil.example"],
213 ["git@github.com.evil.example:acme/web.git", "github.com.evil.example"],
214 ["git@evil-github.com:acme/web.git", null],
215 ["https://github.com-scale/acme/web.git", "github.com-scale"],
216 ] as const) {
217 assert.strictEqual(gitHubApiHostForRemote(url), host, url);
218 }
219 });
220
221 it("resolves the API host and repository from an alias remote and its host hint", () => {
222 const remotes = [
223 "origin\tgit@github.com-scale:scalable-so/scalablev2.git (fetch)",
224 "origin\tgit@github.com-scale:scalable-so/scalablev2.git (push)",
225 ].join("\n");
226 const expected = {
227 host: "github.com",
228 locator: { host: "github.com", owner: "scalable-so", name: "scalablev2" },
229 };
230 for (const hostHint of ["github.com-scale", undefined]) {
231 assert.deepStrictEqual(
232 resolveGitHubRepository({ remotes, resolved: "", hostHint, defaultHost: "github.com" }),
233 expected,
234 );
235 }
236 });
237
238 it.each([false, true])("preserves an HTTPS alias-shaped host hint (upstream: %s)", (upstream) => {
239 const remotes = [
240 "origin\thttps://github.com-scale/me/web.git (fetch)",
241 ...(upstream ? ["upstream\thttps://github.com/acme/web.git (fetch)"] : []),
242 ].join("\n");
243 assert.deepStrictEqual(
244 resolveGitHubRepository({
245 remotes,
246 resolved: "",
247 hostHint: "github.com-scale",
248 defaultHost: "github.com",
249 }),
250 {
251 host: "github.com-scale",
252 locator: { host: "github.com-scale", owner: "me", name: "web" },
253 },
254 );
255 });
256
257 it("requires matching SSH evidence to normalize a dotted hint", () => {
258 for (const remotes of [
259 "",
260 "origin\tgit@github.com-work:me/web.git (fetch)",
261 "origin\tgit@github.com-scale:me/web.git (push)",
262 ]) {
263 assert.deepStrictEqual(
264 resolveGitHubRepository({
265 remotes,
266 resolved: "",
267 hostHint: "GitHub.com-Scale",
268 defaultHost: "github.com",
269 }),
270 { host: "github.com-scale", locator: null },
271 );
272 }
273 });
274
275 it("preserves a dotted hint when matching remotes use both transports", () => {
276 const entries = [
277 "origin\thttps://github.com-scale/me/web.git (fetch)",
278 "upstream\tgit@github.com-scale:acme/web.git (fetch)",
279 ];
280 for (const [remotes, hostHint, resolved] of [
281 [entries.join("\n"), "github.com-scale", "remote.origin.gh-resolved base"],
282 [entries.join("\n"), "github.com-scale", ""],
283 [entries.toReversed().join("\n"), undefined, "remote.origin.gh-resolved base"],
284 [entries.toReversed().join("\n"), undefined, ""],
285 ] as const) {
286 assert.deepStrictEqual(
287 resolveGitHubRepository({
288 remotes,
289 resolved,
290 hostHint,
291 defaultHost: "github.com",
292 }),
293 {
294 host: "github.com-scale",
295 locator: { host: "github.com-scale", owner: "me", name: "web" },
296 },
297 );
298 }
299 });
300
301 it("retains dotless hint compatibility without remote evidence", () => {
302 for (const hostHint of ["github", "github-work", "workgithub"]) {
303 assert.deepStrictEqual(
304 resolveGitHubRepository({ remotes: "", resolved: "", hostHint, defaultHost: hostHint }),
305 { host: "github.com", locator: null },
306 );
307 }
308 });
309
310 it("keeps a hostile host hint and its remote off github.com", () => {
311 const remotes = "origin\tgit@github.com-evil.example:acme/web.git (fetch)";
312 assert.deepStrictEqual(
313 resolveGitHubRepository({
314 remotes,
315 resolved: "",
316 hostHint: "github.com-evil.example",
317 defaultHost: "github.com-evil.example",
318 }),
319 {
320 host: "github.com-evil.example",
321 locator: { host: "github.com-evil.example", owner: "acme", name: "web" },
322 },
323 );
324 });
325});
326
327describe("parseGitHubRepositorySelector", () => {
328 it("reads host/owner/name only when the host is a plain hostname", () => {
329 assert.deepStrictEqual(parseGitHubRepositorySelector("GHE.corp:8443/acme/web", "github.com"), {
330 host: "ghe.corp:8443",
331 owner: "acme",
332 name: "web",
333 });
334 for (const selector of [
335 "evil.com#.ghe.com/acme/web",
336 "evil.com?.ghe.com/acme/web",
337 "token@evil.com/acme/web",
338 "evil.com\\.ghe.com/acme/web",
339 "-evil.com/acme/web",
340 ]) {
341 assert.strictEqual(parseGitHubRepositorySelector(selector, "github.com"), null, selector);
342 }
343 });
344});