Skip to content

Commit 90741f6

Browse files
committed
feat: add concept of unonwed globs to team configs
1 parent 259d602 commit 90741f6

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ Gemfile.lock
1313
.rspec_status
1414

1515
.DS_Store
16+
.idea/

lib/code_ownership/private/ownership_mappers/team_globs.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ class TeamGlobs
1414
@@map_files_to_owners = {} # rubocop:disable Style/ClassVars
1515

1616
sig do
17-
params(files: T::Array[String])
18-
.returns(T::Hash[String, ::CodeTeams::Team])
17+
returns(T::Hash[String, ::CodeTeams::Team])
1918
end
20-
def map_files_to_owners(files)
19+
def map_files_to_owners
2120
return @@map_files_to_owners if @@map_files_to_owners&.keys && @@map_files_to_owners.keys.count.positive?
2221

2322
@@map_files_to_owners = CodeTeams.all.each_with_object({}) do |team, map| # rubocop:disable Style/ClassVars
@@ -26,6 +25,13 @@ def map_files_to_owners(files)
2625
map[filename] = team
2726
end
2827
end
28+
29+
# Remove anything that is unowned
30+
TeamPlugins::Ownership.for(team).unowned_globs.each do |glob|
31+
Dir.glob(glob).each do |filename|
32+
map.delete(filename)
33+
end
34+
end
2935
end
3036
end
3137

@@ -63,6 +69,12 @@ def find_overlapping_globs
6369
T.must(mapped_files[filename]) << MappingContext.new(glob: glob, team: team)
6470
end
6571
end
72+
# Remove anything that is unowned
73+
TeamPlugins::Ownership.for(team).unowned_globs.each do |glob|
74+
Dir.glob(glob).each do |filename|
75+
T.must(mapped_files.delete(filename))
76+
end
77+
end
6678
end
6779

6880
overlaps = T.let([], T::Array[GlobOverlap])
@@ -81,10 +93,10 @@ def find_overlapping_globs
8193

8294
sig do
8395
override.params(file: String)
84-
.returns(T.nilable(::CodeTeams::Team))
96+
.returns(T.nilable(::CodeTeams::Team))
8597
end
8698
def map_file_to_owner(file)
87-
map_files_to_owners([file])[file]
99+
map_files_to_owners[file]
88100
end
89101

90102
sig do
@@ -96,7 +108,7 @@ def update_cache(cache, files)
96108

97109
sig do
98110
override.params(files: T::Array[String])
99-
.returns(T::Hash[String, ::CodeTeams::Team])
111+
.returns(T::Hash[String, ::CodeTeams::Team])
100112
end
101113
def globs_to_owner(files)
102114
CodeTeams.all.each_with_object({}) do |team, map|

lib/code_ownership/private/team_plugins/ownership.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ class Ownership < CodeTeams::Plugin
1111
def owned_globs
1212
@team.raw_hash['owned_globs'] || []
1313
end
14+
15+
sig { returns(T::Array[String]) }
16+
def unowned_globs
17+
@team.raw_hash['unowned_globs'] || []
18+
end
1419
end
1520
end
1621
end

spec/lib/code_ownership/private/ownership_mappers/team_globs_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,26 @@ module CodeOwnership
99
owned_globs:
1010
- app/services/bar_stuff/**/**
1111
- frontend/javascripts/bar_stuff/**/**
12+
- '**/team_thing/**/*'
13+
unowned_globs:
14+
- shared/**/team_thing/**/*
1215
CONTENTS
1316

1417
write_file('app/services/bar_stuff/thing.rb')
1518
write_file('app/services/bar_stuff/[test]/thing.rb')
1619
write_file('frontend/javascripts/bar_stuff/thing.jsx')
20+
write_file('app/services/team_thing/thing.rb')
21+
write_file('shared/config/team_thing/something.rb')
1722
end
1823

1924
it 'can find the owner of ruby files in owned_globs' do
2025
expect(CodeOwnership.for_file('app/services/bar_stuff/thing.rb').name).to eq 'Bar'
2126
expect(CodeOwnership.for_file('app/services/bar_stuff/[test]/thing.rb').name).to eq 'Bar'
27+
expect(CodeOwnership.for_file('app/services/team_thing/thing.rb').name).to eq 'Bar'
28+
end
29+
30+
it 'does not find the owner of ruby files in unowned_globs' do
31+
expect(CodeOwnership.for_file('shared/config/team_thing/something.rb')).to be_nil
2232
end
2333

2434
it 'can find the owner of javascript files in owned_globs' do
@@ -27,6 +37,24 @@ module CodeOwnership
2737
end
2838

2939
describe 'CodeOwnership.validate!' do
40+
context 'has unowned globs' do
41+
before do
42+
write_file('config/teams/bar.yml', <<~CONTENTS)
43+
name: Bar
44+
owned_globs:
45+
- app/services/bar_stuff/**/**
46+
- frontend/javascripts/bar_stuff/**/**
47+
- '**/team_thing/**/*'
48+
unowned_globs:
49+
- shared/**/team_thing/**/*
50+
CONTENTS
51+
end
52+
53+
it 'considers the combination of owned_globs and unowned_globs' do
54+
expect { CodeOwnership.validate! }.to_not raise_error
55+
end
56+
end
57+
3058
context 'two teams own the same exact glob' do
3159
before do
3260
write_configuration

0 commit comments

Comments
 (0)