From 93521cac744d858585552c228e86a53ec2acc6a2 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Fri, 19 Jan 2018 05:46:28 +0100
Subject: [PATCH] fix: check for not-pending only if no existing PR exists

Closes #1408
---
 lib/workers/pr/index.js       | 6 +++---
 test/workers/pr/index.spec.js | 4 +++-
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/lib/workers/pr/index.js b/lib/workers/pr/index.js
index 3a39a78200..8bfdd4d85f 100644
--- a/lib/workers/pr/index.js
+++ b/lib/workers/pr/index.js
@@ -17,6 +17,8 @@ async function ensurePr(prConfig) {
   logger.trace({ config }, 'ensurePr');
   // If there is a group, it will use the config of the first upgrade in the array
   const { branchName, upgrades } = config;
+  // Check if existing PR exists
+  const existingPr = await platform.getBranchPr(branchName);
   config.upgrades = [];
   const branchStatus = await platform.getBranchStatus(
     branchName,
@@ -41,7 +43,7 @@ async function ensurePr(prConfig) {
       return null;
     }
     logger.debug('Branch status success');
-  } else if (config.prCreation === 'not-pending') {
+  } else if (config.prCreation === 'not-pending' && !existingPr) {
     logger.debug('Checking branch combined status');
     if (branchStatus === 'pending' || branchStatus === 'running') {
       logger.debug(`Branch status is "${branchStatus}" - checking timeout`);
@@ -157,8 +159,6 @@ async function ensurePr(prConfig) {
   }
 
   try {
-    // Check if existing PR exists
-    const existingPr = await platform.getBranchPr(branchName);
     if (existingPr) {
       if (config.automerge && branchStatus === 'failure') {
         logger.debug(`Setting assignees and reviewers as status checks failed`);
diff --git a/test/workers/pr/index.spec.js b/test/workers/pr/index.spec.js
index 50d8ffb8fb..231a56e37c 100644
--- a/test/workers/pr/index.spec.js
+++ b/test/workers/pr/index.spec.js
@@ -107,9 +107,11 @@ describe('workers/pr', () => {
       jest.clearAllMocks();
     });
     it('should return null if check fails', async () => {
-      platform.getBranchPr.mockImplementationOnce(() => {
+      platform.updatePr.mockImplementationOnce(() => {
         throw new Error('oops');
       });
+      config.newVersion = '1.2.0';
+      platform.getBranchPr.mockReturnValueOnce(existingPr);
       const pr = await prWorker.ensurePr(config);
       expect(pr).toBe(null);
     });
-- 
GitLab