From 9ca19dbe249ab51aa973dbe45cc53ba589959e49 Mon Sep 17 00:00:00 2001 From: Hasan Awad <90554456+hasanwhitesource@users.noreply.github.com> Date: Fri, 22 Jul 2022 15:53:56 +0300 Subject: [PATCH] refactor(config/presets): for loop of `resolveConfigPresets` (#16677) --- lib/config/presets/index.ts | 127 +++++++++++++++++++++--------------- 1 file changed, 76 insertions(+), 51 deletions(-) diff --git a/lib/config/presets/index.ts b/lib/config/presets/index.ts index 9736ecc1c2c14f..24459e102cb960 100644 --- a/lib/config/presets/index.ts +++ b/lib/config/presets/index.ts @@ -286,58 +286,14 @@ export async function resolveConfigPresets( // First, merge all the preset configs from left to right if (inputConfig.extends?.length) { for (const preset of inputConfig.extends) { - // istanbul ignore if - if (existingPresets.includes(preset)) { - logger.debug( - `Already seen preset ${preset} in [${existingPresets.join(', ')}]` - ); - } else if (ignorePresets.includes(preset)) { - // istanbul ignore next - logger.debug( - `Ignoring preset ${preset} in [${existingPresets.join(', ')}]` - ); - } else { + if (shouldResolvePreset(preset, existingPresets, ignorePresets)) { logger.trace(`Resolving preset "${preset}"`); - let fetchedPreset: RenovateConfig; - try { - fetchedPreset = await getPreset(preset, baseConfig ?? inputConfig); - } catch (err) { - logger.debug({ preset, err }, 'Preset fetch error'); - // istanbul ignore if - if (err instanceof ExternalHostError) { - throw err; - } - // istanbul ignore if - if (err.message === PLATFORM_RATE_LIMIT_EXCEEDED) { - throw err; - } - const error = new Error(CONFIG_VALIDATION); - if (err.message === PRESET_DEP_NOT_FOUND) { - error.validationError = `Cannot find preset's package (${preset})`; - } else if (err.message === PRESET_RENOVATE_CONFIG_NOT_FOUND) { - error.validationError = `Preset package is missing a renovate-config entry (${preset})`; - } else if (err.message === PRESET_NOT_FOUND) { - error.validationError = `Preset name not found within published preset config (${preset})`; - } else if (err.message === PRESET_INVALID) { - error.validationError = `Preset is invalid (${preset})`; - } else if (err.message === PRESET_PROHIBITED_SUBPRESET) { - error.validationError = `Sub-presets cannot be combined with a custom path (${preset})`; - } else if (err.message === PRESET_INVALID_JSON) { - error.validationError = `Preset is invalid JSON (${preset})`; - } else { - error.validationError = `Preset caused unexpected error (${preset})`; - } - // istanbul ignore if - if (existingPresets.length) { - error.validationError += - '. Note: this is a *nested* preset so please contact the preset author if you are unable to fix it yourself.'; - } - logger.info( - { validationError: error.validationError }, - 'Throwing preset error' - ); - throw error; - } + const fetchedPreset = await fetchPreset( + preset, + baseConfig, + inputConfig, + existingPresets + ); const presetConfig = await resolveConfigPresets( fetchedPreset, baseConfig ?? inputConfig, @@ -392,3 +348,72 @@ export async function resolveConfigPresets( logger.trace({ config }, 'Resolved config'); return config; } + +async function fetchPreset( + preset: string, + baseConfig: RenovateConfig | undefined, + inputConfig: AllConfig, + existingPresets: string[] +): Promise { + try { + return await getPreset(preset, baseConfig ?? inputConfig); + } catch (err) { + logger.debug({ preset, err }, 'Preset fetch error'); + // istanbul ignore if + if (err instanceof ExternalHostError) { + throw err; + } + // istanbul ignore if + if (err.message === PLATFORM_RATE_LIMIT_EXCEEDED) { + throw err; + } + const error = new Error(CONFIG_VALIDATION); + if (err.message === PRESET_DEP_NOT_FOUND) { + error.validationError = `Cannot find preset's package (${preset})`; + } else if (err.message === PRESET_RENOVATE_CONFIG_NOT_FOUND) { + error.validationError = `Preset package is missing a renovate-config entry (${preset})`; + } else if (err.message === PRESET_NOT_FOUND) { + error.validationError = `Preset name not found within published preset config (${preset})`; + } else if (err.message === PRESET_INVALID) { + error.validationError = `Preset is invalid (${preset})`; + } else if (err.message === PRESET_PROHIBITED_SUBPRESET) { + error.validationError = `Sub-presets cannot be combined with a custom path (${preset})`; + } else if (err.message === PRESET_INVALID_JSON) { + error.validationError = `Preset is invalid JSON (${preset})`; + } else { + error.validationError = `Preset caused unexpected error (${preset})`; + } + // istanbul ignore if + if (existingPresets.length) { + error.validationError += + '. Note: this is a *nested* preset so please contact the preset author if you are unable to fix it yourself.'; + } + logger.info( + { validationError: error.validationError }, + 'Throwing preset error' + ); + throw error; + } +} + +function shouldResolvePreset( + preset: string, + existingPresets: string[], + ignorePresets: string[] +): boolean { + // istanbul ignore if + if (existingPresets.includes(preset)) { + logger.debug( + `Already seen preset ${preset} in [${existingPresets.join(', ')}]` + ); + return false; + } + if (ignorePresets.includes(preset)) { + // istanbul ignore next + logger.debug( + `Ignoring preset ${preset} in [${existingPresets.join(', ')}]` + ); + return false; + } + return true; +}