Merge branch 'main' into main

This commit is contained in:
lbarros-asml 2024-12-31 15:11:04 +01:00 committed by GitHub
commit 1d8c5072ed
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 102 additions and 83 deletions

View file

@ -306,7 +306,11 @@ describe('workers/global/config/parse/env', () => {
it('crashes', async () => { it('crashes', async () => {
const envParam: NodeJS.ProcessEnv = { RENOVATE_CONFIG: '!@#' }; const envParam: NodeJS.ProcessEnv = { RENOVATE_CONFIG: '!@#' };
await env.getConfig(envParam); processExit.mockImplementationOnce(() => {
throw new Error('terminate function to simulate process.exit call');
});
await expect(env.getConfig(envParam)).toReject();
expect(processExit).toHaveBeenCalledWith(1); expect(processExit).toHaveBeenCalledWith(1);
}); });

View file

@ -3,6 +3,7 @@ import JSON5 from 'json5';
import { getOptions } from '../../../../config/options'; import { getOptions } from '../../../../config/options';
import type { AllConfig } from '../../../../config/types'; import type { AllConfig } from '../../../../config/types';
import { logger } from '../../../../logger'; import { logger } from '../../../../logger';
import { parseJson } from '../../../../util/common';
import { coersions } from './coersions'; import { coersions } from './coersions';
import type { ParseConfigOptions } from './types'; import type { ParseConfigOptions } from './types';
import { migrateAndValidateConfig } from './util'; import { migrateAndValidateConfig } from './util';
@ -118,9 +119,9 @@ function massageConvertedExperimentalVars(
export async function getConfig( export async function getConfig(
inputEnv: NodeJS.ProcessEnv, inputEnv: NodeJS.ProcessEnv,
configEnvKey = 'RENOVATE_CONFIG',
): Promise<AllConfig> { ): Promise<AllConfig> {
let env = inputEnv; let env = normalizePrefixes(inputEnv, inputEnv.ENV_PREFIX);
env = normalizePrefixes(inputEnv, inputEnv.ENV_PREFIX);
env = massageConvertedExperimentalVars(env); env = massageConvertedExperimentalVars(env);
env = renameEnvKeys(env); env = renameEnvKeys(env);
// massage the values of migrated configuration keys // massage the values of migrated configuration keys
@ -128,92 +129,82 @@ export async function getConfig(
const options = getOptions(); const options = getOptions();
let config: AllConfig = {}; const config = await parseAndValidateOrExit(env, configEnvKey);
if (env.RENOVATE_CONFIG) {
try {
config = JSON5.parse(env.RENOVATE_CONFIG);
logger.debug({ config }, 'Detected config in env RENOVATE_CONFIG');
config = await migrateAndValidateConfig(config, 'RENOVATE_CONFIG');
} catch (err) {
logger.fatal({ err }, 'Could not parse RENOVATE_CONFIG');
process.exit(1);
}
}
config.hostRules ??= []; config.hostRules ??= [];
options.forEach((option) => { for (const option of options) {
if (option.env !== false) { if (option.env === false) {
const envName = getEnvName(option); continue;
const envVal = env[envName]; }
if (envVal) {
if (option.type === 'array' && option.subType === 'object') { const envName = getEnvName(option);
try { const envVal = env[envName];
const parsed = JSON5.parse(envVal); if (!envVal) {
if (is.array(parsed)) { continue;
config[option.name] = parsed; }
} else {
logger.debug( if (option.type === 'array' && option.subType === 'object') {
{ val: envVal, envName }, try {
'Could not parse object array', const parsed = JSON5.parse(envVal);
); if (is.array(parsed)) {
} config[option.name] = parsed;
} catch {
logger.debug(
{ val: envVal, envName },
'Could not parse environment variable',
);
}
} else { } else {
const coerce = coersions[option.type]; logger.debug(
config[option.name] = coerce(envVal); { val: envVal, envName },
if (option.name === 'dryRun') { 'Could not parse object array',
if ((config[option.name] as string) === 'true') { );
logger.warn( }
'env config dryRun property has been changed to full', } catch {
); logger.debug(
config[option.name] = 'full'; { val: envVal, envName },
} else if ((config[option.name] as string) === 'false') { 'Could not parse environment variable',
logger.warn( );
'env config dryRun property has been changed to null', }
); } else {
delete config[option.name]; const coerce = coersions[option.type];
} else if ((config[option.name] as string) === 'null') { config[option.name] = coerce(envVal);
delete config[option.name]; if (option.name === 'dryRun') {
} if ((config[option.name] as string) === 'true') {
} logger.warn('env config dryRun property has been changed to full');
if (option.name === 'requireConfig') { config[option.name] = 'full';
if ((config[option.name] as string) === 'true') { } else if ((config[option.name] as string) === 'false') {
logger.warn( logger.warn('env config dryRun property has been changed to null');
'env config requireConfig property has been changed to required', delete config[option.name];
); } else if ((config[option.name] as string) === 'null') {
config[option.name] = 'required'; delete config[option.name];
} else if ((config[option.name] as string) === 'false') { }
logger.warn( }
'env config requireConfig property has been changed to optional',
); if (option.name === 'requireConfig') {
config[option.name] = 'optional'; if ((config[option.name] as string) === 'true') {
} logger.warn(
} 'env config requireConfig property has been changed to required',
if (option.name === 'platformCommit') { );
if ((config[option.name] as string) === 'true') { config[option.name] = 'required';
logger.warn( } else if ((config[option.name] as string) === 'false') {
'env config platformCommit property has been changed to enabled', logger.warn(
); 'env config requireConfig property has been changed to optional',
config[option.name] = 'enabled'; );
} else if ((config[option.name] as string) === 'false') { config[option.name] = 'optional';
logger.warn( }
'env config platformCommit property has been changed to disabled', }
);
config[option.name] = 'disabled'; if (option.name === 'platformCommit') {
} if ((config[option.name] as string) === 'true') {
} logger.warn(
'env config platformCommit property has been changed to enabled',
);
config[option.name] = 'enabled';
} else if ((config[option.name] as string) === 'false') {
logger.warn(
'env config platformCommit property has been changed to disabled',
);
config[option.name] = 'disabled';
} }
} }
} }
}); }
if (env.GITHUB_COM_TOKEN) { if (env.GITHUB_COM_TOKEN) {
logger.debug(`Converting GITHUB_COM_TOKEN into a global host rule`); logger.debug(`Converting GITHUB_COM_TOKEN into a global host rule`);
@ -237,7 +228,31 @@ export async function getConfig(
'VSTS_TOKEN', 'VSTS_TOKEN',
]; ];
unsupportedEnv.forEach((val) => delete env[val]); for (const val of unsupportedEnv) {
delete env[val];
}
return config; return config;
} }
async function parseAndValidateOrExit(
env: NodeJS.ProcessEnv,
configEnvKey: string,
): Promise<AllConfig> {
if (!env[configEnvKey]) {
return {};
}
try {
const config = parseJson(
env[configEnvKey],
`${configEnvKey}.env.json5`,
) as AllConfig;
logger.debug({ config }, `Detected config in env ${configEnvKey}`);
return await migrateAndValidateConfig(config, `${configEnvKey}`);
} catch (err) {
logger.fatal({ err }, `Could not parse ${configEnvKey}`);
process.exit(1);
}
}