From 00cab7c3a7f9abb09a16bdc34c9681ed3bd04fea Mon Sep 17 00:00:00 2001
From: Maksim <m.v.sharipov@gmail.com>
Date: Tue, 15 Feb 2022 08:15:09 +0100
Subject: [PATCH] refactor(migrations): pathRules (#14203)

* refactor(migrations): pathRules

* refactor(migrations): fix typings & test

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
---
 .../__snapshots__/migration.spec.ts.snap      | 16 ++--
 lib/config/migration.ts                       | 10 +--
 .../custom/path-rules-migration.spec.ts       | 86 +++++++++++++++++++
 .../migrations/custom/path-rules-migration.ts | 17 ++++
 lib/config/migrations/migrations-service.ts   |  2 +
 5 files changed, 114 insertions(+), 17 deletions(-)
 create mode 100644 lib/config/migrations/custom/path-rules-migration.spec.ts
 create mode 100644 lib/config/migrations/custom/path-rules-migration.ts

diff --git a/lib/config/__snapshots__/migration.spec.ts.snap b/lib/config/__snapshots__/migration.spec.ts.snap
index eb7ccf4579..9bcf417796 100644
--- a/lib/config/__snapshots__/migration.spec.ts.snap
+++ b/lib/config/__snapshots__/migration.spec.ts.snap
@@ -153,14 +153,6 @@ Object {
   },
   "onboarding": false,
   "packageRules": Array [
-    Object {
-      "extends": Array [
-        "foo",
-      ],
-      "matchPaths": Array [
-        "examples/**",
-      ],
-    },
     Object {
       "excludePackageNames": "foo",
       "groupName": "angular packages",
@@ -196,6 +188,14 @@ Object {
         "foo",
       ],
     },
+    Object {
+      "extends": Array [
+        "foo",
+      ],
+      "matchPaths": Array [
+        "examples/**",
+      ],
+    },
     Object {
       "matchDepTypes": Array [
         "peerDependencies",
diff --git a/lib/config/migration.ts b/lib/config/migration.ts
index d17bce97ff..d2b7ff357f 100644
--- a/lib/config/migration.ts
+++ b/lib/config/migration.ts
@@ -46,15 +46,7 @@ export function migrateConfig(
     ];
     const { migratePresets } = GlobalConfig.get();
     for (const [key, val] of Object.entries(newConfig)) {
-      if (key === 'pathRules') {
-        if (is.array(val)) {
-          migratedConfig.packageRules = is.array(migratedConfig.packageRules)
-            ? migratedConfig.packageRules
-            : [];
-          migratedConfig.packageRules = val.concat(migratedConfig.packageRules);
-        }
-        delete migratedConfig.pathRules;
-      } else if (key === 'suppressNotifications') {
+      if (key === 'suppressNotifications') {
         if (is.nonEmptyArray(val) && val.includes('prEditNotification')) {
           migratedConfig.suppressNotifications =
             migratedConfig.suppressNotifications.filter(
diff --git a/lib/config/migrations/custom/path-rules-migration.spec.ts b/lib/config/migrations/custom/path-rules-migration.spec.ts
new file mode 100644
index 0000000000..e7ab026919
--- /dev/null
+++ b/lib/config/migrations/custom/path-rules-migration.spec.ts
@@ -0,0 +1,86 @@
+import { PathRulesMigration } from './path-rules-migration';
+
+describe('config/migrations/custom/path-rules-migration', () => {
+  it('should migrate to packageRules', () => {
+    expect(PathRulesMigration).toMigrate(
+      {
+        pathRules: [
+          {
+            paths: ['examples/**'],
+            extends: ['foo'],
+          },
+        ],
+      },
+      {
+        packageRules: [
+          {
+            paths: ['examples/**'],
+            extends: ['foo'],
+          },
+        ],
+      }
+    );
+  });
+
+  it('should rewrite packageRules when it is not array', () => {
+    expect(PathRulesMigration).toMigrate(
+      {
+        packageRules: 'test',
+        pathRules: [
+          {
+            paths: ['examples/**'],
+            extends: ['foo'],
+          },
+        ],
+      } as any,
+      {
+        packageRules: [
+          {
+            paths: ['examples/**'],
+            extends: ['foo'],
+          },
+        ],
+      }
+    );
+  });
+
+  it('should not migrate non array value', () => {
+    expect(PathRulesMigration).toMigrate(
+      {
+        pathRules: 'test',
+      },
+      {}
+    );
+  });
+
+  it('should concat with existing package rules', () => {
+    expect(PathRulesMigration).toMigrate(
+      {
+        pathRules: [
+          {
+            paths: ['examples/**'],
+            extends: ['foo'],
+          },
+        ],
+        packageRules: [
+          {
+            packageNames: ['guava'],
+            versionScheme: 'maven',
+          },
+        ],
+      },
+      {
+        packageRules: [
+          {
+            packageNames: ['guava'],
+            versionScheme: 'maven',
+          },
+          {
+            paths: ['examples/**'],
+            extends: ['foo'],
+          },
+        ],
+      }
+    );
+  });
+});
diff --git a/lib/config/migrations/custom/path-rules-migration.ts b/lib/config/migrations/custom/path-rules-migration.ts
new file mode 100644
index 0000000000..1aaff3d89f
--- /dev/null
+++ b/lib/config/migrations/custom/path-rules-migration.ts
@@ -0,0 +1,17 @@
+import { AbstractMigration } from '../base/abstract-migration';
+
+export class PathRulesMigration extends AbstractMigration {
+  override readonly deprecated = true;
+  override readonly propertyName = 'pathRules';
+
+  override run(value: unknown): void {
+    const packageRules = this.get('packageRules');
+
+    if (Array.isArray(value)) {
+      this.setHard(
+        'packageRules',
+        Array.isArray(packageRules) ? packageRules.concat(value) : value
+      );
+    }
+  }
+}
diff --git a/lib/config/migrations/migrations-service.ts b/lib/config/migrations/migrations-service.ts
index 01685187b3..accd7d5b56 100644
--- a/lib/config/migrations/migrations-service.ts
+++ b/lib/config/migrations/migrations-service.ts
@@ -9,6 +9,7 @@ import { EnabledManagersMigration } from './custom/enabled-managers-migration';
 import { GoModTidyMigration } from './custom/go-mod-tidy-migration';
 import { HostRulesMigration } from './custom/host-rules-migration';
 import { IgnoreNodeModulesMigration } from './custom/ignore-node-modules-migration';
+import { PathRulesMigration } from './custom/path-rules-migration';
 import { PinVersionsMigration } from './custom/pin-versions-migration';
 import { RaiseDeprecationWarningsMigration } from './custom/raise-deprecation-warnings-migration';
 import { RebaseConflictedPrs } from './custom/rebase-conflicted-prs-migration';
@@ -56,6 +57,7 @@ export class MigrationsService {
     GoModTidyMigration,
     HostRulesMigration,
     IgnoreNodeModulesMigration,
+    PathRulesMigration,
     PinVersionsMigration,
     RaiseDeprecationWarningsMigration,
     RebaseConflictedPrs,
-- 
GitLab