diff --git a/lib/platform/gitlab/index.js b/lib/platform/gitlab/index.js index 2c0600b3a5406658a9c84b64872aa26abd3d160b..a8eda3893850c4cde64a38cd8df18dc0273d9698 100644 --- a/lib/platform/gitlab/index.js +++ b/lib/platform/gitlab/index.js @@ -338,8 +338,26 @@ async function deleteBranch(branchName, closePr = false) { ); } -function mergeBranch() { - logger.warn('Unimplemented in GitLab: mergeBranch'); +async function mergeBranch(branchName) { + logger.debug(`mergeBranch(${branchName}`); + const branchURI = encodeURIComponent(branchName); + try { + await get.post( + `projects/${ + config.repository + }/repository/commits/${branchURI}/cherry_pick?branch=${config.baseBranch}` + ); + } catch (err) { + logger.info( + expandError(err), + `Error pushing branch merge for ${branchName}` + ); + throw new Error('Branch automerge failed'); + } + // Update base commit + config.baseCommitSHA = null; + // Delete branch + await deleteBranch(branchName); } async function getBranchLastCommitTime(branchName) { diff --git a/test/platform/gitlab/__snapshots__/index.spec.js.snap b/test/platform/gitlab/__snapshots__/index.spec.js.snap index 991b35fe067b2813b52009bf4c4a03d7fa9063cc..f192d986d0a7fd3d72fe966727317e904c40e416 100644 --- a/test/platform/gitlab/__snapshots__/index.spec.js.snap +++ b/test/platform/gitlab/__snapshots__/index.spec.js.snap @@ -409,6 +409,34 @@ Array [ exports[`platform/gitlab initRepo should initialise the config for the repo - 3 2`] = `Object {}`; +exports[`platform/gitlab mergeBranch(branchName) should perform a branch merge 1`] = ` +Array [ + Array [ + "projects/some%2Frepo/repository/commits/thebranchname/cherry_pick?branch=undefined", + ], +] +`; + +exports[`platform/gitlab mergeBranch(branchName) should perform a branch merge 2`] = ` +Array [ + Array [ + "projects/some%2Frepo/repository/branches/thebranchname", + ], +] +`; + +exports[`platform/gitlab mergeBranch(branchName) should throw if branch merge throws 1`] = `[Error: Branch automerge failed]`; + +exports[`platform/gitlab mergeBranch(branchName) should throw if branch merge throws 2`] = ` +Array [ + Array [ + "projects/some%2Frepo/repository/commits/thebranchname/cherry_pick?branch=undefined", + ], +] +`; + +exports[`platform/gitlab mergeBranch(branchName) should throw if branch merge throws 3`] = `Array []`; + exports[`platform/gitlab setBaseBranch(branchName) sets the base branch 1`] = ` Array [ Array [ diff --git a/test/platform/gitlab/index.spec.js b/test/platform/gitlab/index.spec.js index 6c172bb4342eb72a6769b2d6ffcb26a056102dd2..8b255dae51f1bff9c0590f0ae19f19507b37ed44 100644 --- a/test/platform/gitlab/index.spec.js +++ b/test/platform/gitlab/index.spec.js @@ -452,6 +452,44 @@ describe('platform/gitlab', () => { expect(get.post.mock.calls).toHaveLength(1); }); }); + describe('mergeBranch(branchName)', () => { + it('should perform a branch merge', async () => { + await initRepo({ + repository: 'some/repo', + token: 'token', + }); + + await gitlab.mergeBranch('thebranchname'); + + // deleteBranch + get.delete.mockImplementationOnce(); + + expect(get.post.mock.calls).toMatchSnapshot(); + expect(get.delete.mock.calls).toMatchSnapshot(); + }); + it('should throw if branch merge throws', async () => { + await initRepo({ + repository: 'some/repo', + token: 'token', + }); + get.post.mockImplementationOnce(() => { + throw new Error('branch-push failed'); + }); + let e; + try { + await gitlab.mergeBranch('thebranchname'); + } catch (err) { + e = err; + } + + // deleteBranch + get.delete.mockImplementationOnce(); + + expect(e).toMatchSnapshot(); + expect(get.post.mock.calls).toMatchSnapshot(); + expect(get.delete.mock.calls).toMatchSnapshot(); + }); + }); describe('deleteBranch(branchName)', () => { it('should send delete', async () => { get.delete = jest.fn(); @@ -465,11 +503,6 @@ describe('platform/gitlab', () => { expect(get.delete.mock.calls.length).toBe(1); }); }); - describe('mergeBranch()', () => { - it('exists', () => { - gitlab.mergeBranch(); - }); - }); describe('getBranchLastCommitTime', () => { it('should return a Date', async () => { get.mockReturnValueOnce({