Skip to content

Commit 7918b97

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

File tree

7 files changed

+29
-11
lines changed

7 files changed

+29
-11
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 = [

ext/code_ownership/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn for_team(team_name: String) -> Result<Value, Error> {
1818
validate_result(&team)
1919
}
2020

21-
fn team_names_for_files(file_paths: Vec<String>) -> Result<Value, Error> {
21+
fn teams_for_files(file_paths: Vec<String>) -> Result<Value, Error> {
2222
let run_config = build_run_config();
2323
let path_teams = runner::teams_for_files_from_codeowners(&run_config, &file_paths);
2424
match path_teams {
@@ -126,7 +126,7 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
126126
module.define_singleton_method("validate", function!(validate, 0))?;
127127
module.define_singleton_method("for_team", function!(for_team, 1))?;
128128
module.define_singleton_method("version", function!(version, 0))?;
129-
module.define_singleton_method("team_names_for_files", function!(team_names_for_files, 1))?;
129+
module.define_singleton_method("teams_for_files", function!(teams_for_files, 1))?;
130130

131131
Ok(())
132132
}

lib/code_ownership.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,8 @@ def for_file(file)
4646
end
4747

4848
sig { params(files: T::Array[String]).returns(T::Hash[String, T.nilable(CodeTeams::Team)]) }
49-
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
49+
def teams_for_files(files)
50+
Private::TeamFinder.teams_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 teams_for_files(files)
35+
::RustCodeOwners.teams_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: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
expect(CodeOwnership::VERSION).not_to be nil
66
end
77

8-
describe '.team_names_for_files' do
9-
subject { CodeOwnership.team_names_for_files(files) }
8+
describe '.teams_for_files' do
9+
subject { CodeOwnership.teams_for_files(files) }
1010
let(:files) { ['app/services/my_file.rb'] }
1111

1212
context 'when config is not found' do
@@ -42,12 +42,22 @@
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
4858
let(:file_path) { ['packs/my_pack/owned_file.rb'] }
4959
before do
50-
allow(RustCodeOwners).to receive(:team_names_for_files).and_return({ file_path.first => {team_name: 'Made Up Team'} })
60+
allow(RustCodeOwners).to receive(:teams_for_files).and_return({ file_path.first => {team_name: 'Made Up Team'} })
5161
end
5262

5363
it 'returns nil' do

0 commit comments

Comments
 (0)