Files
renovate/lib/modules/manager/bazel/artifacts.ts
Sebastian Poxhofer 58771bb183 refactor(linters): enabled oxlints func-style (#44691)
* refactor(oxlint): enable func-style

Enable the built-in func-style rule as ["warn", "declaration"] so that
named functions are written as function declarations instead of arrow or
function expressions assigned to variables, matching the convention in
docs/development/best-practices.md (Functions section). Inline arrow
callbacks are unaffected; prefer-arrow-callback continues to cover the
callback direction.

Severity is "warn" instead of "error" as a fallback: a repo-wide run
found 196 pre-existing violations (70 in spec files, 126 in production
code), exceeding the 80-violation fix threshold for this change, so no
violations were fixed here. Follow-up work can convert the offenders
(largest clusters: lib/modules/versioning/ruby, lib/util/cache/package,
lib/modules/versioning/maven, lib/modules/versioning/gradle) and then
raise the rule to "error". Note oxlint 1.72 also supports an
allowTypeAnnotation option if typed consts should remain arrows.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* ci(linter): enforce func-style at error

* refactor(func-style): convert const-arrow functions to declarations in with-cache spec and ruby versioning

* refactor(func-style): convert const-arrow functions to declarations in maven/gradle modules

* refactor(func-style): convert const-arrow functions to declarations in remaining versioning modules

* refactor(func-style): convert const-arrow functions to declarations in platform modules

Discriminated-union closures (ensureCommentRemoval byTopic/byContent
helpers in github, gitlab, bitbucket, bitbucket-server) needed the
narrowed field passed explicitly as a parameter, since function
declarations are hoisted and lose the outer if-narrowing that inline
arrow-function closures previously relied on.

* refactor(func-style): convert const-arrow functions to declarations across util/manager/datasource modules

One case (logger/renovate-logger.ts logOnceFn) is kept as an arrow
function with a documented oxlint-disable: it is stored on
this.logger.once[level] and relies on lexically-bound this, which a
hoisted function declaration would not preserve.

* refactor(func-style): convert remaining const-arrow test helpers to declarations

get-updated.spec.ts: hoisted expectPipCompilePackageAndLockFile to
module scope (it only closes over the module-level pipCompile mock)
and added a documented oxlint-disable for vitest/expect-expect on its
caller test, since oxlint's expect-expect check does not trace
assertions through an externally-defined helper function.

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-19 11:14:22 +00:00

208 lines
5.7 KiB
TypeScript

import { isTruthy } from '@sindresorhus/is';
import { logger } from '../../../logger/index.ts';
import * as packageCache from '../../../util/cache/package/index.ts';
import { hashStream } from '../../../util/hash.ts';
import { Http } from '../../../util/http/index.ts';
import { map as pMap } from '../../../util/promises.ts';
import { regEx } from '../../../util/regex.ts';
import type {
ArtifactError,
UpdateArtifact,
UpdateArtifactsResult,
Upgrade,
} from '../types.ts';
import {
findCodeFragment,
patchCodeAtFragments,
updateCode,
} from './common.ts';
import type { RecordFragment, StringFragment } from './types.ts';
const http = new Http('bazel');
function getUrlFragments(rule: RecordFragment): StringFragment[] {
const urls: StringFragment[] = [];
const urlRecord = rule.children.url;
if (urlRecord?.type === 'string') {
urls.push(urlRecord);
}
const urlsRecord = rule.children.urls;
if (urlsRecord?.type === 'array') {
for (const urlRecord of urlsRecord.children) {
if (urlRecord.type === 'string') {
urls.push(urlRecord);
}
}
}
return urls;
}
const urlMassages = {
'bazel-skylib.': 'bazel_skylib-',
'/bazel-gazelle/releases/download/0': '/bazel-gazelle/releases/download/v0',
'/bazel-gazelle-0': '/bazel-gazelle-v0',
'/rules_go/releases/download/0': '/rules_go/releases/download/v0',
'/rules_go-0': '/rules_go-v0',
};
function massageUrl(url: string): string {
let result = url;
for (const [from, to] of Object.entries(urlMassages)) {
result = result.replace(from, to);
}
return result;
}
function migrateUrl(url: string, upgrade: Upgrade): string {
const newValue = upgrade.newValue?.replace(regEx(/^v/), '');
// @see https://github.com/bazelbuild/rules_webtesting/releases/tag/0.3.5
// @see https://github.com/bazelbuild/rules_webtesting/releases/tag/0.4.0
if (
url.endsWith('/rules_webtesting.tar.gz') &&
!newValue?.match(regEx(/^0\.[0123]\./))
) {
return url.replace(regEx(/\.tar\.gz$/), `-${newValue}.tar.gz`);
}
return url;
}
function replaceAll(input: string, from: string, to: string): string {
return input.split(from).join(to);
}
function replaceValues(
content: string,
from: string | null | undefined,
to: string | null | undefined,
): string {
// istanbul ignore if
if (!from || !to || from === to) {
return content;
}
const massagedFrom = from.replace(regEx(/^v/), '');
const massagedTo = to.replace(regEx(/^v/), '');
return replaceAll(content, massagedFrom, massagedTo);
}
async function getHashFromUrl(url: string): Promise<string | null> {
const cacheNamespace = 'url-sha256';
const cachedResult = await packageCache.get<string | null>(
cacheNamespace,
url,
);
/* istanbul ignore next line */
if (cachedResult) {
return cachedResult;
}
try {
const hash = await hashStream(http.stream(url), 'sha256');
const cacheMinutes = 3 * 24 * 60; // 3 days
await packageCache.set(cacheNamespace, url, hash, cacheMinutes);
return hash;
} catch /* istanbul ignore next */ {
return null;
}
}
async function getHashFromUrls(urls: string[]): Promise<string | null> {
const hashes = (
await pMap(urls, (url) => getHashFromUrl(massageUrl(url)))
).filter(isTruthy);
if (!hashes.length) {
logger.debug({ urls }, 'Could not calculate hash for URLs');
return null;
}
const distinctHashes = new Set(hashes);
// istanbul ignore if
if (distinctHashes.size > 1) {
logger.warn({ urls }, 'Found multiple hashes for single def');
}
return hashes[0];
}
export async function updateArtifacts(
updateArtifact: UpdateArtifact,
): Promise<UpdateArtifactsResult[] | null> {
const { packageFileName: path, updatedDeps: upgrades } = updateArtifact;
const oldContents = updateArtifact.newPackageFileContent;
let newContents = oldContents;
const artifactErrors: ArtifactError[] = [];
for (const upgrade of upgrades) {
const { managerData } = upgrade;
const idx = managerData?.idx as number;
if (upgrade.depType === 'http_file' || upgrade.depType === 'http_archive') {
const rule = findCodeFragment(newContents, [idx]);
/* v8 ignore next -- used only for type narrowing */
if (rule?.type !== 'record') {
continue;
}
const urlFragments = getUrlFragments(rule);
if (!urlFragments?.length) {
logger.debug(`def: ${rule.value}, urls is empty`);
continue;
}
function updateValues(oldUrl: string): string {
let url = oldUrl;
url = replaceValues(url, upgrade.currentValue, upgrade.newValue);
url = replaceValues(url, upgrade.currentDigest, upgrade.newDigest);
url = migrateUrl(url, upgrade);
return url;
}
const urls = urlFragments.map(({ value }) => updateValues(value));
const hash = await getHashFromUrls(urls);
if (!hash) {
if (urlFragments.length >= 1) {
artifactErrors.push({
fileName: path,
stderr: `Could not calculate sha256 for ${upgrade.depName} at ${upgrade.newValue}. Checked URLs: ${urls.join(', ')}`,
});
}
continue;
}
newContents = patchCodeAtFragments(
newContents,
urlFragments,
updateValues,
);
newContents = updateCode(
newContents,
[idx, 'strip_prefix'],
updateValues,
);
newContents = updateCode(newContents, [idx, 'sha256'], hash);
}
}
if (oldContents === newContents) {
if (artifactErrors.length) {
// If we have artifact errors, return them even if we have file changes
return artifactErrors.map((error) => ({
artifactError: error,
}));
}
return null;
}
return [
{
file: {
type: 'addition',
path,
contents: newContents,
},
},
];
}