Skip to content

Commit edde479

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

File tree

6 files changed

+23
-5
lines changed

6 files changed

+23
-5
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/code_ownership/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ rb-sys = { version = "0.9.111", features = [
1717
magnus = { version = "0.7.1" }
1818
serde = { version = "1.0.219", features = ["derive"] }
1919
serde_magnus = "0.9.0"
20-
# codeowners = { git = "https://github.com/rubyatscale/codeowners-rs.git", tag = "v0.2.14" }
21-
codeowners = { path = "../../../codeowners-rs" }
20+
codeowners = { git = "https://github.com/rubyatscale/codeowners-rs.git", tag = "v0.2.15" }
2221

2322
[dev-dependencies]
2423
rb-sys = { version = "0.9.117", features = [

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])) }
-77.4 KB
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)