Class: CodeOwnership::Private::OwnershipMappers::FileAnnotations
- Inherits:
-
Object
- Object
- CodeOwnership::Private::OwnershipMappers::FileAnnotations
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
|
#description ⇒ Object
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)
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)
return if File.directory?(filename)
return unless File.file?(filename)
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] }
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)
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)
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|
unescaped_file = unescaped_path_for_codeowners_file(file)
!Private.file_tracked?(unescaped_file) ||
(fileset.include?(file) && !updated_cache_for_files.key?(file))
end
invalid_files.each do |invalid_file|
cache.delete(invalid_file)
end
cache
end
|