Class: CodeOwnership::Private::OwnershipMappers::TeamGlobs

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Mapper, Validator
Defined in:
lib/code_ownership/private/ownership_mappers/team_globs.rb

Defined Under Namespace

Classes: GlobOverlap, MappingContext

Constant Summary collapse

@@map_files_to_owners =

rubocop:disable Style/ClassVars

{}

Instance Method Summary collapse

Methods included from Validator

all, included

Methods included from Mapper

all, included, to_glob_cache

Instance Method Details

#bust_caches!Object



110
111
112
# File 'lib/code_ownership/private/ownership_mappers/team_globs.rb', line 110

def bust_caches!
  @@map_files_to_owners = {} # rubocop:disable Style/ClassVars
end

#descriptionObject



115
116
117
# File 'lib/code_ownership/private/ownership_mappers/team_globs.rb', line 115

def description
  'Team-specific owned globs'
end

#find_overlapping_globsObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/code_ownership/private/ownership_mappers/team_globs.rb', line 57

def find_overlapping_globs
  mapped_files = T.let({}, T::Hash[String, T::Array[MappingContext]])
  CodeTeams.all.each_with_object({}) do |team, _map|
    TeamPlugins::Ownership.for(team).owned_globs.each do |glob|
      Dir.glob(glob).each do |filename|
        mapped_files[filename] ||= []
        T.must(mapped_files[filename]) << MappingContext.new(glob: glob, team: team)
      end
    end
  end

  overlaps = T.let([], T::Array[GlobOverlap])
  mapped_files.each_value do |mapping_contexts|
    if mapping_contexts.count > 1
      overlaps << GlobOverlap.new(mapping_contexts: mapping_contexts)
    end
  end

  overlaps.uniq do |glob_overlap|
    glob_overlap.mapping_contexts.map do |context|
      [context.glob, context.team.name]
    end
  end
end

#globs_to_owner(files) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/code_ownership/private/ownership_mappers/team_globs.rb', line 101

def globs_to_owner(files)
  CodeTeams.all.each_with_object({}) do |team, map|
    TeamPlugins::Ownership.for(team).owned_globs.each do |owned_glob|
      map[owned_glob] = team
    end
  end
end

#map_file_to_owner(file) ⇒ Object



86
87
88
# File 'lib/code_ownership/private/ownership_mappers/team_globs.rb', line 86

def map_file_to_owner(file)
  map_files_to_owners([file])[file]
end

#map_files_to_owners(files) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/code_ownership/private/ownership_mappers/team_globs.rb', line 20

def map_files_to_owners(files)
  return @@map_files_to_owners if @@map_files_to_owners&.keys && @@map_files_to_owners.keys.count.positive?

  @@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
      end
    end
  end
end

#update_cache(cache, files) ⇒ Object



93
94
95
# File 'lib/code_ownership/private/ownership_mappers/team_globs.rb', line 93

def update_cache(cache, files)
  globs_to_owner(files)
end

#validation_errors(files:, autocorrect: true, stage_changes: true) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/code_ownership/private/ownership_mappers/team_globs.rb', line 120

def validation_errors(files:, autocorrect: true, stage_changes: true)
  overlapping_globs = OwnershipMappers::TeamGlobs.new.find_overlapping_globs

  errors = T.let([], T::Array[String])

  if overlapping_globs.any?
    errors << <<~MSG
      `owned_globs` cannot overlap between teams. The following globs overlap:

      #{overlapping_globs.map { |overlap| "- #{overlap.description}" }.join("\n")}
    MSG
  end

  errors
end