@@ -59,36 +59,67 @@ export interface PartitionModulesResult<TModule extends RepositoryModuleLike> {
5959 processedCount : number ;
6060}
6161
62- export function getRepositoryType ( url : string ) : RepositoryType {
63- if ( url . includes ( "github.com" ) ) {
64- return "github" ;
62+ const REPOSITORY_HOSTS : ReadonlyMap < string , Exclude < RepositoryType , "unknown" > > = new Map ( [
63+ [ "github.com" , "github" ] ,
64+ [ "www.github.com" , "github" ] ,
65+ [ "gitlab.com" , "gitlab" ] ,
66+ [ "www.gitlab.com" , "gitlab" ] ,
67+ [ "bitbucket.org" , "bitbucket" ] ,
68+ [ "www.bitbucket.org" , "bitbucket" ] ,
69+ [ "codeberg.org" , "codeberg" ] ,
70+ [ "www.codeberg.org" , "codeberg" ]
71+ ] ) ;
72+
73+ function normalizeRepositoryUrl ( url : string ) : string {
74+ return url . startsWith ( "git+https://" ) || url . startsWith ( "git+http://" )
75+ ? url . slice ( 4 )
76+ : url ;
77+ }
78+
79+ function parseRepositoryUrl ( url : string ) : URL | null {
80+ try {
81+ return new URL ( normalizeRepositoryUrl ( url ) ) ;
6582 }
66- if ( url . includes ( "gitlab.com" ) ) {
67- return "gitlab" ;
83+ catch {
84+ return null ;
6885 }
69- if ( url . includes ( "bitbucket.org" ) ) {
70- return "bitbucket" ;
86+ }
87+
88+ function getRepositoryUrlParts ( url : string ) : { pathParts : string [ ] ; type : Exclude < RepositoryType , "unknown" > } | null {
89+ const parsedUrl = parseRepositoryUrl ( url ) ;
90+ if ( ! parsedUrl ) {
91+ return null ;
7192 }
72- if ( url . includes ( "codeberg.org" ) ) {
73- return "codeberg" ;
93+
94+ const type = REPOSITORY_HOSTS . get ( parsedUrl . hostname . toLowerCase ( ) ) ;
95+ if ( ! type ) {
96+ return null ;
7497 }
75- return "unknown" ;
98+
99+ const rawPathParts = parsedUrl . pathname . split ( "/" ) . filter ( Boolean ) ;
100+ const pathParts = rawPathParts . map ( ( part , index ) =>
101+ index === rawPathParts . length - 1 ? part . replace ( / \. g i t $ / u, "" ) : part
102+ ) ;
103+
104+ return { type, pathParts } ;
105+ }
106+
107+ export function getRepositoryType ( url : string ) : RepositoryType {
108+ return getRepositoryUrlParts ( url ) ?. type ?? "unknown" ;
76109}
77110
78111export function getRepositoryId ( url : string ) : string | null {
79- const urlParts = url . split ( "/" ) ;
80- const hostIndex = urlParts . findIndex ( part =>
81- part . includes ( "github.com" )
82- || part . includes ( "gitlab.com" )
83- || part . includes ( "bitbucket.org" )
84- || part . includes ( "codeberg.org" ) ) ;
85-
86- if ( hostIndex !== - 1 && urlParts . length > hostIndex + 2 ) {
87- return `${ urlParts [ hostIndex + 1 ] } /${ urlParts [ hostIndex + 2 ] } ` ;
112+ const repositoryUrlParts = getRepositoryUrlParts ( url ) ;
113+ if ( repositoryUrlParts && repositoryUrlParts . pathParts . length >= 2 ) {
114+ return `${ repositoryUrlParts . pathParts [ 0 ] } /${ repositoryUrlParts . pathParts [ 1 ] } ` ;
88115 }
89116 return null ;
90117}
91118
119+ export function isRepositoryType ( url : string , repositoryType : Exclude < RepositoryType , "unknown" > ) : boolean {
120+ return getRepositoryUrlParts ( url ) ?. type === repositoryType ;
121+ }
122+
92123export function sortModuleListByLastUpdate < TModule extends RepositoryModuleLike > ( previousData : PreviousRepositoryData , moduleList : TModule [ ] ) : void {
93124 moduleList . sort ( ( a , b ) => {
94125 const lastUpdateA = previousData . repositories ?. find ( repo => repo . id === a . id ) ?. gitHubDataLastUpdate ;
0 commit comments