Commit e3716f67 authored by Kerri Miller's avatar Kerri Miller Committed by Thong Kuah

Add section when creating a codeowners rule

parent 7a998d4e
...@@ -67,6 +67,7 @@ class ApprovalMergeRequestRule < ApplicationRecord ...@@ -67,6 +67,7 @@ class ApprovalMergeRequestRule < ApplicationRecord
def self.find_or_create_code_owner_rule(merge_request, entry) 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| merge_request.approval_rules.code_owner.where(name: entry.pattern).first_or_create do |rule|
rule.section = entry.section
rule.rule_type = :code_owner rule.rule_type = :code_owner
rule.code_owner = true # deprecated, replaced with `rule_type: :code_owner` rule.code_owner = true # deprecated, replaced with `rule_type: :code_owner`
end end
......
...@@ -155,29 +155,32 @@ describe ApprovalMergeRequestRule do ...@@ -155,29 +155,32 @@ describe ApprovalMergeRequestRule do
end end
describe '.find_or_create_code_owner_rule' do describe '.find_or_create_code_owner_rule' do
let!(:existing_code_owner_rule) { create(:code_owner_rule, name: '*.rb', merge_request: merge_request) } subject(:rule) { described_class.find_or_create_code_owner_rule(merge_request, entry) }
it 'finds an existing rule' do let(:entry) { Gitlab::CodeOwners::Entry.new("*.js", "@user") }
entry = Gitlab::CodeOwners::Entry.new("*.rb", "@user")
expect(described_class.find_or_create_code_owner_rule(merge_request, entry)) context "when there is an existing rule" do
.to eq(existing_code_owner_rule) let!(:existing_code_owner_rule) do
create(:code_owner_rule, name: '*.rb', merge_request: merge_request)
end
let(:entry) { Gitlab::CodeOwners::Entry.new("*.rb", "@user") }
it 'finds the existing rule' do
expect(rule).to eq(existing_code_owner_rule)
end
end end
it 'creates a new rule if it does not exist' do it 'creates a new rule if it does not exist' do
entry = Gitlab::CodeOwners::Entry.new("*.js", "@user") expect { rule }
expect { described_class.find_or_create_code_owner_rule(merge_request, entry) }
.to change { merge_request.approval_rules.matching_pattern('*.js').count }.by(1) .to change { merge_request.approval_rules.matching_pattern('*.js').count }.by(1)
end end
it 'finds an existing rule using deprecated code_owner column' do it 'finds an existing rule using deprecated code_owner column' do
deprecated_code_owner_rule = create(:code_owner_rule, name: '*.md', merge_request: merge_request) deprecated_code_owner_rule = create(:code_owner_rule, name: '*.js', merge_request: merge_request)
deprecated_code_owner_rule.update_column(:rule_type, described_class.rule_types[:regular]) deprecated_code_owner_rule.update_column(:rule_type, described_class.rule_types[:regular])
entry = Gitlab::CodeOwners::Entry.new("*.md", "@user") expect(rule)
expect(described_class.find_or_create_code_owner_rule(merge_request, entry))
.to eq(deprecated_code_owner_rule) .to eq(deprecated_code_owner_rule)
end end
...@@ -185,9 +188,15 @@ describe ApprovalMergeRequestRule do ...@@ -185,9 +188,15 @@ describe ApprovalMergeRequestRule do
expect(described_class).to receive(:code_owner).and_raise(ActiveRecord::RecordNotUnique) expect(described_class).to receive(:code_owner).and_raise(ActiveRecord::RecordNotUnique)
allow(described_class).to receive(:code_owner).and_call_original allow(described_class).to receive(:code_owner).and_call_original
entry = Gitlab::CodeOwners::Entry.new("*.js", "@user") expect(rule).not_to be_nil
end
expect(described_class.find_or_create_code_owner_rule(merge_request, entry)).not_to be_nil context "when section is present" do
let(:entry) { Gitlab::CodeOwners::Entry.new("*.js", "@user", "Test Section") }
it "creates a new rule and saves section when present" do
expect(subject.section).to eq(entry.section)
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