Commit f0464a1f authored by Igor Drozdov's avatar Igor Drozdov

Merge branch '225234-improve-loading-logic-for-sectional-codeowners' into 'master'

Improve #find_or_create_code_owner_rule to handle sections

See merge request gitlab-org/gitlab!35550
parents f68a8a31 382ab43b
...@@ -66,8 +66,7 @@ class ApprovalMergeRequestRule < ApplicationRecord ...@@ -66,8 +66,7 @@ class ApprovalMergeRequestRule < ApplicationRecord
scope :for_checks_that_can_be_refreshed, -> { license_compliance.open_merge_requests.with_head_pipeline } 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, 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).where(section: entry.section).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
......
...@@ -11,6 +11,7 @@ FactoryBot.define do ...@@ -11,6 +11,7 @@ FactoryBot.define do
rule_type { :code_owner } rule_type { :code_owner }
code_owner { true } # deprecated, replaced with `rule_type: :code_owner` code_owner { true } # deprecated, replaced with `rule_type: :code_owner`
sequence(:name) { |n| "*-#{n}.js" } sequence(:name) { |n| "*-#{n}.js" }
section { Gitlab::CodeOwners::Entry::DEFAULT_SECTION }
end end
factory :report_approver_rule, parent: :approval_merge_request_rule do factory :report_approver_rule, parent: :approval_merge_request_rule do
......
...@@ -169,6 +169,14 @@ RSpec.describe ApprovalMergeRequestRule do ...@@ -169,6 +169,14 @@ RSpec.describe ApprovalMergeRequestRule do
it 'finds the existing rule' do it 'finds the existing rule' do
expect(rule).to eq(existing_code_owner_rule) expect(rule).to eq(existing_code_owner_rule)
end end
context "when the existing rule matches name but not section" do
let(:entry) { Gitlab::CodeOwners::Entry.new("*.rb", "@user", "example_section") }
it "creates a new rule" do
expect(rule).not_to eq(existing_code_owner_rule)
end
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
......
...@@ -125,7 +125,7 @@ RSpec.describe MergeRequests::RefreshService do ...@@ -125,7 +125,7 @@ RSpec.describe MergeRequests::RefreshService do
end end
it_behaves_like 'creates an approval rule based on current diff' do it_behaves_like 'creates an approval rule based on current diff' do
let(:approval_rules_size) { 4 } let(:approval_rules_size) { 5 }
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