Skip to content

Commit 8e89d92

Browse files
Add special sorting for special characters in globs (#109)
* Add special sorting for special characters in globs * bump version
1 parent e1e01e5 commit 8e89d92

File tree

5 files changed

+54
-3
lines changed

5 files changed

+54
-3
lines changed

code_ownership.gemspec

+1-1
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.38.0'
3+
spec.version = '1.38.1'
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

+20-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,26 @@ def self.expected_contents_lines
7373

7474
next if ownership_entries.none?
7575

76-
codeowners_file_lines += ['', "# #{mapper_description}", *ownership_entries.sort]
76+
# When we have a special character at the beginning of a folder name, then this character
77+
# may be prioritized over *. However, we want the most specific folder to be listed last
78+
# in the CODEOWNERS file, so we should prioritize any character above an asterisk in the
79+
# same position.
80+
if mapper_description == OwnershipMappers::FileAnnotations::DESCRIPTION
81+
# individually owned files definitely won't have globs so we don't need to do special sorting
82+
sorted_ownership_entries = ownership_entries.sort
83+
else
84+
sorted_ownership_entries = ownership_entries.sort do |entry1, entry2|
85+
if entry2.start_with?(entry1.split('**').first)
86+
-1
87+
elsif entry1.start_with?(entry2.split('**').first)
88+
1
89+
else
90+
entry1 <=> entry2
91+
end
92+
end
93+
end
94+
95+
codeowners_file_lines += ['', "# #{mapper_description}", *sorted_ownership_entries]
7796
end
7897

7998
[

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

+22
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ module CodeOwnership
5353
5454
# Owner in .codeowner
5555
/directory/owner/**/** @MyOrg/bar-team
56+
/directory/owner/(my_folder)/**/** @MyOrg/foo-team
5657
5758
# Owner metadata key in package.yml
5859
/packs/my_other_package/**/** @MyOrg/bar-team
@@ -62,6 +63,7 @@ module CodeOwnership
6263
6364
# Team YML ownership
6465
/config/teams/bar.yml @MyOrg/bar-team
66+
/config/teams/foo.yml @MyOrg/foo-team
6567
EXPECTED
6668
end
6769

@@ -90,6 +92,7 @@ module CodeOwnership
9092
9193
# Owner in .codeowner
9294
/directory/owner/**/** @MyOrg/bar-team
95+
/directory/owner/(my_folder)/**/** @MyOrg/foo-team
9396
9497
# Owner metadata key in package.yml
9598
/packs/my_other_package/**/** @MyOrg/bar-team
@@ -99,6 +102,7 @@ module CodeOwnership
99102
100103
# Team YML ownership
101104
/config/teams/bar.yml @MyOrg/bar-team
105+
/config/teams/foo.yml @MyOrg/foo-team
102106
EXPECTED
103107
end
104108
end
@@ -126,6 +130,12 @@ module CodeOwnership
126130
# code/file owner is notified. Reference GitHub docs for more details:
127131
# https://help.github.com/en/articles/about-code-owners
128132
133+
134+
# Owner in .codeowner
135+
/directory/owner/(my_folder)/**/** @MyOrg/foo-team
136+
137+
# Team YML ownership
138+
/config/teams/foo.yml @MyOrg/foo-team
129139
EXPECTED
130140
end
131141
end
@@ -167,6 +177,7 @@ module CodeOwnership
167177
168178
# Owner in .codeowner
169179
# /directory/owner/**/** @MyOrg/bar-team
180+
/directory/owner/(my_folder)/**/** @MyOrg/foo-team
170181
171182
# Owner metadata key in package.yml
172183
# /packs/my_other_package/**/** @MyOrg/bar-team
@@ -176,6 +187,7 @@ module CodeOwnership
176187
177188
# Team YML ownership
178189
# /config/teams/bar.yml @MyOrg/bar-team
190+
/config/teams/foo.yml @MyOrg/foo-team
179191
EXPECTED
180192
end
181193
end
@@ -336,8 +348,10 @@ module CodeOwnership
336348
337349
CODEOWNERS should contain the following lines, but does not:
338350
- "/packs/my_pack/owned_file.rb @MyOrg/bar-team"
351+
- "/config/teams/foo.yml @MyOrg/foo-team"
339352
- "# Owner in .codeowner"
340353
- "/directory/owner/**/** @MyOrg/bar-team"
354+
- "/directory/owner/(my_folder)/**/** @MyOrg/foo-team"
341355
- "# Owner metadata key in package.yml"
342356
- "/packs/my_other_package/**/** @MyOrg/bar-team"
343357
@@ -377,6 +391,7 @@ module CodeOwnership
377391
378392
# Owner in .codeowner
379393
/directory/owner/**/** @MyOrg/bar-team
394+
/directory/owner/(my_folder)/**/** @MyOrg/foo-team
380395
381396
# Owner metadata key in package.yml
382397
/packs/my_other_package/**/** @MyOrg/bar-team
@@ -388,6 +403,7 @@ module CodeOwnership
388403
389404
# Team YML ownership
390405
/config/teams/bar.yml @MyOrg/bar-team
406+
/config/teams/foo.yml @MyOrg/foo-team
391407
CODEOWNERS
392408

393409
expect_any_instance_of(codeowners_validation).to_not receive(:`)
@@ -443,8 +459,10 @@ module CodeOwnership
443459
444460
CODEOWNERS should contain the following lines, but does not:
445461
- "/packs/my_pack/owned_file.rb @MyOrg/bar-team"
462+
- "/config/teams/foo.yml @MyOrg/foo-team"
446463
- "# Owner in .codeowner"
447464
- "/directory/owner/**/** @MyOrg/bar-team"
465+
- "/directory/owner/(my_folder)/**/** @MyOrg/foo-team"
448466
- "# Owner metadata key in package.yml"
449467
- "/packs/my_other_package/**/** @MyOrg/bar-team"
450468
@@ -475,6 +493,7 @@ module CodeOwnership
475493
476494
# Owner in .codeowner
477495
/directory/owner/**/** @MyOrg/bar-team
496+
/directory/owner/(my_folder)/**/** @MyOrg/foo-team
478497
479498
# Owner metadata key in package.yml
480499
/packs/my_other_package/**/** @MyOrg/bar-team
@@ -488,6 +507,7 @@ module CodeOwnership
488507
489508
# Team YML ownership
490509
/config/teams/bar.yml @MyOrg/bar-team
510+
/config/teams/foo.yml @MyOrg/foo-team
491511
CODEOWNERS
492512

493513
expect_any_instance_of(codeowners_validation).to_not receive(:`)
@@ -516,6 +536,7 @@ module CodeOwnership
516536
517537
# Owner in .codeowner
518538
/directory/owner/**/** @MyOrg/bar-team
539+
/directory/owner/(my_folder)/**/** @MyOrg/foo-team
519540
520541
# Owner metadata key in package.yml
521542
/packs/my_other_package/**/** @MyOrg/bar-team
@@ -529,6 +550,7 @@ module CodeOwnership
529550
530551
# Team YML ownership
531552
/config/teams/bar.yml @MyOrg/bar-team
553+
/config/teams/foo.yml @MyOrg/foo-team
532554
CODEOWNERS
533555

534556
expect_any_instance_of(codeowners_validation).to_not receive(:`)

spec/lib/code_ownership_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
RSpec.describe CodeOwnership do
2-
# Look at individual validations spec to see other validaions that ship with CodeOwnership
2+
# Look at individual validations spec to see other validations that ship with CodeOwnership
33
describe '.validate!' do
44
describe 'teams must exist validation' do
55
before do

spec/support/application_fixtures.rb

+10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def write_configuration(owned_globs: nil, **kwargs)
2525
Bar
2626
CONTENTS
2727
write_file('directory/owner/some_directory_file.ts')
28+
write_file('directory/owner/(my_folder)/.codeowner', <<~CONTENTS)
29+
Foo
30+
CONTENTS
31+
write_file('directory/owner/(my_folder)/some_other_file.ts')
2832

2933
write_file('frontend/javascripts/packages/my_other_package/package.json', <<~CONTENTS)
3034
{
@@ -36,6 +40,12 @@ def write_configuration(owned_globs: nil, **kwargs)
3640
CONTENTS
3741
write_file('frontend/javascripts/packages/my_other_package/my_file.jsx')
3842

43+
write_file('config/teams/foo.yml', <<~CONTENTS)
44+
name: Foo
45+
github:
46+
team: '@MyOrg/foo-team'
47+
CONTENTS
48+
3949
write_file('config/teams/bar.yml', <<~CONTENTS)
4050
name: Bar
4151
github:

0 commit comments

Comments
 (0)