Files
renovate/lib/modules/platform/index.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

97 lines
3.0 KiB
TypeScript

import type { AllConfig } from '../../config/types.ts';
import { PLATFORM_NOT_FOUND } from '../../constants/error-messages.ts';
import type { PlatformId } from '../../constants/index.ts';
import { logger } from '../../logger/index.ts';
import type { HostRule } from '../../types/index.ts';
import { coerceArray } from '../../util/array.ts';
import {
setGitAuthor,
setNoVerify,
setPrivateKey,
} from '../../util/git/index.ts';
import * as hostRules from '../../util/host-rules.ts';
import { parseUrl } from '../../util/url.ts';
import platforms from './api.ts';
import { setPlatformScmApi } from './scm.ts';
import type { Platform } from './types.ts';
export type * from './types.ts';
export function getPlatformList(): string[] {
return Array.from(platforms.keys());
}
let _platform: Platform | undefined;
const handler: ProxyHandler<Platform> = {
get(_target: Platform, prop: keyof Platform) {
if (!_platform) {
throw new Error(PLATFORM_NOT_FOUND);
}
return _platform[prop];
},
};
export const platform = new Proxy<Platform>({} as any, handler);
export function setPlatformApi(name: PlatformId): void {
if (!platforms.has(name)) {
throw new Error(
`Init: Platform "${name}" not found. Must be one of: ${getPlatformList().join(
', ',
)}`,
);
}
_platform = platforms.get(name);
setPlatformScmApi(name);
}
export async function initPlatform(config: AllConfig): Promise<AllConfig> {
setPrivateKey(config.gitPrivateKey, config.gitPrivateKeyPassphrase);
setNoVerify(coerceArray(config.gitNoVerify));
// TODO: `platform` (#22198)
setPlatformApi(config.platform!);
// TODO: types
const platformInfo = await platform.initPlatform(config);
const returnConfig: any = {
...config,
...platformInfo,
hostRules: [
...coerceArray(platformInfo?.hostRules),
...coerceArray(config.hostRules),
],
};
// v8 ignore else -- TODO: add test #40625
if (config?.gitAuthor) {
logger.debug(`Using configured gitAuthor (${config.gitAuthor})`);
returnConfig.gitAuthor = config.gitAuthor;
} else if (platformInfo?.gitAuthor) {
logger.debug(`Using platform gitAuthor: ${String(platformInfo.gitAuthor)}`);
returnConfig.gitAuthor = platformInfo.gitAuthor;
}
// This is done for validation and will be overridden later once repo config is incorporated
setGitAuthor(returnConfig.gitAuthor);
const platformRule: HostRule = {
matchHost: parseUrl(returnConfig.endpoint)?.hostname,
};
// There might have been platform-specific modifications to the token
if (returnConfig.token) {
config.token = returnConfig.token;
}
(
['token', 'username', 'password'] as ('token' | 'username' | 'password')[]
).forEach((field) => {
if (config[field]) {
platformRule[field] = config[field];
delete returnConfig[field];
}
});
const typedPlatformRule = {
...platformRule,
hostType: returnConfig.platform,
};
returnConfig.hostRules.push(typedPlatformRule);
hostRules.add(typedPlatformRule);
return returnConfig;
}