Commit 77e70906 authored by Kerri Miller's avatar Kerri Miller Committed by Mayra Cabrera

Disregard case of section names when parsing

We want to make sure that we match section names regardless of case, so
[Database], [DataBase], and [database] would all be considered a single
seciton. The first case pattern that is encountered will be considered
the canonical spelling.
parent 65f04870
......@@ -3,6 +3,8 @@
module Gitlab
module CodeOwners
class File
SECTION_HEADER_REGEX = /\[(.*?)\]/.freeze
def initialize(blob, project = nil)
@blob = blob
@project = project
......@@ -52,37 +54,51 @@ module Gitlab
next if skip?(line)
extract_entry_and_populate_parsed(line, parsed)
extract_entry_and_populate_parsed_data(line, parsed)
end
parsed
end
def get_parsed_sectional_data
parsed = {}
section = ::Gitlab::CodeOwners::Entry::DEFAULT_SECTION
parsed_sectional_data = {}
canonical_section_name = ::Gitlab::CodeOwners::Entry::DEFAULT_SECTION
parsed[section] = {}
parsed_sectional_data[canonical_section_name] = {}
data.lines.each do |line|
line = line.strip
next if skip?(line)
if line.starts_with?('[') && line.end_with?(']')
section = line[1...-1].strip
parsed[section] ||= {}
# Detect section headers, and if found, make sure data structure is
# set up to hold the entries it contains, and proceed to the next
# 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
end
extract_entry_and_populate_parsed(line, parsed, section)
extract_entry_and_populate_parsed_data(line, parsed_sectional_data, canonical_section_name)
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
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+/)
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
end
context "when feature flag `:sectional_codeowners` is enabled" do
using RSpec::Parameterized::TableSyntax
before do
stub_feature_flags(sectional_codeowners: true)
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
using RSpec::Parameterized::TableSyntax
let(:file_content) do
File.read(Rails.root.join("ee", "spec", "fixtures", "sectional_codeowners_example"))
end
shared_examples_for "creates expected parsed results" do
it "is a hash sorted by sections without duplicates" do
data = file.parsed_data
......@@ -87,6 +70,35 @@ describe Gitlab::CodeOwners::File do
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
......
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