diff --git a/lib/config/migrate-validate.spec.ts b/lib/config/migrate-validate.spec.ts index c0d87d4afa9165..5b93666a6346d4 100644 --- a/lib/config/migrate-validate.spec.ts +++ b/lib/config/migrate-validate.spec.ts @@ -44,5 +44,33 @@ describe('config/migrate-validate', () => { expect(res.warnings).toBeUndefined(); expect(res).toMatchSnapshot(); }); + + it('does not remove approval rules for merge requests without automerge enabled', async () => { + const input: RenovateConfig = { + platformAutomerge: true, + gitLabIgnoreApprovals: true, + automergeType: 'pr', + packageRules: [ + { + matchPackageNames: ['/azure/'], + automerge: true, + }, + ], + }; + const res = await migrateAndValidate(config, input); + expect(res).toEqual({ + platformAutomerge: true, + gitLabIgnoreApprovals: true, + automergeType: 'pr', + packageRules: [ + { + matchPackageNames: ['/azure/'], + automerge: true, + }, + ], + errors: [], + warnings: [], + }); + }); }); }); diff --git a/lib/config/presets/gitlab/index.spec.ts b/lib/config/presets/gitlab/index.spec.ts index bccecb87ab3a32..7f4d77a3b7900b 100644 --- a/lib/config/presets/gitlab/index.spec.ts +++ b/lib/config/presets/gitlab/index.spec.ts @@ -124,6 +124,44 @@ describe('config/presets/gitlab/index', () => { }); expect(content).toEqual({ foo: 'bar' }); }); + + it('should remove approval rules for merge requests with automerge enabled', async () => { + httpMock + .scope(gitlabApiHost) + .get(projectPath) + .reply(200, { + default_branch: 'main', + }) + .get(`${basePath}/files/default.json/raw?ref=main`) + .reply(200, { foo: 'bar' }, {}); + + const content = await gitlab.getPreset({ repo: 'some/repo' }); + expect(content).toEqual({ foo: 'bar' }); + + // Simulate automerge enabled + const automergeEnabled = true; + const approvalRulesRemoved = await gitlab.tryPrAutomerge(1, { gitLabIgnoreApprovals: true }, automergeEnabled); + expect(approvalRulesRemoved).toBe(true); + }); + + it('should not remove approval rules for merge requests without automerge enabled', async () => { + httpMock + .scope(gitlabApiHost) + .get(projectPath) + .reply(200, { + default_branch: 'main', + }) + .get(`${basePath}/files/default.json/raw?ref=main`) + .reply(200, { foo: 'bar' }, {}); + + const content = await gitlab.getPreset({ repo: 'some/repo' }); + expect(content).toEqual({ foo: 'bar' }); + + // Simulate automerge disabled + const automergeDisabled = false; + const approvalRulesRemoved = await gitlab.tryPrAutomerge(1, { gitLabIgnoreApprovals: true }, automergeDisabled); + expect(approvalRulesRemoved).toBe(false); + }); }); describe('getPresetFromEndpoint()', () => { diff --git a/lib/modules/platform/gitlab/index.ts b/lib/modules/platform/gitlab/index.ts index de544f48cce3e0..8f1234154d4854 100644 --- a/lib/modules/platform/gitlab/index.ts +++ b/lib/modules/platform/gitlab/index.ts @@ -652,10 +652,6 @@ async function tryPrAutomerge( platformPrOptions: PlatformPrOptions | undefined, ): Promise { try { - if (platformPrOptions?.gitLabIgnoreApprovals) { - await ignoreApprovals(pr); - } - if (platformPrOptions?.usePlatformAutomerge) { // https://docs.gitlab.com/ee/api/merge_requests.html#merge-status const desiredDetailedMergeStatus = [ @@ -757,6 +753,7 @@ export async function createPr({ draftPR, labels, platformPrOptions, + automerge, }: CreatePRConfig): Promise { let title = prTitle; if (draftPR) { @@ -790,7 +787,13 @@ export async function createPr({ await approvePr(pr.iid); } - await tryPrAutomerge(pr.iid, platformPrOptions); + if (platformPrOptions?.gitLabIgnoreApprovals && automerge) { + await ignoreApprovals(pr.iid); + } + + if (automerge) { + await tryPrAutomerge(pr.iid, platformPrOptions); + } return massagePr(pr); }