-
Notifications
You must be signed in to change notification settings - Fork 919
/
file_fetcher.rb
295 lines (235 loc) 路 9.86 KB
/
file_fetcher.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# frozen_string_literal: true
require "pathname"
require "toml-rb"
require "dependabot/file_fetchers"
require "dependabot/file_fetchers/base"
require "dependabot/cargo/file_parser"
# Docs on Cargo workspaces:
# https://doc.rust-lang.org/cargo/reference/manifest.html#the-workspace-section
module Dependabot
module Cargo
class FileFetcher < Dependabot::FileFetchers::Base
def self.required_files_in?(filenames)
filenames.include?("Cargo.toml")
end
def self.required_files_message
"Repo must contain a Cargo.toml."
end
private
def fetch_files
fetched_files = []
fetched_files << cargo_toml
fetched_files << cargo_lock if cargo_lock
fetched_files << rust_toolchain if rust_toolchain
fetched_files += fetch_path_dependency_and_workspace_files
fetched_files.uniq
end
def fetch_path_dependency_and_workspace_files(files = nil)
fetched_files = files || [cargo_toml]
fetched_files += path_dependency_files(fetched_files)
fetched_files += fetched_files.flat_map { |f| workspace_files(f) }
updated_files = fetched_files.reject(&:support_file?).uniq
updated_files +=
fetched_files.uniq.
reject { |f| updated_files.map(&:name).include?(f.name) }
return updated_files if updated_files == files
fetch_path_dependency_and_workspace_files(updated_files)
end
def workspace_files(cargo_toml)
@workspace_files ||= {}
@workspace_files[cargo_toml.name] ||=
fetch_workspace_files(
file: cargo_toml,
previously_fetched_files: []
)
end
def path_dependency_files(fetched_files)
@path_dependency_files ||= {}
fetched_path_dependency_files = []
fetched_files.each do |file|
@path_dependency_files[file.name] ||=
fetch_path_dependency_files(
file: file,
previously_fetched_files: fetched_files +
fetched_path_dependency_files
)
fetched_path_dependency_files += @path_dependency_files[file.name]
end
fetched_path_dependency_files
end
def fetch_workspace_files(file:, previously_fetched_files:)
current_dir = file.name.rpartition("/").first
current_dir = nil if current_dir == ""
files = workspace_dependency_paths_from_file(file).flat_map do |path|
path = File.join(current_dir, path) unless current_dir.nil?
path = Pathname.new(path).cleanpath.to_path
next if previously_fetched_files.map(&:name).include?(path)
next if file.name == path
fetched_file = fetch_file_from_host(path)
previously_fetched_files << fetched_file
grandchild_requirement_files =
fetch_workspace_files(
file: fetched_file,
previously_fetched_files: previously_fetched_files
)
[fetched_file, *grandchild_requirement_files]
end.compact
files.each { |f| f.support_file = file != cargo_toml }
files
end
# rubocop:disable Metrics/PerceivedComplexity
def fetch_path_dependency_files(file:, previously_fetched_files:)
current_dir = file.name.rpartition("/").first
current_dir = nil if current_dir == ""
unfetchable_required_path_deps = []
path_dependency_files ||=
path_dependency_paths_from_file(file).flat_map do |path|
path = File.join(current_dir, path) unless current_dir.nil?
path = Pathname.new(path).cleanpath.to_path
next if previously_fetched_files.map(&:name).include?(path)
next if file.name == path
fetched_file = fetch_file_from_host(path, fetch_submodules: true).
tap { |f| f.support_file = true }
previously_fetched_files << fetched_file
grandchild_requirement_files =
fetch_path_dependency_files(
file: fetched_file,
previously_fetched_files: previously_fetched_files
)
[fetched_file, *grandchild_requirement_files]
rescue Dependabot::DependencyFileNotFound
next unless required_path?(file, path)
unfetchable_required_path_deps << path
end.compact
return path_dependency_files if unfetchable_required_path_deps.none?
raise Dependabot::PathDependenciesNotReachable,
unfetchable_required_path_deps
end
# rubocop:enable Metrics/PerceivedComplexity
def path_dependency_paths_from_file(file)
paths = []
# Paths specified in dependency declaration
Cargo::FileParser::DEPENDENCY_TYPES.each do |type|
parsed_file(file).fetch(type, {}).each do |_, details|
next unless details.is_a?(Hash)
next unless details["path"]
paths << File.join(details["path"], "Cargo.toml")
end
end
# Paths specified for target-specific dependencies
parsed_file(file).fetch("target", {}).each do |_, t_details|
Cargo::FileParser::DEPENDENCY_TYPES.each do |type|
t_details.fetch(type, {}).each do |_, details|
next unless details.is_a?(Hash)
next unless details["path"]
paths << File.join(details["path"], "Cargo.toml")
end
end
end
paths += replacement_path_dependency_paths_from_file(file)
paths
end
def replacement_path_dependency_paths_from_file(file)
paths = []
# Paths specified as replacements
parsed_file(file).fetch("replace", {}).each do |_, details|
next unless details.is_a?(Hash)
next unless details["path"]
paths << File.join(details["path"], "Cargo.toml")
end
# Paths specified as patches
parsed_file(file).fetch("patch", {}).each do |_, details|
next unless details.is_a?(Hash)
details.each do |_, dep_details|
next unless dep_details.is_a?(Hash)
next unless dep_details["path"]
paths << File.join(dep_details["path"], "Cargo.toml")
end
end
paths
end
def workspace_dependency_paths_from_file(file)
if parsed_file(file)["workspace"] &&
!parsed_file(file)["workspace"].key?("members")
return path_dependency_paths_from_file(file)
end
workspace_paths = parsed_file(file).dig("workspace", "members")
return [] unless workspace_paths&.any?
# Expand any workspace paths that specify a `*`
workspace_paths = workspace_paths.flat_map do |path|
path.include?("*") ? expand_workspaces(path) : [path]
end
# Excluded paths, to be subtracted for the workspaces array
excluded_paths =
(parsed_file(file).dig("workspace", "excluded_paths") || []) +
(parsed_file(file).dig("workspace", "exclude") || [])
(workspace_paths - excluded_paths).map do |path|
File.join(path, "Cargo.toml")
end
end
# Check whether a path is required or not. It will not be required if
# an alternative source (i.e., a git source) is also specified
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
# rubocop:disable Metrics/AbcSize
def required_path?(file, path)
# Paths specified in dependency declaration
Cargo::FileParser::DEPENDENCY_TYPES.each do |type|
parsed_file(file).fetch(type, {}).each do |_, details|
next unless details.is_a?(Hash)
next unless details["path"]
next unless path == File.join(details["path"], "Cargo.toml")
return true if details["git"].nil?
end
end
# Paths specified for target-specific dependencies
parsed_file(file).fetch("target", {}).each do |_, t_details|
Cargo::FileParser::DEPENDENCY_TYPES.each do |type|
t_details.fetch(type, {}).each do |_, details|
next unless details.is_a?(Hash)
next unless details["path"]
next unless path == File.join(details["path"], "Cargo.toml")
return true if details["git"].nil?
end
end
end
# Paths specified as replacements
parsed_file(file).fetch("replace", {}).each do |_, details|
next unless details.is_a?(Hash)
next unless details["path"]
next unless path == File.join(details["path"], "Cargo.toml")
return true if details["git"].nil?
end
false
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Metrics/CyclomaticComplexity
def expand_workspaces(path)
path = Pathname.new(path).cleanpath.to_path
dir = directory.gsub(%r{(^/|/$)}, "")
unglobbed_path = path.split("*").first.gsub(%r{(?<=/)[^/]*$}, "")
repo_contents(dir: unglobbed_path, raise_errors: false).
select { |file| file.type == "dir" }.
map { |f| f.path.gsub(%r{^/?#{Regexp.escape(dir)}/?}, "") }.
select { |filename| File.fnmatch?(path, filename) }
end
def parsed_file(file)
TomlRB.parse(file.content)
rescue TomlRB::ParseError, TomlRB::ValueOverwriteError
raise Dependabot::DependencyFileNotParseable, file.path
end
def cargo_toml
@cargo_toml ||= fetch_file_from_host("Cargo.toml")
end
def cargo_lock
@cargo_lock ||= fetch_file_if_present("Cargo.lock")
end
def rust_toolchain
@rust_toolchain ||= fetch_file_if_present("rust-toolchain")&.
tap { |f| f.support_file = true }
end
end
end
end
Dependabot::FileFetchers.register("cargo", Dependabot::Cargo::FileFetcher)