From 74d77db65d0cad0bca0f91da566308a0789f57c9 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Sat, 18 Apr 2020 16:39:02 +0200
Subject: [PATCH] fix(internal): deduplicate updates during branchify

---
 .../repository/updates/branchify.spec.ts      | 27 +++++++++++++++
 lib/workers/repository/updates/branchify.ts   | 33 +++++++++++++++++++
 2 files changed, 60 insertions(+)

diff --git a/lib/workers/repository/updates/branchify.spec.ts b/lib/workers/repository/updates/branchify.spec.ts
index fc76231228..7c3812abd0 100644
--- a/lib/workers/repository/updates/branchify.spec.ts
+++ b/lib/workers/repository/updates/branchify.spec.ts
@@ -38,6 +38,33 @@ describe('workers/repository/updates/branchify', () => {
       expect(res.branches[0].isMinor).toBe(true);
       expect(res.branches[0].upgrades[0].isMinor).toBe(true);
     });
+    it('deduplicates', async () => {
+      flattenUpdates.mockResolvedValueOnce([
+        {
+          depName: 'foo',
+          branchName: 'foo-{{version}}',
+          currentValue: '1.1.0',
+          newValue: '1.3.0',
+          prTitle: 'some-title',
+          updateType: 'minor',
+          packageFile: 'foo/package.json',
+        },
+        {
+          depName: 'foo',
+          branchName: 'foo-{{version}}',
+          currentValue: '1.1.0',
+          newValue: '1.2.0',
+          prTitle: 'some-title',
+          updateType: 'minor',
+          packageFile: 'foo/package.json',
+        },
+      ]);
+      config.repoIsOnboarded = true;
+      const res = await branchifyUpgrades(config, {});
+      expect(Object.keys(res.branches)).toHaveLength(1);
+      expect(res.branches[0].isMinor).toBe(true);
+      expect(res.branches[0].upgrades[0].isMinor).toBe(true);
+    });
     it('uses major/minor/patch slugs', async () => {
       flattenUpdates.mockResolvedValueOnce([
         {
diff --git a/lib/workers/repository/updates/branchify.ts b/lib/workers/repository/updates/branchify.ts
index e70de9771a..686fdde0fc 100644
--- a/lib/workers/repository/updates/branchify.ts
+++ b/lib/workers/repository/updates/branchify.ts
@@ -117,6 +117,39 @@ export async function branchifyUpgrades(
     for (const upgrade of branchUpgrades[branchName]) {
       upgrade.logJSON = await getChangeLogJSON(upgrade);
     }
+    const seenUpdates = {};
+    // Filter out duplicates
+    branchUpgrades[branchName] = branchUpgrades[branchName].filter(
+      (upgrade) => {
+        const {
+          manager,
+          packageFile,
+          depName,
+          currentValue,
+          newValue,
+        } = upgrade;
+        const upgradeKey = `${packageFile}:${depName}:${currentValue}`;
+        const previousNewValue = seenUpdates[upgradeKey];
+        if (previousNewValue) {
+          if (previousNewValue !== newValue) {
+            logger.info(
+              {
+                manager,
+                packageFile,
+                depName,
+                currentValue,
+                previousNewValue,
+                thisNewValue: newValue,
+              },
+              'Ignoring upgrade collision'
+            );
+          }
+          return false;
+        }
+        seenUpdates[upgradeKey] = newValue;
+        return true;
+      }
+    );
     const branch = generateBranchConfig(branchUpgrades[branchName]);
     branch.branchName = branchName;
     branches.push(branch);
-- 
GitLab