Files
renovate/lib/modules/datasource/go/releases-goproxy.ts
Sebastian Poxhofer 3412de84ce refactor(linters): enable oxlint rule prefer-named-capture-group (#44714)
* refactor(lint): enable oxlint prefer-named-capture-group at error

Numbered capture groups (m[1], $1) break silently when a regex is
edited and group order shifts. Named groups (?<name>...) make intent
explicit and accesses (m.groups.name) resilient to reordering.

This enables the built-in eslint rule prefer-named-capture-group,
surfacing 321 pre-existing unnamed-group violations across ~121
files to be burned down incrementally in follow-up commits.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RxHFNsSworLcv23xXk9Hk

* refactor(versioning): name capture groups for prefer-named-capture-group

Burns down all prefer-named-capture-group violations in
lib/modules/versioning/. Unused capture groups (only reachable via
.test() or whole-match) become non-capturing (?:...); groups whose
value is read become named (?<name>...), with accesses rewritten to
match.groups!.<name> (or destructured from .groups!) instead of
positional match[n]/$n so a future edit that reorders groups cannot
silently break the reader.

Two cases needed extra care to preserve behavior:
- semver/common.ts and poetry/patterns.ts split() on a regex whose
  capture group is relied upon by String.prototype.split to keep the
  separator in the output array; the group is kept capturing and
  named, not converted to non-capturing.
- hashicorp/convertor.ts's semverRegex is only ever embedded via
  .source into a named `(?<version>...)` group elsewhere and never
  matched directly, so its internal groups are safe to make
  non-capturing.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RxHFNsSworLcv23xXk9Hk

* refactor(datasource): name capture groups for prefer-named-capture-group

Burns down all prefer-named-capture-group violations under
lib/modules/datasource/. Same treatment as the versioning module:
unused groups become non-capturing, used groups get names, and
positional match[n] reads move to match.groups!.<name>.

docker/ecr.ts and pypi/common.ts keep their positional destructuring
(`const [, region] = ...`, `!!m &&`) working unchanged since a named
capture group is still addressable by its numeric index in
JavaScript — only pypi/common.ts's access was rewritten to
m.groups!.owner for clarity since it read a single group directly.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RxHFNsSworLcv23xXk9Hk

* refactor(platform): name capture groups for prefer-named-capture-group

Burns down all prefer-named-capture-group violations under
lib/modules/platform/. Uses the repo's established idiom of
`match.groups?.<name>` after an optional-chained exec()/match() (see
bitbucket/index.ts hostnameWithoutApiPrefix, gerrit/utils.ts
extractSourceBranch) and `$<name>` in String.replace replacement
strings. Unused groups (markdown/HTML massaging regexes matched only
to be stripped) become non-capturing.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RxHFNsSworLcv23xXk9Hk

* refactor(manager): name capture groups in ansible-galaxy for prefer-named-capture-group

Burns down prefer-named-capture-group violations in
lib/modules/manager/ansible-galaxy/. newBlockRegEx and blockLineRegEx
dropped their unused outer wrapping group (only the inner key/value
groups were ever read) and named the key/value groups; nameMatchRegex's
remaining unused groups (protocol/user-info/port alternatives) become
non-capturing since only source/hostname/depName/version are read via
.groups. Accesses in collections.ts and roles.ts move from
lineMatch[2]/lineMatch[3] to lineMatch.groups!.key/value.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RxHFNsSworLcv23xXk9Hk

* refactor(manager): name capture groups for prefer-named-capture-group (batch 1)

Burns down prefer-named-capture-group violations across the first
alphabetical slice of lib/modules/manager/ (ansible through mix).
Used groups get names with accesses rewritten to match.groups!.<name>
(or $<name> in replacement strings); groups whose value is never read
become non-capturing (?:...). Two String.split() call sites
(git-submodules/extract.ts, meteor/extract.ts) keep the established
"named group still readable positionally from the split() result
array" idiom instead of restructuring the destructuring.

bazel/rules/go.ts and buildpacks/extract.ts dockerRef verified
byte-for-byte equivalent before/after via a quick node script since
several nested groups collapsed to non-capturing at once.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RxHFNsSworLcv23xXk9Hk

* refactor(manager): name capture groups for prefer-named-capture-group (batch 2)

Burns down prefer-named-capture-group violations across the remaining
alphabetical slice of lib/modules/manager/ (npm through terragrunt).
Same treatment as batch 1: used groups get names with accesses moved
to match.groups!.<name> or $<name> replacement strings; unused groups
(wrapping alternations, quantifier repetition groups only consumed via
.test(), or values never read from a match) become non-capturing.

terraform/extractors/others/modules.ts and terragrunt/modules.ts share
near-identical URL-parsing regexes (github/bitbucket/git/azure-devops
ref matchers) — the same set of always-unused `depth=`/`&depth=`
query-string groups is converted to non-capturing in both files.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RxHFNsSworLcv23xXk9Hk

* refactor(config): name capture groups for prefer-named-capture-group

Burns down prefer-named-capture-group violations in lib/config/.
Used groups get names with replacement strings switched from $1/$2 to
$<name>; the unused separator group in schedule-migration.ts's
afterBeforeRegex becomes non-capturing. presets/parse.ts keeps
assigning through the already-declared `repo` variable rather than
array-destructuring the exec() result, matching this file's existing
style for the other named-group regexes just above it.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RxHFNsSworLcv23xXk9Hk

* refactor(util): name capture groups for prefer-named-capture-group

Burns down prefer-named-capture-group violations across lib/util/ and
lib/logger/. Same treatment as previous batches: named groups for
values that are read (via $<name> replacement strings, .groups!.<name>,
or destructured from .groups), non-capturing groups where nothing reads
the captured value (e.g. semantic.ts's angular-commit .test()-only
regex). www-authenticate.ts's tokenizer entries each get their own
named group but keep accessing the match generically via match[1],
since a positionally-indexed named group is still readable by index.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RxHFNsSworLcv23xXk9Hk

* refactor(workers): name capture groups for prefer-named-capture-group

Burns down prefer-named-capture-group violations in lib/workers/.
Used groups get names with $<name> replacement strings; unused
wrapper/alternation groups (e.g. semantic-commit-message.ts's optional
scope/issue wrappers, release-notes.ts's date-heading and
horizontal-rule matchers that are only probed with .search()/replaced
wholesale) become non-capturing.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RxHFNsSworLcv23xXk9Hk

* refactor(test,tools): name capture groups for prefer-named-capture-group

Finishes the prefer-named-capture-group burndown: test/, tools/, and
vitest.config.mts. Groups whose value is read get names ($<name>
replacement strings or .groups.<name> access); groups only used via
.test()/.search() or that feed a .match(/…/g) call (whose return value
is the array of whole matches, not captured groups) become
non-capturing.

This is the last remaining slice of pre-existing violations, so the
rule (already enabled at error repo-wide) now passes with zero
findings across the whole codebase.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RxHFNsSworLcv23xXk9Hk

* fix prettier failure

* refactor(lint): use a named capture group in `v8-ignore-no-count`

New rule from main trips the newly enabled `prefer-named-capture-group` oxlint rule.

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-09-03 09:54:40 +00:00

552 lines
17 KiB
TypeScript

import {
isNonEmptyString,
isNonEmptyStringAndNotWhitespace,
isTruthy,
} from '@sindresorhus/is';
import type { ConstraintsFilter } from '../../../config/types.ts';
import { logger } from '../../../logger/index.ts';
import { ExternalHostError } from '../../../types/errors/external-host-error.ts';
import { withCache } from '../../../util/cache/package/with-cache.ts';
import { detectPlatform } from '../../../util/common.ts';
import { getEnv } from '../../../util/env.ts';
import { filterMap } from '../../../util/filter-map.ts';
import { queryReleases } from '../../../util/github/graphql/index.ts';
import { GithubHttp } from '../../../util/http/github.ts';
import { HttpError } from '../../../util/http/index.ts';
import * as p from '../../../util/promises.ts';
import { newlineRegex, regEx } from '../../../util/regex.ts';
import type { Timestamp } from '../../../util/timestamp.ts';
import { asTimestamp } from '../../../util/timestamp.ts';
import {
joinUrlParts,
parseUrl,
trimLeadingSlash,
trimTrailingSlash,
} from '../../../util/url.ts';
import goVersioning from '../../versioning/go-mod-directive/index.ts';
import { Datasource } from '../datasource.ts';
import { GithubReleasesDatasource } from '../github-releases/index.ts';
import type { GetReleasesConfig, Release, ReleaseResult } from '../types.ts';
import { BaseGoDatasource } from './base.ts';
import { getSourceUrl } from './common.ts';
import { parseGoproxy, parseNoproxy } from './goproxy-parser.ts';
import { GoDirectDatasource } from './releases-direct.ts';
import { VersionInfo } from './schema.ts';
/** TODO #42566 */
const goVersionRegex = regEx(/^\s*go\s+(?<version>[^\s]+)\s*$/);
const modRegex = regEx(/^(?<baseMod>.*?)(?:[./]v(?<majorVersion>\d+))?$/);
const versionTagRegex = regEx(/^v\d/);
/**
* Modules with a major version of v2 or above which have no `go.mod` are reported by a Go proxy with a `+incompatible` suffix, which is absent from the tag they were published as.
*
* @see https://go.dev/ref/mod#non-module-compat
*/
const incompatibleSuffixRegex = regEx(/\+incompatible$/);
/**
* @see https://go.dev/ref/mod#pseudo-versions
*/
const pseudoVersionRegex = regEx(
/v\d+\.\d+\.\d+-(?:\w+\.)?(?:0\.)?(?<timestamp>\d{14})-(?<digest>[a-f0-9]{12})/i,
);
export function pseudoVersionToRelease(pseudoVersion: string): Release | null {
const match = pseudoVersion.match(pseudoVersionRegex)?.groups;
if (!match) {
return null;
}
const { digest: newDigest, timestamp } = match;
const releaseTimestamp = asTimestamp(timestamp);
return {
version: pseudoVersion,
newDigest,
releaseTimestamp,
};
}
/**
* A Go module which lives in a subdirectory of its repository is tagged with that subdirectory as a prefix, so `github.com/aws/aws-sdk-go-v2/service/s3` publishes `v1.2.3` as the tag `service/s3/v1.2.3`.
*
* @see https://go.dev/ref/mod#vcs-version
*
* Because we can't tell from the module path alone where the repository ends and the subdirectory begins, we try each candidate prefix - longest first - against the tags we actually found, in the same way as `filterByPrefix` does for direct lookups.
*/
export function getTagPrefix(goModule: string, tags: Iterable<string>): string {
const nameParts = goModule
.replace(regEx(/\/v\d+$/), '')
.split('/')
.slice(1);
const tagNames = [...tags];
while (nameParts.length) {
const prefix = `${nameParts.join('/')}/`;
const isUsed = tagNames.some(
(tag) =>
tag.startsWith(prefix) &&
versionTagRegex.test(tag.slice(prefix.length)),
);
if (isUsed) {
return prefix;
}
nameParts.shift();
}
return '';
}
export class GoProxyDatasource extends Datasource {
static readonly id = 'go-proxy';
constructor() {
super(GoProxyDatasource.id);
}
readonly direct = new GoDirectDatasource();
private readonly githubHttp = new GithubHttp(GithubReleasesDatasource.id);
private async _getReleases(
config: GetReleasesConfig,
): Promise<ReleaseResult | null> {
const { packageName } = config;
logger.trace(`goproxy.getReleases(${packageName})`);
const goproxy = getEnv().GOPROXY ?? 'https://proxy.golang.org,direct';
if (goproxy === 'direct') {
return this.direct.getReleases(config);
}
const proxyList = parseGoproxy(goproxy);
const noproxy = parseNoproxy();
let result: ReleaseResult | null = null;
let servedByProxy = false;
if (noproxy?.test(packageName)) {
logger.debug(`Fetching ${packageName} via GONOPROXY match`);
result = await this.direct.getReleases(config);
return result;
}
for (const { url, fallback } of proxyList) {
try {
if (url === 'off') {
break;
} else if (url === 'direct') {
result = await this.direct.getReleases(config);
break;
}
const res = await this.getVersionsWithInfo(
url,
packageName,
config.constraintsFiltering,
);
if (res.releases.length) {
result = res;
servedByProxy = true;
break;
}
} catch (err) {
const potentialHttpError =
err instanceof ExternalHostError ? err.err : err;
const statusCode = potentialHttpError?.response?.statusCode;
const canFallback =
fallback === '|' ? true : statusCode === 404 || statusCode === 410;
if (!canFallback) {
logger.debug(
{ err },
'Goproxy error: not falling back to other URLs provided with GOPROXY, rethrowing',
);
this.handleGenericErrors(err);
}
logger.debug(
{ err },
'Goproxy error: trying next URL provided with GOPROXY',
);
}
}
if (result && !result.sourceUrl) {
try {
const datasource = await BaseGoDatasource.getDatasource(packageName);
const sourceUrl = getSourceUrl(datasource);
if (sourceUrl) {
result.sourceUrl = sourceUrl;
}
} catch (err) {
logger.trace({ err }, `Can't get datasource for ${packageName}`);
}
}
if (result?.sourceUrl && servedByProxy) {
await this.addGithubReleaseTimestamps(
packageName,
result.sourceUrl,
result.releases,
);
}
return result;
}
/**
* A Go proxy reports the commit time of the tagged commit as a version's `Time`, which can be much earlier than the point at which that version was released.
*
* When the module is hosted on GitHub and the version has a GitHub Release, the Release's publication time is a better indicator of when the version became available.
*/
async addGithubReleaseTimestamps(
packageName: string,
sourceUrl: string,
releases: Release[],
): Promise<void> {
if (detectPlatform(sourceUrl) !== 'github') {
return;
}
const parsedUrl = parseUrl(sourceUrl);
/* v8 ignore next -- detectPlatform only returns a platform for parseable URLs */
if (!parsedUrl) {
return;
}
const repository = trimTrailingSlash(
trimLeadingSlash(parsedUrl.pathname),
).replace(regEx(/\.git$/), '');
try {
const githubReleases = await queryReleases(
{
packageName: repository,
registryUrl: parsedUrl.origin,
},
this.githubHttp,
);
const timestamps = new Map<string, Timestamp>();
for (const { version, releaseTimestamp } of githubReleases) {
timestamps.set(version, releaseTimestamp);
}
const tagPrefix = getTagPrefix(packageName, timestamps.keys());
for (const release of releases) {
const version = release.version.replace(incompatibleSuffixRegex, '');
const releaseTimestamp = timestamps.get(`${tagPrefix}${version}`);
if (
releaseTimestamp &&
(!release.releaseTimestamp ||
releaseTimestamp > release.releaseTimestamp)
) {
release.releaseTimestamp = releaseTimestamp;
}
}
} catch (err) {
logger.debug(
{ err, packageName },
'Error fetching GitHub Releases for Go module',
);
}
}
getReleases(config: GetReleasesConfig): Promise<ReleaseResult | null> {
return withCache(
{
namespace: `datasource-${GoProxyDatasource.id}`,
key: GoProxyDatasource.getCacheKey(config),
fallback: true,
},
() => this._getReleases(config),
);
}
/**
* Avoid ambiguity when serving from case-insensitive file systems.
*
* @see https://golang.org/ref/mod#goproxy-protocol
*/
encodeCase(input: string): string {
return input.replace(regEx(/(?:[A-Z])/g), (x) => `!${x.toLowerCase()}`);
}
async listVersions(baseUrl: string, packageName: string): Promise<Release[]> {
const url = joinUrlParts(
baseUrl,
this.encodeCase(packageName),
'@v',
'list',
);
const { body } = await this.http.getText(url);
return filterMap(body.split(newlineRegex), (str) => {
if (!isNonEmptyStringAndNotWhitespace(str)) {
return null;
}
const [version, timestamp] = str.trim().split(regEx(/\s+/));
const release: Release = pseudoVersionToRelease(version) ?? { version };
const releaseTimestamp = asTimestamp(timestamp);
if (releaseTimestamp) {
release.releaseTimestamp = releaseTimestamp;
}
return release;
});
}
async versionInfo(
baseUrl: string,
packageName: string,
version: string,
): Promise<Release> {
const url = joinUrlParts(
baseUrl,
this.encodeCase(packageName),
'@v',
`${version}.info`,
);
const res = await this.http.getJson(url, VersionInfo);
const result: Release = {
version: res.body.Version,
};
const releaseTimestamp = asTimestamp(res.body.Time);
if (releaseTimestamp) {
result.releaseTimestamp = releaseTimestamp;
}
return result;
}
/**
* Retrieve the `go` directive for a given Go Module.
*
* NOTE that this means the `go` directive, not the `toolchain` directive.
*/
async retrieveGoDirectiveForModule(
baseUrl: string,
packageName: string,
version: string,
): Promise<string | undefined> {
return withCache(
{
namespace: `datasource-${GoProxyDatasource.id}`,
key: GoProxyDatasource.getVersionedCacheKey(packageName, version),
// a module's `go.mod` should /never/ change after it's published. If going via the Go Proxy and the Go Checksum Database, a change in this value will result in build failures.
ttlMinutes: 100 * 24 * 60,
},
() => this._retrieveGoDirectiveForModule(baseUrl, packageName, version),
);
}
async _retrieveGoDirectiveForModule(
baseUrl: string,
packageName: string,
version: string,
): Promise<string | undefined> {
const url = joinUrlParts(
baseUrl,
this.encodeCase(packageName),
'@v',
`${version}.mod`,
);
const res = await this.http.getText(url);
let goDirective: string | undefined = undefined;
for (const line of res.body.split('\n')) {
const goVersionMatches = goVersionRegex.exec(line)?.groups;
if (goVersionMatches) {
goDirective = goVersionMatches.version;
break;
}
}
if (!goDirective) {
return goDirective;
}
// always return it in full SemVer format, which can then be matched on using `semver` or `semver-coerced`
const parts = goDirective.split('.');
if (parts.length === 1) {
return `${parts[0]}.0.0`;
}
if (parts.length === 2) {
return `${parts[0]}.${parts[1]}.0`;
}
return `${parts[0]}.${parts[1]}.${parts[2]}`;
}
async getLatestVersion(
baseUrl: string,
packageName: string,
): Promise<{ version: string; sourceUrl?: string } | null> {
try {
const url = joinUrlParts(
baseUrl,
this.encodeCase(packageName),
'@latest',
);
const res = await this.http.getJson(url, VersionInfo);
const { Version: version, Origin: origin } = res.body;
// Extract sourceUrl from GOPROXY Origin when present, avoiding go-get to
// vanity hosts (https://github.com/renovatebot/renovate/discussions/44898)
const sourceUrl =
origin?.VCS === 'git' && isNonEmptyString(origin.URL)
? origin.URL.replace(regEx(/\.git$/), '')
: undefined;
return { version, sourceUrl };
} catch (err) {
logger.trace({ err }, 'Failed to get latest version');
return null;
}
}
async getVersionsWithInfo(
baseUrl: string,
packageName: string,
constraintsFiltering: ConstraintsFilter | undefined,
): Promise<ReleaseResult> {
const isGopkgin = packageName.startsWith('gopkg.in/');
const majorSuffixSeparator = isGopkgin ? '.' : '/';
const modParts = packageName.match(modRegex)?.groups;
const baseMod =
modParts?.baseMod ??
/* v8 ignore next -- defensive: modRegex matches any non-empty package name, so baseMod is always set */ packageName;
const packageMajor = parseInt(modParts?.majorVersion ?? '0', 10);
const result: ReleaseResult = { releases: [] };
for (let major = packageMajor; ; major += 1) {
let pkg = `${baseMod}${majorSuffixSeparator}v${major}`;
if (!isGopkgin && major < 2) {
pkg = baseMod;
major += 1; // v0 and v1 are the same module
}
let releases: Release[] = [];
try {
const res = await this.listVersions(baseUrl, pkg);
// Artifactory returns all versions in any major (past and future),
// so starting from v2, we filter them in order to avoid the infinite loop
const filteredReleases = res.filter(({ version }) => {
if (major < 2) {
return true;
}
return (
version.split(regEx(/[^\d]+/)).find(isTruthy) === major.toString()
);
});
releases = await p.map(filteredReleases, async (versionInfo) => {
const { version, newDigest, releaseTimestamp } = versionInfo;
if (releaseTimestamp) {
return { version, newDigest, releaseTimestamp };
}
try {
return await this.versionInfo(baseUrl, pkg, version);
} catch (err) {
logger.trace({ err }, `Can't obtain data from ${baseUrl}`);
return { version };
}
});
if (constraintsFiltering === 'strict') {
releases = await p.map(releases, async (rel) => {
try {
const goDirective = await this.retrieveGoDirectiveForModule(
baseUrl,
pkg,
rel.version,
);
if (goDirective) {
rel.constraints ??= {};
rel.constraints['%goMod'] ??= [];
rel.constraints['%goMod'].push(goDirective);
}
} catch (err) {
logger.trace(
{ err },
`Can't obtain \`go\` directive from ${baseUrl}`,
);
}
return rel;
});
}
result.releases.push(...releases);
} catch (err) {
const potentialHttpError =
err instanceof ExternalHostError ? err.err : err;
const status = potentialHttpError.response?.statusCode;
if (
potentialHttpError instanceof HttpError &&
(status === 404 || status === 403) &&
major !== packageMajor
) {
break;
}
throw err;
}
const latest = await this.getLatestVersion(baseUrl, pkg);
if (latest) {
const { version: latestVersion, sourceUrl } = latest;
result.tags ??= {};
result.tags.latest ??= latestVersion;
if (goVersioning.isGreaterThan(latestVersion, result.tags.latest)) {
result.tags.latest = latestVersion;
}
if (sourceUrl) {
result.sourceUrl ??= sourceUrl;
}
if (!result.releases.length) {
const releaseFromLatest = pseudoVersionToRelease(latestVersion);
if (releaseFromLatest) {
result.releases.push(releaseFromLatest);
}
}
}
if (!releases.length) {
break;
}
}
return result;
}
static getCacheKey({
packageName,
constraintsFiltering,
}: GetReleasesConfig): string {
const goproxy = getEnv().GOPROXY;
const noproxy = parseNoproxy();
const constraintsFilteringKey =
constraintsFiltering && constraintsFiltering !== 'none'
? `@@${constraintsFiltering}`
: '';
// TODO: types (#22198)
return `${packageName}@@${goproxy}@@${noproxy?.toString()}${constraintsFilteringKey}`;
}
static getVersionedCacheKey(packageName: string, version: string): string {
const goproxy = getEnv().GOPROXY;
const noproxy = parseNoproxy();
// TODO: types (#22198)
return `${packageName}@@${version}@@${goproxy}@@${noproxy?.toString()}`;
}
}