diff --git a/lib/code_ownership/private/team_finder.rb b/lib/code_ownership/private/team_finder.rb index 2dd4931..fc3cb97 100644 --- a/lib/code_ownership/private/team_finder.rb +++ b/lib/code_ownership/private/team_finder.rb @@ -32,12 +32,29 @@ def for_file(file_path, allow_raise: false) sig { params(files: T::Array[String], allow_raise: T::Boolean).returns(T::Hash[String, T.nilable(CodeTeams::Team)]) } def teams_for_files(files, allow_raise: false) - ::RustCodeOwners.teams_for_files(files).each_with_object({}) do |path_team, hash| + result = {} + + # Collect cached results and identify non-cached files + not_cached_files = [] + files.each do |file_path| + if FilePathTeamCache.cached?(file_path) + result[file_path] = FilePathTeamCache.get(file_path) + else + not_cached_files << file_path + end + end + + return result if not_cached_files.empty? + + # Process non-cached files + ::RustCodeOwners.teams_for_files(not_cached_files).each do |path_team| file_path, team = path_team found_team = team ? find_team!(team[:team_name], allow_raise: allow_raise) : nil FilePathTeamCache.set(file_path, found_team) - hash[file_path] = found_team + result[file_path] = found_team end + + result end sig { params(klass: T.nilable(T.any(T::Class[T.anything], Module))).returns(T.nilable(::CodeTeams::Team)) } diff --git a/lib/code_ownership/version.rb b/lib/code_ownership/version.rb index 635a2c7..d50e712 100644 --- a/lib/code_ownership/version.rb +++ b/lib/code_ownership/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module CodeOwnership - VERSION = '2.0.0-3' + VERSION = '2.0.0-4' end diff --git a/spec/lib/code_ownership_spec.rb b/spec/lib/code_ownership_spec.rb index aed33b6..66f7f3a 100644 --- a/spec/lib/code_ownership_spec.rb +++ b/spec/lib/code_ownership_spec.rb @@ -184,6 +184,15 @@ it 'returns the correct team' do expect(subject).to eq CodeTeams.find('Bar') end + + context 'when ownership is cached' do + it 'returns the correct team' do + expect(subject).to eq CodeTeams.find('Bar') + allow(RustCodeOwners).to receive(:teams_for_files) + expect(CodeOwnership.for_file(file_path)).to eq CodeTeams.find('Bar') + expect(RustCodeOwners).not_to have_received(:teams_for_files) + end + end end context 'when ownership is found but team is not found' do