Skip to content

Commit

Permalink
bin/codeownership for_team should use cache (90% perf improvement) (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Evanczuk authored Apr 12, 2023
1 parent 951c21a commit 7b70b69
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 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.32.10)
code_ownership (1.32.11)
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.32.10'
spec.version = '1.32.11'
spec.authors = ['Gusto Engineers']
spec.email = ['[email protected]']
spec.summary = 'A gem to help engineering teams declare ownership of code'
Expand Down
18 changes: 7 additions & 11 deletions lib/code_ownership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,23 @@ def for_file(file)

sig { params(team: T.any(CodeTeams::Team, String)).returns(String) }
def for_team(team)
Private.load_configuration!

team = T.must(CodeTeams.find(team)) if team.is_a?(String)
ownership_information = T.let([], T::Array[String])

ownership_information << "# Code Ownership Report for `#{team.name}` Team"
Mapper.all.each do |mapper|
ownership_information << "## #{mapper.description}"
codeowners_lines = mapper.globs_to_owner(Private.tracked_files)

Private.glob_cache.raw_cache_contents.each do |mapper_description, glob_to_owning_team_map|
ownership_information << "## #{mapper_description}"
ownership_for_mapper = []
codeowners_lines.each do |line, team_for_line|
next if team_for_line.nil?
if team_for_line.name == team.name
ownership_for_mapper << "- #{line}"
end
glob_to_owning_team_map.each do |glob, owning_team|
next if owning_team != team
ownership_for_mapper << "- #{glob}"
end

if ownership_for_mapper.empty?
ownership_information << 'This team owns nothing in this category.'
else
ownership_information += ownership_for_mapper
ownership_information += ownership_for_mapper.sort
end

ownership_information << ""
Expand Down
3 changes: 2 additions & 1 deletion lib/code_ownership/mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ def self.to_glob_cache

Mapper.all.each do |mapper|
mapped_files = mapper.globs_to_owner(Private.tracked_files)
glob_to_owner_map_by_mapper_description[mapper.description] ||= {}

mapped_files.each do |glob, owner|
next if owner.nil?
glob_to_owner_map_by_mapper_description[mapper.description] ||= {}
glob_to_owner_map_by_mapper_description.fetch(mapper.description)[glob] = owner
end
end
Expand Down

0 comments on commit 7b70b69

Please sign in to comment.