Commit fd49ef88 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch 'id-fix-api-for-mr-approval-rules' into 'master'

Fix create/delete API calls for approval rules

See merge request gitlab-org/gitlab!23107
parents 60787452 36ed813f
......@@ -50,4 +50,8 @@ module ApprovalRuleLike
def any_approver?
raise NotImplementedError
end
def user_defined?
regular? || any_approver?
end
end
......@@ -4,7 +4,7 @@ class ApprovalMergeRequestRulePolicy < BasePolicy
delegate { @subject.merge_request }
condition(:editable) do
can?(:update_merge_request, @subject.merge_request) && @subject.regular?
can?(:update_merge_request, @subject.merge_request) && @subject.user_defined?
end
rule { editable }.enable :edit_approval_rule
......
......@@ -14,7 +14,7 @@ module ApprovalRules
params.reverse_merge!(rule_type: :report_approver)
end
handle_any_approver_rule_creation(target, params) if target.is_a?(Project)
handle_any_approver_rule_creation(target, @rule.project, params)
copy_approval_project_rule_properties(params) if target.is_a?(MergeRequest)
super(@rule.project, user, params)
......@@ -38,14 +38,14 @@ module ApprovalRules
params[:groups] = approval_project_rule.groups
end
def handle_any_approver_rule_creation(target, params)
def handle_any_approver_rule_creation(target, project, params)
if params[:user_ids].blank? && params[:group_ids].blank?
params.reverse_merge!(rule_type: :any_approver, name: ApprovalRuleLike::ALL_MEMBERS)
return
end
return if target.multiple_approval_rules_available?
return if project.multiple_approval_rules_available?
target.approval_rules.any_approver.delete_all
end
......
---
title: Fix create/delete API calls for approval rules
merge_request: 23107
author:
type: fixed
......@@ -15,7 +15,15 @@ describe ApprovalMergeRequestRulePolicy do
expect(permissions(merge_request.author, approval_rule)).to be_allowed(:edit_approval_rule)
end
context 'when rule is not regular type' do
context 'when rule is any-approval' do
let(:approval_rule) { build(:any_approver_rule, merge_request: merge_request) }
it 'allows updating approval rule' do
expect(permissions(merge_request.author, approval_rule)).to be_allowed(:edit_approval_rule)
end
end
context 'when rule is not user editable' do
let(:approval_rule) { create(:code_owner_rule, merge_request: merge_request) }
it 'disallows updating approval rule' do
......
......@@ -76,6 +76,45 @@ describe ApprovalRules::CreateService do
expect(result[:message]).to include('Prohibited')
end
end
context 'when approval rule with empty users and groups is being created' do
subject { described_class.new(target, user, { user_ids: [], group_ids: [] }) }
it 'sets default attributes for any-approver rule' do
rule = subject.execute[:rule]
expect(rule[:rule_type]).to eq('any_approver')
expect(rule[:name]).to eq('All Members')
end
end
context 'when any-approver rule exists' do
before do
target.approval_rules.create!(rule_type: :any_approver, name: 'All members')
end
context 'multiple approval rules are not enabled' do
subject { described_class.new(target, user, { user_ids: [1], group_ids: [] }) }
it 'removes the rule if a regular one is created' do
expect { subject.execute }.to change(
target.approval_rules.any_approver, :count
).from(1).to(0)
end
end
context 'multiple approval rules are enabled' do
subject { described_class.new(target, user, { user_ids: [1], group_ids: [] }) }
before do
stub_licensed_features(multiple_approval_rules: true)
end
it 'does not remove any approval rule' do
expect { subject.execute }.not_to change(target.approval_rules.any_approver, :count)
end
end
end
end
context 'when target is project' do
......@@ -148,37 +187,6 @@ describe ApprovalRules::CreateService do
specify { expect(result[:rule].rule_type).to eq('report_approver') }
end
end
context 'when approval rule is being created' do
subject { described_class.new(target, user, { user_ids: [], group_ids: [] }) }
it 'sets default attributes for any-approver rule' do
rule = subject.execute[:rule]
expect(rule[:rule_type]).to eq('any_approver')
expect(rule[:name]).to eq('All Members')
end
end
context 'when any-approver rule exists' do
let!(:any_approver_rule) do
create(:approval_project_rule, project: target, rule_type: :any_approver)
end
context 'multiple approval rules are not enabled' do
subject { described_class.new(target, user, { user_ids: [1], group_ids: [] }) }
before do
stub_licensed_features(multiple_approval_rules: false)
end
it 'removes the rule if a regular one is created' do
expect { subject.execute }.to change(
target.approval_rules.any_approver, :count
).from(1).to(0)
end
end
end
end
context 'when target is merge request' 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