From fa48a7fdeff2cad5df0bb09a896b3d1838ffb821 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@keylocation.sg>
Date: Thu, 19 Oct 2017 07:36:09 +0200
Subject: [PATCH] feat: rename open prs before autoclosing (#990)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This is a failsafe against Renovate bugs and potentially also mistakes on the user’s part. Before deleting/pruning any branches, Renovate will rename the PR’s title so that it should not block future PRs.

Closes #989
---
 lib/api/github.js                       | 7 ++++++-
 lib/workers/repository/cleanup.js       | 5 +++++
 test/workers/repository/cleanup.spec.js | 6 +++++-
 3 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/lib/api/github.js b/lib/api/github.js
index cc0ab896a9..8615d258bf 100644
--- a/lib/api/github.js
+++ b/lib/api/github.js
@@ -621,8 +621,13 @@ async function getPr(prNo) {
 }
 
 async function updatePr(prNo, title, body) {
+  logger.debug(`updatePr(${prNo}, ${title}, body)`);
+  const patchBody = { title };
+  if (body) {
+    patchBody.body = body;
+  }
   await get.patch(`repos/${config.repoName}/pulls/${prNo}`, {
-    body: { title, body },
+    body: patchBody,
   });
 }
 
diff --git a/lib/workers/repository/cleanup.js b/lib/workers/repository/cleanup.js
index 559aadb7ad..b58232b1af 100644
--- a/lib/workers/repository/cleanup.js
+++ b/lib/workers/repository/cleanup.js
@@ -56,6 +56,11 @@ async function pruneStaleBranches(config, branchList) {
   }
   for (const branchName of remainingBranches) {
     logger.debug({ branch: branchName }, `Deleting orphan branch`);
+    const pr = await config.api.findPr(branchName, null, 'open');
+    if (pr) {
+      logger.info({ prNo: pr.number, prTitle: pr.title }, 'Autoclosing PR');
+      await config.api.updatePr(pr.number, `${pr.title} - autoclosed`);
+    }
     await config.api.deleteBranch(branchName);
   }
 }
diff --git a/test/workers/repository/cleanup.spec.js b/test/workers/repository/cleanup.spec.js
index 756404eb8f..5164214118 100644
--- a/test/workers/repository/cleanup.spec.js
+++ b/test/workers/repository/cleanup.spec.js
@@ -13,6 +13,8 @@ describe('workers/repository/cleanup', () => {
         getAllRenovateBranches: jest.fn(),
         getPr: jest.fn(),
         deleteBranch: jest.fn(),
+        findPr: jest.fn(),
+        updatePr: jest.fn(),
       };
       config.logger = logger;
     });
@@ -27,14 +29,16 @@ describe('workers/repository/cleanup', () => {
       await cleanup.pruneStaleBranches(config, branchNames);
       expect(config.api.getAllRenovateBranches.mock.calls).toHaveLength(1);
     });
-    it('deletes remaining branch', async () => {
+    it('renames deletes remaining branch', async () => {
       branchNames = ['renovate/a', 'renovate/b'];
       config.api.getAllRenovateBranches.mockReturnValueOnce(
         branchNames.concat(['renovate/c'])
       );
+      config.api.findPr.mockReturnValueOnce({});
       await cleanup.pruneStaleBranches(config, branchNames);
       expect(config.api.getAllRenovateBranches.mock.calls).toHaveLength(1);
       expect(config.api.deleteBranch.mock.calls).toHaveLength(1);
+      expect(config.api.updatePr.mock.calls).toHaveLength(1);
     });
     it('deletes lock file maintenance if pr is closed', async () => {
       branchNames = ['renovate/lock-file-maintenance'];
-- 
GitLab