Files
renovate/lib/modules/manager/vendir/artifacts.ts
Sebastian Poxhofer 97327c3d28 refactor(linters): add prefer-coerce-array rule (#45492)
* refactor(linters): add prefer-coerce-array rule

Refs #45217

`x ?? []` and `coerceArray(x)` say the same thing, but only the second
one is searchable and consistent. The rule flags a `??` whose default is
a bare `[]` literal; a non-empty default or an asserted `([] as Foo[])`
still passes.

Applying it across `lib/` surfaced three spots the helper cannot cover,
which keep `?? []` with a disable comment or a narrower fix:

- `package-files.ts` destructures a tuple, which `coerceArray()` widens
- `generate.spec.ts` defaults `matchAll()`, an iterator rather than an array
- `mise/upgradeable-tooling.ts` declares a `Record` return, so its
  `?? []` fallback was wrong to begin with and becomes `coerceObject()`

* refactor(platform/github): hoist the GHE version header lookup

`Object.entries(...).find()` returns a tuple, and defaulting it with
`?? []` only to destructure the second element reads as an array
default when it is not one. Name the found entry instead, which drops
the `?? []` rather than routing a tuple through `coerceArray()`.
2026-08-28 09:27:58 +00:00

116 lines
3.4 KiB
TypeScript

import { TEMPORARY_ERROR } from '../../../constants/error-messages.ts';
import { logger } from '../../../logger/index.ts';
import { coerceArray } from '../../../util/array.ts';
import type { ExecOptions } from '../../../util/exec/types.ts';
import {
getParentDir,
getSiblingFileName,
readLocalFile,
writeLocalFile,
} from '../../../util/fs/index.ts';
import { withGitEnvironment } from '../../../util/git/exec.ts';
import { getRepoStatus } from '../../../util/git/index.ts';
import type { UpdateArtifact, UpdateArtifactsResult } from '../types.ts';
const gitExec = withGitEnvironment();
export async function updateArtifacts({
packageFileName,
newPackageFileContent,
config,
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
logger.debug(`vendir.updateArtifacts(${packageFileName})`);
const lockFileName = getSiblingFileName(packageFileName, 'vendir.lock.yml');
if (!lockFileName) {
logger.warn('No vendir.lock.yml found');
return null;
}
const existingLockFileContent = await readLocalFile(lockFileName, 'utf8');
if (!existingLockFileContent) {
logger.warn('Empty vendir.lock.yml found');
return null;
}
try {
await writeLocalFile(packageFileName, newPackageFileContent);
logger.debug('Updating Vendir artifacts');
const execOptions: ExecOptions = {
cwdFile: packageFileName,
docker: {},
toolConstraints: [
{ toolName: 'vendir', constraint: config.constraints?.vendir },
{ toolName: 'helm', constraint: config.constraints?.helm },
],
};
await gitExec(`vendir sync`, execOptions);
logger.debug('Returning updated Vendir artifacts');
const fileChanges: UpdateArtifactsResult[] = [];
const newVendirLockContent = await readLocalFile(lockFileName, 'utf8');
const isLockFileChanged = existingLockFileContent !== newVendirLockContent;
if (isLockFileChanged) {
fileChanges.push({
file: {
type: 'addition',
path: lockFileName,
contents: newVendirLockContent,
},
});
}
// add modified vendir archives to artifacts
logger.debug("Adding Sync'd files to git");
// Files must be in the vendor path to get added
const vendorDir = getParentDir(packageFileName);
const status = await getRepoStatus();
if (status) {
const modifiedFiles = coerceArray(status.modified);
const notAddedFiles = status.not_added;
const deletedFiles = coerceArray(status.deleted);
for (const f of modifiedFiles.concat(notAddedFiles)) {
const isFileInVendorDir = f.startsWith(vendorDir);
if (vendorDir || isFileInVendorDir) {
fileChanges.push({
file: {
type: 'addition',
path: f,
contents: await readLocalFile(f),
},
});
}
}
for (const f of deletedFiles) {
fileChanges.push({
file: {
type: 'deletion',
path: f,
},
});
}
} else {
logger.error('Failed to get git status');
}
return fileChanges.length ? fileChanges : null;
} catch (err) {
if (err.message === TEMPORARY_ERROR) {
throw err;
}
logger.debug({ err }, 'Failed to update Vendir lock file');
return [
{
artifactError: {
fileName: lockFileName,
stderr: err.message,
},
},
];
}
}