@@ -2,6 +2,7 @@ import { assert, describe, it } from "@effect/vitest";
22
33import {
44 gitHubApiHostForRemote ,
5+ resolveGitHubRepository ,
56 parseGitHubRepositorySelector ,
67 selectGitHubBaseRepository ,
78} from "./gitHubRepositoryResolution.ts" ;
@@ -48,6 +49,64 @@ describe("selectGitHubBaseRepository", () => {
4849 ) ;
4950 } ) ;
5051
52+ it ( "honors a marked SSH alias origin over upstream" , ( ) => {
53+ for ( const url of [
54+ 55+ "ssh://[email protected] :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+ 74+ 75+ 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+
51110 it ( "leaves gh to choose when it might weigh the remotes differently" , ( ) => {
52111 for ( const input of [
53112 { remotes : "" } ,
@@ -60,7 +119,7 @@ describe("selectGitHubBaseRepository", () => {
60119 [ "mirror" , "[email protected] :me/web.git" ] , 61120 ) ,
62121 } ,
63- { remotes : remotes ( [ "origin" , "git@github- work:me/web.git" ] ) } ,
122+ { remotes : remotes ( [ "origin" , "git@work:me/web.git" ] ) } ,
64123 {
65124 remotes : remotes (
66125 [ "origin" , "[email protected] :me/web.git" ] , @@ -111,6 +170,136 @@ describe("gitHubApiHostForRemote", () => {
111170 } ) ;
112171} ) ;
113172
173+ describe ( "github.com-<name> SSH aliases" , ( ) => {
174+ it ( "reads a per-account alias through github.com" , ( ) => {
175+ for ( const url of [
176+ "[email protected] :scalable-so/scalablev2.git" , 177+ 178+ "ssh://[email protected] /acme/web.git" , 179+ "ssh://[email protected] :22/acme/web.git" , 180+ ] ) {
181+ assert . strictEqual ( gitHubApiHostForRemote ( url ) , "github.com" , url ) ;
182+ }
183+ } ) ;
184+
185+ it ( "never maps a hostile or real DNS host onto github.com" , ( ) => {
186+ for ( const [ url , host ] of [
187+ [ "[email protected] :acme/web.git" , "github.com-evil.example" ] , 188+ [ "ssh://[email protected] /acme/web.git" , "github.com-evil.example" ] , 189+ [ "[email protected] :acme/web.git" , "github.com.evil.example" ] , 190+ [ "[email protected] :acme/web.git" , null ] , 191+ [ "https://github.com-scale/acme/web.git" , "github.com-scale" ] ,
192+ ] as const ) {
193+ assert . strictEqual ( gitHubApiHostForRemote ( url ) , host , url ) ;
194+ }
195+ } ) ;
196+
197+ it ( "resolves the API host and repository from an alias remote and its host hint" , ( ) => {
198+ const remotes = [
199+ "origin\[email protected] :scalable-so/scalablev2.git (fetch)" , 200+ "origin\[email protected] :scalable-so/scalablev2.git (push)" , 201+ ] . join ( "\n" ) ;
202+ const expected = {
203+ host : "github.com" ,
204+ locator : { host : "github.com" , owner : "scalable-so" , name : "scalablev2" } ,
205+ } ;
206+ for ( const hostHint of [ "github.com-scale" , undefined ] ) {
207+ assert . deepStrictEqual (
208+ resolveGitHubRepository ( { remotes, resolved : "" , hostHint, defaultHost : "github.com" } ) ,
209+ expected ,
210+ ) ;
211+ }
212+ } ) ;
213+
214+ it . each ( [ false , true ] ) ( "preserves an HTTPS alias-shaped host hint (upstream: %s)" , ( upstream ) => {
215+ const remotes = [
216+ "origin\thttps://github.com-scale/me/web.git (fetch)" ,
217+ ...( upstream ? [ "upstream\thttps://github.com/acme/web.git (fetch)" ] : [ ] ) ,
218+ ] . join ( "\n" ) ;
219+ assert . deepStrictEqual (
220+ resolveGitHubRepository ( {
221+ remotes,
222+ resolved : "" ,
223+ hostHint : "github.com-scale" ,
224+ defaultHost : "github.com" ,
225+ } ) ,
226+ {
227+ host : "github.com-scale" ,
228+ locator : { host : "github.com-scale" , owner : "me" , name : "web" } ,
229+ } ,
230+ ) ;
231+ } ) ;
232+
233+ it ( "requires matching SSH evidence to normalize a dotted hint" , ( ) => {
234+ for ( const remotes of [
235+ "" ,
236+ "origin\[email protected] :me/web.git (fetch)" , 237+ "origin\[email protected] :me/web.git (push)" , 238+ ] ) {
239+ assert . deepStrictEqual (
240+ resolveGitHubRepository ( {
241+ remotes,
242+ resolved : "" ,
243+ hostHint : "GitHub.com-Scale" ,
244+ defaultHost : "github.com" ,
245+ } ) ,
246+ { host : "github.com-scale" , locator : null } ,
247+ ) ;
248+ }
249+ } ) ;
250+
251+ it ( "preserves a dotted hint when matching remotes use both transports" , ( ) => {
252+ const entries = [
253+ "origin\thttps://github.com-scale/me/web.git (fetch)" ,
254+ "upstream\[email protected] :acme/web.git (fetch)" , 255+ ] ;
256+ for ( const [ remotes , hostHint , resolved ] of [
257+ [ entries . join ( "\n" ) , "github.com-scale" , "remote.origin.gh-resolved base" ] ,
258+ [ entries . join ( "\n" ) , "github.com-scale" , "" ] ,
259+ [ entries . toReversed ( ) . join ( "\n" ) , undefined , "remote.origin.gh-resolved base" ] ,
260+ [ entries . toReversed ( ) . join ( "\n" ) , undefined , "" ] ,
261+ ] as const ) {
262+ assert . deepStrictEqual (
263+ resolveGitHubRepository ( {
264+ remotes,
265+ resolved,
266+ hostHint,
267+ defaultHost : "github.com" ,
268+ } ) ,
269+ {
270+ host : "github.com-scale" ,
271+ locator : { host : "github.com-scale" , owner : "me" , name : "web" } ,
272+ } ,
273+ ) ;
274+ }
275+ } ) ;
276+
277+ it ( "retains dotless hint compatibility without remote evidence" , ( ) => {
278+ for ( const hostHint of [ "github" , "github-work" , "workgithub" ] ) {
279+ assert . deepStrictEqual (
280+ resolveGitHubRepository ( { remotes : "" , resolved : "" , hostHint, defaultHost : hostHint } ) ,
281+ { host : "github.com" , locator : null } ,
282+ ) ;
283+ }
284+ } ) ;
285+
286+ it ( "keeps a hostile host hint and its remote off github.com" , ( ) => {
287+ const remotes = "origin\[email protected] :acme/web.git (fetch)" ; 288+ assert . deepStrictEqual (
289+ resolveGitHubRepository ( {
290+ remotes,
291+ resolved : "" ,
292+ hostHint : "github.com-evil.example" ,
293+ defaultHost : "github.com-evil.example" ,
294+ } ) ,
295+ {
296+ host : "github.com-evil.example" ,
297+ locator : { host : "github.com-evil.example" , owner : "acme" , name : "web" } ,
298+ } ,
299+ ) ;
300+ } ) ;
301+ } ) ;
302+
114303describe ( "parseGitHubRepositorySelector" , ( ) => {
115304 it ( "reads host/owner/name only when the host is a plain hostname" , ( ) => {
116305 assert . deepStrictEqual ( parseGitHubRepositorySelector ( "GHE.corp:8443/acme/web" , "github.com" ) , {
0 commit comments