Skip to content

Commit f7696fb

Browse files
author
Alex Evanczuk
authored
Fix bug where team YML files are considered unowned (#32)
* Add failing test * Fix failing test and update existing tests with new expectations * Fix bug where team YML files are considered unowned
1 parent 8913407 commit f7696fb

File tree

5 files changed

+89
-2
lines changed

5 files changed

+89
-2
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.30.0)
4+
code_ownership (1.31.0)
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.30.0'
3+
spec.version = '1.31.0'
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.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
require 'code_ownership/private/ownership_mappers/team_globs'
1717
require 'code_ownership/private/ownership_mappers/package_ownership'
1818
require 'code_ownership/private/ownership_mappers/js_package_ownership'
19+
require 'code_ownership/private/ownership_mappers/team_yml_ownership'
1920

2021
module CodeOwnership
2122
module Private
@@ -64,6 +65,7 @@ def self.mappers
6465
Private::OwnershipMappers::TeamGlobs.new,
6566
Private::OwnershipMappers::PackageOwnership.new,
6667
Private::OwnershipMappers::JsPackageOwnership.new,
68+
Private::OwnershipMappers::TeamYmlOwnership.new,
6769
]
6870
end
6971

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# frozen_string_literal: true
2+
3+
# typed: true
4+
5+
module CodeOwnership
6+
module Private
7+
module OwnershipMappers
8+
class TeamYmlOwnership
9+
extend T::Sig
10+
include Interface
11+
12+
@@map_files_to_owners = T.let(@map_files_to_owners, T.nilable(T::Hash[String, T.nilable(::CodeTeams::Team)])) # rubocop:disable Style/ClassVars
13+
@@map_files_to_owners = {} # rubocop:disable Style/ClassVars
14+
@@codeowners_lines_to_owners = T.let(@codeowners_lines_to_owners, T.nilable(T::Hash[String, T.nilable(::CodeTeams::Team)])) # rubocop:disable Style/ClassVars
15+
@@codeowners_lines_to_owners = {} # rubocop:disable Style/ClassVars
16+
17+
sig do
18+
override.
19+
params(files: T::Array[String]).
20+
returns(T::Hash[String, T.nilable(::CodeTeams::Team)])
21+
end
22+
def map_files_to_owners(files) # rubocop:disable Lint/UnusedMethodArgument
23+
return @@map_files_to_owners if @@map_files_to_owners&.keys && @@map_files_to_owners.keys.count > 0
24+
25+
@@map_files_to_owners = CodeTeams.all.each_with_object({}) do |team, map| # rubocop:disable Style/ClassVars
26+
map[team.config_yml] = team
27+
end
28+
end
29+
30+
sig do
31+
override.params(file: String).
32+
returns(T.nilable(::CodeTeams::Team))
33+
end
34+
def map_file_to_owner(file)
35+
map_files_to_owners([file])[file]
36+
end
37+
38+
sig do
39+
override.returns(T::Hash[String, T.nilable(::CodeTeams::Team)])
40+
end
41+
def codeowners_lines_to_owners
42+
return @@codeowners_lines_to_owners if @@codeowners_lines_to_owners&.keys && @@codeowners_lines_to_owners.keys.count > 0
43+
44+
@@codeowners_lines_to_owners = CodeTeams.all.each_with_object({}) do |team, map| # rubocop:disable Style/ClassVars
45+
map[team.config_yml] = team
46+
end
47+
end
48+
49+
sig { override.void }
50+
def bust_caches!
51+
@@codeowners_lines_to_owners = {} # rubocop:disable Style/ClassVars
52+
@@map_files_to_owners = {} # rubocop:disable Style/ClassVars
53+
end
54+
55+
sig { override.returns(String) }
56+
def description
57+
'Team YML ownership'
58+
end
59+
end
60+
end
61+
end
62+
end

spec/lib/code_ownership_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@
189189
190190
# Owner metadata key in package.json
191191
/frontend/javascripts/packages/my_other_package/**/** @MyOrg/bar-team
192+
193+
# Team YML ownership
194+
/config/teams/bar.yml @MyOrg/bar-team
192195
EXPECTED
193196
end
194197

@@ -220,6 +223,9 @@
220223
221224
# Owner metadata key in package.json
222225
/frontend/javascripts/packages/my_other_package/**/** @MyOrg/bar-team
226+
227+
# Team YML ownership
228+
/config/teams/bar.yml @MyOrg/bar-team
223229
EXPECTED
224230
end
225231
end
@@ -428,6 +434,9 @@
428434
429435
# Owner metadata key in package.json
430436
/frontend/javascripts/packages/my_other_package/**/** @MyOrg/bar-team
437+
438+
# Team YML ownership
439+
/config/teams/bar.yml @MyOrg/bar-team
431440
CODEOWNERS
432441

433442
expect_any_instance_of(codeowners_validation).to_not receive(:`) # rubocop:disable RSpec/AnyInstance
@@ -486,6 +495,8 @@
486495
# Owner metadata key in package.json
487496
/frontend/javascripts/packages/my_other_package/**/** @MyOrg/bar-team
488497
498+
# Team YML ownership
499+
/config/teams/bar.yml @MyOrg/bar-team
489500
CODEOWNERS
490501

491502
expect_any_instance_of(codeowners_validation).to_not receive(:`) # rubocop:disable RSpec/AnyInstance
@@ -530,6 +541,8 @@
530541
# Owner metadata key in package.json
531542
/frontend/javascripts/packages/my_other_package/**/** @MyOrg/bar-team
532543
544+
# Team YML ownership
545+
/config/teams/bar.yml @MyOrg/bar-team
533546
CODEOWNERS
534547

535548
expect_any_instance_of(codeowners_validation).to_not receive(:`) # rubocop:disable RSpec/AnyInstance
@@ -736,6 +749,10 @@
736749
expect(CodeOwnership.for_file('frontend/javascripts/packages/my_other_package/my_file.jsx')).to eq CodeTeams.find('Bar')
737750
end
738751

752+
it 'maps a team YML to be owned by the team itself' do
753+
expect(CodeOwnership.for_file('config/teams/bar.yml')).to eq CodeTeams.find('Bar')
754+
end
755+
739756
describe 'path formatting expectations' do
740757
# All file paths must be clean paths relative to the root: https://apidock.com/ruby/Pathname/cleanpath
741758
it 'will not find the ownership of a file that is not a cleanpath' do
@@ -996,6 +1013,9 @@
9961013
9971014
## Owner metadata key in package.json
9981015
- frontend/javascripts/packages/my_other_package/**/**
1016+
1017+
## Team YML ownership
1018+
- config/teams/bar.yml
9991019
OWNERSHIP
10001020
end
10011021

@@ -1024,6 +1044,9 @@
10241044
10251045
## Owner metadata key in package.json
10261046
This team owns nothing in this category.
1047+
1048+
## Team YML ownership
1049+
- config/teams/foo.yml
10271050
OWNERSHIP
10281051
end
10291052
end

0 commit comments

Comments
 (0)