Skip to content

Commit

Permalink
feat(platform/gitlab): support autoApprove option (#23460)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjpearson committed Aug 1, 2023
1 parent ec4ed8f commit cdf1db7
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/config/options/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
89 changes: 89 additions & 0 deletions lib/modules/platform/gitlab/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)', () => {
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions lib/modules/platform/gitlab/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,16 @@ async function tryPrAutomerge(
}
}

async function approvePr(pr: number): Promise<void> {
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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -733,6 +747,10 @@ export async function updatePr({
{ body }
);

if (platformOptions?.autoApprove) {
await approvePr(iid);
}

await tryPrAutomerge(iid, platformOptions);
}

Expand Down

0 comments on commit cdf1db7

Please sign in to comment.