Skip to content

Commit

Permalink
Fix bug where team YML files are considered unowned (#32)
Browse files Browse the repository at this point in the history
* Add failing test

* Fix failing test and update existing tests with new expectations

* Fix bug where team YML files are considered unowned
  • Loading branch information
Alex Evanczuk authored Feb 22, 2023
1 parent 8913407 commit f7696fb
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
code_ownership (1.30.0)
code_ownership (1.31.0)
code_teams (~> 1.0)
packs
sorbet-runtime
Expand Down
2 changes: 1 addition & 1 deletion code_ownership.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |spec|
spec.name = "code_ownership"
spec.version = '1.30.0'
spec.version = '1.31.0'
spec.authors = ['Gusto Engineers']
spec.email = ['[email protected]']
spec.summary = 'A gem to help engineering teams declare ownership of code'
Expand Down
2 changes: 2 additions & 0 deletions lib/code_ownership/private.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
require 'code_ownership/private/ownership_mappers/team_globs'
require 'code_ownership/private/ownership_mappers/package_ownership'
require 'code_ownership/private/ownership_mappers/js_package_ownership'
require 'code_ownership/private/ownership_mappers/team_yml_ownership'

module CodeOwnership
module Private
Expand Down Expand Up @@ -64,6 +65,7 @@ def self.mappers
Private::OwnershipMappers::TeamGlobs.new,
Private::OwnershipMappers::PackageOwnership.new,
Private::OwnershipMappers::JsPackageOwnership.new,
Private::OwnershipMappers::TeamYmlOwnership.new,
]
end

Expand Down
62 changes: 62 additions & 0 deletions lib/code_ownership/private/ownership_mappers/team_yml_ownership.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# frozen_string_literal: true

# typed: true

module CodeOwnership
module Private
module OwnershipMappers
class TeamYmlOwnership
extend T::Sig
include Interface

@@map_files_to_owners = T.let(@map_files_to_owners, T.nilable(T::Hash[String, T.nilable(::CodeTeams::Team)])) # rubocop:disable Style/ClassVars
@@map_files_to_owners = {} # rubocop:disable Style/ClassVars
@@codeowners_lines_to_owners = T.let(@codeowners_lines_to_owners, T.nilable(T::Hash[String, T.nilable(::CodeTeams::Team)])) # rubocop:disable Style/ClassVars
@@codeowners_lines_to_owners = {} # rubocop:disable Style/ClassVars

sig do
override.
params(files: T::Array[String]).
returns(T::Hash[String, T.nilable(::CodeTeams::Team)])
end
def map_files_to_owners(files) # rubocop:disable Lint/UnusedMethodArgument
return @@map_files_to_owners if @@map_files_to_owners&.keys && @@map_files_to_owners.keys.count > 0

@@map_files_to_owners = CodeTeams.all.each_with_object({}) do |team, map| # rubocop:disable Style/ClassVars
map[team.config_yml] = team
end
end

sig do
override.params(file: String).
returns(T.nilable(::CodeTeams::Team))
end
def map_file_to_owner(file)
map_files_to_owners([file])[file]
end

sig do
override.returns(T::Hash[String, T.nilable(::CodeTeams::Team)])
end
def codeowners_lines_to_owners
return @@codeowners_lines_to_owners if @@codeowners_lines_to_owners&.keys && @@codeowners_lines_to_owners.keys.count > 0

@@codeowners_lines_to_owners = CodeTeams.all.each_with_object({}) do |team, map| # rubocop:disable Style/ClassVars
map[team.config_yml] = team
end
end

sig { override.void }
def bust_caches!
@@codeowners_lines_to_owners = {} # rubocop:disable Style/ClassVars
@@map_files_to_owners = {} # rubocop:disable Style/ClassVars
end

sig { override.returns(String) }
def description
'Team YML ownership'
end
end
end
end
end
23 changes: 23 additions & 0 deletions spec/lib/code_ownership_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@
# Owner metadata key in package.json
/frontend/javascripts/packages/my_other_package/**/** @MyOrg/bar-team
# Team YML ownership
/config/teams/bar.yml @MyOrg/bar-team
EXPECTED
end

Expand Down Expand Up @@ -220,6 +223,9 @@
# Owner metadata key in package.json
/frontend/javascripts/packages/my_other_package/**/** @MyOrg/bar-team
# Team YML ownership
/config/teams/bar.yml @MyOrg/bar-team
EXPECTED
end
end
Expand Down Expand Up @@ -428,6 +434,9 @@
# Owner metadata key in package.json
/frontend/javascripts/packages/my_other_package/**/** @MyOrg/bar-team
# Team YML ownership
/config/teams/bar.yml @MyOrg/bar-team
CODEOWNERS

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

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

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

it 'maps a team YML to be owned by the team itself' do
expect(CodeOwnership.for_file('config/teams/bar.yml')).to eq CodeTeams.find('Bar')
end

describe 'path formatting expectations' do
# All file paths must be clean paths relative to the root: https://apidock.com/ruby/Pathname/cleanpath
it 'will not find the ownership of a file that is not a cleanpath' do
Expand Down Expand Up @@ -996,6 +1013,9 @@
## Owner metadata key in package.json
- frontend/javascripts/packages/my_other_package/**/**
## Team YML ownership
- config/teams/bar.yml
OWNERSHIP
end

Expand Down Expand Up @@ -1024,6 +1044,9 @@
## Owner metadata key in package.json
This team owns nothing in this category.
## Team YML ownership
- config/teams/foo.yml
OWNERSHIP
end
end
Expand Down

0 comments on commit f7696fb

Please sign in to comment.