From 1cf285d74440dc767a50f8f09ae3846c7e8b64c7 Mon Sep 17 00:00:00 2001 From: Bryan Lee <38807139+liby@users.noreply.github.com> Date: Fri, 11 Aug 2023 10:35:42 +0800 Subject: [PATCH 1/6] fix(util/cache): throw an error when the config cannot be found --- lib/workers/global/config/parse/file.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/workers/global/config/parse/file.ts b/lib/workers/global/config/parse/file.ts index ba255ecd10d794..add3547ff9135d 100644 --- a/lib/workers/global/config/parse/file.ts +++ b/lib/workers/global/config/parse/file.ts @@ -69,6 +69,10 @@ export async function getConfig(env: NodeJS.ProcessEnv): Promise { } else { // istanbul ignore next: we can ignore this logger.debug('No config file found on disk - skipping'); + if(config.onboarding && config.requireConfig === "required"){ + logger.fatal('No Config File Found '); + process.exit(1); + } } } From dae7636912fb89c7f2932ca65cf51542a4d27a45 Mon Sep 17 00:00:00 2001 From: Bryan Lee <38807139+liby@users.noreply.github.com> Date: Fri, 11 Aug 2023 19:54:37 +0800 Subject: [PATCH 2/6] feat(config-validator): enhance configuration file detection and validation - Refactor the code to use a single function for validating and setting a flag if a valid configuration file is found - Introduce a flag to track if any configuration files are found and exit with an error if none are detected - Improve code readability and reduce redundancy by encapsulating repeated logic --- lib/config-validator.ts | 31 +++++++++++++++++++++---- lib/workers/global/config/parse/file.ts | 4 ---- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/lib/config-validator.ts b/lib/config-validator.ts index eb2ca7a97795dc..4ec3d2307ab505 100644 --- a/lib/config-validator.ts +++ b/lib/config-validator.ts @@ -58,6 +58,17 @@ type PackageJson = { 'renovate-config'?: Record; }; +let isFoundConfigFile = false; +async function validateAndSetFlag( + file: string, + config: RenovateConfig, + strict: boolean, + isPreset = false +): Promise { + isFoundConfigFile = true; + await validate(file, config, strict, isPreset); +} + (async () => { const strictArgIndex = process.argv.indexOf('--strict'); const strict = strictArgIndex >= 0; @@ -75,7 +86,7 @@ type PackageJson = { const parsedContent = await getParsedContent(file); try { logger.info(`Validating ${file}`); - await validate(file, parsedContent, strict); + await validateAndSetFlag(file, parsedContent, strict); } catch (err) { logger.warn({ file, err }, 'File is not valid Renovate config'); returnVal = 1; @@ -96,7 +107,7 @@ type PackageJson = { const parsedContent = await getParsedContent(file); try { logger.info(`Validating ${file}`); - await validate(file, parsedContent, strict); + await validateAndSetFlag(file, parsedContent, strict); } catch (err) { logger.warn({ file, err }, 'File is not valid Renovate config'); returnVal = 1; @@ -112,12 +123,16 @@ type PackageJson = { ) as PackageJson; if (pkgJson.renovate) { logger.info(`Validating package.json > renovate`); - await validate('package.json > renovate', pkgJson.renovate, strict); + await validateAndSetFlag( + 'package.json > renovate', + pkgJson.renovate, + strict + ); } if (pkgJson['renovate-config']) { logger.info(`Validating package.json > renovate-config`); for (const presetConfig of Object.values(pkgJson['renovate-config'])) { - await validate( + await validateAndSetFlag( 'package.json > renovate-config', presetConfig, strict, @@ -134,7 +149,7 @@ type PackageJson = { const file = process.env.RENOVATE_CONFIG_FILE ?? 'config.js'; logger.info(`Validating ${file}`); try { - await validate(file, fileConfig, strict); + await validateAndSetFlag(file, fileConfig, strict); } catch (err) { logger.error({ file, err }, 'File is not valid Renovate config'); returnVal = 1; @@ -144,6 +159,12 @@ type PackageJson = { // ignore } } + + if (!isFoundConfigFile) { + logger.error('No configuration files found.'); + process.exit(1); + } + if (returnVal !== 0) { process.exit(returnVal); } diff --git a/lib/workers/global/config/parse/file.ts b/lib/workers/global/config/parse/file.ts index add3547ff9135d..ba255ecd10d794 100644 --- a/lib/workers/global/config/parse/file.ts +++ b/lib/workers/global/config/parse/file.ts @@ -69,10 +69,6 @@ export async function getConfig(env: NodeJS.ProcessEnv): Promise { } else { // istanbul ignore next: we can ignore this logger.debug('No config file found on disk - skipping'); - if(config.onboarding && config.requireConfig === "required"){ - logger.fatal('No Config File Found '); - process.exit(1); - } } } From 13ccbd07d4427b7bbfe9568b6e00677de9ddc5b3 Mon Sep 17 00:00:00 2001 From: Bryan Lee <38807139+liby@users.noreply.github.com> Date: Sun, 15 Oct 2023 15:26:32 +0800 Subject: [PATCH 3/6] Adjust validation logic to only enforce config file presence when `RENOVATE_CONFIG_FILE` is set --- lib/config-validator.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/config-validator.ts b/lib/config-validator.ts index 4ec3d2307ab505..1d8e7f6e532f7d 100644 --- a/lib/config-validator.ts +++ b/lib/config-validator.ts @@ -149,7 +149,11 @@ async function validateAndSetFlag( const file = process.env.RENOVATE_CONFIG_FILE ?? 'config.js'; logger.info(`Validating ${file}`); try { - await validateAndSetFlag(file, fileConfig, strict); + await validateAndSetFlag( + file, + fileConfig, + strict && !!process.env.RENOVATE_CONFIG_FILE + ); } catch (err) { logger.error({ file, err }, 'File is not valid Renovate config'); returnVal = 1; From 46e0bab516d63bf7fa5b2105ef94109aaab1bddb Mon Sep 17 00:00:00 2001 From: Bryan Lee <38807139+liby@users.noreply.github.com> Date: Mon, 23 Oct 2023 20:11:29 +0800 Subject: [PATCH 4/6] refactor(config-validator): integrate `validateAndSetFlag` into `validate` Merged the functionality of `validateAndSetFlag` directly into `validate` function to streamline the validation process. This change was suggested to simplify the codebase since validate was only being called from `validateAndSetFlag`. --- lib/config-validator.ts | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/lib/config-validator.ts b/lib/config-validator.ts index 1d8e7f6e532f7d..b7bc12f2854be0 100644 --- a/lib/config-validator.ts +++ b/lib/config-validator.ts @@ -15,6 +15,7 @@ import { } from './workers/global/config/parse/file'; let returnVal = 0; +let isFoundConfigFile = false; async function validate( desc: string, @@ -22,6 +23,8 @@ async function validate( strict: boolean, isPreset = false ): Promise { + isFoundConfigFile = true; + const { isMigrated, migratedConfig } = migrateConfig(config); if (isMigrated) { logger.warn( @@ -58,16 +61,6 @@ type PackageJson = { 'renovate-config'?: Record; }; -let isFoundConfigFile = false; -async function validateAndSetFlag( - file: string, - config: RenovateConfig, - strict: boolean, - isPreset = false -): Promise { - isFoundConfigFile = true; - await validate(file, config, strict, isPreset); -} (async () => { const strictArgIndex = process.argv.indexOf('--strict'); @@ -86,7 +79,7 @@ async function validateAndSetFlag( const parsedContent = await getParsedContent(file); try { logger.info(`Validating ${file}`); - await validateAndSetFlag(file, parsedContent, strict); + await validate(file, parsedContent, strict); } catch (err) { logger.warn({ file, err }, 'File is not valid Renovate config'); returnVal = 1; @@ -107,7 +100,7 @@ async function validateAndSetFlag( const parsedContent = await getParsedContent(file); try { logger.info(`Validating ${file}`); - await validateAndSetFlag(file, parsedContent, strict); + await validate(file, parsedContent, strict); } catch (err) { logger.warn({ file, err }, 'File is not valid Renovate config'); returnVal = 1; @@ -123,7 +116,7 @@ async function validateAndSetFlag( ) as PackageJson; if (pkgJson.renovate) { logger.info(`Validating package.json > renovate`); - await validateAndSetFlag( + await validate( 'package.json > renovate', pkgJson.renovate, strict @@ -132,7 +125,7 @@ async function validateAndSetFlag( if (pkgJson['renovate-config']) { logger.info(`Validating package.json > renovate-config`); for (const presetConfig of Object.values(pkgJson['renovate-config'])) { - await validateAndSetFlag( + await validate( 'package.json > renovate-config', presetConfig, strict, @@ -149,7 +142,7 @@ async function validateAndSetFlag( const file = process.env.RENOVATE_CONFIG_FILE ?? 'config.js'; logger.info(`Validating ${file}`); try { - await validateAndSetFlag( + await validate( file, fileConfig, strict && !!process.env.RENOVATE_CONFIG_FILE From 0946839471c42b8c2417ac42740167aae4e46cbe Mon Sep 17 00:00:00 2001 From: Bryan Lee <38807139+liby@users.noreply.github.com> Date: Mon, 23 Oct 2023 20:41:59 +0800 Subject: [PATCH 5/6] style: code style issues fixed --- lib/config-validator.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/config-validator.ts b/lib/config-validator.ts index b7bc12f2854be0..8933eb221c54ca 100644 --- a/lib/config-validator.ts +++ b/lib/config-validator.ts @@ -61,7 +61,6 @@ type PackageJson = { 'renovate-config'?: Record; }; - (async () => { const strictArgIndex = process.argv.indexOf('--strict'); const strict = strictArgIndex >= 0; @@ -116,11 +115,7 @@ type PackageJson = { ) as PackageJson; if (pkgJson.renovate) { logger.info(`Validating package.json > renovate`); - await validate( - 'package.json > renovate', - pkgJson.renovate, - strict - ); + await validate('package.json > renovate', pkgJson.renovate, strict); } if (pkgJson['renovate-config']) { logger.info(`Validating package.json > renovate-config`); From 4641ac26cce5327b536776bc6d915e230c92f7ec Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Wed, 15 Nov 2023 20:54:14 +0100 Subject: [PATCH 6/6] prettier-fix --- lib/config-validator.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/config-validator.ts b/lib/config-validator.ts index e3935da523b887..759773321d4133 100644 --- a/lib/config-validator.ts +++ b/lib/config-validator.ts @@ -140,7 +140,7 @@ type PackageJson = { await validate( file, fileConfig, - strict && !!process.env.RENOVATE_CONFIG_FILE + strict && !!process.env.RENOVATE_CONFIG_FILE, ); } catch (err) { logger.error({ file, err }, 'File is not valid Renovate config');