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): remove approval rules only if automerging #31720

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/config/migrate-validate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ describe('config/migrate-validate', () => {
expect(res.warnings).toBeUndefined();
expect(res).toMatchSnapshot();
});

rarkins marked this conversation as resolved.
Show resolved Hide resolved
});
});
38 changes: 38 additions & 0 deletions lib/config/presets/gitlab/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
rarkins marked this conversation as resolved.
Show resolved Hide resolved
});

describe('getPresetFromEndpoint()', () => {
Expand Down
13 changes: 8 additions & 5 deletions lib/modules/platform/gitlab/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,10 +652,6 @@ async function tryPrAutomerge(
platformPrOptions: PlatformPrOptions | undefined,
): Promise<void> {
try {
if (platformPrOptions?.gitLabIgnoreApprovals) {
await ignoreApprovals(pr);
}

if (platformPrOptions?.usePlatformAutomerge) {
// https://docs.gitlab.com/ee/api/merge_requests.html#merge-status
const desiredDetailedMergeStatus = [
Expand Down Expand Up @@ -757,6 +753,7 @@ export async function createPr({
draftPR,
labels,
platformPrOptions,
automerge,
}: CreatePRConfig): Promise<Pr> {
let title = prTitle;
if (draftPR) {
Expand Down Expand Up @@ -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);
}
Expand Down
Loading