From 26cdabe57e9c9bb624495ff67986dcde9e8d1a92 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@keylocation.sg>
Date: Fri, 18 Aug 2017 19:47:13 +0200
Subject: [PATCH] fix: array objects should be migrated (#697)

---
 lib/config/migration.js                       | 19 +++++++++++++
 .../__snapshots__/migration.spec.js.snap      | 21 +++++++++++++++
 test/config/migration.spec.js                 | 27 ++++++++++++++++++-
 3 files changed, 66 insertions(+), 1 deletion(-)

diff --git a/lib/config/migration.js b/lib/config/migration.js
index b65c8500a6..59cbaf7435 100644
--- a/lib/config/migration.js
+++ b/lib/config/migration.js
@@ -94,12 +94,31 @@ function migrateConfig(config, parentConfig) {
       } else if (val === 'false') {
         migratedConfig[key] = false;
       }
+    } else if (
+      optionTypes[key] === 'string' &&
+      Array.isArray(val) &&
+      val.length === 1
+    ) {
+      migratedConfig[key] = `${val[0]}`;
     } else if (isObject(val)) {
       const subMigrate = migrateConfig(val);
       if (subMigrate.isMigrated) {
         isMigrated = true;
         migratedConfig[key] = subMigrate.migratedConfig;
       }
+    } else if (Array.isArray(val)) {
+      migratedConfig[key] = [];
+      for (const item of val) {
+        if (isObject(item)) {
+          const arrMigrate = migrateConfig(item);
+          migratedConfig[key].push(arrMigrate.migratedConfig);
+          if (arrMigrate.isMigrated) {
+            isMigrated = true;
+          }
+        } else {
+          migratedConfig[key].push(item);
+        }
+      }
     }
   }
   return { isMigrated, migratedConfig };
diff --git a/test/config/__snapshots__/migration.spec.js.snap b/test/config/__snapshots__/migration.spec.js.snap
index 6a70ff1546..74623d899b 100644
--- a/test/config/__snapshots__/migration.spec.js.snap
+++ b/test/config/__snapshots__/migration.spec.js.snap
@@ -11,6 +11,16 @@ Object {
     "respectLatest": false,
   },
   "packageRules": Array [
+    Object {
+      "groupName": "angular packages",
+      "packagePatterns": "^(@angular|typescript)",
+    },
+    Object {
+      "groupName": "foo",
+      "packagePatterns": Array [
+        "^foo",
+      ],
+    },
     Object {
       "enabled": false,
       "packageNames": Array [
@@ -29,6 +39,17 @@ Object {
 }
 `;
 
+exports[`config/migration migrateConfig(config, parentConfig) it migrates packages 1`] = `
+Object {
+  "packageRules": Array [
+    Object {
+      "groupName": "angular packages",
+      "packagePatterns": "^(@angular|typescript)",
+    },
+  ],
+}
+`;
+
 exports[`config/migration migrateConfig(config, parentConfig) it migrates subconfig 1`] = `
 Object {
   "lockFileMaintenance": Object {
diff --git a/test/config/migration.spec.js b/test/config/migration.spec.js
index 6d9307dc37..7952d0cbc5 100644
--- a/test/config/migration.spec.js
+++ b/test/config/migration.spec.js
@@ -15,7 +15,15 @@ describe('config/migration', () => {
         prTitle: '{{semanticPrefix}}some pr title',
         semanticPrefix: 'fix(deps): ',
         semanticCommits: false,
-        packages: [
+        packageRules: [
+          {
+            packagePatterns: '^(@angular|typescript)',
+            groupName: ['angular packages'],
+          },
+          {
+            packagePatterns: ['^foo'],
+            groupName: ['foo'],
+          },
           {
             packageName: 'angular',
             packagePattern: 'ang',
@@ -41,6 +49,23 @@ describe('config/migration', () => {
       expect(migratedConfig.automerge).toEqual('none');
       expect(migratedConfig).toMatchSnapshot();
     });
+    it('it migrates packages', () => {
+      const config = {
+        packages: [
+          {
+            packagePatterns: '^(@angular|typescript)',
+            groupName: ['angular packages'],
+          },
+        ],
+      };
+      const parentConfig = { ...defaultConfig };
+      const { isMigrated, migratedConfig } = configMigration.migrateConfig(
+        config,
+        parentConfig
+      );
+      expect(isMigrated).toBe(true);
+      expect(migratedConfig).toMatchSnapshot();
+    });
     it('it does not migrate config', () => {
       const config = {
         enabled: true,
-- 
GitLab