Skip to content

Commit

Permalink
feat(config-validator): add --strict option (#23677)
Browse files Browse the repository at this point in the history
  • Loading branch information
fgreinacher committed Aug 3, 2023
1 parent 52e229d commit 7b7670c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
3 changes: 3 additions & 0 deletions docs/usage/config-validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ INFO: Validating renovate.json
INFO: Config validated successfully
```

The validator program fails with a non-zero exit code if there are any validation warnings or errors.
You can pass the `--strict` flag to make it fail if a scanned config needs migration.

You can configure a [pre-commit](https://pre-commit.com) hook to validate your configuration automatically.
Please check out the [`renovatebot/pre-commit-hooks` repository](https://github.com/renovatebot/pre-commit-hooks) for more information.
24 changes: 19 additions & 5 deletions lib/config-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ let returnVal = 0;
async function validate(
desc: string,
config: RenovateConfig,
strict: boolean,
isPreset = false
): Promise<void> {
const { isMigrated, migratedConfig } = migrateConfig(config);
Expand All @@ -30,6 +31,9 @@ async function validate(
},
'Config migration necessary'
);
if (strict) {
returnVal = 1;
}
}
const massagedConfig = massageConfig(migratedConfig);
const res = await validateConfig(massagedConfig, isPreset);
Expand All @@ -55,6 +59,11 @@ type PackageJson = {
};

(async () => {
const strictArgIndex = process.argv.indexOf('--strict');
const strict = strictArgIndex >= 0;
if (strict) {
process.argv.splice(strictArgIndex);
}
if (process.argv.length > 2) {
for (const file of process.argv.slice(2)) {
try {
Expand All @@ -66,7 +75,7 @@ type PackageJson = {
const parsedContent = await getParsedContent(file);
try {
logger.info(`Validating ${file}`);
await validate(file, parsedContent);
await validate(file, parsedContent, strict);
} catch (err) {
logger.warn({ file, err }, 'File is not valid Renovate config');
returnVal = 1;
Expand All @@ -87,7 +96,7 @@ type PackageJson = {
const parsedContent = await getParsedContent(file);
try {
logger.info(`Validating ${file}`);
await validate(file, parsedContent);
await validate(file, parsedContent, strict);
} catch (err) {
logger.warn({ file, err }, 'File is not valid Renovate config');
returnVal = 1;
Expand All @@ -103,12 +112,17 @@ type PackageJson = {
) as PackageJson;
if (pkgJson.renovate) {
logger.info(`Validating package.json > renovate`);
await validate('package.json > renovate', pkgJson.renovate);
await validate('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('package.json > renovate-config', presetConfig, true);
await validate(
'package.json > renovate-config',
presetConfig,
strict,
true
);
}
}
} catch (err) {
Expand All @@ -120,7 +134,7 @@ type PackageJson = {
const file = process.env.RENOVATE_CONFIG_FILE ?? 'config.js';
logger.info(`Validating ${file}`);
try {
await validate(file, fileConfig);
await validate(file, fileConfig, strict);
} catch (err) {
logger.error({ file, err }, 'File is not valid Renovate config');
returnVal = 1;
Expand Down

0 comments on commit 7b7670c

Please sign in to comment.