diff --git a/lib/config/options/index.ts b/lib/config/options/index.ts index 345d43e70f10c0..094e9f52a33c55 100644 --- a/lib/config/options/index.ts +++ b/lib/config/options/index.ts @@ -999,7 +999,7 @@ const options: RenovateOptions[] = [ description: 'Set to `true` to automatically approve PRs.', type: 'boolean', default: false, - supportedPlatforms: ['azure'], + supportedPlatforms: ['azure', 'gitlab'], }, // depType { diff --git a/lib/modules/platform/gitlab/index.spec.ts b/lib/modules/platform/gitlab/index.spec.ts index 55d72e2778dc0e..03157d7b30ed08 100644 --- a/lib/modules/platform/gitlab/index.spec.ts +++ b/lib/modules/platform/gitlab/index.spec.ts @@ -2054,6 +2054,64 @@ describe('modules/platform/gitlab/index', () => { } `); }); + + it('auto-approves when enabled', async () => { + await initPlatform('13.3.6-ee'); + httpMock + .scope(gitlabApiHost) + .post('/api/v4/projects/undefined/merge_requests') + .reply(200, { + id: 1, + iid: 12345, + title: 'some title', + }) + .post('/api/v4/projects/undefined/merge_requests/12345/approve') + .reply(200); + expect( + await gitlab.createPr({ + sourceBranch: 'some-branch', + targetBranch: 'master', + prTitle: 'some-title', + prBody: 'the-body', + labels: [], + platformOptions: { + autoApprove: true, + }, + }) + ).toStrictEqual({ + id: 1, + iid: 12345, + number: 12345, + sourceBranch: 'some-branch', + title: 'some title', + }); + }); + + it('should swallow an error on auto-approve', async () => { + await initPlatform('13.3.6-ee'); + httpMock + .scope(gitlabApiHost) + .post('/api/v4/projects/undefined/merge_requests') + .reply(200, { + id: 1, + iid: 12345, + title: 'some title', + }) + .post('/api/v4/projects/undefined/merge_requests/12345/approve') + .replyWithError('some error'); + await expect( + gitlab.createPr({ + sourceBranch: 'some-branch', + targetBranch: 'master', + prTitle: 'some-title', + prBody: 'the-body', + labels: [], + platformOptions: { + autoApprove: true, + }, + }) + ).toResolve(); + }); }); describe('getPr(prNo)', () => { @@ -2316,6 +2374,37 @@ describe('modules/platform/gitlab/index', () => { ).toResolve(); }); + it('auto-approves when enabled', async () => { + await initPlatform('13.3.6-ee'); + httpMock + .scope(gitlabApiHost) + .get( + '/api/v4/projects/undefined/merge_requests?per_page=100&scope=created_by_me' + ) + .reply(200, [ + { + iid: 1, + source_branch: 'branch-a', + title: 'branch a pr', + state: 'open', + }, + ]) + .put('/api/v4/projects/undefined/merge_requests/1') + .reply(200) + .post('/api/v4/projects/undefined/merge_requests/1/approve') + .reply(200); + await expect( + gitlab.updatePr({ + number: 1, + prTitle: 'title', + prBody: 'body', + platformOptions: { + autoApprove: true, + }, + }) + ).toResolve(); + }); + it('closes the PR', async () => { await initPlatform('13.3.6-ee'); httpMock diff --git a/lib/modules/platform/gitlab/index.ts b/lib/modules/platform/gitlab/index.ts index 866b5d0f61ec75..58614913f7f814 100644 --- a/lib/modules/platform/gitlab/index.ts +++ b/lib/modules/platform/gitlab/index.ts @@ -637,6 +637,16 @@ async function tryPrAutomerge( } } +async function approvePr(pr: number): Promise { + try { + await gitlabApi.postJson( + `projects/${config.repository}/merge_requests/${pr}/approve` + ); + } catch (err) { + logger.warn({ err }, 'GitLab: Error approving merge request'); + } +} + export async function createPr({ sourceBranch, targetBranch, @@ -674,6 +684,10 @@ export async function createPr({ config.prList.push(pr); } + if (platformOptions?.autoApprove) { + await approvePr(pr.iid); + } + await tryPrAutomerge(pr.iid, platformOptions); return massagePr(pr); @@ -733,6 +747,10 @@ export async function updatePr({ { body } ); + if (platformOptions?.autoApprove) { + await approvePr(iid); + } + await tryPrAutomerge(iid, platformOptions); }