Commit dfbb3aac authored by Rémy Coutable's avatar Rémy Coutable

Merge branch '215191-load-codeowner-entries-from-sectional-codeowners' into 'master'

Enable  #entry_for_path for sectional CODEOWNERS behind feature flag

See merge request gitlab-org/gitlab!31044
parents 67e266d7 beac214b
......@@ -3,6 +3,8 @@
module Gitlab
module CodeOwners
class File
include ::Gitlab::Utils::StrongMemoize
SECTION_HEADER_REGEX = /\[(.*?)\]/.freeze
def initialize(blob, project = nil)
......@@ -21,11 +23,11 @@ module Gitlab
def entry_for_path(path)
path = "/#{path}" unless path.start_with?('/')
matching_pattern = parsed_data.keys.reverse.detect do |pattern|
path_matches?(pattern, path)
if sectional_codeowners?
sectional_entry_for_path(path)
else
non_sectional_entry_for_path(path)
end
parsed_data[matching_pattern].dup if matching_pattern
end
def path
......@@ -34,6 +36,28 @@ module Gitlab
private
def non_sectional_entry_for_path(path)
matching_pattern = parsed_data.keys.reverse.detect do |pattern|
path_matches?(pattern, path)
end
matching_pattern ? [parsed_data[matching_pattern].dup] : []
end
def sectional_entry_for_path(path)
matches = []
parsed_data.each do |_, section_entries|
matching_pattern = section_entries.keys.reverse.detect do |pattern|
path_matches?(pattern, path)
end
matches << section_entries[matching_pattern].dup if matching_pattern
end
matches
end
def data
if @blob && !@blob.binary?
@blob.data
......@@ -43,10 +67,14 @@ module Gitlab
end
def get_parsed_data
if Feature.enabled?(:sectional_codeowners, @project, default_enabled: false)
return get_parsed_sectional_data
if sectional_codeowners?
get_parsed_sectional_data
else
get_parsed_non_sectional_data
end
end
def get_parsed_non_sectional_data
parsed = {}
data.lines.each do |line|
......@@ -140,6 +168,12 @@ module Gitlab
::File.fnmatch?(pattern, path, flags)
end
def sectional_codeowners?
strong_memoize(:sectional_codeowners_check) do
Feature.enabled?(:sectional_codeowners, @project, default_enabled: false)
end
end
end
end
end
......@@ -48,7 +48,7 @@ module Gitlab
code_owners_file.entry_for_path(path)
end
entries.compact.uniq
entries.flatten.compact.uniq
end
def code_owners_file
......
ee/ @gl-admin
# This is an example code owners file, lines starting with a `#` will
# be ignored.
# app/ @commented-rule
# We can specify a default match using wildcards:
* @default-codeowner
# Rules defined later in the file take precedence over the rules
# defined before.
# This will match all files for which the file name ends in `.rb`
*.rb @ruby-owner
# Files with a `#` can still be accesssed by escaping the pound sign
\#file_with_pound.rb @owner-file-with-pound
# Multiple codeowners can be specified, separated by whitespace
CODEOWNERS @multiple @owners @tab-separated
# Both usernames or email addresses can be used to match
# users. Everything else will be ignored. For example this will
# specify `@legal` and a user with email `janedoe@gitlab.com` as the
# owner for the LICENSE file
LICENSE @legal this does not match janedoe@gitlab.com
# Ending a path in a `/` will specify the code owners for every file
# nested in that directory, on any level
/docs/ @all-docs
# Ending a path in `/*` will specify code owners for every file in
# that directory, but not nested deeper. This will match
# `docs/index.md` but not `docs/projects/index.md`
/docs/* @root-docs
# This will make a `lib` directory nested anywhere in the repository
# match
lib/ @lib-owner
# This will only match a `config` directory in the root of the
# repository
/config/ @config-owner
# If the path contains spaces, these need to be escaped like this:
path\ with\ spaces/ @space-owner
[Documentation]
ee/docs @gl-docs
......
ee/ @gl-admin
# This is an example code owners file, lines starting with a `#` will
# be ignored.
# app/ @commented-rule
# We can specify a default match using wildcards:
* @default-codeowner
# Rules defined later in the file take precedence over the rules
# defined before.
# This will match all files for which the file name ends in `.rb`
*.rb @ruby-owner
# Files with a `#` can still be accesssed by escaping the pound sign
\#file_with_pound.rb @owner-file-with-pound
# Multiple codeowners can be specified, separated by whitespace
CODEOWNERS @multiple @owners @tab-separated
# Both usernames or email addresses can be used to match
# users. Everything else will be ignored. For example this will
# specify `@legal` and a user with email `janedoe@gitlab.com` as the
# owner for the LICENSE file
LICENSE @legal this does not match janedoe@gitlab.com
# Ending a path in a `/` will specify the code owners for every file
# nested in that directory, on any level
/docs/ @all-docs
# Ending a path in `/*` will specify code owners for every file in
# that directory, but not nested deeper. This will match
# `docs/index.md` but not `docs/projects/index.md`
/docs/* @root-docs
# This will make a `lib` directory nested anywhere in the repository
# match
lib/ @lib-owner
# This will only match a `config` directory in the root of the
# repository
/config/ @config-owner
# If the path contains spaces, these need to be escaped like this:
path\ with\ spaces/ @space-owner
[Documentation]
ee/docs @gl-docs
......
This diff is collapsed.
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