2022-03-14 11:15:49 +01:00
import is from '@sindresorhus/is' ;
2024-03-01 14:59:23 -03:00
import { logger } from '../../../logger' ;
2022-03-03 10:35:26 +01:00
import { cache } from '../../../util/cache/package/decorator' ;
2025-05-07 00:07:28 +05:30
import { getEnv } from '../../../util/env' ;
2022-12-07 21:29:39 +02:00
import { regEx } from '../../../util/regex' ;
2022-03-14 11:15:49 +01:00
import { addSecretForSanitizing } from '../../../util/sanitize' ;
import { parseUrl } from '../../../util/url' ;
2023-07-12 09:23:36 +02:00
import { id as semverId } from '../../versioning/semver' ;
2023-03-17 08:13:37 -04:00
import { BitbucketTagsDatasource } from '../bitbucket-tags' ;
2022-01-29 06:48:28 -08:00
import { Datasource } from '../datasource' ;
2022-10-21 20:42:14 +01:00
import { GitTagsDatasource } from '../git-tags' ;
2024-11-15 09:52:37 +01:00
import { GiteaTagsDatasource } from '../gitea-tags' ;
2022-02-13 06:24:40 +03:00
import { GithubTagsDatasource } from '../github-tags' ;
2022-02-11 08:42:14 +03:00
import { GitlabTagsDatasource } from '../gitlab-tags' ;
2022-01-29 06:48:28 -08:00
import type { DigestConfig , GetReleasesConfig , ReleaseResult } from '../types' ;
import { BaseGoDatasource } from './base' ;
2024-03-01 14:59:23 -03:00
import { parseGoproxy } from './goproxy-parser' ;
2022-01-29 06:48:28 -08:00
import { GoDirectDatasource } from './releases-direct' ;
import { GoProxyDatasource } from './releases-goproxy' ;
2020-03-01 08:01:12 +01:00
2022-01-29 06:48:28 -08:00
export class GoDatasource extends Datasource {
static readonly id = 'go' ;
2021-08-10 14:35:43 +03:00
2023-07-12 09:23:36 +02:00
override readonly defaultVersioning = semverId ;
2022-01-29 06:48:28 -08:00
constructor ( ) {
super ( GoDatasource . id ) ;
}
2021-11-15 09:16:39 +03:00
2023-07-04 11:41:19 +02:00
override readonly defaultConfig = {
commitMessageTopic : 'module {{depName}}' ,
} ;
2022-01-29 06:48:28 -08:00
override readonly customRegistrySupport = false ;
2018-10-01 13:50:36 +02:00
2024-05-21 12:49:12 +05:45
override readonly releaseTimestampSupport = true ;
override readonly releaseTimestampNote =
'If the release timestamp is not returned from the respective datasoure used to fetch the releases, then Renovate uses the `Time` field in the results instead.' ;
override readonly sourceUrlSupport = 'package' ;
override readonly sourceUrlNote =
'The source URL is determined from the `packageName` and `registryUrl`.' ;
2022-01-29 06:48:28 -08:00
readonly goproxy = new GoProxyDatasource ( ) ;
readonly direct = new GoDirectDatasource ( ) ;
2022-12-07 21:29:39 +02:00
// Pseudo versions https://go.dev/ref/mod#pseudo-versions
static readonly pversionRegexp = regEx (
2023-11-07 12:50:29 -03:00
/v\d+\.\d+\.\d+-(?:\w+\.)?(?:0\.)?\d{14}-(?<digest>[a-f0-9]{12})/ ,
2022-12-07 21:29:39 +02:00
) ;
2022-01-29 06:48:28 -08:00
@cache ( {
namespace : ` datasource- ${ GoDatasource . id } ` ,
2023-08-15 11:31:15 +02:00
// TODO: types (#22198)
2024-09-03 00:59:14 -03:00
key : ( { packageName } : GetReleasesConfig ) = > ` getReleases: ${ packageName } ` ,
2022-01-29 06:48:28 -08:00
} )
getReleases ( config : GetReleasesConfig ) : Promise < ReleaseResult | null > {
2023-01-29 07:11:52 +01:00
return this . goproxy . getReleases ( config ) ;
2022-01-29 06:48:28 -08:00
}
/ * *
* go . getDigest
*
* This datasource resolves a go module URL into its source repository
2024-05-21 12:49:12 +05:45
* and then fetches the digest if it is on GitHub .
2022-01-29 06:48:28 -08:00
*
* This function will :
* - Determine the source URL for the module
* - Call the respective getDigest in github to retrieve the commit hash
* /
@cache ( {
2024-08-27 00:48:52 -03:00
namespace : ` datasource- ${ GoDatasource . id } ` ,
2024-09-03 00:59:14 -03:00
key : ( { packageName } : DigestConfig , newValue? : string ) = >
` getDigest: ${ packageName } : ${ newValue } ` ,
2022-01-29 06:48:28 -08:00
} )
override async getDigest (
2022-04-16 13:57:12 +02:00
{ packageName } : DigestConfig ,
2024-09-03 00:59:14 -03:00
newValue? : string ,
2022-01-29 06:48:28 -08:00
) : Promise < string | null > {
2024-03-01 14:59:23 -03:00
if ( parseGoproxy ( ) . some ( ( { url } ) = > url === 'off' ) ) {
logger . debug (
` Skip digest fetch for ${ packageName } with GOPROXY containing "off" ` ,
) ;
return null ;
}
2022-03-03 16:08:43 +01:00
const source = await BaseGoDatasource . getDatasource ( packageName ) ;
2022-01-29 06:48:28 -08:00
if ( ! source ) {
return null ;
}
2022-12-07 21:29:39 +02:00
// ignore vX.Y.Z-(0.)? pseudo versions that are used Go Modules - look up default branch instead
2023-02-09 15:11:21 +03:00
// ignore v0.0.0 versions to fetch the digest of default branch, not the commit of non-existing tag `v0.0.0`
2022-12-07 21:29:39 +02:00
const tag =
2024-09-03 00:59:14 -03:00
newValue &&
! GoDatasource . pversionRegexp . test ( newValue ) &&
newValue !== 'v0.0.0'
? newValue
2023-02-09 15:11:21 +03:00
: undefined ;
2022-01-29 06:48:28 -08:00
switch ( source . datasource ) {
2022-10-21 20:42:14 +01:00
case GitTagsDatasource . id : {
2023-09-06 15:35:52 +02:00
return this . direct . git . getDigest ( source , tag ) ;
2022-10-21 20:42:14 +01:00
}
2024-11-15 09:52:37 +01:00
case GiteaTagsDatasource . id : {
return this . direct . gitea . getDigest ( source , tag ) ;
}
2022-02-13 06:24:40 +03:00
case GithubTagsDatasource . id : {
return this . direct . github . getDigest ( source , tag ) ;
2022-01-29 06:48:28 -08:00
}
2023-03-17 08:13:37 -04:00
case BitbucketTagsDatasource . id : {
2023-09-06 15:35:52 +02:00
return this . direct . bitbucket . getDigest ( source , tag ) ;
2022-01-29 06:48:28 -08:00
}
2022-02-11 08:42:14 +03:00
case GitlabTagsDatasource . id : {
2023-09-06 15:35:52 +02:00
return this . direct . gitlab . getDigest ( source , tag ) ;
2022-01-29 06:48:28 -08:00
}
/* istanbul ignore next: can never happen, makes lint happy */
default : {
return null ;
}
}
}
2018-10-05 15:15:55 +02:00
}
2022-03-14 11:15:49 +01:00
2025-05-07 00:07:28 +05:30
const env = getEnv ( ) ;
2025-03-06 18:05:19 +01:00
/* v8 ignore next 3 -- hard to test */
2025-05-07 00:07:28 +05:30
if ( is . string ( env . GOPROXY ) ) {
const uri = parseUrl ( env . GOPROXY ) ;
2022-03-14 11:15:49 +01:00
if ( uri ? . password ) {
addSecretForSanitizing ( uri . password , 'global' ) ;
2023-01-03 13:29:07 +01:00
} else if ( uri ? . username ) {
addSecretForSanitizing ( uri . username , 'global' ) ;
2022-03-14 11:15:49 +01:00
}
}