Commit 3455e77e authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch 'kerrizor/update-accepted-param-on-find_or_create_code_owner_rule' into 'master'

Update accepted params to accept an Entry object

See merge request gitlab-org/gitlab!30123
parents 014df187 c5da4406
......@@ -65,8 +65,8 @@ class ApprovalMergeRequestRule < ApplicationRecord
scope :open_merge_requests, -> { merge(MergeRequest.opened) }
scope :for_checks_that_can_be_refreshed, -> { license_compliance.open_merge_requests.with_head_pipeline }
def self.find_or_create_code_owner_rule(merge_request, pattern)
merge_request.approval_rules.code_owner.where(name: pattern).first_or_create do |rule|
def self.find_or_create_code_owner_rule(merge_request, entry)
merge_request.approval_rules.code_owner.where(name: entry.pattern).first_or_create do |rule|
rule.rule_type = :code_owner
rule.code_owner = true # deprecated, replaced with `rule_type: :code_owner`
end
......
......@@ -30,7 +30,7 @@ module MergeRequests
attr_reader :merge_request, :previous_diff
def create_rule(entry)
ApprovalMergeRequestRule.find_or_create_code_owner_rule(merge_request, entry.pattern)
ApprovalMergeRequestRule.find_or_create_code_owner_rule(merge_request, entry)
end
def delete_outdated_code_owner_rules
......
......@@ -44,10 +44,10 @@ module EE
merge_request.target_project
end
def self.find_or_create_code_owner_rule(merge_request, pattern)
def self.find_or_create_code_owner_rule(merge_request, entry)
merge_request.approval_rules.safe_find_or_create_by(
code_owner: true,
name: pattern
name: entry.pattern
)
end
......@@ -130,7 +130,7 @@ module EE
rule = approval_rules.code_owner.first
rule ||= ApprovalMergeRequestRule.find_or_create_code_owner_rule(
self,
'Code Owner'
::Gitlab::CodeOwners::Entry.new("Code Owner", owners)
)
rule.users = owners.uniq
......
......@@ -158,12 +158,16 @@ describe ApprovalMergeRequestRule do
let!(:existing_code_owner_rule) { create(:code_owner_rule, name: '*.rb', merge_request: merge_request) }
it 'finds an existing rule' do
expect(described_class.find_or_create_code_owner_rule(merge_request, '*.rb'))
entry = Gitlab::CodeOwners::Entry.new("*.rb", "@user")
expect(described_class.find_or_create_code_owner_rule(merge_request, entry))
.to eq(existing_code_owner_rule)
end
it 'creates a new rule if it does not exist' do
expect { described_class.find_or_create_code_owner_rule(merge_request, '*.js') }
entry = Gitlab::CodeOwners::Entry.new("*.js", "@user")
expect { described_class.find_or_create_code_owner_rule(merge_request, entry) }
.to change { merge_request.approval_rules.matching_pattern('*.js').count }.by(1)
end
......@@ -171,7 +175,9 @@ describe ApprovalMergeRequestRule do
deprecated_code_owner_rule = create(:code_owner_rule, name: '*.md', merge_request: merge_request)
deprecated_code_owner_rule.update_column(:rule_type, described_class.rule_types[:regular])
expect(described_class.find_or_create_code_owner_rule(merge_request, '*.md'))
entry = Gitlab::CodeOwners::Entry.new("*.md", "@user")
expect(described_class.find_or_create_code_owner_rule(merge_request, entry))
.to eq(deprecated_code_owner_rule)
end
......@@ -179,7 +185,9 @@ describe ApprovalMergeRequestRule do
expect(described_class).to receive(:code_owner).and_raise(ActiveRecord::RecordNotUnique)
allow(described_class).to receive(:code_owner).and_call_original
expect(described_class.find_or_create_code_owner_rule(merge_request, '*.js')).not_to be_nil
entry = Gitlab::CodeOwners::Entry.new("*.js", "@user")
expect(described_class.find_or_create_code_owner_rule(merge_request, entry)).not_to be_nil
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