2022-10-11 18:00:52 +02:00
import upath from 'upath' ;
2022-03-03 10:35:26 +01:00
import { logger } from '../../../logger' ;
2022-02-13 22:13:13 +03:00
import { DockerDatasource } from '../../datasource/docker' ;
2022-01-20 10:31:16 +01:00
import type { PackageDependency } from '../types' ;
2024-04-20 13:16:24 +02:00
import { removeOCIPrefix } from './oci' ;
2022-01-24 17:15:23 +01:00
import type { ChartDefinition , Repository } from './types' ;
2022-01-20 10:31:16 +01:00
export function parseRepository (
depName : string ,
2023-11-07 12:50:29 -03:00
repositoryURL : string ,
2022-01-20 10:31:16 +01:00
) : PackageDependency {
const res : PackageDependency = { } ;
try {
const url = new URL ( repositoryURL ) ;
switch ( url . protocol ) {
case 'oci:' :
2022-02-13 22:13:13 +03:00
res . datasource = DockerDatasource . id ;
2024-04-20 13:16:24 +02:00
res . packageName = ` ${ removeOCIPrefix ( repositoryURL ) } / ${ depName } ` ;
2024-01-27 06:26:07 +00:00
// https://github.com/helm/helm/issues/10312
// https://github.com/helm/helm/issues/10678
res . pinDigests = false ;
2022-01-20 10:31:16 +01:00
break ;
case 'file:' :
2022-01-21 10:59:36 +03:00
res . skipReason = 'local-dependency' ;
2022-01-20 10:31:16 +01:00
break ;
default :
res . registryUrls = [ repositoryURL ] ;
}
} catch ( err ) {
logger . debug ( { err } , 'Error parsing url' ) ;
2022-01-21 10:59:36 +03:00
res . skipReason = 'invalid-url' ;
2022-01-20 10:31:16 +01:00
}
return res ;
}
/ * *
* Resolves alias in repository string .
*
* @param repository to be resolved string
2022-06-10 10:44:49 +05:30
* @param registryAliases Records containing registryAliases as key and to be resolved URLs as values
2022-01-20 10:31:16 +01:00
*
2022-06-10 10:44:49 +05:30
* @returns resolved alias . If repository does not contain an alias the repository string will be returned . Should it contain an alias which can not be resolved using ` registryAliases ` , null will be returned
2022-01-20 10:31:16 +01:00
* /
export function resolveAlias (
repository : string ,
2023-11-07 12:50:29 -03:00
registryAliases : Record < string , string > ,
2022-01-20 10:31:16 +01:00
) : string | null {
2022-01-31 22:34:31 +01:00
if ( ! isAlias ( repository ) ) {
2022-01-20 10:31:16 +01:00
return repository ;
}
2025-02-20 12:31:13 +01:00
const repoWithPrefixRemoved = repository . slice (
repository . startsWith ( '@' ) ? 1 : 6 ,
) ;
2022-06-10 10:44:49 +05:30
const alias = registryAliases [ repoWithPrefixRemoved ] ;
2022-01-20 10:31:16 +01:00
if ( alias ) {
return alias ;
}
return null ;
}
2022-01-24 17:15:23 +01:00
export function getRepositories ( definitions : ChartDefinition [ ] ) : Repository [ ] {
const repositoryList = definitions
. flatMap ( ( value ) = > value . dependencies )
2022-02-17 08:53:10 +01:00
. filter ( ( dependency ) = > dependency . repository ) // only keep non-local references --> if no repository is defined the chart will be searched in charts/<name>
2022-06-10 10:44:49 +05:30
. filter ( ( dependency ) = > ! isAlias ( dependency . repository ) ) // do not add registryAliases
2022-05-02 09:49:17 +02:00
. filter ( ( dependency ) = > ! dependency . repository . startsWith ( 'file:' ) ) // skip repositories which are locally referenced
2022-01-24 17:15:23 +01:00
. map ( ( dependency ) = > {
// remove additional keys to prevent interference at deduplication
return {
name : dependency.name ,
repository : dependency.repository ,
} ;
} ) ;
const dedup = new Set ( ) ;
return repositoryList . filter ( ( el ) = > {
const duplicate = dedup . has ( el . repository ) ;
dedup . add ( el . repository ) ;
return ! duplicate ;
} ) ;
}
2022-02-17 08:53:10 +01:00
export function isAlias ( repository : string ) : boolean {
if ( ! repository ) {
return false ;
}
2022-01-31 22:34:31 +01:00
return repository . startsWith ( '@' ) || repository . startsWith ( 'alias:' ) ;
}
2022-01-24 17:15:23 +01:00
export function aliasRecordToRepositories (
2023-11-07 12:50:29 -03:00
registryAliases : Record < string , string > ,
2022-01-24 17:15:23 +01:00
) : Repository [ ] {
2025-03-07 07:13:34 +01:00
return Object . entries ( registryAliases )
. filter ( ( [ , url ] ) = > /^(https?|oci):\/\/.+/ . exec ( url ) )
. map ( ( [ alias , url ] ) = > {
return {
name : alias ,
repository : url ,
} ;
} ) ;
2022-01-24 17:15:23 +01:00
}
2022-10-11 18:00:52 +02:00
export function isFileInDir ( dir : string , file : string ) : boolean {
return upath . dirname ( file ) === dir ;
}