From a25eca7cb139a9c754e990447e62a3777fa03651 Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Sat, 1 May 2021 21:54:41 +0200 Subject: [PATCH] fix(config): nested packageRules migration --- .../__snapshots__/migration.spec.ts.snap | 50 +++++++++++++++---- lib/config/migration.spec.ts | 31 ++++++++++++ lib/config/migration.ts | 20 ++------ 3 files changed, 77 insertions(+), 24 deletions(-) diff --git a/lib/config/__snapshots__/migration.spec.ts.snap b/lib/config/__snapshots__/migration.spec.ts.snap index 48456d788edf78..e93dd6c5d204f1 100644 --- a/lib/config/__snapshots__/migration.spec.ts.snap +++ b/lib/config/__snapshots__/migration.spec.ts.snap @@ -1,5 +1,37 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`config/migration it migrates nested packageRules 1`] = ` +Object { + "packageRules": Array [ + Object { + "enabled": false, + "matchDepTypes": Array [ + "devDependencies", + ], + }, + Object { + "automerge": true, + "excludePackageNames": Array [ + "@types/react-table", + ], + "groupName": "definitelyTyped", + "matchPackagePrefixes": Array [ + "@types/", + ], + }, + Object { + "automerge": false, + "excludePackageNames": Array [ + "@types/react-table", + ], + "matchDepTypes": Array [ + "dependencies", + ], + }, + ], +} +`; + exports[`config/migration migrateConfig(config, parentConfig) does not migrate multi days 1`] = ` Object { "schedule": "after 5:00pm on wednesday and thursday", @@ -169,6 +201,15 @@ Object { ], "versioning": "maven", }, + Object { + "automerge": true, + "matchDepTypes": Array [ + "bar", + ], + "matchPackageNames": Array [ + "foo", + ], + }, Object { "matchDepTypes": Array [ "peerDependencies", @@ -200,15 +241,6 @@ Object { "respectLatest": false, "schedule": "before 5am on Monday", }, - Object { - "automerge": true, - "matchDepTypes": Array [ - "bar", - ], - "matchPackageNames": Array [ - "foo", - ], - }, ], "patch": Object { "automerge": true, diff --git a/lib/config/migration.spec.ts b/lib/config/migration.spec.ts index 7bd348c5ea4996..f3d6787ec887fc 100644 --- a/lib/config/migration.spec.ts +++ b/lib/config/migration.spec.ts @@ -626,4 +626,35 @@ describe(getName(__filename), () => { expect(migratedConfig).toMatchSnapshot(); }); }); + it('it migrates nested packageRules', () => { + const config: RenovateConfig = { + packageRules: [ + { + matchDepTypes: ['devDependencies'], + enabled: false, + }, + { + automerge: true, + excludePackageNames: ['@types/react-table'], + packageRules: [ + { + groupName: 'definitelyTyped', + matchPackagePrefixes: ['@types/'], + }, + { + matchDepTypes: ['dependencies'], + automerge: false, + }, + ], + }, + ], + }; + const { isMigrated, migratedConfig } = configMigration.migrateConfig( + config, + defaultConfig + ); + expect(isMigrated).toBe(true); + expect(migratedConfig).toMatchSnapshot(); + expect(migratedConfig.packageRules).toHaveLength(3); + }); }); diff --git a/lib/config/migration.ts b/lib/config/migration.ts index 8b4f7ad5b18d5b..0cab13bdb6e2cc 100644 --- a/lib/config/migration.ts +++ b/lib/config/migration.ts @@ -546,7 +546,9 @@ export function migrateConfig( } // Migrate nested packageRules if (is.nonEmptyArray(migratedConfig.packageRules)) { - for (const packageRule of migratedConfig.packageRules) { + const existingRules = migratedConfig.packageRules; + migratedConfig.packageRules = []; + for (const packageRule of existingRules) { if (is.array(packageRule.packageRules)) { logger.debug('Flattening nested packageRules'); // merge each subrule and add to the parent list @@ -555,22 +557,10 @@ export function migrateConfig( delete combinedRule.packageRules; migratedConfig.packageRules.push(combinedRule); } - // delete the nested packageRules - delete packageRule.packageRules; - // mark the original rule for deletion if it's now pointless - if ( - !Object.keys(packageRule).some( - (key) => !key.startsWith('match') && !key.startsWith('exclude') - ) - ) { - packageRule._delete = true; - } + } else { + migratedConfig.packageRules.push(packageRule); } } - // filter out any rules which were marked for deletion in the previous step - migratedConfig.packageRules = migratedConfig.packageRules.filter( - (rule) => !rule._delete - ); } const isMigrated = !dequal(config, migratedConfig); if (isMigrated) {