diff --git a/lib/platform/github/storage.js b/lib/platform/github/storage.js index 2d747db259748ed4b0eff1eaddd3e8d3e94da05b..8c219e028732550e25a3fe328d2c22cc31b6376d 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 bec94cc416036196ab52325c9adbd2b92a369c11..fb9fb126093cea58dc582debc305b344e0f59579 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 f4532b175037d6839f49c1c5143b24cbcda6baaa..5a55507ef8aded7b84160d06e4b6f8db7a485cb2 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)', () => {