Skip to content

Commit 078db28

Browse files
committed
caching file_path teams on team_names_for_files
1 parent 467d35d commit 078db28

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

lib/code_ownership.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ def for_file(file)
4747

4848
sig { params(files: T::Array[String]).returns(T::Hash[String, T.nilable(CodeTeams::Team)]) }
4949
def team_names_for_files(files)
50-
::RustCodeOwners.team_names_for_files(files).transform_values do |team|
51-
team ? CodeTeams.find(team[:team_name]) : nil
52-
end
50+
Private::TeamFinder.team_names_for_files(files)
5351
end
5452

5553
sig { params(file: String).returns(T.nilable(T::Hash[Symbol, String])) }
0 Bytes
Binary file not shown.

lib/code_ownership/private/team_finder.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ def for_file(file_path)
3030
FilePathTeamCache.get(file_path)
3131
end
3232

33+
sig { params(files: T::Array[String]).returns(T::Hash[String, T.nilable(CodeTeams::Team)]) }
34+
def team_names_for_files(files)
35+
::RustCodeOwners.team_names_for_files(files).each_with_object({}) do |path_team, hash|
36+
file_path, team = path_team
37+
found_team = team ? CodeTeams.find(team[:team_name]) : nil
38+
FilePathTeamCache.set(file_path, found_team)
39+
hash[file_path] = found_team
40+
end
41+
end
42+
3343
sig { params(klass: T.nilable(T.any(T::Class[T.anything], Module))).returns(T.nilable(::CodeTeams::Team)) }
3444
def for_class(klass)
3545
file_path = FilePathFinder.path_from_klass(klass)

spec/lib/code_ownership_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@
4242
it 'returns the correct team' do
4343
expect(subject).to eq({ 'packs/my_pack/owned_file.rb' => CodeTeams.find('Bar') })
4444
end
45+
46+
context 'subsequent for_file utilizes cached team' do
47+
let(:files) { ['packs/my_pack/owned_file.rb', 'packs/my_pack/owned_file2.rb'] }
48+
it 'returns the correct team' do
49+
subject # caches paths -> teams
50+
allow(RustCodeOwners).to receive(:for_file)
51+
expect(described_class.for_file('packs/my_pack/owned_file.rb')).to eq(CodeTeams.find('Bar'))
52+
expect(RustCodeOwners).to_not have_received(:for_file)
53+
end
54+
end
4555
end
4656

4757
context 'when ownership is found but team is not found' do

0 commit comments

Comments
 (0)