From f3daa9e555b653b886d0d9a3085b70fc0d5a18d5 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Sun, 28 Feb 2021 08:36:13 +0100
Subject: [PATCH] =?UTF-8?q?fix:=20don=E2=80=99t=20skip=20modified=20branch?=
 =?UTF-8?q?=20from=20old=20PR=20(#8885)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 lib/workers/branch/index.spec.ts | 16 ++++++++++++++++
 lib/workers/branch/index.ts      | 25 +++++++++++++++++++++++--
 2 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/lib/workers/branch/index.spec.ts b/lib/workers/branch/index.spec.ts
index 0c61dd50df..0e328b0ee5 100644
--- a/lib/workers/branch/index.spec.ts
+++ b/lib/workers/branch/index.spec.ts
@@ -219,6 +219,22 @@ describe('workers/branch', () => {
       const res = await branchWorker.processBranch(config);
       expect(res).toEqual(ProcessBranchResult.PrEdited);
     });
+    it('continues branch if branch edited and but PR found', async () => {
+      git.branchExists.mockReturnValueOnce(true);
+      git.isBranchModified.mockResolvedValueOnce(true);
+      git.getBranchCommit.mockReturnValueOnce('abc123');
+      platform.findPr.mockResolvedValueOnce({ sha: 'abc123' } as any);
+      const res = await branchWorker.processBranch(config);
+      expect(res).toEqual(ProcessBranchResult.Error);
+    });
+    it('skips branch if branch edited and and PR found with sha mismatch', async () => {
+      git.branchExists.mockReturnValueOnce(true);
+      git.isBranchModified.mockResolvedValueOnce(true);
+      git.getBranchCommit.mockReturnValueOnce('abc123');
+      platform.findPr.mockResolvedValueOnce({ sha: 'def456' } as any);
+      const res = await branchWorker.processBranch(config);
+      expect(res).toEqual(ProcessBranchResult.PrEdited);
+    });
     it('returns if branch creation limit exceeded', async () => {
       getUpdated.getUpdatedPackageFiles.mockResolvedValueOnce({
         ...updatedPackageFiles,
diff --git a/lib/workers/branch/index.ts b/lib/workers/branch/index.ts
index 512c606519..25f6bf3824 100644
--- a/lib/workers/branch/index.ts
+++ b/lib/workers/branch/index.ts
@@ -25,6 +25,7 @@ import { readLocalFile, writeLocalFile } from '../../util/fs';
 import {
   checkoutBranch,
   deleteBranch,
+  getBranchCommit,
   getRepoStatus,
   branchExists as gitBranchExists,
   isBranchModified,
@@ -208,8 +209,28 @@ export async function processBranch(
           }
         }
       } else if (branchIsModified) {
-        logger.debug('Branch has been edited');
-        return ProcessBranchResult.PrEdited;
+        const oldPr = await platform.findPr({
+          branchName: config.branchName,
+          state: PrState.NotOpen,
+        });
+        if (!oldPr) {
+          logger.debug('Branch has been edited but found no PR - skipping');
+          return ProcessBranchResult.PrEdited;
+        }
+        const branchSha = getBranchCommit(config.branchName);
+        const oldPrSha = oldPr?.sha;
+        if (!oldPrSha || oldPrSha === branchSha) {
+          logger.debug(
+            { oldPrNumber: oldPr.number, oldPrSha, branchSha },
+            'Found old PR matching this branch - will override it'
+          );
+        } else {
+          logger.debug(
+            { oldPrNumber: oldPr.number, oldPrSha, branchSha },
+            'Found old PR but the SHA is different'
+          );
+          return ProcessBranchResult.PrEdited;
+        }
       }
     }
 
-- 
GitLab