From d0e0bbeaafa2a3becbb7ff04156962e555e2f6ff Mon Sep 17 00:00:00 2001 From: Kyle Niewiada Date: Mon, 1 Jul 2024 15:54:22 -0400 Subject: [PATCH] fix(config): Remove usePlatformAutomerge restriction with gitLabIgnoreApprovals (#29972) --- docs/usage/configuration-options.md | 2 +- lib/modules/platform/gitlab/index.spec.ts | 43 +++++++++++++++++++++++ lib/modules/platform/gitlab/index.ts | 14 ++++---- 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/docs/usage/configuration-options.md b/docs/usage/configuration-options.md index 125bccf0971000..09dc38bc540f13 100644 --- a/docs/usage/configuration-options.md +++ b/docs/usage/configuration-options.md @@ -1510,7 +1510,7 @@ Example: Ignore the default project level approval(s), so that Renovate bot can automerge its merge requests, without needing approval(s). Under the hood, it creates a MR-level approval rule where `approvals_required` is set to `0`. -This option works only when `automerge=true`, `automergeType=pr` or `automergeType=branch`, and `platformAutomerge=true`. +This option works only when `automerge=true` and either `automergeType=pr` or `automergeType=branch`. Also, approval rules overriding should not be [prevented in GitLab settings](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/settings.html#prevent-editing-approval-rules-in-merge-requests). ## goGetDirs diff --git a/lib/modules/platform/gitlab/index.spec.ts b/lib/modules/platform/gitlab/index.spec.ts index 3878f49d6229f1..f327ec9a5d4ec9 100644 --- a/lib/modules/platform/gitlab/index.spec.ts +++ b/lib/modules/platform/gitlab/index.spec.ts @@ -2228,6 +2228,49 @@ describe('modules/platform/gitlab/index', () => { `); }); + it('adds approval rule to ignore all approvals when platformAutomerge is false', 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', + }) + .get('/api/v4/projects/undefined/merge_requests/12345/approval_rules') + .reply(200, [ + { + name: 'AnyApproverRule', + rule_type: 'any_approver', + id: 50005, + }, + ]) + .put( + '/api/v4/projects/undefined/merge_requests/12345/approval_rules/50005', + ) + .reply(200); + expect( + await gitlab.createPr({ + sourceBranch: 'some-branch', + targetBranch: 'master', + prTitle: 'some-title', + prBody: 'the-body', + labels: [], + platformOptions: { + usePlatformAutomerge: false, + gitLabIgnoreApprovals: true, + }, + }), + ).toEqual({ + id: 1, + iid: 12345, + number: 12345, + sourceBranch: 'some-branch', + title: 'some title', + }); + }); + it('will modify a rule of type any_approvers, if such a rule exists', 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 90eccc206c9640..4a28db74a02aee 100644 --- a/lib/modules/platform/gitlab/index.ts +++ b/lib/modules/platform/gitlab/index.ts @@ -648,12 +648,12 @@ async function tryPrAutomerge( pr: number, platformOptions: PlatformPrOptions | undefined, ): Promise { - if (platformOptions?.usePlatformAutomerge) { - try { - if (platformOptions?.gitLabIgnoreApprovals) { - await ignoreApprovals(pr); - } + try { + if (platformOptions?.gitLabIgnoreApprovals) { + await ignoreApprovals(pr); + } + if (platformOptions?.usePlatformAutomerge) { // https://docs.gitlab.com/ee/api/merge_requests.html#merge-status const desiredDetailedMergeStatus = [ 'mergeable', @@ -730,9 +730,9 @@ async function tryPrAutomerge( } await setTimeout(mergeDelay * attempt ** 2); // exponential backoff } - } catch (err) /* istanbul ignore next */ { - logger.debug({ err }, 'Automerge on PR creation failed'); } + } catch (err) /* istanbul ignore next */ { + logger.debug({ err }, 'Automerge on PR creation failed'); } }