Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/code_ownership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ module CodeOwnership

GlobsToOwningTeamMap = T.type_alias { T::Hash[String, CodeTeams::Team] }

# The `Team` struct that the Rust extension serializes out of `RustCodeOwners.for_file`.
FileOwnershipDetails = T.type_alias do
{ team_name: String, team_config_yml: String, reasons: T::Array[String] }
end

# Returns the version of the code_ownership gem and the codeowners-rs gem.
sig { returns(T::Array[String]) }
def self.version
Expand Down Expand Up @@ -154,7 +159,7 @@ def self.teams_for_files_from_codeowners(files, allow_raise: false)
# @see #for_file for a simpler ownership lookup that returns just the team
# @see CLI#for_file for the command-line interface that uses this method
#
sig { params(file: String).returns(T.nilable(T::Hash[Symbol, String])) }
sig { params(file: String).returns(T.nilable(FileOwnershipDetails)) }
def self.for_file_verbose(file)
::RustCodeOwners.for_file(file)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/code_ownership/private/team_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ def self.for_file(file_path, allow_raise: false)

return FilePathTeamCache.get(file_path) if FilePathTeamCache.cached?(file_path)

result = T.let(RustCodeOwners.for_file(file_path), T.nilable(T::Hash[Symbol, String]))
result = T.let(RustCodeOwners.for_file(file_path), T.nilable(FileOwnershipDetails))

if result.nil? || result[:team_name].nil?
if result.nil?
FilePathTeamCache.set(file_path, nil)
else
FilePathTeamCache.set(file_path, T.let(find_team!(T.must(result[:team_name]), allow_raise: allow_raise), T.nilable(CodeTeams::Team)))
FilePathTeamCache.set(file_path, T.let(find_team!(result[:team_name], allow_raise: allow_raise), T.nilable(CodeTeams::Team)))
end

FilePathTeamCache.get(file_path)
Expand Down
2 changes: 1 addition & 1 deletion lib/code_ownership/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
# frozen_string_literal: true

module CodeOwnership
VERSION = '2.1.3'
VERSION = '2.1.4'
end
16 changes: 6 additions & 10 deletions spec/lib/code_ownership/private/team_finder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
describe '.for_file' do
let(:file_path) { 'packs/my_pack/owned_file.rb' }

let(:rust_result) do
{ team_name: 'Bar', team_config_yml: 'config/teams/bar.yml', reasons: [] }
end

before do
create_non_empty_application
end

it 'caches positive results' do
allow(RustCodeOwners).to receive(:for_file).with(file_path)
.and_return({ team_name: 'Bar' }, nil)
.and_return(rust_result, nil)

first = described_class.for_file(file_path)
second = described_class.for_file(file_path)
Expand All @@ -22,7 +26,7 @@

it 'caches nil when rust returns nil' do
allow(RustCodeOwners).to receive(:for_file).with(file_path)
.and_return(nil, { team_name: 'Bar' })
.and_return(nil, rust_result)

first = described_class.for_file(file_path)
second = described_class.for_file(file_path)
Expand All @@ -31,14 +35,6 @@
expect(second).to be_nil
expect(CodeOwnership::Private::FilePathTeamCache.cached?(file_path)).to be true
end

it 'caches nil when team_name is nil' do
allow(RustCodeOwners).to receive(:for_file).with(file_path).and_return({ team_name: nil })

expect(described_class.for_file(file_path)).to be_nil
expect(CodeOwnership::Private::FilePathTeamCache.cached?(file_path)).to be true
expect(CodeOwnership::Private::FilePathTeamCache.get(file_path)).to be_nil
end
end

describe '.for_backtrace' do
Expand Down
3 changes: 2 additions & 1 deletion spec/lib/code_ownership_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@
end

it 'raises an error when using single-file path' do
allow(RustCodeOwners).to receive(:for_file).and_return({ team_name: 'Made Up Team' })
allow(RustCodeOwners).to receive(:for_file)
.and_return({ team_name: 'Made Up Team', team_config_yml: 'config/teams/made_up_team.yml', reasons: [] })
expect { CodeOwnership.for_file(file_path, from_codeowners: false, allow_raise: true) }.to raise_error(StandardError, /Could not find team with name:/)
end
end
Expand Down
Loading