Skip to content

Commit

Permalink
Fix bug with running bin/codeownership validate when a GitHub team ha…
Browse files Browse the repository at this point in the history
…s changed (#57)
  • Loading branch information
Alex Evanczuk authored Apr 13, 2023
1 parent 7b70b69 commit 6a19b20
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 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.11)
code_ownership (1.32.12)
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.11'
spec.version = '1.32.12'
spec.authors = ['Gusto Engineers']
spec.email = ['[email protected]']
spec.summary = 'A gem to help engineering teams declare ownership of code'
Expand Down
6 changes: 6 additions & 0 deletions lib/code_ownership/private/codeowners_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ def self.to_glob_cache
# This will skip over lines that are not of the correct form
next if split_line.count > 2
entry, github_team = split_line
code_team = github_team_to_code_team_map[T.must(github_team)]
# If a GitHub team is changed and a user runs `bin/codeownership validate`, we won't be able to identify
# what team is associated with the removed github team.
# Therefore, if we can't determine the team, we just skip it.
# This affects how complete the cache is, but that will still be caught by `bin/codeownership validate`.
next if code_team.nil?
raw_cache_contents[current_mapper] ||= {}
raw_cache_contents.fetch(current_mapper)[T.must(entry)] = github_team_to_code_team_map.fetch(T.must(github_team))
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,57 @@ module CodeOwnership
expect { CodeOwnership.validate!(autocorrect: false) }.to_not raise_error
end
end

context 'in an application with a CODEOWNERS file that has a reference to a github team that no longer exists' do
before do
write_configuration

write_file('packs/my_pack/owned_file.rb', <<~CONTENTS)
# @team Bar
CONTENTS

write_file('config/teams/bar.yml', <<~CONTENTS)
name: Bar
github:
team: '@MyOrg/bar-team'
CONTENTS
end

it 'prints out the diff' do
FileUtils.mkdir('.github')
codeowners_path.write <<~CODEOWNERS
# STOP! - DO NOT EDIT THIS FILE MANUALLY
# This file was automatically generated by "bin/codeownership validate".
#
# CODEOWNERS is used for GitHub to suggest code/file owners to various GitHub
# teams. This is useful when developers create Pull Requests since the
# code/file owner is notified. Reference GitHub docs for more details:
# https://help.github.com/en/articles/about-code-owners
# Annotations at the top of file
/packs/my_pack/owned_file.rb @MyOrg/this-team-does-not-exist
CODEOWNERS

expect_any_instance_of(codeowners_validation).to_not receive(:`) # rubocop:disable RSpec/AnyInstance
expect { CodeOwnership.validate!(autocorrect: false) }.to raise_error do |e|
expect(e).to be_a CodeOwnership::InvalidCodeOwnershipConfigurationError
expect(e.message).to eq <<~EXPECTED.chomp
CODEOWNERS out of date. Run `bin/codeownership validate` to update the CODEOWNERS file
CODEOWNERS should contain the following lines, but does not:
- "/packs/my_pack/owned_file.rb @MyOrg/bar-team"
- "# Team YML ownership"
- "/config/teams/bar.yml @MyOrg/bar-team"
CODEOWNERS should not contain the following lines, but it does:
- "/packs/my_pack/owned_file.rb @MyOrg/this-team-does-not-exist"
See https://github.com/rubyatscale/code_ownership#README.md for more details
EXPECTED
end
end
end
end

context 'code_ownership.yml has skip_codeowners_validation set' do
Expand Down

0 comments on commit 6a19b20

Please sign in to comment.