Skip to content

Commit 5b5fb1c

Browse files
author
Alex Evanczuk
authored
Fix bug with codeowners file validation when existing file has new line (#50)
* Fix a bug that results in false positive failure for codeowners validation * Bump version
1 parent 8599e31 commit 5b5fb1c

File tree

5 files changed

+56
-13
lines changed

5 files changed

+56
-13
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.4)
4+
code_ownership (1.32.5)
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.4'
3+
spec.version = '1.32.5'
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: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@ class CodeownersFile
1313

1414
sig { returns(T::Array[String]) }
1515
def self.actual_contents_lines
16-
(path.exist? ? path.read : "").split("\n")
16+
if !path.exist?
17+
[""]
18+
else
19+
content = path.read
20+
lines = path.read.split("\n")
21+
if content.end_with?("\n")
22+
lines << ""
23+
end
24+
lines
25+
end
1726
end
1827

1928
sig { returns(T::Array[T.nilable(String)]) }
@@ -64,9 +73,9 @@ def self.expected_contents_lines
6473

6574
[
6675
*header.split("\n"),
67-
nil, # For line between header and codeowners_file_lines
76+
"", # For line between header and codeowners_file_lines
6877
*codeowners_file_lines,
69-
nil, # For end-of-file newline
78+
"", # For end-of-file newline
7079
]
7180
end
7281

lib/code_ownership/private/validations/github_codeowners_up_to_date.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ 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-
codeowners_up_to_date = actual_content_lines == expected_content_lines
17+
missing_lines = expected_content_lines - actual_content_lines
18+
extra_lines = actual_content_lines - expected_content_lines
1819

20+
codeowners_up_to_date = !missing_lines.any? && !extra_lines.any?
1921
errors = T.let([], T::Array[String])
2022

2123
if !codeowners_up_to_date
@@ -26,8 +28,6 @@ def validation_errors(files:, autocorrect: true, stage_changes: true)
2628
end
2729
else
2830
# If there is no current file or its empty, display a shorter message.
29-
missing_lines = expected_content_lines - actual_content_lines
30-
extra_lines = actual_content_lines - expected_content_lines
3131

3232
missing_lines_text = if missing_lines.any?
3333
<<~COMMENT
@@ -53,7 +53,7 @@ def validation_errors(files:, autocorrect: true, stage_changes: true)
5353
""
5454
end
5555

56-
if actual_content_lines == []
56+
if actual_content_lines == [""]
5757
errors << <<~CODEOWNERS_ERROR
5858
CODEOWNERS out of date. Run `bin/codeownership validate` to update the CODEOWNERS file
5959
CODEOWNERS_ERROR

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

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,9 @@ module CodeOwnership
314314
CODEOWNERS out of date. Run `bin/codeownership validate` to update the CODEOWNERS file
315315
316316
CODEOWNERS should contain the following lines, but does not:
317-
- ""
318317
- "/packs/my_pack/owned_file.rb @MyOrg/bar-team"
319318
- "# Owner metadata key in package.yml"
320319
- "/packs/my_other_package/**/** @MyOrg/bar-team"
321-
- ""
322320
323321
CODEOWNERS should not contain the following lines, but it does:
324322
- "/frontend/some/extra/line/that/should/not/exist @MyOrg/bar-team"
@@ -420,17 +418,53 @@ module CodeOwnership
420418
CODEOWNERS out of date. Run `bin/codeownership validate` to update the CODEOWNERS file
421419
422420
CODEOWNERS should contain the following lines, but does not:
423-
- ""
424421
- "/packs/my_pack/owned_file.rb @MyOrg/bar-team"
425422
- "# Owner metadata key in package.yml"
426423
- "/packs/my_other_package/**/** @MyOrg/bar-team"
427-
- ""
428424
429425
See https://github.com/rubyatscale/code_ownership#README.md for more details
430426
EXPECTED
431427
end
432428
end
433429
end
430+
431+
context 'in an application with a CODEOWNERS file with no issue' do
432+
before { create_non_empty_application }
433+
434+
it 'prints out the diff' do
435+
FileUtils.mkdir('.github')
436+
codeowners_path.write <<~CODEOWNERS
437+
# STOP! - DO NOT EDIT THIS FILE MANUALLY
438+
# This file was automatically generated by "bin/codeownership validate".
439+
#
440+
# CODEOWNERS is used for GitHub to suggest code/file owners to various GitHub
441+
# teams. This is useful when developers create Pull Requests since the
442+
# code/file owner is notified. Reference GitHub docs for more details:
443+
# https://help.github.com/en/articles/about-code-owners
444+
445+
446+
# Annotations at the top of file
447+
/frontend/javascripts/packages/my_package/owned_file.jsx @MyOrg/bar-team
448+
/packs/my_pack/owned_file.rb @MyOrg/bar-team
449+
450+
# Owner metadata key in package.yml
451+
/packs/my_other_package/**/** @MyOrg/bar-team
452+
453+
# Team-specific owned globs
454+
/app/services/bar_stuff/** @MyOrg/bar-team
455+
/frontend/javascripts/bar_stuff/** @MyOrg/bar-team
456+
457+
# Owner metadata key in package.json
458+
/frontend/javascripts/packages/my_other_package/**/** @MyOrg/bar-team
459+
460+
# Team YML ownership
461+
/config/teams/bar.yml @MyOrg/bar-team
462+
CODEOWNERS
463+
464+
expect_any_instance_of(codeowners_validation).to_not receive(:`) # rubocop:disable RSpec/AnyInstance
465+
expect { CodeOwnership.validate!(autocorrect: false) }.to_not raise_error
466+
end
467+
end
434468
end
435469

436470
context 'code_ownership.yml has skip_codeowners_validation set' do

0 commit comments

Comments
 (0)