Skip to content

Commit

Permalink
refactor(config/presets): for loop of resolveConfigPresets (#16677)
Browse files Browse the repository at this point in the history
  • Loading branch information
hasanwhitesource committed Jul 22, 2022
1 parent 2241daa commit 9ca19db
Showing 1 changed file with 76 additions and 51 deletions.
127 changes: 76 additions & 51 deletions lib/config/presets/index.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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<AllConfig> {
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;
}

0 comments on commit 9ca19db

Please sign in to comment.