Skip to content

Commit

Permalink
fix(config): nested packageRules migration (#9814)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed May 1, 2021
1 parent 915bd15 commit ffd0269
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 24 deletions.
50 changes: 41 additions & 9 deletions 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",
Expand Down Expand Up @@ -173,6 +205,15 @@ Object {
],
"versioning": "maven",
},
Object {
"automerge": true,
"matchDepTypes": Array [
"bar",
],
"matchPackageNames": Array [
"foo",
],
},
Object {
"matchDepTypes": Array [
"peerDependencies",
Expand Down Expand Up @@ -204,15 +245,6 @@ Object {
"respectLatest": false,
"schedule": "before 5am on Monday",
},
Object {
"automerge": true,
"matchDepTypes": Array [
"bar",
],
"matchPackageNames": Array [
"foo",
],
},
],
"patch": Object {
"automerge": true,
Expand Down
31 changes: 31 additions & 0 deletions lib/config/migration.spec.ts
Expand Up @@ -628,4 +628,35 @@ describe(getName(), () => {
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);
});
});
20 changes: 5 additions & 15 deletions lib/config/migration.ts
Expand Up @@ -554,7 +554,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
Expand All @@ -563,22 +565,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) {
Expand Down

0 comments on commit ffd0269

Please sign in to comment.