Skip to content

Commit 0383709

Browse files
author
Alex Evanczuk
authored
Use ParsePackwerk.package_from_path (#19)
1 parent d04f245 commit 0383709

File tree

5 files changed

+24
-39
lines changed

5 files changed

+24
-39
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
code_ownership (1.28.1)
4+
code_ownership (1.28.2)
55
code_teams (~> 1.0)
66
parse_packwerk
77
sorbet-runtime

code_ownership.gemspec

Lines changed: 1 addition & 1 deletion
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.28.1'
3+
spec.version = '1.28.2'
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/package_ownership.rb

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class PackageOwnership
1616
returns(T.nilable(::CodeTeams::Team))
1717
end
1818
def map_file_to_owner(file)
19-
package = map_file_to_relevant_package(file)
19+
package = ParsePackwerk.package_from_path(file)
2020

2121
return nil if package.nil?
2222

@@ -80,41 +80,6 @@ def owner_for_package(package)
8080
def bust_caches!
8181
@@package_yml_cache = {} # rubocop:disable Style/ClassVars
8282
end
83-
84-
private
85-
86-
# takes a file and finds the relevant `package.yml` file by walking up the directory
87-
# structure. Example, given `packs/a/b/c.rb`, this looks for `packs/a/b/package.yml`, `packs/a/package.yml`,
88-
# `packs/package.yml`, and `package.yml` in that order, stopping at the first file to actually exist.
89-
# We do additional caching so that we don't have to check for file existence every time
90-
sig { params(file: String).returns(T.nilable(ParsePackwerk::Package)) }
91-
def map_file_to_relevant_package(file)
92-
file_path = Pathname.new(file)
93-
path_components = file_path.each_filename.to_a.map { |path| Pathname.new(path) }
94-
95-
(path_components.length - 1).downto(0).each do |i|
96-
potential_relative_path_name = T.must(path_components[0...i]).reduce(Pathname.new('')) { |built_path, path| built_path.join(path) }
97-
potential_package_yml_path = potential_relative_path_name.
98-
join(ParsePackwerk::PACKAGE_YML_NAME)
99-
100-
potential_package_yml_string = potential_package_yml_path.to_s
101-
102-
package = nil
103-
if @@package_yml_cache.key?(potential_package_yml_string)
104-
package = @@package_yml_cache[potential_package_yml_string]
105-
elsif potential_package_yml_path.exist?
106-
package = ParsePackwerk::Package.from(potential_package_yml_path)
107-
108-
@@package_yml_cache[potential_package_yml_string] = package
109-
else
110-
@@package_yml_cache[potential_package_yml_string] = nil
111-
end
112-
113-
return package unless package.nil?
114-
end
115-
116-
nil
117-
end
11883
end
11984
end
12085
end

spec/lib/code_ownership_spec.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@
103103
metadata:
104104
owner: Bar
105105
CONTENTS
106+
107+
write_file('package.yml', <<~CONTENTS)
108+
enforce_dependency: true
109+
enforce_privacy: true
110+
CONTENTS
106111
end
107112

108113
it 'lets the user know that each file can only have ownership defined in one way' do
@@ -757,7 +762,7 @@ def prevent_false_positive!
757762
before { create_non_empty_application }
758763

759764
it 'returns the right team' do
760-
team = CodeOwnership.for_package(ParsePackwerk.all.last)
765+
team = CodeOwnership.for_package(ParsePackwerk.find('packs/my_other_package'))
761766
expect(team.name).to eq 'Bar'
762767
end
763768
end
@@ -802,6 +807,11 @@ def prevent_false_positive!
802807
803808
# Some content
804809
CONTENTS
810+
811+
write_file('package.yml', <<~CONTENTS)
812+
enforce_dependency: true
813+
enforce_privacy: true
814+
CONTENTS
805815
end
806816

807817
it 'removes the annotation' do
@@ -834,6 +844,11 @@ def prevent_false_positive!
834844
835845
// Some content
836846
CONTENTS
847+
848+
write_file('package.yml', <<~CONTENTS)
849+
enforce_dependency: true
850+
enforce_privacy: true
851+
CONTENTS
837852
end
838853

839854
it 'removes the annotation' do

spec/support/application_fixtures.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
owner: Bar
4343
CONTENTS
4444

45+
write_file('package.yml', <<~CONTENTS)
46+
enforce_dependency: true
47+
enforce_privacy: true
48+
CONTENTS
49+
4550
write_file('packs/my_other_package/my_file.rb')
4651
end
4752

0 commit comments

Comments
 (0)