Skip to content

Commit

Permalink
Migrate from bigrails-teams to code_teams (#12)
Browse files Browse the repository at this point in the history
* Use CodeTeams instead of Teams

* add major version constarint on code_teams

* fix other uses of bigrails
  • Loading branch information
Alex Evanczuk authored Jun 14, 2022
1 parent 610d408 commit 130f20d
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 88 deletions.
8 changes: 4 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
PATH
remote: .
specs:
code_ownership (1.27.1)
bigrails-teams
code_ownership (1.28.0)
code_teams (~> 1.0)
parse_packwerk
sorbet-runtime

GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
bigrails-teams (0.1.1)
code_teams (1.0.0)
sorbet-runtime
coderay (1.1.3)
diff-lcs (1.4.4)
method_source (1.0.0)
parse_packwerk (0.10.0)
parse_packwerk (0.10.1)
sorbet-runtime
parser (3.1.2.0)
ast (~> 2.4.1)
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ metadata:
```
### Glob-Based Ownership
In your team's configured YML (see [`bigrails-teams`](https://github.com/rubyatscale/bigrails-teams)), you can set `owned_globs` to be a glob of files your team owns. For example, in `my_team.yml`:
In your team's configured YML (see [`code_teams`](https://github.com/rubyatscale/code_teams)), you can set `owned_globs` to be a glob of files your team owns. For example, in `my_team.yml`:
```yml
name: My Team
owned_globs:
Expand All @@ -33,7 +33,7 @@ File annotations are a last resort if there is no clear home for your code. File
```
## Usage: Reading CodeOwnership
### `for_file`
`CodeOwnership.for_file`, given a relative path to a file returns a `Teams::Team` if there is a team that owns the file, `nil` otherwise.
`CodeOwnership.for_file`, given a relative path to a file returns a `CodeTeams::Team` if there is a team that owns the file, `nil` otherwise.

```ruby
CodeOwnership.for_file('path/to/file/relative/to/application/root.rb')
Expand All @@ -44,7 +44,7 @@ Contributor note: If you are making updates to this method or the methods gettin
See `code_ownership_spec.rb` for examples.

### `for_backtrace`
`CodeOwnership.for_backtrace` can be given a backtrace and will either return `nil`, or a `Teams::Team`.
`CodeOwnership.for_backtrace` can be given a backtrace and will either return `nil`, or a `CodeTeams::Team`.

```ruby
CodeOwnership.for_backtrace(exception.backtrace)
Expand All @@ -56,7 +56,7 @@ See `code_ownership_spec.rb` for an example.

### `for_class`

`CodeOwnership.for_class` can be given a class and will either return `nil`, or a `Teams::Team`.
`CodeOwnership.for_class` can be given a class and will either return `nil`, or a `CodeTeams::Team`.

```ruby
CodeOwnership.for_class(MyClass.name)
Expand All @@ -73,7 +73,7 @@ A `CODEOWNERS` file defines who owns specific files or paths in a repository. Wh
## Proper Configuration & Validation
CodeOwnership comes with a validation function to ensure the following things are true:
1) Only one mechanism is defining file ownership. That is -- you can't have a file annotation on a file owned via package-based or glob-based ownership. This helps make ownership behavior more clear by avoiding concerns about precedence.
2) All teams referenced as an owner for any file or package is a valid team (i.e. it's in the list of `Teams.all`).
2) All teams referenced as an owner for any file or package is a valid team (i.e. it's in the list of `CodeTeams.all`).
3) All files have ownership. You can specify in `unowned_globs` to represent a TODO list of files to add ownership to.
3) The `.github/CODEOWNERS` file is up to date. This is automatically corrected and staged unless specified otherwise with `bin/codeownership validate --skip-autocorrect --skip-stage`. You can turn this validation off by setting `skip_codeowners_validation: true` in `code_ownership.yml`.

Expand Down
4 changes: 2 additions & 2 deletions 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.27.1'
spec.version = '1.28.0'
spec.authors = ['Gusto Engineers']
spec.email = ['[email protected]']
spec.summary = 'A gem to help engineering teams declare ownership of code'
Expand All @@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
spec.files = Dir['README.md', 'sorbet/**/*', 'lib/**/*', 'bin/**/*']
spec.require_paths = ['lib']

spec.add_dependency 'bigrails-teams'
spec.add_dependency 'code_teams', '~> 1.0'
spec.add_dependency 'parse_packwerk'
spec.add_dependency 'sorbet-runtime'

Expand Down
16 changes: 8 additions & 8 deletions lib/code_ownership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# typed: strict

require 'set'
require 'teams'
require 'code_teams'
require 'sorbet-runtime'
require 'json'
require 'parse_packwerk'
Expand All @@ -17,15 +17,15 @@ module CodeOwnership

requires_ancestor { Kernel }

sig { params(file: String).returns(T.nilable(Teams::Team)) }
sig { params(file: String).returns(T.nilable(CodeTeams::Team)) }
def for_file(file)
@for_file ||= T.let(@for_file, T.nilable(T::Hash[String, T.nilable(Teams::Team)]))
@for_file ||= T.let(@for_file, T.nilable(T::Hash[String, T.nilable(CodeTeams::Team)]))
@for_file ||= {}

return nil if file.start_with?('./')
return @for_file[file] if @for_file.key?(file)

owner = T.let(nil, T.nilable(Teams::Team))
owner = T.let(nil, T.nilable(CodeTeams::Team))

Private.mappers.each do |mapper|
owner = mapper.map_file_to_owner(file)
Expand Down Expand Up @@ -61,7 +61,7 @@ def validate!(

# Given a backtrace from either `Exception#backtrace` or `caller`, find the
# first line that corresponds to a file with assigned ownership
sig { params(backtrace: T.nilable(T::Array[String]), excluded_teams: T::Array[::Teams::Team]).returns(T.nilable(::Teams::Team)) }
sig { params(backtrace: T.nilable(T::Array[String]), excluded_teams: T::Array[::CodeTeams::Team]).returns(T.nilable(::CodeTeams::Team)) }
def for_backtrace(backtrace, excluded_teams: [])
return unless backtrace

Expand Down Expand Up @@ -93,9 +93,9 @@ def for_backtrace(backtrace, excluded_teams: [])
nil
end

sig { params(klass: T.nilable(T.any(Class, Module))).returns(T.nilable(::Teams::Team)) }
sig { params(klass: T.nilable(T.any(Class, Module))).returns(T.nilable(::CodeTeams::Team)) }
def for_class(klass)
@memoized_values ||= T.let(@memoized_values, T.nilable(T::Hash[String, T.nilable(::Teams::Team)]))
@memoized_values ||= T.let(@memoized_values, T.nilable(T::Hash[String, T.nilable(::CodeTeams::Team)]))
@memoized_values ||= {}
# We use key because the memoized value could be `nil`
if !@memoized_values.key?(klass.to_s)
Expand All @@ -110,7 +110,7 @@ def for_class(klass)
end
end

sig { params(package: ParsePackwerk::Package).returns(T.nilable(::Teams::Team)) }
sig { params(package: ParsePackwerk::Package).returns(T.nilable(::CodeTeams::Team)) }
def for_package(package)
Private::OwnershipMappers::PackageOwnership.new.owner_for_package(package)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/code_ownership/private.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ def self.tracked_files
@tracked_files ||= Dir.glob(configuration.owned_globs)
end

sig { params(team_name: String, location_of_reference: String).returns(Teams::Team) }
sig { params(team_name: String, location_of_reference: String).returns(CodeTeams::Team) }
def self.find_team!(team_name, location_of_reference)
found_team = Teams.find(team_name)
found_team = CodeTeams.find(team_name)
if found_team.nil?
raise StandardError, "Could not find team with name: `#{team_name}` in #{location_of_reference}. Make sure the team is one of `#{Teams.all.map(&:name).sort}`"
raise StandardError, "Could not find team with name: `#{team_name}` in #{location_of_reference}. Make sure the team is one of `#{CodeTeams.all.map(&:name).sort}`"
else
found_team
end
Expand Down
10 changes: 5 additions & 5 deletions lib/code_ownership/private/ownership_mappers/file_annotations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ class FileAnnotations
extend T::Sig
include Interface

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

TEAM_PATTERN = T.let(/\A(?:#|\/\/) @team (?<team>.*)\Z/.freeze, Regexp)

sig do
override.params(file: String).
returns(T.nilable(::Teams::Team))
returns(T.nilable(::CodeTeams::Team))
end
def map_file_to_owner(file)
file_annotation_based_owner(file)
Expand All @@ -33,7 +33,7 @@ def map_file_to_owner(file)
sig do
override.
params(files: T::Array[String]).
returns(T::Hash[String, T.nilable(::Teams::Team)])
returns(T::Hash[String, T.nilable(::CodeTeams::Team)])
end
def map_files_to_owners(files)
return @@map_files_to_owners if @@map_files_to_owners&.keys && @@map_files_to_owners.keys.count > 0
Expand All @@ -46,7 +46,7 @@ def map_files_to_owners(files)
end
end

sig { params(filename: String).returns(T.nilable(Teams::Team)) }
sig { params(filename: String).returns(T.nilable(CodeTeams::Team)) }
def file_annotation_based_owner(filename)
# If for a directory is named with an ownable extension, we need to skip
# so File.foreach doesn't blow up below. This was needed because Cypress
Expand Down Expand Up @@ -97,7 +97,7 @@ def remove_file_annotation!(filename)
end

sig do
override.returns(T::Hash[String, T.nilable(::Teams::Team)])
override.returns(T::Hash[String, T.nilable(::CodeTeams::Team)])
end
def codeowners_lines_to_owners
@@map_files_to_owners = nil # rubocop:disable Style/ClassVars
Expand Down
6 changes: 3 additions & 3 deletions lib/code_ownership/private/ownership_mappers/interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module Interface
#
sig do
abstract.params(file: String).
returns(T.nilable(::Teams::Team))
returns(T.nilable(::CodeTeams::Team))
end
def map_file_to_owner(file)
end
Expand All @@ -26,13 +26,13 @@ def map_file_to_owner(file)
#
sig do
abstract.params(files: T::Array[String]).
returns(T::Hash[String, T.nilable(::Teams::Team)])
returns(T::Hash[String, T.nilable(::CodeTeams::Team)])
end
def map_files_to_owners(files)
end

sig do
abstract.returns(T::Hash[String, T.nilable(::Teams::Team)])
abstract.returns(T::Hash[String, T.nilable(::CodeTeams::Team)])
end
def codeowners_lines_to_owners
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class JsPackageOwnership

sig do
override.params(file: String).
returns(T.nilable(::Teams::Team))
returns(T.nilable(::CodeTeams::Team))
end
def map_file_to_owner(file)
package = map_file_to_relevant_package(file)
Expand All @@ -26,7 +26,7 @@ def map_file_to_owner(file)
sig do
override.
params(files: T::Array[String]).
returns(T::Hash[String, T.nilable(::Teams::Team)])
returns(T::Hash[String, T.nilable(::CodeTeams::Team)])
end
def map_files_to_owners(files) # rubocop:disable Lint/UnusedMethodArgument
ParseJsPackages.all.each_with_object({}) do |package, res|
Expand All @@ -49,7 +49,7 @@ def map_files_to_owners(files) # rubocop:disable Lint/UnusedMethodArgument
# subset of files, but rather we want code ownership for all files.
#
sig do
override.returns(T::Hash[String, T.nilable(::Teams::Team)])
override.returns(T::Hash[String, T.nilable(::CodeTeams::Team)])
end
def codeowners_lines_to_owners
ParseJsPackages.all.each_with_object({}) do |package, res|
Expand All @@ -65,7 +65,7 @@ def description
'Owner metadata key in package.json'
end

sig { params(package: ParseJsPackages::Package).returns(T.nilable(Teams::Team)) }
sig { params(package: ParseJsPackages::Package).returns(T.nilable(CodeTeams::Team)) }
def owner_for_package(package)
raw_owner_value = package.metadata['owner']
return nil if !raw_owner_value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class PackageOwnership

sig do
override.params(file: String).
returns(T.nilable(::Teams::Team))
returns(T.nilable(::CodeTeams::Team))
end
def map_file_to_owner(file)
package = map_file_to_relevant_package(file)
Expand All @@ -26,7 +26,7 @@ def map_file_to_owner(file)
sig do
override.
params(files: T::Array[String]).
returns(T::Hash[String, T.nilable(::Teams::Team)])
returns(T::Hash[String, T.nilable(::CodeTeams::Team)])
end
def map_files_to_owners(files) # rubocop:disable Lint/UnusedMethodArgument
ParsePackwerk.all.each_with_object({}) do |package, res|
Expand All @@ -49,7 +49,7 @@ def map_files_to_owners(files) # rubocop:disable Lint/UnusedMethodArgument
# subset of files, but rather we want code ownership for all files.
#
sig do
override.returns(T::Hash[String, T.nilable(::Teams::Team)])
override.returns(T::Hash[String, T.nilable(::CodeTeams::Team)])
end
def codeowners_lines_to_owners
ParsePackwerk.all.each_with_object({}) do |package, res|
Expand All @@ -65,7 +65,7 @@ def description
'Owner metadata key in package.yml'
end

sig { params(package: ParsePackwerk::Package).returns(T.nilable(Teams::Team)) }
sig { params(package: ParsePackwerk::Package).returns(T.nilable(CodeTeams::Team)) }
def owner_for_package(package)
raw_owner_value = package.metadata['owner']
return nil if !raw_owner_value
Expand Down
14 changes: 7 additions & 7 deletions lib/code_ownership/private/ownership_mappers/team_globs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ class TeamGlobs
extend T::Sig
include Interface

@@map_files_to_owners = T.let(@map_files_to_owners, T.nilable(T::Hash[String, T.nilable(::Teams::Team)])) # rubocop:disable Style/ClassVars
@@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(::Teams::Team)])) # 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(::Teams::Team)])
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 = Teams.all.each_with_object({}) do |team, map| # rubocop:disable Style/ClassVars
@@map_files_to_owners = CodeTeams.all.each_with_object({}) do |team, map| # rubocop:disable Style/ClassVars
TeamPlugins::Ownership.for(team).owned_globs.each do |glob|
Dir.glob(glob).each do |filename|
map[filename] = team
Expand All @@ -33,19 +33,19 @@ def map_files_to_owners(files) # rubocop:disable Lint/UnusedMethodArgument

sig do
override.params(file: String).
returns(T.nilable(::Teams::Team))
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(::Teams::Team)])
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 = Teams.all.each_with_object({}) do |team, map| # rubocop:disable Style/ClassVars
@@codeowners_lines_to_owners = CodeTeams.all.each_with_object({}) do |team, map| # rubocop:disable Style/ClassVars
TeamPlugins::Ownership.for(team).owned_globs.each do |owned_glob|
map[owned_glob] = team
end
Expand Down
2 changes: 1 addition & 1 deletion lib/code_ownership/private/team_plugins/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module CodeOwnership
module Private
module TeamPlugins
class Github < Teams::Plugin
class Github < CodeTeams::Plugin
extend T::Sig
extend T::Helpers

Expand Down
2 changes: 1 addition & 1 deletion lib/code_ownership/private/team_plugins/ownership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module CodeOwnership
module Private
module TeamPlugins
class Ownership < Teams::Plugin
class Ownership < CodeTeams::Plugin
extend T::Sig
extend T::Helpers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def validation_errors(files:, autocorrect: true, stage_changes: true)
# https://help.github.com/articles/about-codeowners/
sig { returns(T::Array[String]) }
def codeowners_file_lines
github_team_map = Teams.all.each_with_object({}) do |team, map|
github_team_map = CodeTeams.all.each_with_object({}) do |team, map|
team_github = TeamPlugins::Github.for(team).github
next if team_github.do_not_add_to_codeowners_file

Expand Down
Loading

0 comments on commit 130f20d

Please sign in to comment.