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
17 changes: 8 additions & 9 deletions lib/code_teams.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,27 @@ module CodeTeams
extend T::Sig

class IncorrectPublicApiUsageError < StandardError; end
class TeamNotFoundError < StandardError; end

UNKNOWN_TEAM_STRING = 'Unknown Team'
@plugins_registered = T.let(false, T::Boolean)

sig { returns(T::Array[Team]) }
def self.all
@all = T.let(@all, T.nilable(T::Array[Team]))
@all ||= for_directory('config/teams')
@all ||= T.let(for_directory('config/teams'), T.nilable(T::Array[Team]))
end

sig { params(name: String).returns(T.nilable(Team)) }
def self.find(name)
@index_by_name = T.let(@index_by_name, T.nilable(T::Hash[String, CodeTeams::Team]))
@index_by_name ||= begin
result = {}
all.each { |t| result[t.name] = t }
result
end

@index_by_name ||= T.let(all.to_h { |t| [t.name, t] }, T.nilable(T::Hash[String, CodeTeams::Team]))
@index_by_name[name]
end

sig { params(name: String).returns(Team) }
def self.find!(name)
find(name) || raise(TeamNotFoundError, "No team found with name: #{name}")
end

sig { params(dir: String).returns(T::Array[Team]) }
def self.for_directory(dir)
unless @plugins_registered
Expand Down
28 changes: 28 additions & 0 deletions spec/lib/code_teams_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,34 @@
end
end

describe '.find' do
it 'returns the team when found' do
team = described_class.find('My Team')
expect(team).to be_a(CodeTeams::Team)
expect(team.name).to eq('My Team')
end

it 'returns nil when the team is not found' do
team = described_class.find('Nonexistent Team')
expect(team).to be_nil
end
end

describe '.find!' do
it 'returns the team when found' do
team = described_class.find!('My Team')
expect(team).to be_a(CodeTeams::Team)
expect(team.name).to eq('My Team')
end

it 'raises TeamNotFoundError when the team is not found' do
expect { described_class.find!('Nonexistent Team') }.to raise_error(
CodeTeams::TeamNotFoundError,
'No team found with name: Nonexistent Team'
)
end
end

describe 'validation_errors' do
subject(:validation_errors) { described_class.validation_errors(described_class.all) }

Expand Down