Commit 1adabce4 authored by Kerri Miller's avatar Kerri Miller

Update accepted params to accept and Entry object

parent d44d494c
......@@ -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
......
......@@ -130,7 +130,7 @@ module EE
rule = approval_rules.code_owner.first
rule ||= ApprovalMergeRequestRule.find_or_create_code_owner_rule(
self,
'Code Owner'
Struct.new(:pattern, :owner_line).new("Code Owner")
)
rule.users = owners.uniq
......
......@@ -156,14 +156,19 @@ describe ApprovalMergeRequestRule do
describe '.find_or_create_code_owner_rule' do
let!(:existing_code_owner_rule) { create(:code_owner_rule, name: '*.rb', merge_request: merge_request) }
let(:entry) { Struct.new(:pattern, :owner_line).new }
it 'finds an existing rule' do
expect(described_class.find_or_create_code_owner_rule(merge_request, '*.rb'))
entry.pattern = "*.rb"
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.pattern = "*.js"
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 +176,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.pattern = "*.md"
expect(described_class.find_or_create_code_owner_rule(merge_request, entry))
.to eq(deprecated_code_owner_rule)
end
......@@ -179,7 +186,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.pattern = "*.js"
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