Skip to content

Commit fb9434d

Browse files
authored
fix: go back to old way of creating paths in directory ownership (#84)
* fix: go back to old way of creating paths in directory ownership * add some more tests for `..` in filename
1 parent bd75d69 commit fb9434d

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
code_ownership (1.36.0)
4+
code_ownership (1.36.1)
55
code_teams (~> 1.0)
66
packs-specification
77
sorbet-runtime (>= 0.5.10821)

code_ownership.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |spec|
22
spec.name = 'code_ownership'
3-
spec.version = '1.36.0'
3+
spec.version = '1.36.1'
44
spec.authors = ['Gusto Engineers']
55
spec.email = ['[email protected]']
66
spec.summary = 'A gem to help engineering teams declare ownership of code'

lib/code_ownership/private/ownership_mappers/directory_ownership.rb

+11-5
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ class DirectoryOwnership
1010
include Mapper
1111

1212
CODEOWNERS_DIRECTORY_FILE_NAME = '.codeowner'
13-
RELATIVE_ROOT = Pathname('.').freeze
14-
ABSOLUTE_ROOT = Pathname('/').freeze
1513

1614
@@directory_cache = T.let({}, T::Hash[String, T.nilable(CodeTeams::Team)]) # rubocop:disable Style/ClassVars
1715

@@ -88,11 +86,19 @@ def map_file_to_relevant_owner(file)
8886

8987
if File.directory?(file)
9088
team = get_team_from_codeowners_file_within_directory(file_path)
89+
return team unless team.nil?
9190
end
9291

93-
while team.nil? && file_path != RELATIVE_ROOT && file_path != ABSOLUTE_ROOT
94-
file_path = file_path.parent
95-
team = get_team_from_codeowners_file_within_directory(file_path)
92+
path_components = file_path.each_filename.to_a
93+
if file_path.absolute?
94+
path_components = ['/', *path_components]
95+
end
96+
97+
(path_components.length - 1).downto(0).each do |i|
98+
team = get_team_from_codeowners_file_within_directory(
99+
Pathname.new(File.join(*T.unsafe(path_components[0...i])))
100+
)
101+
return team unless team.nil?
96102
end
97103

98104
team

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

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module CodeOwnership
2-
RSpec.describe Private::OwnershipMappers::JsPackageOwnership do
2+
RSpec.describe Private::OwnershipMappers::DirectoryOwnership do
33
describe 'CodeOwnershp.for_file' do
44
before do
55
write_configuration
@@ -14,17 +14,30 @@ module CodeOwnership
1414
CONTENTS
1515
end
1616

17+
subject { described_class.new }
18+
19+
before do
20+
subject.bust_caches!
21+
end
22+
1723
it 'can find the owner of files in team-owned directory' do
18-
expect(CodeOwnership.for_file('a/b/b_file.jsx').name).to eq 'Bar'
24+
expect(subject.map_file_to_owner('a/b/b_file.jsx').name).to eq 'Bar'
1925
end
2026

2127
it 'can find the owner of files in a sub-directory of a team-owned directory' do
22-
expect(CodeOwnership.for_file('a/b/c/c_file.jsx').name).to eq 'Bar'
28+
expect(subject.map_file_to_owner('a/b/c/c_file.jsx').name).to eq 'Bar'
29+
end
30+
31+
it 'returns null when no team is found' do
32+
expect(subject.map_file_to_owner('tmp/tmp/foo.txt')).to be_nil
33+
expect(subject.map_file_to_owner('../tmp/tmp/foo.txt')).to be_nil
34+
expect(subject.map_file_to_owner(Pathname.pwd.join('tmp/tmp/foo.txt').to_s)).to be_nil
2335
end
2436

2537
it 'looks for codeowner file within directory' do
26-
expect(CodeOwnership.for_file('a/b').name).to eq 'Bar'
27-
expect(CodeOwnership.for_file(Pathname.pwd.join('a/b').to_s).name).to eq 'Bar'
38+
expect(subject.map_file_to_owner('a/b').name).to eq 'Bar'
39+
expect(subject.map_file_to_owner('a/../a/b').name).to eq 'Bar'
40+
expect(subject.map_file_to_owner(Pathname.pwd.join('a/b').to_s).name).to eq 'Bar'
2841
end
2942
end
3043
end

0 commit comments

Comments
 (0)