Skip to content

Commit

Permalink
Load configuration before calling public APIs (#40)
Browse files Browse the repository at this point in the history
* Load configuration before calling public APIs

* fix typo

* fix tests
  • Loading branch information
Alex Evanczuk authored Mar 9, 2023
1 parent 80d85b4 commit 9021506
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
code_ownership (1.32.0)
code_ownership (1.32.1)
code_teams (~> 1.0)
packs
sorbet-runtime
Expand Down
2 changes: 1 addition & 1 deletion code_ownership.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |spec|
spec.name = "code_ownership"
spec.version = '1.32.0'
spec.version = '1.32.1'
spec.authors = ['Gusto Engineers']
spec.email = ['[email protected]']
spec.summary = 'A gem to help engineering teams declare ownership of code'
Expand Down
5 changes: 5 additions & 0 deletions lib/code_ownership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def for_file(file)
return nil if file.start_with?('./')
return @for_file[file] if @for_file.key?(file)

Private.load_configuration!

owner = T.let(nil, T.nilable(CodeTeams::Team))

Mapper.all.each do |mapper|
Expand All @@ -40,6 +42,8 @@ def for_file(file)

sig { params(team: T.any(CodeTeams::Team, String)).returns(String) }
def for_team(team)
Private.load_configuration!

team = T.must(CodeTeams.find(team)) if team.is_a?(String)
ownership_information = T.let([], T::Array[String])

Expand Down Expand Up @@ -87,6 +91,7 @@ def validate!(
autocorrect: true,
stage_changes: true
)
Private.load_configuration!
tracked_file_subset = Private.tracked_files & files
Private.validate!(files: tracked_file_subset, autocorrect: autocorrect, stage_changes: stage_changes)
end
Expand Down
8 changes: 8 additions & 0 deletions lib/code_ownership/private.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ def self.configuration
@configuration ||= Configuration.fetch
end

# This is just an alias for `configuration` that makes it more explicit what we're doing instead of just calling `configuration`.
# This is necessary because configuration may contain extensions of code ownership, so those extensions should be loaded prior to
# calling APIs that provide ownership information.
sig { returns(Configuration) }
def self.load_configuration!
configuration
end

sig { void }
def self.bust_caches!
@configuration = nil
Expand Down
10 changes: 7 additions & 3 deletions spec/lib/code_ownership_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@

describe '.for_backtrace' do
before do
create_files_with_defined_classe
create_files_with_defined_classes
create_minimal_configuration
end

context 'excluded_teams is not passed in as an input parameter' do
Expand All @@ -111,7 +112,7 @@
describe '.first_owned_file_for_backtrace' do
before do
create_minimal_configuration
create_files_with_defined_classe
create_files_with_defined_classes
end


Expand Down Expand Up @@ -142,7 +143,10 @@
end

describe '.for_class' do
before { create_files_with_defined_classe }
before do
create_files_with_defined_classes
create_minimal_configuration
end

it 'can find the right owner for a class' do
expect(CodeOwnership.for_class(MyFile)).to eq CodeTeams.find('Foo')
Expand Down
2 changes: 1 addition & 1 deletion spec/support/application_fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
end


let(:create_files_with_defined_classe) do
let(:create_files_with_defined_classes) do
write_file('app/my_file.rb', <<~CONTENTS)
# @team Foo
Expand Down

0 comments on commit 9021506

Please sign in to comment.