Skip to content

Commit 8bd467a

Browse files
author
Alex Evanczuk
authored
Explicitly sort entries in .github/CODEOWNERS file (#69)
* Explicitly sort entries in CODEOWNERS file * bump version
1 parent f8b7e9a commit 8bd467a

File tree

5 files changed

+75
-18
lines changed

5 files changed

+75
-18
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.18)
4+
code_ownership (1.32.19)
55
code_teams (~> 1.0)
66
packs
77
sorbet-runtime (>= 0.5.10821)

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.18'
3+
spec.version = '1.32.19'
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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ def self.expected_contents_lines
5353

5454
cache.each do |mapper_description, ownership_map_cache|
5555
ownership_entries = []
56-
ownership_map_cache.each do |path, code_team|
56+
sorted_ownership_map_cache = ownership_map_cache.sort_by do |glob, team|
57+
glob
58+
end
59+
sorted_ownership_map_cache.to_h.each do |path, code_team|
5760
team_mapping = github_team_map[code_team.name]
5861
next if team_mapping.nil?
5962

lib/code_ownership/private/validations/github_codeowners_up_to_date.rb

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ def validation_errors(files:, autocorrect: true, stage_changes: true)
1414

1515
actual_content_lines = CodeownersFile.actual_contents_lines
1616
expected_content_lines = CodeownersFile.expected_contents_lines
17-
missing_lines = expected_content_lines - actual_content_lines
18-
extra_lines = actual_content_lines - expected_content_lines
1917

20-
codeowners_up_to_date = !missing_lines.any? && !extra_lines.any?
18+
codeowners_up_to_date = actual_content_lines == expected_content_lines
2119
errors = T.let([], T::Array[String])
2220

2321
if !codeowners_up_to_date
@@ -26,8 +24,14 @@ def validation_errors(files:, autocorrect: true, stage_changes: true)
2624
if stage_changes
2725
`git add #{CodeownersFile.path}`
2826
end
27+
# If there is no current file or its empty, display a shorter message.
28+
elsif actual_content_lines == [""]
29+
errors << <<~CODEOWNERS_ERROR
30+
CODEOWNERS out of date. Run `bin/codeownership validate` to update the CODEOWNERS file
31+
CODEOWNERS_ERROR
2932
else
30-
# If there is no current file or its empty, display a shorter message.
33+
missing_lines = expected_content_lines - actual_content_lines
34+
extra_lines = actual_content_lines - expected_content_lines
3135

3236
missing_lines_text = if missing_lines.any?
3337
<<~COMMENT
@@ -50,20 +54,19 @@ def validation_errors(files:, autocorrect: true, stage_changes: true)
5054
elsif extra_lines_text
5155
extra_lines_text
5256
else
53-
""
57+
<<~TEXT
58+
There may be extra lines, or lines are out of order.
59+
You can try to regenerate the CODEOWNERS file from scratch:
60+
1) `rm .github/CODEOWNERS`
61+
2) `bin/codeownership validate`
62+
TEXT
5463
end
5564

56-
if actual_content_lines == [""]
57-
errors << <<~CODEOWNERS_ERROR
58-
CODEOWNERS out of date. Run `bin/codeownership validate` to update the CODEOWNERS file
59-
CODEOWNERS_ERROR
60-
else
61-
errors << <<~CODEOWNERS_ERROR
62-
CODEOWNERS out of date. Run `bin/codeownership validate` to update the CODEOWNERS file
65+
errors << <<~CODEOWNERS_ERROR
66+
CODEOWNERS out of date. Run `bin/codeownership validate` to update the CODEOWNERS file
6367
64-
#{diff_text.chomp}
65-
CODEOWNERS_ERROR
66-
end
68+
#{diff_text.chomp}
69+
CODEOWNERS_ERROR
6770
end
6871
end
6972

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
@@ -476,6 +476,56 @@ module CodeOwnership
476476
end
477477
end
478478

479+
context 'in an application with an unsorted CODEOWNERS file' do
480+
before { create_non_empty_application }
481+
482+
it 'prints out the diff' do
483+
FileUtils.mkdir('.github')
484+
codeowners_path.write <<~CODEOWNERS
485+
# STOP! - DO NOT EDIT THIS FILE MANUALLY
486+
# This file was automatically generated by "bin/codeownership validate".
487+
#
488+
# CODEOWNERS is used for GitHub to suggest code/file owners to various GitHub
489+
# teams. This is useful when developers create Pull Requests since the
490+
# code/file owner is notified. Reference GitHub docs for more details:
491+
# https://help.github.com/en/articles/about-code-owners
492+
493+
494+
# Annotations at the top of file
495+
/packs/my_pack/owned_file.rb @MyOrg/bar-team
496+
/frontend/javascripts/packages/my_package/owned_file.jsx @MyOrg/bar-team
497+
498+
# Owner metadata key in package.yml
499+
/packs/my_other_package/**/** @MyOrg/bar-team
500+
501+
# Team-specific owned globs
502+
/app/services/bar_stuff/** @MyOrg/bar-team
503+
/frontend/javascripts/bar_stuff/** @MyOrg/bar-team
504+
505+
# Owner metadata key in package.json
506+
/frontend/javascripts/packages/my_other_package/**/** @MyOrg/bar-team
507+
508+
# Team YML ownership
509+
/config/teams/bar.yml @MyOrg/bar-team
510+
CODEOWNERS
511+
512+
expect_any_instance_of(codeowners_validation).to_not receive(:`) # rubocop:disable RSpec/AnyInstance
513+
expect { CodeOwnership.validate!(autocorrect: false) }.to raise_error do |e|
514+
expect(e).to be_a CodeOwnership::InvalidCodeOwnershipConfigurationError
515+
expect(e.message).to eq <<~EXPECTED.chomp
516+
CODEOWNERS out of date. Run `bin/codeownership validate` to update the CODEOWNERS file
517+
518+
There may be extra lines, or lines are out of order.
519+
You can try to regenerate the CODEOWNERS file from scratch:
520+
1) `rm .github/CODEOWNERS`
521+
2) `bin/codeownership validate`
522+
523+
See https://github.com/rubyatscale/code_ownership#README.md for more details
524+
EXPECTED
525+
end
526+
end
527+
end
528+
479529
context 'in an application with a CODEOWNERS file that has a reference to a github team that no longer exists' do
480530
before do
481531
write_configuration
@@ -656,6 +706,7 @@ module CodeOwnership
656706
# code/file owner is notified. Reference GitHub docs for more details:
657707
# https://help.github.com/en/articles/about-code-owners
658708
709+
659710
# Annotations at the top of file
660711
/packs/my_pack/had_annotation_file.rb @MyOrg/bar-team
661712

0 commit comments

Comments
 (0)