Commit 8df499ce authored by Kerri Miller's avatar Kerri Miller

Add method for sectional processing of CODEOWNERS

This adds the initial pass at addion sectional processing of CODEOWNERS
file(s) as a new. feature-flag restricted method. It does not actively
use this code in this commit.
parent 78b68298
......@@ -5,6 +5,8 @@ module Gitlab
class Entry
include ::Gitlab::Utils::StrongMemoize
DEFAULT_SECTION = "codeowners"
Data = Struct.new(:pattern, :owner_line, :section)
attr_reader :data
......@@ -12,8 +14,8 @@ module Gitlab
delegate :pattern, :hash, :owner_line, :section, to: :data
def initialize(pattern, owner_line, section = "CODEOWNERS")
@data = Data.new(pattern, owner_line, section)
def initialize(pattern, owner_line, section = DEFAULT_SECTION)
@data = Data.new(pattern, owner_line, section.downcase)
end
def all_users
......
......@@ -63,7 +63,34 @@ module Gitlab
end
def get_parsed_sectional_data
{}
parsed = {}
section = ::Gitlab::CodeOwners::Entry::DEFAULT_SECTION.downcase
parsed[section] = {}
data.lines.each do |line|
line = line.strip
next unless line.present?
next if line.starts_with?('#')
if line.starts_with?('[') && line.end_with?(']')
section = line[1...-1].downcase
parsed[section] ||= {}
next
end
pattern, _separator, owners = line.partition(/(?<!\\)\s+/)
normalized_pattern = normalize_pattern(pattern)
# We still suffer from last-in overwrites, as we don't yet (%13.0)
# allow for multiple matches, even within the section.
#
parsed[section][normalized_pattern] = Entry.new(pattern, owners, section)
end
parsed
end
def 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
......@@ -43,9 +43,44 @@ describe Gitlab::CodeOwners::File do
end
it "passes the call to #get_parsed_sectional_data" do
expect(file).to receive(:get_parsed_sectional_data).and_return({})
expect(file).to receive(:get_parsed_sectional_data)
expect(file.parsed_data).to be_empty
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.downcase)
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
data = file.parsed_data
expect(data.keys.length).to eq(3)
expect(data.keys).to contain_exactly("codeowners", "documentation", "database")
end
where(:section, :patterns) do
"codeowners" | ["/**/ee/**/*"]
"documentation" | ["/**/README.md", "/**/ee/docs", "/**/docs"]
"database" | ["/**/README.md", "/**/model/db"]
end
with_them do
it "assigns the correct paths to each section" do
expect(file.parsed_data[section].keys).to contain_exactly(*patterns)
expect(file.parsed_data[section].values.detect { |entry| entry.section != section }).to be_nil
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