From 64cf41e76e1c28d868c6e7325f4b33ce8df3c468 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Sun, 28 Jan 2018 16:14:04 +0100
Subject: [PATCH] refactor: sort PRs by type and then title

---
 lib/workers/repository/index.js                 | 12 +++++++++---
 .../repository/__snapshots__/index.spec.js.snap | 17 +++++++++++++++++
 test/workers/repository/index.spec.js           | 16 +++++++++++++++-
 3 files changed, 41 insertions(+), 4 deletions(-)

diff --git a/lib/workers/repository/index.js b/lib/workers/repository/index.js
index b5c175ba3b..b451acf8f1 100644
--- a/lib/workers/repository/index.js
+++ b/lib/workers/repository/index.js
@@ -63,9 +63,15 @@ async function renovateRepository(repoConfig, token, loop = 1) {
       'major',
       'lockFileMaintenance',
     ];
-    config.branches.sort(
-      (a, b) => sortOrder.indexOf(a.type) - sortOrder.indexOf(b.type)
-    );
+    config.branches.sort((a, b) => {
+      const sortDiff = sortOrder.indexOf(a.type) - sortOrder.indexOf(b.type);
+      if (sortDiff !== 0) {
+        // type is different
+        return sortDiff;
+      }
+      // Sort by prTitle
+      return a.prTitle < b.prTitle ? -1 : 1;
+    });
     const res = config.repoIsOnboarded
       ? await writeUpdates(config)
       : await ensureOnboardingPr(config);
diff --git a/test/workers/repository/__snapshots__/index.spec.js.snap b/test/workers/repository/__snapshots__/index.spec.js.snap
index d617b64e2b..c0b0a555be 100644
--- a/test/workers/repository/__snapshots__/index.spec.js.snap
+++ b/test/workers/repository/__snapshots__/index.spec.js.snap
@@ -2,6 +2,23 @@
 
 exports[`workers/repository renovateRepository() ensures onboarding pr 1`] = `"onboarding"`;
 
+exports[`workers/repository renovateRepository() ensures onboarding pr 2`] = `
+Array [
+  Object {
+    "prTitle": "aaa",
+    "type": "pin",
+  },
+  Object {
+    "prTitle": "bbb",
+    "type": "pin",
+  },
+  Object {
+    "prTitle": "aaa",
+    "type": "minor",
+  },
+]
+`;
+
 exports[`workers/repository renovateRepository() exits after 6 loops 1`] = `"loops>5"`;
 
 exports[`workers/repository renovateRepository() handles baseBranches 1`] = `"onboarded"`;
diff --git a/test/workers/repository/index.spec.js b/test/workers/repository/index.spec.js
index 2dd664e9bd..4a821c5b99 100644
--- a/test/workers/repository/index.spec.js
+++ b/test/workers/repository/index.spec.js
@@ -41,11 +41,25 @@ describe('workers/repository', () => {
       initRepo.mockReturnValue({});
       determineUpdates.mockReturnValue({
         repoIsOnboarded: false,
-        branches: [],
+        branches: [
+          {
+            type: 'pin',
+            prTitle: 'bbb',
+          },
+          {
+            type: 'pin',
+            prTitle: 'aaa',
+          },
+          {
+            type: 'minor',
+            prTitle: 'aaa',
+          },
+        ],
       });
       ensureOnboardingPr.mockReturnValue('onboarding');
       const res = await renovateRepository(config, 'some-token');
       expect(res).toMatchSnapshot();
+      expect(ensureOnboardingPr.mock.calls[0][0].branches).toMatchSnapshot();
     });
     it('handles baseBranches', async () => {
       initRepo.mockReturnValue({ baseBranches: ['master', 'next'] });
-- 
GitLab