import { logger } from '../../logger' ; import { regEx } from '../../util/regex' ; import * as github from '../github-tags' ; import * as gitlab from '../gitlab-tags' ; import type { GetReleasesConfig , ReleaseResult } from '../types' ; import { bitbucket , getSourceUrl } from './common' ; import { getDatasource } from './get-datasource' ; /**
 * go.getReleases
 *
 * This datasource resolves a go module URL into its source repository
 *  and then fetch it if it is on GitHub.
 *
 * This function will:
 *  - Determine the source URL for the module
 *  - Call the respective getReleases in github/gitlab to retrieve the tags
 *  - Filter module tags according to the module path
 */ export async function getReleases ( config ) { const { lookupName } = config ; let res = null ; logger . trace ( `go.getReleases(${ lookupName })` ) ; const source = await getDatasource ( lookupName ) ; if ( ! source ) { logger . info ( { lookupName } , 'Unsupported go host - cannot look up versions' ) ; return null ; } switch ( source . datasource ) { case github . id : { res = await github . getReleases ( source ) ; break ; } case gitlab . id : { res = await gitlab . getReleases ( source ) ; break ; } case bitbucket . id : { res = await bitbucket . getReleases ( source ) ; break ; } default : { return null ; } } if ( ! res ) { return null ; } const sourceUrl = getSourceUrl ( source ) ; /**
     * github.com/org/mod/submodule should be tagged as submodule/va.b.c
     * and that tag should be used instead of just va.b.c, although for compatibility
     * the old behaviour stays the same.
     */ const nameParts = lookupName . replace ( regEx ( /\/v\d+$/ ) , '' ) . split ( '/' ) ; logger . trace ( { nameParts , releases : res . releases } , 'go.getReleases' ) ; if ( nameParts . length > 3 ) { const prefix = nameParts . slice ( 3 , nameParts . length ) . join ( '/' ) ; logger . trace ( `go.getReleases.prefix:${ prefix }` ) ; const submodReleases = res . releases . filter ( ( release ) => release . version ?. startsWith ( prefix ) ) . map ( ( release ) => { const r2 = release ; r2 . version = r2 . version . replace ( `${ prefix }/` , '' ) ; return r2 ; } ) ; logger . trace ( { submodReleases } , 'go.getReleases' ) ; if ( ! ( source . datasource === gitlab . id ) || ( source . datasource === gitlab . id && submodReleases . length ) ) { return { sourceUrl , releases : submodReleases , } ; } } if ( res . releases ) { res . releases = res . releases . filter ( ( release ) => release . version ?. startsWith ( 'v' ) ) ; } return { ... res , sourceUrl } ; }