Commit a2242f53 authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch 'kerrizor/sectional-codeowners-case-sensitivity-on-section-names' into 'master'

Disregard case of section names when parsing

See merge request gitlab-org/gitlab!30898
parents 3fcec333 77e70906
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
module Gitlab module Gitlab
module CodeOwners module CodeOwners
class File class File
SECTION_HEADER_REGEX = /\[(.*?)\]/.freeze
def initialize(blob, project = nil) def initialize(blob, project = nil)
@blob = blob @blob = blob
@project = project @project = project
...@@ -52,37 +54,51 @@ module Gitlab ...@@ -52,37 +54,51 @@ module Gitlab
next if skip?(line) next if skip?(line)
extract_entry_and_populate_parsed(line, parsed) extract_entry_and_populate_parsed_data(line, parsed)
end end
parsed parsed
end end
def get_parsed_sectional_data def get_parsed_sectional_data
parsed = {} parsed_sectional_data = {}
section = ::Gitlab::CodeOwners::Entry::DEFAULT_SECTION canonical_section_name = ::Gitlab::CodeOwners::Entry::DEFAULT_SECTION
parsed[section] = {} parsed_sectional_data[canonical_section_name] = {}
data.lines.each do |line| data.lines.each do |line|
line = line.strip line = line.strip
next if skip?(line) next if skip?(line)
if line.starts_with?('[') && line.end_with?(']') # Detect section headers, and if found, make sure data structure is
section = line[1...-1].strip # set up to hold the entries it contains, and proceed to the next
parsed[section] ||= {} # line in the file.
#
if line.match?(SECTION_HEADER_REGEX)
parsed_section_name = line[1...-1].strip
canonical_section_name = find_section_name(parsed_section_name, parsed_sectional_data)
parsed_sectional_data[canonical_section_name] ||= {}
next next
end end
extract_entry_and_populate_parsed(line, parsed, section) extract_entry_and_populate_parsed_data(line, parsed_sectional_data, canonical_section_name)
end end
parsed parsed_sectional_data
end
def find_section_name(section, parsed_sectional_data)
section_headers = parsed_sectional_data.keys
return section if section_headers.last == ::Gitlab::CodeOwners::Entry::DEFAULT_SECTION
section_headers.find { |k| k.casecmp?(section) } || section
end end
def extract_entry_and_populate_parsed(line, parsed, section = nil) def extract_entry_and_populate_parsed_data(line, parsed, section = nil)
pattern, _separator, owners = line.partition(/(?<!\\)\s+/) pattern, _separator, owners = line.partition(/(?<!\\)\s+/)
normalized_pattern = normalize_pattern(pattern) normalized_pattern = normalize_pattern(pattern)
......
ee/ @gl-admin
[Documentation]
ee/docs @gl-docs
docs @gl-docs
[Database]
README.md @gl-database
model/db @gl-database
[dOcUmEnTaTiOn]
README.md @gl-docs
...@@ -38,30 +38,13 @@ describe Gitlab::CodeOwners::File do ...@@ -38,30 +38,13 @@ describe Gitlab::CodeOwners::File do
end end
context "when feature flag `:sectional_codeowners` is enabled" do context "when feature flag `:sectional_codeowners` is enabled" do
using RSpec::Parameterized::TableSyntax
before do before do
stub_feature_flags(sectional_codeowners: true) stub_feature_flags(sectional_codeowners: true)
end end
it "passes the call to #get_parsed_sectional_data" do shared_examples_for "creates expected parsed results" do
expect(file).to receive(:get_parsed_sectional_data)
file.parsed_data
end
it "populates a hash with a single default section" do
data = file.parsed_data
expect(data.keys.length).to eq(1)
expect(data.keys).to contain_exactly(::Gitlab::CodeOwners::Entry::DEFAULT_SECTION)
end
context "when CODEOWNERS file contains multiple sections" do
using RSpec::Parameterized::TableSyntax
let(:file_content) do
File.read(Rails.root.join("ee", "spec", "fixtures", "sectional_codeowners_example"))
end
it "is a hash sorted by sections without duplicates" do it "is a hash sorted by sections without duplicates" do
data = file.parsed_data data = file.parsed_data
...@@ -87,6 +70,35 @@ describe Gitlab::CodeOwners::File do ...@@ -87,6 +70,35 @@ describe Gitlab::CodeOwners::File do
end end
end end
end end
it "passes the call to #get_parsed_sectional_data" do
expect(file).to receive(:get_parsed_sectional_data)
file.parsed_data
end
it "populates a hash with a single default section" do
data = file.parsed_data
expect(data.keys.length).to eq(1)
expect(data.keys).to contain_exactly(::Gitlab::CodeOwners::Entry::DEFAULT_SECTION)
end
context "when CODEOWNERS file contains multiple sections" do
let(:file_content) do
File.read(Rails.root.join("ee", "spec", "fixtures", "sectional_codeowners_example"))
end
it_behaves_like "creates expected parsed results"
end
context "when CODEOWNERS file contains multiple sections with mixed-case names" do
let(:file_content) do
File.read(Rails.root.join("ee", "spec", "fixtures", "mixed_case_sectional_codeowners_example"))
end
it_behaves_like "creates expected parsed results"
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