diff --git a/lib/platform/github/index.js b/lib/platform/github/index.js index 3ef5226937dce83fe96dee4aab11e15da93d1e8b..350e20aa56f970172d3f54c43b06f140d8167c91 100644 --- a/lib/platform/github/index.js +++ b/lib/platform/github/index.js @@ -205,34 +205,12 @@ async function getFileList(branchName = config.baseBranch) { // Returns true if branch exists, otherwise false async function branchExists(branchName) { - logger.debug(`Checking if branch exists: ${branchName}`); - try { - const res = await get( - `repos/${config.repoName}/git/refs/heads/${branchName}` - ); - if (Array.isArray(res.body)) { - // This seems to happen if GitHub has partial matches, so we check ref - const matchedBranch = res.body.some( - branch => branch.ref === `refs/heads/${branchName}` - ); - if (matchedBranch) { - logger.debug('Branch exists'); - } else { - logger.debug('No matching branches'); - } - return matchedBranch; - } - // This should happen if there's an exact match - return res.body.ref === `refs/heads/${branchName}`; - } catch (error) { - if (error.statusCode === 404) { - // If file not found, then return false - logger.debug("Branch doesn't exist"); - return false; - } - // Propagate if it's any other error - throw error; - } + logger.debug(`branchExists(${branchName})`); + const branchList = (await get( + `repos/${config.repoName}/branches?per_page=100`, + { paginate: true } + )).body.map(branch => branch.name); + return branchList.includes(branchName); } async function getAllRenovateBranches(branchPrefix) { diff --git a/test/platform/github/__snapshots__/index.spec.js.snap b/test/platform/github/__snapshots__/index.spec.js.snap index 7eedf8da843aba20b32db8a68044a9a5636ddc85..1bd4e7c2a88e995c9c0d26ebd355eb411d0603e1 100644 --- a/test/platform/github/__snapshots__/index.spec.js.snap +++ b/test/platform/github/__snapshots__/index.spec.js.snap @@ -36,156 +36,6 @@ Array [ ] `; -exports[`platform/github branchExists(branchName) should propagate unknown errors 1`] = ` -Array [ - Array [ - "repos/some/repo", - Object { - "headers": Object { - "accept": "application/vnd.github.loki-preview+json", - }, - }, - ], - Array [ - "repos/some/repo/pulls?per_page=100&state=all", - Object { - "paginate": true, - }, - ], - Array [ - "repos/some/repo/git/trees/master?recursive=true", - ], - Array [ - "repos/some/repo/git/refs/heads/thebranchname", - ], -] -`; - -exports[`platform/github branchExists(branchName) should return false if a 404 is returned 1`] = ` -Array [ - Array [ - "repos/some/repo", - Object { - "headers": Object { - "accept": "application/vnd.github.loki-preview+json", - }, - }, - ], - Array [ - "repos/some/repo/pulls?per_page=100&state=all", - Object { - "paginate": true, - }, - ], - Array [ - "repos/some/repo/git/trees/master?recursive=true", - ], - Array [ - "repos/some/repo/git/refs/heads/thebranchname", - ], -] -`; - -exports[`platform/github branchExists(branchName) should return false if the branch does not exist (multiple results) 1`] = ` -Array [ - Array [ - "repos/some/repo", - Object { - "headers": Object { - "accept": "application/vnd.github.loki-preview+json", - }, - }, - ], - Array [ - "repos/some/repo/pulls?per_page=100&state=all", - Object { - "paginate": true, - }, - ], - Array [ - "repos/some/repo/git/trees/master?recursive=true", - ], - Array [ - "repos/some/repo/git/refs/heads/thebranchname", - ], -] -`; - -exports[`platform/github branchExists(branchName) should return false if the branch does not exist (one result) 1`] = ` -Array [ - Array [ - "repos/some/repo", - Object { - "headers": Object { - "accept": "application/vnd.github.loki-preview+json", - }, - }, - ], - Array [ - "repos/some/repo/pulls?per_page=100&state=all", - Object { - "paginate": true, - }, - ], - Array [ - "repos/some/repo/git/trees/master?recursive=true", - ], - Array [ - "repos/some/repo/git/refs/heads/thebranchname", - ], -] -`; - -exports[`platform/github branchExists(branchName) should return true if the branch exists (multiple results) 1`] = ` -Array [ - Array [ - "repos/some/repo", - Object { - "headers": Object { - "accept": "application/vnd.github.loki-preview+json", - }, - }, - ], - Array [ - "repos/some/repo/pulls?per_page=100&state=all", - Object { - "paginate": true, - }, - ], - Array [ - "repos/some/repo/git/trees/master?recursive=true", - ], - Array [ - "repos/some/repo/git/refs/heads/thebranchname", - ], -] -`; - -exports[`platform/github branchExists(branchName) should return true if the branch exists (one result) 1`] = ` -Array [ - Array [ - "repos/some/repo", - Object { - "headers": Object { - "accept": "application/vnd.github.loki-preview+json", - }, - }, - ], - Array [ - "repos/some/repo/pulls?per_page=100&state=all", - Object { - "paginate": true, - }, - ], - Array [ - "repos/some/repo/git/trees/master?recursive=true", - ], - Array [ - "repos/some/repo/git/refs/heads/thebranchname", - ], -] -`; - exports[`platform/github commitFilesToBranch(branchName, files, message, parentBranch) should add a commit to a new branch if the branch does not already exist 1`] = ` Array [ Array [ @@ -212,7 +62,10 @@ Array [ "repos/some/repo/git/commits/1111", ], Array [ - "repos/some/repo/git/refs/heads/package.json", + "repos/some/repo/branches?per_page=100", + Object { + "paginate": true, + }, ], ] `; @@ -260,7 +113,7 @@ Array [ "repos/some/repo/git/refs", Object { "body": Object { - "ref": "refs/heads/package.json", + "ref": "refs/heads/the-branch", "sha": "5555", }, }, @@ -296,7 +149,10 @@ Array [ "repos/some/repo/git/commits/1111", ], Array [ - "repos/some/repo/git/refs/heads/package.json", + "repos/some/repo/branches?per_page=100", + Object { + "paginate": true, + }, ], ] `; @@ -346,7 +202,7 @@ Array [ exports[`platform/github commitFilesToBranch(branchName, files, message, parentBranch) should add a new commit to the branch 3`] = ` Array [ Array [ - "repos/some/repo/git/refs/heads/package.json", + "repos/some/repo/git/refs/heads/the-branch", Object { "body": Object { "force": true, diff --git a/test/platform/github/index.spec.js b/test/platform/github/index.spec.js index f5b9315545aecffc42f7dfda5752cd93ccf0ba5f..cd19bddcbe5ed0b9eede7997c10810bed75558fe 100644 --- a/test/platform/github/index.spec.js +++ b/test/platform/github/index.spec.js @@ -329,84 +329,17 @@ describe('platform/github', () => { }); describe('branchExists(branchName)', () => { it('should return true if the branch exists (one result)', async () => { - await initRepo('some/repo', 'token'); - get.mockImplementationOnce(() => ({ - body: { - ref: 'refs/heads/thebranchname', - }, - })); - const exists = await github.branchExists('thebranchname'); - expect(get.mock.calls).toMatchSnapshot(); - expect(exists).toBe(true); - }); - it('should return true if the branch exists (multiple results)', async () => { await initRepo('some/repo', 'token'); get.mockImplementationOnce(() => ({ body: [ { - ref: 'refs/heads/notthebranchname', - }, - { - ref: 'refs/heads/thebranchname', + name: 'thebranchname', }, ], })); const exists = await github.branchExists('thebranchname'); - expect(get.mock.calls).toMatchSnapshot(); expect(exists).toBe(true); }); - it('should return false if the branch does not exist (one result)', async () => { - await initRepo('some/repo', 'token'); - get.mockImplementationOnce(() => ({ - body: { - ref: 'refs/heads/notthebranchname', - }, - })); - const exists = await github.branchExists('thebranchname'); - expect(get.mock.calls).toMatchSnapshot(); - expect(exists).toBe(false); - }); - it('should return false if the branch does not exist (multiple results)', async () => { - await initRepo('some/repo', 'token'); - get.mockImplementationOnce(() => ({ - body: [ - { - ref: 'refs/heads/notthebranchname', - }, - { - ref: 'refs/heads/alsonotthebranchname', - }, - ], - })); - const exists = await github.branchExists('thebranchname'); - expect(get.mock.calls).toMatchSnapshot(); - expect(exists).toBe(false); - }); - it('should return false if a 404 is returned', async () => { - await initRepo('some/repo', 'token'); - get.mockImplementationOnce(() => - Promise.reject({ - statusCode: 404, - }) - ); - const exists = await github.branchExists('thebranchname'); - expect(get.mock.calls).toMatchSnapshot(); - expect(exists).toBe(false); - }); - it('should propagate unknown errors', async () => { - await initRepo('some/repo', 'token'); - get.mockImplementationOnce(() => - Promise.reject(new Error('Something went wrong')) - ); - let err; - try { - await github.branchExists('thebranchname'); - } catch (e) { - err = e; - } - expect(get.mock.calls).toMatchSnapshot(); - expect(err.message).toBe('Something went wrong'); - }); }); describe('getAllRenovateBranches()', () => { it('should return all renovate branches', async () => { @@ -1313,9 +1246,14 @@ describe('platform/github', () => { it('should add a new commit to the branch', async () => { // branchExists get.mockImplementationOnce(() => ({ - body: { - ref: 'refs/heads/package.json', - }, + body: [ + { + name: 'master', + }, + { + name: 'the-branch', + }, + ], })); const files = [ { @@ -1324,7 +1262,7 @@ describe('platform/github', () => { }, ]; await github.commitFilesToBranch( - 'package.json', + 'the-branch', files, 'my commit message' ); @@ -1334,11 +1272,13 @@ describe('platform/github', () => { }); it('should add a commit to a new branch if the branch does not already exist', async () => { // branchExists - get.mockImplementationOnce(() => - Promise.reject({ - statusCode: 404, - }) - ); + get.mockImplementationOnce(() => ({ + body: [ + { + name: 'master', + }, + ], + })); const files = [ { name: 'package.json', @@ -1346,7 +1286,7 @@ describe('platform/github', () => { }, ]; await github.commitFilesToBranch( - 'package.json', + 'the-branch', files, 'my other commit message' );