Commit 6a25a603 authored by Vasilii Iakliushin's avatar Vasilii Iakliushin Committed by Igor Drozdov

Extend Gitlab::Codeowners to include a method to return sections

Closes https://gitlab.com/gitlab-org/gitlab/-/issues/232988

* Add method `.sections` to `Gitlab::CodeOwners`
parent 1036a1d0
---
title: Extend Gitlab::Codeowners to include a method for returning the sections only
merge_request: 48898
author:
type: added
......@@ -13,6 +13,15 @@ module Gitlab
end
end
# @param project [Project]
# @param ref [String]
# Fetch sections from CODEOWNERS file
def self.sections(project, ref)
return [] unless project.feature_available?(:code_owners)
Loader.new(project, ref, []).code_owners_sections
end
# @param merge_request [MergeRequest]
# @param merge_request_diff [MergeRequestDiff]
# Find code owners entries at a particular MergeRequestDiff.
......
......@@ -31,6 +31,10 @@ module Gitlab
@blob&.path
end
def sections
parsed_data.keys
end
def entry_for_path(path)
path = "/#{path}" unless path.start_with?('/')
......
......@@ -41,6 +41,10 @@ module Gitlab
code_owners_file&.path
end
def code_owners_sections
code_owners_file&.sections
end
private
def load_bare_entries_for_paths
......
......@@ -153,6 +153,39 @@ RSpec.describe Gitlab::CodeOwners::File do
end
end
describe '#sections' do
subject { file.sections }
context 'when CODEOWNERS file contains sections' do
let(:file_content) do
<<~CONTENT
*.rb @ruby-owner
[Documentation]
*.md @gl-docs
[Test]
*_spec.rb @gl-test
[Documentation]
doc/* @gl-docs
CONTENT
end
it 'returns unique sections' do
is_expected.to match_array(%w[codeowners Documentation Test])
end
end
context 'when CODEOWNERS file is missing' do
let(:blob) { nil }
it 'returns a default section' do
is_expected.to match_array(['codeowners'])
end
end
end
describe '#entry_for_path' do
shared_examples_for "returns expected matches" do
context 'for a path without matches' do
......
......@@ -165,6 +165,28 @@ RSpec.describe Gitlab::CodeOwners::Loader do
end
end
describe '#code_owners_sections' do
subject { loader.code_owners_sections }
context 'when CODEOWNERS does not have sections' do
it { is_expected.to match_array(['codeowners']) }
end
context 'when CODEOWNERS contains sections' do
let(:codeowner_content) do
<<~CODEOWNERS
[Documentation]
docs/* @documentation-owner
docs/CODEOWNERS @owner-1 owner2@gitlab.org @owner-3 @documentation-owner
[Testing]
spec/* @test-owner @test-group @test-group/nested-group
CODEOWNERS
end
it { is_expected.to match_array(%w[codeowners Documentation Testing]) }
end
end
describe '#empty_code_owners?' do
context 'when file does not exist' do
let(:codeowner_blob) { nil }
......
......@@ -44,6 +44,33 @@ RSpec.describe Gitlab::CodeOwners do
end
end
describe '.sections' do
subject { described_class.sections(project, branch) }
let(:branch) { TestEnv::BRANCH_SHA['with-codeowners'] }
let(:codeowner_lookup_ref) { branch }
context 'when the feature is available' do
before do
stub_licensed_features(code_owners: true)
end
it 'returns sections' do
is_expected.to match_array(['codeowners'])
end
end
context 'when the feature is not available' do
before do
stub_licensed_features(code_owners: false)
end
it 'returns empty array' do
is_expected.to be_empty
end
end
end
describe ".fast_path_lookup and .slow_path_lookup" do
let(:codeowner_lookup_ref) { merge_request.target_branch }
let(:codeowner_content) { 'files/ruby/feature.rb @owner-1' }
......
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