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



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

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

#descriptionObject



121
122
123
# File 'lib/code_ownership/private/ownership_mappers/team_globs.rb', line 121

def description
  'Team-specific owned globs'
end

#find_overlapping_globsObject



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

def find_overlapping_globs
  mapped_files = T.let({}, T::Hash[String, T::Array[MappingContext]])
  CodeTeams.all.each do |team|
    code_team = TeamPlugins::Ownership.for(team)

    code_team.owned_globs.each do |glob|
      Dir.glob(glob) do |filename|
        mapped_files[filename] ||= []
        T.must(mapped_files[filename]) << MappingContext.new(glob: glob, team: team)
      end
    end

    # Remove anything that is unowned, globbing them all at once
    Dir.glob(code_team.unowned_globs) do |filename|
      mapped_files.reject! { |key, value| key == filename && value.any? { |context| context.team == team } }
    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



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

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



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

def map_file_to_owner(file)
  map_files_to_owners[file]
end

#map_files_to_ownersObject



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

def map_files_to_owners
  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
    code_team = TeamPlugins::Ownership.for(team)

    (Dir.glob(code_team.owned_globs) - Dir.glob(code_team.unowned_globs)).each do |filename|
      map[filename] = team
    end
  end
end

#update_cache(cache, files) ⇒ Object



99
100
101
# File 'lib/code_ownership/private/ownership_mappers/team_globs.rb', line 99

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

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



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/code_ownership/private/ownership_mappers/team_globs.rb', line 126

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