diff --git a/lib/api/github.js b/lib/api/github.js index 036c188b079d474d2ea887387878d1c29ef0775d..ff64a0422a69056921fbf3081948b2ccae7034a2 100644 --- a/lib/api/github.js +++ b/lib/api/github.js @@ -235,25 +235,20 @@ async function branchExists(branchName) { const res = await ghGot( `repos/${config.repoName}/git/refs/heads/${branchName}` ); - if (res.statusCode === 200) { - 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; + 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'); } - // This should happen if there's an exact match - return res.body.ref === `refs/heads/${branchName}`; + return matchedBranch; } - // This probably shouldn't happen - logger.debug("Branch doesn't exist"); - return false; + // 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 diff --git a/test/api/__snapshots__/github.spec.js.snap b/test/api/__snapshots__/github.spec.js.snap index a4459f8fe5a1de178d00ce328bbd99ab491ccf6f..10fda155e8759a46201e7080a7f294daa29b0a80 100644 --- a/test/api/__snapshots__/github.spec.js.snap +++ b/test/api/__snapshots__/github.spec.js.snap @@ -93,28 +93,6 @@ Array [ ] `; -exports[`api/github branchExists(branchName) should return false if a non-200 response is returned 1`] = ` -Array [ - Array [ - "repos/some/repo", - ], - Array [ - "repos/some/repo/git/refs/heads/master", - ], - Array [ - "repos/some/repo/branches/master/protection/required_status_checks", - Object { - "headers": Object { - "accept": "application/vnd.github.loki-preview+json", - }, - }, - ], - Array [ - "repos/some/repo/git/refs/heads/thebranchname", - ], -] -`; - exports[`api/github branchExists(branchName) should return false if the branch does not exist (multiple results) 1`] = ` Array [ Array [ diff --git a/test/api/github.spec.js b/test/api/github.spec.js index 4aa8f16b0f53385fb9edf011af1e580178221705..8711c4235eff9ed33b743c8134500c8ef2447724 100644 --- a/test/api/github.spec.js +++ b/test/api/github.spec.js @@ -376,15 +376,11 @@ describe('api/github', () => { }, })); // getBranchProtection - ghGot.mockImplementationOnce(() => { - // Create a new object, that prototypically inherits from the Error constructor - function MyError() { - this.statusCode = 404; - } - MyError.prototype = Object.create(Error.prototype); - MyError.prototype.constructor = MyError; - throw new MyError(); - }); + ghGot.mockImplementationOnce(() => + Promise.reject({ + statusCode: 404, + }) + ); return github.initRepo(...args); } const config = await mergeInitRepo('some/repo', 'token'); @@ -410,15 +406,11 @@ describe('api/github', () => { }, })); // getBranchProtection - ghGot.mockImplementationOnce(() => { - // Create a new object, that prototypically inherits from the Error constructor - function MyError() { - this.statusCode = 403; - } - MyError.prototype = Object.create(Error.prototype); - MyError.prototype.constructor = MyError; - throw new MyError(); - }); + ghGot.mockImplementationOnce(() => + Promise.reject({ + statusCode: 403, + }) + ); return github.initRepo(...args); } const config = await mergeInitRepo('some/repo', 'token'); @@ -444,15 +436,11 @@ describe('api/github', () => { }, })); // getBranchProtection - ghGot.mockImplementationOnce(() => { - // Create a new object, that prototypically inherits from the Error constructor - function MyError() { - this.statusCode = 500; - } - MyError.prototype = Object.create(Error.prototype); - MyError.prototype.constructor = MyError; - throw new MyError(); - }); + ghGot.mockImplementationOnce(() => + Promise.reject({ + statusCode: 500, + }) + ); return github.initRepo(...args); } let e; @@ -515,7 +503,6 @@ describe('api/github', () => { it('should return true if the branch exists (one result)', async () => { await initRepo('some/repo', 'token'); ghGot.mockImplementationOnce(() => ({ - statusCode: 200, body: { ref: 'refs/heads/thebranchname', }, @@ -527,7 +514,6 @@ describe('api/github', () => { it('should return true if the branch exists (multiple results)', async () => { await initRepo('some/repo', 'token'); ghGot.mockImplementationOnce(() => ({ - statusCode: 200, body: [ { ref: 'refs/heads/notthebranchname', @@ -544,7 +530,6 @@ describe('api/github', () => { it('should return false if the branch does not exist (one result)', async () => { await initRepo('some/repo', 'token'); ghGot.mockImplementationOnce(() => ({ - statusCode: 200, body: { ref: 'refs/heads/notthebranchname', }, @@ -556,7 +541,6 @@ describe('api/github', () => { it('should return false if the branch does not exist (multiple results)', async () => { await initRepo('some/repo', 'token'); ghGot.mockImplementationOnce(() => ({ - statusCode: 200, body: [ { ref: 'refs/heads/notthebranchname', @@ -570,15 +554,6 @@ describe('api/github', () => { expect(ghGot.mock.calls).toMatchSnapshot(); expect(exists).toBe(false); }); - it('should return false if a non-200 response is returned', async () => { - await initRepo('some/repo', 'token'); - ghGot.mockImplementationOnce(() => ({ - statusCode: 123, - })); - const exists = await github.branchExists('thebranchname'); - expect(ghGot.mock.calls).toMatchSnapshot(); - expect(exists).toBe(false); - }); it('should return false if a 404 is returned', async () => { await initRepo('some/repo', 'token'); ghGot.mockImplementationOnce(() => @@ -1242,11 +1217,11 @@ describe('api/github', () => { }); it('should return null if GitHub returns a 404', async () => { await initRepo('some/repo', 'token'); - ghGot.mockImplementationOnce(() => { - const error = new Error(); - error.statusCode = 404; - throw error; - }); + ghGot.mockImplementationOnce(() => + Promise.reject({ + statusCode: 404, + }) + ); const content = await github.getFileContent('package.json'); expect(ghGot.mock.calls).toMatchSnapshot(); expect(content).toBe(null); @@ -1349,7 +1324,6 @@ describe('api/github', () => { it('should add a new commit to the branch', async () => { // branchExists ghGot.mockImplementationOnce(() => ({ - statusCode: 200, body: { ref: 'refs/heads/package.json', }, @@ -1371,9 +1345,11 @@ describe('api/github', () => { }); it('should add a commit to a new branch if the branch does not already exist', async () => { // branchExists - ghGot.mockImplementationOnce(() => ({ - statusCode: 404, - })); + ghGot.mockImplementationOnce(() => + Promise.reject({ + statusCode: 404, + }) + ); const files = [ { name: 'package.json',