Skip to content

Commit a560c99

Browse files
committed
private for consistency
1 parent e999726 commit a560c99

File tree

11 files changed

+194
-188
lines changed

11 files changed

+194
-188
lines changed

lib/code_ownership.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
require 'json'
99
require 'packs-specification'
1010
require 'code_ownership/version'
11-
require 'code_ownership/file_path_finder'
12-
require 'code_ownership/file_path_team_cache'
13-
require 'code_ownership/team_finder'
11+
require 'code_ownership/private/file_path_finder'
12+
require 'code_ownership/private/file_path_team_cache'
13+
require 'code_ownership/private/team_finder'
1414
require 'code_ownership/cli'
1515
require 'code_ownership/mapper'
1616

@@ -22,7 +22,7 @@
2222
end
2323

2424
if defined?(Packwerk)
25-
require 'code_ownership/permit_pack_owner_top_level_key'
25+
require 'code_ownership/private/permit_pack_owner_top_level_key'
2626
end
2727

2828
module CodeOwnership
@@ -36,7 +36,7 @@ module CodeOwnership
3636

3737
sig { params(file: String).returns(T.nilable(CodeTeams::Team)) }
3838
def for_file(file)
39-
TeamFinder.for_file(file)
39+
Private::TeamFinder.for_file(file)
4040
end
4141

4242
sig { params(team: T.any(CodeTeams::Team, String)).returns(T::Array[String]) }
@@ -71,24 +71,24 @@ def validate!(
7171
# first line that corresponds to a file with assigned ownership
7272
sig { params(backtrace: T.nilable(T::Array[String]), excluded_teams: T::Array[::CodeTeams::Team]).returns(T.nilable(::CodeTeams::Team)) }
7373
def for_backtrace(backtrace, excluded_teams: [])
74-
TeamFinder.for_backtrace(backtrace, excluded_teams: excluded_teams)
74+
Private::TeamFinder.for_backtrace(backtrace, excluded_teams: excluded_teams)
7575
end
7676

7777
# Given a backtrace from either `Exception#backtrace` or `caller`, find the
7878
# first owned file in it, useful for figuring out which file is being blamed.
7979
sig { params(backtrace: T.nilable(T::Array[String]), excluded_teams: T::Array[::CodeTeams::Team]).returns(T.nilable([::CodeTeams::Team, String])) }
8080
def first_owned_file_for_backtrace(backtrace, excluded_teams: [])
81-
TeamFinder.first_owned_file_for_backtrace(backtrace, excluded_teams: excluded_teams)
81+
Private::TeamFinder.first_owned_file_for_backtrace(backtrace, excluded_teams: excluded_teams)
8282
end
8383

8484
sig { params(klass: T.nilable(T.any(T::Class[T.anything], Module))).returns(T.nilable(::CodeTeams::Team)) }
8585
def for_class(klass)
86-
TeamFinder.for_class(klass)
86+
Private::TeamFinder.for_class(klass)
8787
end
8888

8989
sig { params(package: Packs::Pack).returns(T.nilable(::CodeTeams::Team)) }
9090
def for_package(package)
91-
TeamFinder.for_package(package)
91+
Private::TeamFinder.for_package(package)
9292
end
9393

9494
# Generally, you should not ever need to do this, because once your ruby process loads, cached content should not change.
@@ -97,6 +97,6 @@ def for_package(package)
9797
# has different ownership and tracked files.
9898
sig { void }
9999
def self.bust_caches!
100-
FilePathTeamCache.bust_cache!
100+
Private::FilePathTeamCache.bust_cache!
101101
end
102102
end

lib/code_ownership/file_path_finder.rb

Lines changed: 0 additions & 61 deletions
This file was deleted.

lib/code_ownership/file_path_team_cache.rb

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# frozen_string_literal: true
2+
3+
# typed: strict
4+
5+
module CodeOwnership
6+
module Private
7+
module FilePathFinder
8+
module_function
9+
10+
extend T::Sig
11+
extend T::Helpers
12+
13+
# Returns a string version of the relative path to a Rails constant,
14+
# or nil if it can't find anything
15+
sig { params(klass: T.nilable(T.any(T::Class[T.anything], Module))).returns(T.nilable(String)) }
16+
def path_from_klass(klass)
17+
if klass
18+
path = Object.const_source_location(klass.to_s)&.first
19+
(path && Pathname.new(path).relative_path_from(Pathname.pwd).to_s) || nil
20+
end
21+
rescue NameError
22+
nil
23+
end
24+
25+
sig { params(backtrace: T.nilable(T::Array[String])).returns(T::Enumerable[String]) }
26+
def from_backtrace(backtrace)
27+
return [] unless backtrace
28+
29+
# The pattern for a backtrace hasn't changed in forever and is considered
30+
# stable: https://github.com/ruby/ruby/blob/trunk/vm_backtrace.c#L303-L317
31+
#
32+
# This pattern matches a line like the following:
33+
#
34+
# ./app/controllers/some_controller.rb:43:in `block (3 levels) in create'
35+
#
36+
backtrace_line = if RUBY_VERSION >= '3.4.0'
37+
%r{\A(#{Pathname.pwd}/|\./)?
38+
(?<file>.+) # Matches 'app/controllers/some_controller.rb'
39+
:
40+
(?<line>\d+) # Matches '43'
41+
:in\s
42+
'(?<function>.*)' # Matches "`block (3 levels) in create'"
43+
\z}x
44+
else
45+
%r{\A(#{Pathname.pwd}/|\./)?
46+
(?<file>.+) # Matches 'app/controllers/some_controller.rb'
47+
:
48+
(?<line>\d+) # Matches '43'
49+
:in\s
50+
`(?<function>.*)' # Matches "`block (3 levels) in create'"
51+
\z}x
52+
end
53+
54+
backtrace.lazy.filter_map do |line|
55+
match = line.match(backtrace_line)
56+
next unless match
57+
58+
T.must(match[:file])
59+
end
60+
end
61+
end
62+
end
63+
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# frozen_string_literal: true
2+
3+
# typed: strict
4+
5+
module CodeOwnership
6+
module Private
7+
module FilePathTeamCache
8+
module_function
9+
10+
extend T::Sig
11+
extend T::Helpers
12+
13+
sig { params(file_path: String).returns(T.nilable(CodeTeams::Team)) }
14+
def get(file_path)
15+
cache[file_path]
16+
end
17+
18+
sig { params(file_path: String, team: T.nilable(CodeTeams::Team)).void }
19+
def set(file_path, team)
20+
cache[file_path] = team
21+
end
22+
23+
sig { params(file_path: String).returns(T::Boolean) }
24+
def cached?(file_path)
25+
cache.key?(file_path)
26+
end
27+
28+
sig { void }
29+
def bust_cache!
30+
@cache = nil
31+
end
32+
33+
sig { returns(T::Hash[String, T.nilable(CodeTeams::Team)]) }
34+
def cache
35+
@cache ||= T.let(@cache,
36+
T.nilable(T::Hash[String, T.nilable(CodeTeams::Team)]))
37+
@cache ||= {}
38+
end
39+
end
40+
end
41+
end

lib/code_ownership/permit_pack_owner_top_level_key.rb renamed to lib/code_ownership/private/permit_pack_owner_top_level_key.rb

File renamed without changes.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# frozen_string_literal: true
2+
3+
# typed: strict
4+
5+
module CodeOwnership
6+
module Private
7+
module TeamFinder
8+
module_function
9+
10+
extend T::Sig
11+
extend T::Helpers
12+
13+
requires_ancestor { Kernel }
14+
15+
sig { params(file_path: String).returns(T.nilable(CodeTeams::Team)) }
16+
def for_file(file_path)
17+
return nil if file_path.start_with?('./')
18+
19+
return FilePathTeamCache.get(file_path) if FilePathTeamCache.cached?(file_path)
20+
21+
result = T.let(RustCodeOwners.for_file(file_path), T.nilable(T::Hash[Symbol, String]))
22+
return if result.nil?
23+
24+
if result[:team_name].nil?
25+
FilePathTeamCache.set(file_path, nil)
26+
else
27+
FilePathTeamCache.set(file_path, T.let(find_team!(T.must(result[:team_name])), T.nilable(CodeTeams::Team)))
28+
end
29+
30+
FilePathTeamCache.get(file_path)
31+
end
32+
33+
sig { params(klass: T.nilable(T.any(T::Class[T.anything], Module))).returns(T.nilable(::CodeTeams::Team)) }
34+
def for_class(klass)
35+
file_path = FilePathFinder.path_from_klass(klass)
36+
return nil if file_path.nil?
37+
38+
for_file(file_path)
39+
end
40+
41+
sig { params(package: Packs::Pack).returns(T.nilable(::CodeTeams::Team)) }
42+
def for_package(package)
43+
owner_name = package.raw_hash['owner'] || package.metadata['owner']
44+
return nil if owner_name.nil?
45+
46+
find_team!(owner_name)
47+
end
48+
49+
sig { params(backtrace: T.nilable(T::Array[String]), excluded_teams: T::Array[::CodeTeams::Team]).returns(T.nilable(::CodeTeams::Team)) }
50+
def for_backtrace(backtrace, excluded_teams: [])
51+
first_owned_file_for_backtrace(backtrace, excluded_teams: excluded_teams)&.first
52+
end
53+
54+
sig { params(backtrace: T.nilable(T::Array[String]), excluded_teams: T::Array[::CodeTeams::Team]).returns(T.nilable([::CodeTeams::Team, String])) }
55+
def first_owned_file_for_backtrace(backtrace, excluded_teams: [])
56+
FilePathFinder.from_backtrace(backtrace).each do |file|
57+
team = for_file(file)
58+
if team && !excluded_teams.include?(team)
59+
return [team, file]
60+
end
61+
end
62+
63+
nil
64+
end
65+
66+
sig { params(team_name: String).returns(CodeTeams::Team) }
67+
def find_team!(team_name)
68+
CodeTeams.find(team_name) ||
69+
raise(StandardError, "Could not find team with name: `#{team_name}`. Make sure the team is one of `#{CodeTeams.all.map(&:name).sort}`")
70+
end
71+
72+
private_class_method(:find_team!)
73+
end
74+
end
75+
end

0 commit comments

Comments
 (0)