From f5b93de82d9c73e7b27ae24d667a192e8f6be890 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Wed, 5 Sep 2018 06:23:55 +0200
Subject: [PATCH] refactor(github): simplify getAllRenovateBranches

---
 lib/platform/github/storage.js                | 39 ++++++-------------
 .../github/__snapshots__/index.spec.js.snap   |  8 ----
 test/platform/github/index.spec.js            | 11 ++----
 3 files changed, 16 insertions(+), 42 deletions(-)

diff --git a/lib/platform/github/storage.js b/lib/platform/github/storage.js
index 2d747db259..8c219e0287 100644
--- a/lib/platform/github/storage.js
+++ b/lib/platform/github/storage.js
@@ -106,34 +106,19 @@ class Storage {
 
     async function getAllRenovateBranches(branchPrefix) {
       logger.trace('getAllRenovateBranches');
-      try {
-        const allBranches = (await get(
-          `repos/${config.repository}/git/refs/heads/${branchPrefix}`,
-          {
-            paginate: true,
-          }
-        )).body;
-        return allBranches.reduce((arr, branch) => {
-          if (branch.ref.startsWith(`refs/heads/${branchPrefix}`)) {
-            arr.push(branch.ref.substring('refs/heads/'.length));
-          }
-          if (
-            branchPrefix.endsWith('/') &&
-            branch.ref === `refs/heads/${branchPrefix.slice(0, -1)}`
-          ) {
-            logger.warn(
-              `Pruning branch "${branchPrefix.slice(
-                0,
-                -1
-              )}" so that it does not block PRs`
-            );
-            arr.push(branch.ref.substring('refs/heads/'.length));
-          }
-          return arr;
-        }, []);
-      } catch (err) /* istanbul ignore next */ {
-        return [];
+      const allBranches = await getBranchList();
+      if (branchPrefix.endsWith('/')) {
+        const branchPrefixPrefix = branchPrefix.slice(0, -1);
+        if (allBranches.includes(branchPrefixPrefix)) {
+          logger.warn(
+            `Pruning branch "${branchPrefixPrefix}" so that it does not block PRs`
+          );
+          await deleteBranch(branchPrefixPrefix);
+        }
       }
+      return allBranches.filter(branchName =>
+        branchName.startsWith(branchPrefix)
+      );
     }
 
     async function isBranchStale(branchName) {
diff --git a/test/platform/github/__snapshots__/index.spec.js.snap b/test/platform/github/__snapshots__/index.spec.js.snap
index bec94cc416..fb9fb12609 100644
--- a/test/platform/github/__snapshots__/index.spec.js.snap
+++ b/test/platform/github/__snapshots__/index.spec.js.snap
@@ -190,14 +190,6 @@ content",
 ]
 `;
 
-exports[`platform/github getAllRenovateBranches() should return all renovate branches 1`] = `
-Array [
-  "renovate/a",
-  "renovate",
-  "renovate/b",
-]
-`;
-
 exports[`platform/github getBranchLastCommitTime should return a Date 1`] = `2011-04-14T16:00:49.000Z`;
 
 exports[`platform/github getBranchPr(branchName) should return the PR object 1`] = `
diff --git a/test/platform/github/index.spec.js b/test/platform/github/index.spec.js
index f4532b1750..5a55507ef8 100644
--- a/test/platform/github/index.spec.js
+++ b/test/platform/github/index.spec.js
@@ -524,21 +524,18 @@ describe('platform/github', () => {
       get.mockImplementationOnce(() => ({
         body: [
           {
-            ref: 'refs/heads/renovate/a',
-          },
-          {
-            ref: 'refs/heads/master',
+            name: 'thebranchname',
           },
           {
-            ref: 'refs/heads/renovate',
+            name: 'renovate',
           },
           {
-            ref: 'refs/heads/renovate/b',
+            name: 'renovate/abc-1.x',
           },
         ],
       }));
       const res = await github.getAllRenovateBranches('renovate/');
-      expect(res).toMatchSnapshot();
+      expect(res).toHaveLength(1);
     });
   });
   describe('isBranchStale(branchName)', () => {
-- 
GitLab