Commit d641d25d authored by Mark Chao's avatar Mark Chao

Add validation to rule models

Ensure MR rule approvals_required > PR rule's
parent 8ea0444f
...@@ -15,6 +15,8 @@ class ApprovalMergeRequestRule < ApplicationRecord ...@@ -15,6 +15,8 @@ class ApprovalMergeRequestRule < ApplicationRecord
has_one :approval_merge_request_rule_source has_one :approval_merge_request_rule_source
has_one :approval_project_rule, through: :approval_merge_request_rule_source has_one :approval_project_rule, through: :approval_merge_request_rule_source
validate :validate_approvals_required
def project def project
merge_request.target_project merge_request.target_project
end end
...@@ -44,4 +46,15 @@ class ApprovalMergeRequestRule < ApplicationRecord ...@@ -44,4 +46,15 @@ class ApprovalMergeRequestRule < ApplicationRecord
!code_owner? !code_owner?
end end
alias_method :regular?, :regular alias_method :regular?, :regular
private
def validate_approvals_required
return unless approval_project_rule
return unless approvals_required_changed?
if approvals_required < approval_project_rule.approvals_required
errors.add(:approvals_required, :greater_than_or_equal_to, count: approval_project_rule.approvals_required)
end
end
end end
...@@ -12,7 +12,7 @@ module ApprovalRuleLike ...@@ -12,7 +12,7 @@ module ApprovalRuleLike
has_many :group_users, -> { distinct }, through: :groups, source: :users has_many :group_users, -> { distinct }, through: :groups, source: :users
validates :name, presence: true validates :name, presence: true
validates :approvals_required, numericality: { less_than_or_equal_to: APPROVALS_REQUIRED_MAX } validates :approvals_required, numericality: { less_than_or_equal_to: APPROVALS_REQUIRED_MAX, greater_than_or_equal_to: 0 }
end end
# Users who are eligible to approve, including specified group members. # Users who are eligible to approve, including specified group members.
......
...@@ -85,4 +85,32 @@ describe ApprovalMergeRequestRule do ...@@ -85,4 +85,32 @@ describe ApprovalMergeRequestRule do
end end
end end
end end
describe 'validations' do
describe 'approvals_required' do
subject { build(:approval_merge_request_rule, merge_request: merge_request) }
it 'is a natual number' do
subject.assign_attributes(approvals_required: 2)
expect(subject).to be_valid
subject.assign_attributes(approvals_required: 0)
expect(subject).to be_valid
subject.assign_attributes(approvals_required: -1)
expect(subject).to be_invalid
end
context 'when project rule is present' do
let(:project_rule) { create(:approval_project_rule, project: merge_request.project, approvals_required: 3) }
it 'has to be greater than or equal to project rule approvals_required' do
subject.assign_attributes(approval_project_rule: project_rule, approvals_required: 2)
subject.valid?
expect(subject.errors[:approvals_required]).to include("must be greater than or equal to 3")
end
end
end
end
end 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