import { cache } from '../../util/cache/package/decorator' ; import { regEx } from '../../util/regex' ; import { Datasource } from '../datasource' ; import type { DigestConfig , GetReleasesConfig , ReleaseResult } from '../types' ; import { GitDatasource } from './base' ; import type { RawRefs } from './types' ; process . env . GIT_SSH_COMMAND = 'ssh -o BatchMode=yes' ; export class GitRefsDatasource extends Datasource { static readonly id = 'git-refs' ; constructor ( ) { super ( GitRefsDatasource . id ) ; } override readonly customRegistrySupport = false ; @ cache ( { namespace : `datasource-${ GitRefsDatasource . id }` , key : ( { lookupName } ) => lookupName , } ) override async getReleases ( { lookupName , } ) { const rawRefs = await GitDatasource . getRawRefs ( { lookupName } , this . id ) ; const refs = rawRefs . filter ( ( ref ) => ref . type === 'tags' || ref . type === 'heads' ) . map ( ( ref ) => ref . value ) ; const uniqueRefs = [ ... new Set ( refs ) ] ; const sourceUrl = lookupName . replace ( regEx ( /\.git$/ ) , '' ) . replace ( regEx ( /\/$/ ) , '' ) ; const result = { sourceUrl , releases : uniqueRefs . map ( ( ref ) => ( { version : ref , gitRef : ref , newDigest : rawRefs . find ( ( rawRef ) => rawRef . value === ref ) . hash , } ) ) , } ; return result ; } override async getDigest ( { lookupName } , newValue ? ) { const rawRefs = await GitDatasource . getRawRefs ( { lookupName } , this . id ) ; let ref ; if ( newValue ) { ref = rawRefs . find ( ( rawRef ) => [ 'heads' , 'tags' ] . includes ( rawRef . type ) && rawRef . value === newValue ) ; } else { ref = rawRefs . find ( ( rawRef ) => rawRef . type === '' && rawRef . value === 'HEAD' ) ; } if ( ref ) { return ref . hash ; } return null ; } }