@@ -42,24 +42,48 @@ export function stem(value: string): string {
4242}
4343
4444export function github ( uri : string ) : { owner : string ; repo : string } | null {
45- const https = uri . match ( / ^ h t t p s ? : \/ \/ g i t h u b \. c o m \/ ( [ ^ / ] + ) \/ ( [ ^ / ] + ?) (?: \. g i t ) ? \/ ? $ / ) ;
45+ return hosted ( uri , "github.com" ) ;
46+ }
47+
48+ export function codeberg ( uri : string ) : { owner : string ; repo : string } | null {
49+ return hosted ( uri , "codeberg.org" ) ;
50+ }
51+
52+ function hosted ( uri : string , host : string ) : { owner : string ; repo : string } | null {
53+ const escaped = host . replace ( / \. / g, "\\." ) ;
54+ const https = uri . match ( new RegExp ( `^https?:\\/\\/${ escaped } \\/([^/]+)\\/([^/]+?)(?:\\.git)?\\/?$` ) ) ;
4655 if ( https ) {
4756 return { owner : https [ 1 ] ! , repo : https [ 2 ] ! } ;
4857 }
4958
50- const ssh = uri . match ( / ^ s s h : \/ \ /g i t @ g i t h u b \. c o m \/ ( [ ^ / ] + ) \/ ( [ ^ / ] + ?) (?: \. g i t ) ? \/ ? $ / ) ;
59+ const ssh = uri . match ( new RegExp ( ` ^ssh:\\/\\ /git@${ escaped } \ \/([^/]+)\\ /([^/]+?)(?:\\ .git)?\\ /?$` ) ) ;
5160 if ( ssh ) {
5261 return { owner : ssh [ 1 ] ! , repo : ssh [ 2 ] ! } ;
5362 }
5463
55- const scp = uri . match ( / ^ g i t @ g i t h u b \. c o m : ( [ ^ / ] + ) \/ ( [ ^ / ] + ?) (?: \. g i t ) ? $ / ) ;
64+ const scp = uri . match ( new RegExp ( ` ^git@${ escaped } :([^/]+)\\ /([^/]+?)(?:\\ .git)?$` ) ) ;
5665 if ( scp ) {
5766 return { owner : scp [ 1 ] ! , repo : scp [ 2 ] ! } ;
5867 }
5968
6069 return null ;
6170}
6271
72+ async function apiDescription ( url : string , accept : string ) : Promise < string > {
73+ const response = await fetch ( url , {
74+ headers : {
75+ accept,
76+ "user-agent" : "dotllm" ,
77+ } ,
78+ } ) ;
79+ if ( ! response . ok ) return "" ;
80+
81+ const raw = await response . json ( ) ;
82+ const result = RepoShape . safeParse ( raw ) ;
83+ if ( ! result . success ) return "" ;
84+ return result . data . description ?? "" ;
85+ }
86+
6387function gitName ( uri : string ) : string {
6488 const dir = path . resolve ( uri ) ;
6589 if ( ! fs . existsSync ( dir ) ) return "" ;
@@ -78,22 +102,19 @@ function gitName(uri: string): string {
78102}
79103
80104async function remoteDescription ( uri : string ) : Promise < string > {
81- const repo = github ( uri ) ;
82- if ( ! repo ) return "" ;
105+ const gh = github ( uri ) ;
106+ if ( gh ) {
107+ const url = `https://api.github.com/repos/${ gh . owner } /${ gh . repo } ` ;
108+ return apiDescription ( url , "application/vnd.github+json" ) ;
109+ }
83110
84- const url = `https://api.github.com/repos/${ repo . owner } /${ repo . repo } ` ;
85- const response = await fetch ( url , {
86- headers : {
87- accept : "application/vnd.github+json" ,
88- "user-agent" : "dotllm" ,
89- } ,
90- } ) ;
91- if ( ! response . ok ) return "" ;
111+ const cb = codeberg ( uri ) ;
112+ if ( cb ) {
113+ const url = `https://codeberg.org/api/v1/repos/${ cb . owner } /${ cb . repo } ` ;
114+ return apiDescription ( url , "application/json" ) ;
115+ }
92116
93- const raw = await response . json ( ) ;
94- const result = RepoShape . safeParse ( raw ) ;
95- if ( ! result . success ) return "" ;
96- return result . data . description ?? "" ;
117+ return "" ;
97118}
98119
99120async function prefill ( uri : string ) : Promise < { name : string ; description : string } > {
0 commit comments