Commit 4c263881 authored by Nick Thomas's avatar Nick Thomas

Fix creating an MR when a project approval rule has no approvers

Without this change, we assume the regular approval rule is actually an
"any_approver" approval rule. An MR can only have one of those, so we
get a constraint violation error when the regular approval rule is
converted.

It's odd that these rules are entirely valid in the backend, but making
them invalid at this stage feels a bit risky.

Changelog: fixed
parent 0293d150
......@@ -53,7 +53,7 @@ module ApprovalRules
rule_attributes[:user_ids] = provided_user_ids & visible_user_ids
end
if rule_attributes[:group_ids].blank? && rule_attributes[:user_ids].blank?
if rule_attributes[:group_ids].blank? && rule_attributes[:user_ids].blank? && rule_attributes[:name].blank?
rule_attributes[:rule_type] = :any_approver
rule_attributes[:name] = ApprovalRuleLike::ALL_MEMBERS
end
......
---
title: Fix creating an MR when a project approval rule has no approvers
merge_request: 59671
author:
type: fixed
......@@ -80,6 +80,22 @@ RSpec.describe ApprovalRules::ParamsFilteringService do
let(:expected_groups) { [accessible_group] }
end
# When a project approval rule is genuinely empty, it should not be converted
# an any_approver rule
context 'empty project approval rule' do
let(:approval_rules_attributes) { [{ name: 'Foo', user_ids: [], group_ids: [] }] }
it 'adds empty rule', :aggregate_failures do
rules = service.execute[:approval_rules_attributes]
expect(rules.size).to eq(1)
expect(rules[0]['name']).to eq('Foo')
expect(rules[0]['user_ids']).to be_empty
expect(rules[0]['group_ids']).to be_empty
expect(rules[0].key?('rule_type')).to be_falsy
end
end
context 'inapplicable user defined rules' do
let!(:source_rule) { create(:approval_project_rule, project: project) }
let!(:another_source_rule) { create(:approval_project_rule, project: project) }
......@@ -148,7 +164,7 @@ RSpec.describe ApprovalRules::ParamsFilteringService do
let(:can_update_approvers?) { true }
let(:approval_rules_attributes) do
[
{ user_ids: [], group_ids: [] }
{ user_ids: [], group_ids: [], name: '' }
]
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment