import * as utils from '../../platform/bitbucket/utils' ; import { cache } from '../../util/cache/package/decorator' ; import { BitbucketHttp } from '../../util/http/bitbucket' ; import { ensureTrailingSlash } from '../../util/url' ; import { Datasource } from '../datasource' ; import type { DigestConfig , GetReleasesConfig , ReleaseResult } from '../types' ; import { BitbucketCommit , BitbucketTag } from './types' ; export class BitBucketTagsDatasource extends Datasource { bitbucketHttp = new BitbucketHttp ( ) ; static readonly id = 'bitbucket-tags' ; static readonly customRegistrySupport = true ; static readonly registryStrategy = 'first' ; static readonly defaultRegistryUrls = [ 'https://bitbucket.org' ] ; static readonly cacheNamespace = `datasource-${ BitBucketTagsDatasource . id }` ; constructor ( ) { super ( BitBucketTagsDatasource . id ) ; } static getRegistryURL ( registryUrl ) { return registryUrl ?? this . defaultRegistryUrls [ 0 ] ; } static getCacheKey ( registryUrl , repo , type ) { return `${ BitBucketTagsDatasource . getRegistryURL ( registryUrl ) }:${ repo }:${ type }` ; } static getSourceUrl ( lookupName , registryUrl ? ) { const url = BitBucketTagsDatasource . getRegistryURL ( registryUrl ) ; const normalizedUrl = ensureTrailingSlash ( url ) ; return `${ normalizedUrl }${ lookupName }` ; } @ cache ( { namespace : BitBucketTagsDatasource . cacheNamespace , key : ( { registryUrl , lookupName } ) => BitBucketTagsDatasource . getCacheKey ( registryUrl , lookupName , 'tags' ) , } ) async getReleases ( { registryUrl , lookupName : repo , } ) { const url = `/2.0/repositories/${ repo }/refs/tags` ; const bitbucketTags = ( await this . bitbucketHttp . getJson < utils . PagedResult < BitbucketTag > > ( url ) ) . body ; const dependency = { sourceUrl : BitBucketTagsDatasource . getSourceUrl ( repo , registryUrl ) , registryUrl : BitBucketTagsDatasource . getRegistryURL ( registryUrl ) , releases : null , } ; dependency . releases = bitbucketTags . values . map ( ( { name , target } ) => ( { version : name , gitRef : name , releaseTimestamp : target ?. date , } ) ) ; return dependency ; } @ cache ( { namespace : BitBucketTagsDatasource . cacheNamespace , key : ( registryUrl , repo , tag ) => BitBucketTagsDatasource . getCacheKey ( registryUrl , repo , `tag-${ tag }` ) , } ) async getTagCommit ( registryUrl , repo , tag ) { const url = `/2.0/repositories/${ repo }/refs/tags/${ tag }` ; const bitbucketTag = ( await this . bitbucketHttp . getJson < BitbucketTag > ( url ) ) . body ; return bitbucketTag . target . hash ; } @ cache ( { namespace : BitBucketTagsDatasource . cacheNamespace , key : ( registryUrl , repo ) => BitBucketTagsDatasource . getCacheKey ( registryUrl , repo , 'mainbranch' ) , ttlMinutes : 60 , } ) async getMainBranch ( repo ) { return ( await this . bitbucketHttp . getJson < utils . RepoInfoBody > ( `/2.0/repositories/${ repo }` ) ) . body . mainbranch . name ; } @ cache ( { namespace : BitBucketTagsDatasource . cacheNamespace , key : ( { registryUrl , lookupName } ) => BitBucketTagsDatasource . getCacheKey ( registryUrl , lookupName , 'digest' ) , } ) override async getDigest ( { lookupName : repo , registryUrl } , newValue ? ) { if ( newValue ?. length ) { return this . getTagCommit ( registryUrl , repo , newValue ) ; } const mainBranch = await this . getMainBranch ( repo ) ; const url = `/2.0/repositories/${ repo }/commits/${ mainBranch }` ; const bitbucketCommits = ( await this . bitbucketHttp . getJson < utils . PagedResult < BitbucketCommit > > ( url ) ) . body ; if ( bitbucketCommits . values . length === 0 ) { return null ; } return bitbucketCommits . values [ 0 ] . hash ; } }