diff --git a/lib/platform/gitlab/index.js b/lib/platform/gitlab/index.js index a5e9805b26525edb8905223d26e9fb77d0af27b0..939579d02206b7d8e986486ce048cba1de4cbf0e 100644 --- a/lib/platform/gitlab/index.js +++ b/lib/platform/gitlab/index.js @@ -190,6 +190,9 @@ function isBranchStale() { // Returns the Pull Request for a branch. Null if not exists. async function getBranchPr(branchName) { logger.debug(`getBranchPr(${branchName})`); + if (!await branchExists(branchName)) { + return null; + } const urlString = `projects/${ config.repository }/merge_requests?state=opened&per_page=100`; @@ -463,9 +466,18 @@ async function getPr(iid) { const branchUrl = `projects/${ config.repository }/repository/branches/${urlEscape(pr.source_branch)}`; - const branch = (await get(branchUrl)).body; - if (branch && branch.commit && branch.commit.author_email === config.email) { - pr.canRebase = true; + try { + const branch = (await get(branchUrl)).body; + if ( + branch && + branch.commit && + branch.commit.author_email === config.email + ) { + pr.canRebase = true; + } + } catch (err) { + logger.warn({ err }, 'Error getting PR branch'); + pr.isUnmergeable = true; } return pr; } diff --git a/lib/workers/branch/index.js b/lib/workers/branch/index.js index f32a4a597a9b8cabbce383b0a516bdbb633fd5da..16ff277b07f3efd4560cb5e21933bec8b0dc5485 100644 --- a/lib/workers/branch/index.js +++ b/lib/workers/branch/index.js @@ -77,8 +77,10 @@ async function processBranch(branchConfig) { } return 'already-existed'; } - logger.debug('Checking if PR has been edited'); - pr = await platform.findPr(config.branchName, config.prTitle, 'open'); + if (branchExists) { + logger.debug('Checking if PR has been edited'); + pr = await platform.findPr(config.branchName, config.prTitle, 'open'); + } if (pr) { logger.debug({ pr }, 'Found existing PR'); pr = await platform.getPr(pr.number); diff --git a/test/platform/gitlab/__snapshots__/index.spec.js.snap b/test/platform/gitlab/__snapshots__/index.spec.js.snap index f9bdbd1b57770b0a82c29c564d92a341100a64fd..e19ed8345f1bef098ebc0f422b9f357a25638005 100644 --- a/test/platform/gitlab/__snapshots__/index.spec.js.snap +++ b/test/platform/gitlab/__snapshots__/index.spec.js.snap @@ -133,35 +133,7 @@ Array [ exports[`platform/gitlab getBranchLastCommitTime should return a Date 1`] = `2012-09-20T08:50:22.000Z`; -exports[`platform/gitlab getBranchPr(branchName) should return null if no PR exists 1`] = ` -Array [ - Array [ - "projects/undefined/merge_requests?state=opened&per_page=100", - Object { - "paginate": true, - }, - ], -] -`; - exports[`platform/gitlab getBranchPr(branchName) should return the PR object 1`] = ` -Array [ - Array [ - "projects/undefined/merge_requests?state=opened&per_page=100", - Object { - "paginate": true, - }, - ], - Array [ - "projects/undefined/merge_requests/undefined", - ], - Array [ - "projects/undefined/repository/branches/some-branch", - ], -] -`; - -exports[`platform/gitlab getBranchPr(branchName) should return the PR object 2`] = ` Object { "additions": 1, "base": Object { @@ -221,6 +193,23 @@ Object { } `; +exports[`platform/gitlab getPr(prNo) returns the PR with nonexisting branch 1`] = ` +Object { + "body": "a merge request", + "branchName": "some-branch", + "canMerge": false, + "description": "a merge request", + "displayNumber": "Merge Request #12345", + "id": 1, + "iid": 12345, + "isUnmergeable": true, + "merge_status": "cannot_be_merged", + "number": 12345, + "source_branch": "some-branch", + "state": "open", +} +`; + exports[`platform/gitlab getRepos should return an array of repos 1`] = ` Array [ Array [ diff --git a/test/platform/gitlab/index.spec.js b/test/platform/gitlab/index.spec.js index 5b904396aa9cde2c4b313cf07c9a6fd62207e7dc..94bca4325e71073dbe01e1b5c2b297a15ff3eb26 100644 --- a/test/platform/gitlab/index.spec.js +++ b/test/platform/gitlab/index.spec.js @@ -240,15 +240,22 @@ describe('platform/gitlab', () => { }); }); describe('getBranchPr(branchName)', () => { + it('should return null if branch does not exist', async () => { + get.mockReturnValueOnce({ statusCode: 500 }); // branchExists + const pr = await gitlab.getBranchPr('somebranch'); + expect(pr).toBe(null); + }); it('should return null if no PR exists', async () => { - get.mockImplementationOnce(() => ({ + get.mockReturnValueOnce({ statusCode: 200 }); // branchExists + get.mockReturnValueOnce({ + // branchExists body: [], - })); + }); const pr = await gitlab.getBranchPr('somebranch'); - expect(get.mock.calls).toMatchSnapshot(); expect(pr).toBe(null); }); it('should return the PR object', async () => { + get.mockReturnValueOnce({ statusCode: 200 }); // branchExists get.mockReturnValueOnce({ body: [{ number: 91, source_branch: 'somebranch' }], }); @@ -266,7 +273,6 @@ describe('platform/gitlab', () => { }); get.mockReturnValueOnce({ body: 'foo' }); const pr = await gitlab.getBranchPr('somebranch'); - expect(get.mock.calls).toMatchSnapshot(); expect(pr).toMatchSnapshot(); }); }); @@ -584,6 +590,25 @@ describe('platform/gitlab', () => { const pr = await gitlab.getPr(12345); expect(pr).toMatchSnapshot(); }); + it('returns the PR with nonexisting branch', async () => { + get.mockImplementationOnce(() => ({ + body: { + id: 1, + iid: 12345, + description: 'a merge request', + state: 'open', + merge_status: 'cannot_be_merged', + source_branch: 'some-branch', + }, + })); + get.mockImplementationOnce(() => + Promise.reject({ + statusCode: 404, + }) + ); + const pr = await gitlab.getPr(12345); + expect(pr).toMatchSnapshot(); + }); }); describe('getPrFiles()', () => { it('should return empty', async () => {