Skip to content

Commit 5407951

Browse files
author
Alex Evanczuk
authored
Fix another bug with the file annotations cache when validating code ownership with --diff (#64)
* write failing test * fix failing test * Bump version
1 parent 281dba9 commit 5407951

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
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.16)
4+
code_ownership (1.32.17)
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.16'
3+
spec.version = '1.32.17'
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/ownership_mappers/file_annotations.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def globs_to_owner(files)
4949
def update_cache(cache, files)
5050
# We map files to nil owners so that files whose annotation have been removed will be properly
5151
# overwritten (i.e. removed) from the cache.
52+
fileset = Set.new(files)
5253
updated_cache_for_files = globs_to_owner(files)
5354
cache.merge!(updated_cache_for_files)
5455

@@ -57,7 +58,8 @@ def update_cache(cache, files)
5758
!Private.file_tracked?(file) ||
5859
# If a file no longer has a file annotation (i.e. `globs_to_owner` doesn't map it)
5960
# it should be removed from the cache
60-
!updated_cache_for_files.key?(file)
61+
# We make sure to only apply this to the input files since otherwise `updated_cache_for_files.key?(file)` would always return `false` when files == []
62+
(fileset.include?(file) && !updated_cache_for_files.key?(file))
6163
end
6264

6365
invalid_files.each do |invalid_file|

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,44 @@ module CodeOwnership
629629
end
630630
end
631631
end
632+
633+
context 'validating codeowners using --diff in an application with a CODEOWNERS file' do
634+
before do
635+
write_configuration
636+
637+
write_file('packs/my_pack/had_annotation_file.rb', <<~CONTENTS)
638+
# @team Bar
639+
CONTENTS
640+
641+
write_file('config/teams/bar.yml', <<~CONTENTS)
642+
name: Bar
643+
github:
644+
team: '@MyOrg/bar-team'
645+
CONTENTS
646+
end
647+
648+
it 'prints out the diff' do
649+
FileUtils.mkdir('.github')
650+
codeowners_path.write <<~CODEOWNERS
651+
# STOP! - DO NOT EDIT THIS FILE MANUALLY
652+
# This file was automatically generated by "bin/codeownership validate".
653+
#
654+
# CODEOWNERS is used for GitHub to suggest code/file owners to various GitHub
655+
# teams. This is useful when developers create Pull Requests since the
656+
# code/file owner is notified. Reference GitHub docs for more details:
657+
# https://help.github.com/en/articles/about-code-owners
658+
659+
# Annotations at the top of file
660+
/packs/my_pack/had_annotation_file.rb @MyOrg/bar-team
661+
662+
# Team YML ownership
663+
/config/teams/bar.yml @MyOrg/bar-team
664+
CODEOWNERS
665+
666+
expect_any_instance_of(codeowners_validation).to_not receive(:`) # rubocop:disable RSpec/AnyInstance
667+
expect { CodeOwnership.validate!(autocorrect: false, files: []) }.to_not raise_error
668+
end
669+
end
632670
end
633671

634672
context 'code_ownership.yml has skip_codeowners_validation set' do

0 commit comments

Comments
 (0)