From fa68a5a216f5182ec48fdae560435ca3abfa39ea Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Sat, 20 Jan 2018 15:03:57 +0100
Subject: [PATCH] refactor: skip ignore notification comment if PR was merged

---
 lib/workers/branch/index.js       | 52 +++++++++++++++++--------------
 test/workers/branch/index.spec.js | 25 +++++++++++++--
 2 files changed, 50 insertions(+), 27 deletions(-)

diff --git a/lib/workers/branch/index.js b/lib/workers/branch/index.js
index 1079a085f4..a491e66098 100644
--- a/lib/workers/branch/index.js
+++ b/lib/workers/branch/index.js
@@ -40,30 +40,34 @@ async function processBranch(branchConfig) {
         { prTitle: config.prTitle },
         'Closed PR already exists. Skipping branch.'
       );
-      const subject = 'Renovate Ignore Notification';
-      let content;
-      if (config.isMajor) {
-        content = `As this PR has been closed unmerged, Renovate will ignore this upgrade and you will not receive PRs for *any* future ${
-          config.newVersionMajor
-        }.x releases. However, if you upgrade to ${
-          config.newVersionMajor
-        }.x manually then Renovate will then reenable updates for minor and patch updates automatically.`;
-      } else if (config.isDigest) {
-        content = `As this PR has been closed unmerged, Renovate will ignore this upgrade type and you will not receive PRs for *any* future ${
-          config.depName
-        }:${
-          config.currentTag
-        } digest updates. Digest updates will resume if you update the specified tag at any time.`;
-      } else {
-        content = `As this PR has been closed unmerged, Renovate will now ignore this update (${
-          config.newVersion
-        }). You will still receive a PR once a newer version is released, so if you wish to permanently ignore this dependency, please add it to the \`ignoreDeps\` array of your renovate config.`;
-      }
-      content +=
-        '\n\nIf this PR was closed by mistake or you changed your mind, you can simply reopen or rename it to reactivate Renovate for this dependency version.';
-      await platform.ensureComment(pr.number, subject, content);
-      if (branchExists) {
-        await platform.deleteBranch(config.branchName);
+      if (pr.state === 'closed') {
+        const subject = 'Renovate Ignore Notification';
+        let content;
+        if (config.isMajor) {
+          content = `As this PR has been closed unmerged, Renovate will ignore this upgrade and you will not receive PRs for *any* future ${
+            config.newVersionMajor
+          }.x releases. However, if you upgrade to ${
+            config.newVersionMajor
+          }.x manually then Renovate will then reenable updates for minor and patch updates automatically.`;
+        } else if (config.isDigest) {
+          content = `As this PR has been closed unmerged, Renovate will ignore this upgrade type and you will not receive PRs for *any* future ${
+            config.depName
+          }:${
+            config.currentTag
+          } digest updates. Digest updates will resume if you update the specified tag at any time.`;
+        } else {
+          content = `As this PR has been closed unmerged, Renovate will now ignore this update (${
+            config.newVersion
+          }). You will still receive a PR once a newer version is released, so if you wish to permanently ignore this dependency, please add it to the \`ignoreDeps\` array of your renovate config.`;
+        }
+        content +=
+          '\n\nIf this PR was closed by mistake or you changed your mind, you can simply reopen or rename it to reactivate Renovate for this dependency version.';
+        await platform.ensureComment(pr.number, subject, content);
+        if (branchExists) {
+          await platform.deleteBranch(config.branchName);
+        }
+      } else if (pr.state === 'merged') {
+        logger.info({ pr: pr.number }, 'Merged PR is blocking this branch');
       }
       return 'already-existed';
     }
diff --git a/test/workers/branch/index.spec.js b/test/workers/branch/index.spec.js
index 7b2627f40a..5a50498345 100644
--- a/test/workers/branch/index.spec.js
+++ b/test/workers/branch/index.spec.js
@@ -63,7 +63,10 @@ describe('workers/branch', () => {
       schedule.isScheduledNow.mockReturnValueOnce(false);
       platform.branchExists.mockReturnValueOnce(true);
       config.isMajor = true;
-      checkExisting.prAlreadyExisted.mockReturnValueOnce({ number: 13 });
+      checkExisting.prAlreadyExisted.mockReturnValueOnce({
+        number: 13,
+        state: 'closed',
+      });
       await branchWorker.processBranch(config);
       expect(parent.getParentBranch.mock.calls.length).toBe(0);
     });
@@ -71,14 +74,30 @@ describe('workers/branch', () => {
       schedule.isScheduledNow.mockReturnValueOnce(false);
       platform.branchExists.mockReturnValueOnce(true);
       config.isDigest = true;
-      checkExisting.prAlreadyExisted.mockReturnValueOnce({ number: 13 });
+      checkExisting.prAlreadyExisted.mockReturnValueOnce({
+        number: 13,
+        state: 'closed',
+      });
       await branchWorker.processBranch(config);
       expect(parent.getParentBranch.mock.calls.length).toBe(0);
     });
     it('skips branch if closed minor PR found', async () => {
       schedule.isScheduledNow.mockReturnValueOnce(false);
       platform.branchExists.mockReturnValueOnce(true);
-      checkExisting.prAlreadyExisted.mockReturnValueOnce({ number: 13 });
+      checkExisting.prAlreadyExisted.mockReturnValueOnce({
+        number: 13,
+        state: 'closed',
+      });
+      await branchWorker.processBranch(config);
+      expect(parent.getParentBranch.mock.calls.length).toBe(0);
+    });
+    it('skips branch if merged PR found', async () => {
+      schedule.isScheduledNow.mockReturnValueOnce(false);
+      platform.branchExists.mockReturnValueOnce(true);
+      checkExisting.prAlreadyExisted.mockReturnValueOnce({
+        number: 13,
+        state: 'merged',
+      });
       await branchWorker.processBranch(config);
       expect(parent.getParentBranch.mock.calls.length).toBe(0);
     });
-- 
GitLab