Class: CodeOwnership::Private::OwnershipMappers::FileAnnotations

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

Overview

Calculate, cache, and return a mapping of file names (relative to the root of the repository) to team name.

Example:

{
  'app/models/company.rb' => Team.find('Setup & Onboarding'),
  ...
}

Constant Summary collapse

TEAM_PATTERN =
T.let(%r{\A(?:#|//) @team (?<team>.*)\Z}.freeze, Regexp)
DESCRIPTION =
'Annotations at the top of file'

Instance Method Summary collapse

Methods included from Mapper

all, included, to_glob_cache

Instance Method Details

#bust_caches!Object



130
# File 'lib/code_ownership/private/ownership_mappers/file_annotations.rb', line 130

def bust_caches!; end

#descriptionObject



125
126
127
# File 'lib/code_ownership/private/ownership_mappers/file_annotations.rb', line 125

def description
  DESCRIPTION
end

#escaped_path_for_codeowners_file(filename) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/code_ownership/private/ownership_mappers/file_annotations.rb', line 133

def escaped_path_for_codeowners_file(filename)
  # Globs can contain certain regex characters, like "[" and "]".
  # However, when we are generating a glob from a file annotation, we
  # need to escape bracket characters and interpret them literally.
  # Otherwise the resulting glob will not actually match the directory
  # containing the file.
  #
  # Example
  # filename: "/some/[xId]/myfile.tsx"
  # matches: "/some/1/file"
  # matches: "/some/2/file"
  # matches: "/some/3/file"
  # does not match!: "/some/[xId]/myfile.tsx"
  filename.gsub(/[\[\]]/) { |x| "\\#{x}" }
end

#file_annotation_based_owner(filename) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/code_ownership/private/ownership_mappers/file_annotations.rb', line 75

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
  # screenshots are saved to a folder with the test suite filename.
  return if File.directory?(filename)
  return unless File.file?(filename)

  # The annotation should be on line 1 but as of this comment
  # there's no linter installed to enforce that. We therefore check the
  # first line (the Ruby VM makes a single `read(1)` call for 8KB),
  # and if the annotation isn't in the first two lines we assume it
  # doesn't exist.

  line1 = File.foreach(filename).first

  return if !line1

  begin
    team = line1[TEAM_PATTERN, :team]
  rescue ArgumentError => e
    if e.message.include?('invalid byte sequence')
      team = nil
    else
      raise
    end
  end

  return unless team

  Private.find_team!(
    team,
    filename
  )
end

#globs_to_owner(files) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/code_ownership/private/ownership_mappers/file_annotations.rb', line 37

def globs_to_owner(files)
  files.each_with_object({}) do |filename_relative_to_root, mapping|
    owner = file_annotation_based_owner(filename_relative_to_root)
    next unless owner

    escaped_filename = escaped_path_for_codeowners_file(filename_relative_to_root)
    mapping[escaped_filename] = owner
  end
end

#map_file_to_owner(file) ⇒ Object



28
29
30
# File 'lib/code_ownership/private/ownership_mappers/file_annotations.rb', line 28

def map_file_to_owner(file)
  file_annotation_based_owner(file)
end

#remove_file_annotation!(filename) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/code_ownership/private/ownership_mappers/file_annotations.rb', line 111

def remove_file_annotation!(filename)
  if file_annotation_based_owner(filename)
    filepath = Pathname.new(filename)
    lines = filepath.read.split("\n")
    new_lines = lines.reject { |line| line[TEAM_PATTERN] }
    # We explicitly add a final new line since splitting by new line when reading the file lines
    # ignores new lines at the ends of files
    # We also remove leading new lines, since there is after a new line after an annotation
    new_file_contents = "#{new_lines.join("\n")}\n".gsub(/\A\n+/, '')
    filepath.write(new_file_contents)
  end
end

#unescaped_path_for_codeowners_file(filename) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/code_ownership/private/ownership_mappers/file_annotations.rb', line 150

def unescaped_path_for_codeowners_file(filename)
  # Globs can contain certain regex characters, like "[" and "]".
  # We escape bracket characters and interpret them literally for
  # the CODEOWNERS file. However, we want to compare the unescaped
  # glob to the actual file path when we check if the file was deleted.
  filename.gsub(/\\([\[\]])/, '\1')
end

#update_cache(cache, files) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/code_ownership/private/ownership_mappers/file_annotations.rb', line 50

def update_cache(cache, files)
  # We map files to nil owners so that files whose annotation have been removed will be properly
  # overwritten (i.e. removed) from the cache.
  fileset = Set.new(files)
  updated_cache_for_files = globs_to_owner(files)
  cache.merge!(updated_cache_for_files)

  invalid_files = cache.keys.select do |file|
    # If a file is not tracked, it should be removed from the cache
    unescaped_file = unescaped_path_for_codeowners_file(file)
    !Private.file_tracked?(unescaped_file) ||
      # If a file no longer has a file annotation (i.e. `globs_to_owner` doesn't map it)
      # it should be removed from the cache
      # We make sure to only apply this to the input files since otherwise `updated_cache_for_files.key?(file)` would always return `false` when files == []
      (fileset.include?(file) && !updated_cache_for_files.key?(file))
  end

  invalid_files.each do |invalid_file|
    cache.delete(invalid_file)
  end

  cache
end