Skip to content

Commit

Permalink
fix(gitlab): ignoreApprovals add check for existingAnyApproverRule (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrondina committed May 16, 2023
1 parent 2cc2474 commit dc077f3
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
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

0 comments on commit dc077f3

Please sign in to comment.