Skip to content

Commit 9021506

Browse files
author
Alex Evanczuk
authored
Load configuration before calling public APIs (#40)
* Load configuration before calling public APIs * fix typo * fix tests
1 parent 80d85b4 commit 9021506

File tree

6 files changed

+23
-6
lines changed

6 files changed

+23
-6
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
code_ownership (1.32.0)
4+
code_ownership (1.32.1)
55
code_teams (~> 1.0)
66
packs
77
sorbet-runtime

code_ownership.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |spec|
22
spec.name = "code_ownership"
3-
spec.version = '1.32.0'
3+
spec.version = '1.32.1'
44
spec.authors = ['Gusto Engineers']
55
spec.email = ['[email protected]']
66
spec.summary = 'A gem to help engineering teams declare ownership of code'

lib/code_ownership.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def for_file(file)
2828
return nil if file.start_with?('./')
2929
return @for_file[file] if @for_file.key?(file)
3030

31+
Private.load_configuration!
32+
3133
owner = T.let(nil, T.nilable(CodeTeams::Team))
3234

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

4143
sig { params(team: T.any(CodeTeams::Team, String)).returns(String) }
4244
def for_team(team)
45+
Private.load_configuration!
46+
4347
team = T.must(CodeTeams.find(team)) if team.is_a?(String)
4448
ownership_information = T.let([], T::Array[String])
4549

@@ -87,6 +91,7 @@ def validate!(
8791
autocorrect: true,
8892
stage_changes: true
8993
)
94+
Private.load_configuration!
9095
tracked_file_subset = Private.tracked_files & files
9196
Private.validate!(files: tracked_file_subset, autocorrect: autocorrect, stage_changes: stage_changes)
9297
end

lib/code_ownership/private.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ def self.configuration
2525
@configuration ||= Configuration.fetch
2626
end
2727

28+
# This is just an alias for `configuration` that makes it more explicit what we're doing instead of just calling `configuration`.
29+
# This is necessary because configuration may contain extensions of code ownership, so those extensions should be loaded prior to
30+
# calling APIs that provide ownership information.
31+
sig { returns(Configuration) }
32+
def self.load_configuration!
33+
configuration
34+
end
35+
2836
sig { void }
2937
def self.bust_caches!
3038
@configuration = nil

spec/lib/code_ownership_spec.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@
8787

8888
describe '.for_backtrace' do
8989
before do
90-
create_files_with_defined_classe
90+
create_files_with_defined_classes
91+
create_minimal_configuration
9192
end
9293

9394
context 'excluded_teams is not passed in as an input parameter' do
@@ -111,7 +112,7 @@
111112
describe '.first_owned_file_for_backtrace' do
112113
before do
113114
create_minimal_configuration
114-
create_files_with_defined_classe
115+
create_files_with_defined_classes
115116
end
116117

117118

@@ -142,7 +143,10 @@
142143
end
143144

144145
describe '.for_class' do
145-
before { create_files_with_defined_classe }
146+
before do
147+
create_files_with_defined_classes
148+
create_minimal_configuration
149+
end
146150

147151
it 'can find the right owner for a class' do
148152
expect(CodeOwnership.for_class(MyFile)).to eq CodeTeams.find('Foo')

spec/support/application_fixtures.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
end
6464

6565

66-
let(:create_files_with_defined_classe) do
66+
let(:create_files_with_defined_classes) do
6767
write_file('app/my_file.rb', <<~CONTENTS)
6868
# @team Foo
6969

0 commit comments

Comments
 (0)