Commit 304a69bb authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch 'id-scope-name-validation-for-approval-rules' into 'master'

Scope name uniqueness of approval rules to type

See merge request gitlab-org/gitlab!25875
parents 1154086f 579e57dd
......@@ -23,7 +23,7 @@ class ApprovalMergeRequestRule < ApplicationRecord
end
end
validates :name, uniqueness: { scope: [:merge_request, :code_owner] }
validates :name, uniqueness: { scope: [:merge_request_id, :rule_type] }
validates :rule_type, uniqueness: { scope: :merge_request_id, message: proc { _('any-approver for the merge request already exists') } }, if: :any_approver?
validates :report_type, presence: true, if: :report_approver?
# Temporary validations until `code_owner` can be dropped in favor of `rule_type`
......
......@@ -16,7 +16,7 @@ class ApprovalProjectRule < ApplicationRecord
alias_method :code_owner, :code_owner?
validate :validate_default_license_report_name, on: :update, if: :report_approver?
validates :name, uniqueness: { scope: :project_id }
validates :name, uniqueness: { scope: [:project_id, :rule_type] }
validates :rule_type, uniqueness: { scope: :project_id, message: proc { _('any-approver for the project already exists') } }, if: :any_approver?
def self.applicable_to_branch(branch)
......
---
title: Scope name uniqueness of approval rules to type
merge_request: 25875
author:
type: fixed
......@@ -16,6 +16,10 @@ describe ApprovalMergeRequestRule do
expect(build(:approval_merge_request_rule, name: nil)).not_to be_valid
end
it 'is invalid when name not unique within rule type and merge request' do
is_expected.to validate_uniqueness_of(:name).scoped_to([:merge_request_id, :rule_type])
end
context 'approval_project_rule is set' do
let(:approval_project_rule) { build(:approval_project_rule) }
let(:merge_request_rule) { build(:approval_merge_request_rule, merge_request: merge_request, approval_project_rule: approval_project_rule) }
......
......@@ -6,7 +6,9 @@ describe ApprovalProjectRule do
subject { create(:approval_project_rule) }
describe 'validations' do
it { is_expected.to validate_uniqueness_of(:name).scoped_to(:project_id) }
it 'is invalid when name not unique within rule type and project' do
is_expected.to validate_uniqueness_of(:name).scoped_to([:project_id, :rule_type])
end
end
describe '.regular' 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