Commit c00f1154 authored by Igor Drozdov's avatar Igor Drozdov

Fix creating merge requests with approval rules

It failed because any_approver approval rules have projects
but they shouldn't.

This commit fixes the MR creation with such rules.

The data can be fixed later
parent 971aa13b
......@@ -11,17 +11,25 @@ module ApprovalRules
end
def visible_groups
@visible_groups ||= rule.groups.public_or_visible_to_user(current_user)
@visible_groups ||= groups.public_or_visible_to_user(current_user)
end
# rubocop: disable CodeReuse/ActiveRecord
def hidden_groups
@hidden_groups ||= rule.groups.where.not(id: visible_groups.map(&:id))
@hidden_groups ||= groups.where.not(id: visible_groups.map(&:id))
end
def contains_hidden_groups?
hidden_groups.loaded? ? hidden_groups.present? : hidden_groups.exists?
end
# rubocop: enable CodeReuse/ActiveRecord
private
def groups
return Group.none if rule.any_approver?
rule.groups
end
end
end
---
title: Fix creating merge requests with approval rules
merge_request: 33582
author:
type: fixed
......@@ -159,6 +159,33 @@ describe ApprovalRules::ParamsFilteringService do
expect(rule[:name]).to eq('All Members')
end
end
# A test case for https://gitlab.com/gitlab-org/gitlab/-/issues/208978#note_353379792
# Approval project rules with any_approver type have groups, but they shouldn't
context 'any approver rule from a project rule' do
let(:can_update_approvers?) { true }
let(:approval_rules_attributes) do
[
{ user_ids: [""], group_ids: [""], approval_project_rule_id: approval_rule.id }
]
end
context 'and the project rule has hidden groups' do
let(:approval_rule) do
create(:approval_project_rule, project: project, rule_type: :any_approver).tap do |rule|
rule.groups << create(:group, :private)
end
end
it 'sets rule type for the rules attributes' do
params = service.execute
rule = params[:approval_rules_attributes].first
expect(rule[:rule_type]).to eq(:any_approver)
expect(rule[:name]).to eq('All Members')
end
end
end
end
context 'update' do
......
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