import { cache } from '../../util/cache/package/decorator' ; import { GitlabHttp } from '../../util/http/gitlab' ; import { joinUrlParts } from '../../util/url' ; import { Datasource } from '../datasource' ; import type { GetReleasesConfig , ReleaseResult } from '../types' ; import { datasource } from './common' ; import type { GitlabPackage } from './types' ; export class GitlabPackagesDatasource extends Datasource { static readonly id = datasource ; protected override http : GitlabHttp ; override caching = true ; override customRegistrySupport = true ; override defaultRegistryUrls = [ 'https://gitlab.com' ] ; constructor ( ) { super ( datasource ) ; this . http = new GitlabHttp ( ) ; } static getGitlabPackageApiUrl ( registryUrl , projectName , packageName ) { const projectNameEncoded = encodeURIComponent ( projectName ) ; const packageNameEncoded = encodeURIComponent ( packageName ) ; return joinUrlParts ( registryUrl , `api/v4/projects` , projectNameEncoded , `packages?package_name=${ packageNameEncoded }&per_page=100` ) ; } @ cache ( { namespace : `datasource-${ datasource }` , key : ( { registryUrl , lookupName } ) => `${ registryUrl }-${ lookupName }` , } ) async getReleases ( { registryUrl , lookupName , } ) { const [ projectName , packageName ] = lookupName . split ( ':' , 2 ) ; const apiUrl = GitlabPackagesDatasource . getGitlabPackageApiUrl ( registryUrl , projectName , packageName ) ; const result = { releases : null , } ; let response ; try { response = ( await this . http . getJson < GitlabPackage [ ] > ( apiUrl , { paginate : true } ) ) . body ; result . releases = response . filter ( ( r ) => r . name === packageName ) . map ( ( { version , created_at } ) => ( { version , releaseTimestamp : created_at , } ) ) ; } catch ( err ) { this . handleGenericErrors ( err ) ; } return result . releases ?. length ? result : null ; } }