Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gitlab): ignoreApprovals add check for existingAnyApproverRule #22237

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 57 additions & 0 deletions lib/modules/platform/gitlab/index.spec.ts
Expand Up @@ -1713,6 +1713,63 @@ describe('modules/platform/gitlab/index', () => {
`);
});

it('will modify a rule of type any_approvers, if such a rule exists', 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')
.reply(200)
.get('/api/v4/projects/undefined/merge_requests/12345')
.reply(200, {
merge_status: 'can_be_merged',
pipeline: {
id: 29626725,
sha: '2be7ddb704c7b6b83732fdd5b9f09d5a397b5f8f',
ref: 'patch-28',
status: 'success',
},
})
.put('/api/v4/projects/undefined/merge_requests/12345/merge')
.reply(200)
.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: true,
gitLabIgnoreApprovals: true,
},
})
).toStrictEqual({
id: 1,
iid: 12345,
number: 12345,
sourceBranch: 'some-branch',
title: 'some title',
});
});

it('does not try to create already existing approval rule', async () => {
await initPlatform('13.3.6-ee');
httpMock
Expand Down
20 changes: 19 additions & 1 deletion lib/modules/platform/gitlab/index.ts
Expand Up @@ -530,7 +530,25 @@ export async function getPrList(): Promise<Pr[]> {
async function ignoreApprovals(pr: number): Promise<void> {
try {
const url = `projects/${config.repository}/merge_requests/${pr}/approval_rules`;
const { body: rules } = await gitlabApi.getJson<{ name: string }[]>(url);
const { body: rules } = await gitlabApi.getJson<
{
name: string;
rule_type: string;
id: number;
}[]
>(url);

const existingAnyApproverRule = rules?.find(
({ rule_type }) => rule_type === 'any_approver'
);

if (existingAnyApproverRule) {
await gitlabApi.putJson(`${url}/${existingAnyApproverRule.id}`, {
body: { ...existingAnyApproverRule, approvals_required: 0 },
});
return;
}

const ruleName = 'renovateIgnoreApprovals';
const zeroApproversRule = rules?.find(({ name }) => name === ruleName);
if (!zeroApproversRule) {
Expand Down