Skip to content

Commit 6a19b20

Browse files
author
Alex Evanczuk
authored
Fix bug with running bin/codeownership validate when a GitHub team has changed (#57)
1 parent 7b70b69 commit 6a19b20

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
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.11)
4+
code_ownership (1.32.12)
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.11'
3+
spec.version = '1.32.12'
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/private/codeowners_file.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ def self.to_glob_cache
135135
# This will skip over lines that are not of the correct form
136136
next if split_line.count > 2
137137
entry, github_team = split_line
138+
code_team = github_team_to_code_team_map[T.must(github_team)]
139+
# If a GitHub team is changed and a user runs `bin/codeownership validate`, we won't be able to identify
140+
# what team is associated with the removed github team.
141+
# Therefore, if we can't determine the team, we just skip it.
142+
# This affects how complete the cache is, but that will still be caught by `bin/codeownership validate`.
143+
next if code_team.nil?
138144
raw_cache_contents[current_mapper] ||= {}
139145
raw_cache_contents.fetch(current_mapper)[T.must(entry)] = github_team_to_code_team_map.fetch(T.must(github_team))
140146
end

spec/lib/code_ownership/private/validations/github_codeowners_up_to_date_spec.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,57 @@ module CodeOwnership
475475
expect { CodeOwnership.validate!(autocorrect: false) }.to_not raise_error
476476
end
477477
end
478+
479+
context 'in an application with a CODEOWNERS file that has a reference to a github team that no longer exists' do
480+
before do
481+
write_configuration
482+
483+
write_file('packs/my_pack/owned_file.rb', <<~CONTENTS)
484+
# @team Bar
485+
CONTENTS
486+
487+
write_file('config/teams/bar.yml', <<~CONTENTS)
488+
name: Bar
489+
github:
490+
team: '@MyOrg/bar-team'
491+
CONTENTS
492+
end
493+
494+
it 'prints out the diff' do
495+
FileUtils.mkdir('.github')
496+
codeowners_path.write <<~CODEOWNERS
497+
# STOP! - DO NOT EDIT THIS FILE MANUALLY
498+
# This file was automatically generated by "bin/codeownership validate".
499+
#
500+
# CODEOWNERS is used for GitHub to suggest code/file owners to various GitHub
501+
# teams. This is useful when developers create Pull Requests since the
502+
# code/file owner is notified. Reference GitHub docs for more details:
503+
# https://help.github.com/en/articles/about-code-owners
504+
505+
506+
# Annotations at the top of file
507+
/packs/my_pack/owned_file.rb @MyOrg/this-team-does-not-exist
508+
CODEOWNERS
509+
510+
expect_any_instance_of(codeowners_validation).to_not receive(:`) # rubocop:disable RSpec/AnyInstance
511+
expect { CodeOwnership.validate!(autocorrect: false) }.to raise_error do |e|
512+
expect(e).to be_a CodeOwnership::InvalidCodeOwnershipConfigurationError
513+
expect(e.message).to eq <<~EXPECTED.chomp
514+
CODEOWNERS out of date. Run `bin/codeownership validate` to update the CODEOWNERS file
515+
516+
CODEOWNERS should contain the following lines, but does not:
517+
- "/packs/my_pack/owned_file.rb @MyOrg/bar-team"
518+
- "# Team YML ownership"
519+
- "/config/teams/bar.yml @MyOrg/bar-team"
520+
521+
CODEOWNERS should not contain the following lines, but it does:
522+
- "/packs/my_pack/owned_file.rb @MyOrg/this-team-does-not-exist"
523+
524+
See https://github.com/rubyatscale/code_ownership#README.md for more details
525+
EXPECTED
526+
end
527+
end
528+
end
478529
end
479530

480531
context 'code_ownership.yml has skip_codeowners_validation set' do

0 commit comments

Comments
 (0)