-
Notifications
You must be signed in to change notification settings - Fork 971
/
changelog_finder.rb
516 lines (438 loc) · 18 KB
/
changelog_finder.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# typed: strict
# frozen_string_literal: true
require "excon"
require "sorbet-runtime"
require "dependabot/clients/github_with_retries"
require "dependabot/clients/gitlab_with_retries"
require "dependabot/clients/bitbucket_with_retries"
require "dependabot/shared_helpers"
require "dependabot/metadata_finders/base"
module Dependabot
module MetadataFinders
class Base
# rubocop:disable Metrics/ClassLength
class ChangelogFinder
extend T::Sig
require_relative "changelog_pruner"
require_relative "commits_finder"
# Earlier entries are preferred
CHANGELOG_NAMES = T.let(
%w(changelog news changes history release whatsnew releases).freeze,
T::Array[String]
)
sig { returns(T.nilable(Dependabot::Source)) }
attr_reader :source
sig { returns(Dependabot::Dependency) }
attr_reader :dependency
sig { returns(T::Array[Dependabot::Credential]) }
attr_reader :credentials
sig { returns(T.nilable(String)) }
attr_reader :suggested_changelog_url
sig do
params(
source: T.nilable(Dependabot::Source),
dependency: Dependabot::Dependency,
credentials: T::Array[Dependabot::Credential],
suggested_changelog_url: T.nilable(String)
)
.void
end
def initialize(source:, dependency:, credentials:,
suggested_changelog_url: nil)
@source = source
@dependency = dependency
@credentials = credentials
@suggested_changelog_url = suggested_changelog_url
# strip fragment from URL, if present
@suggested_changelog_url = @suggested_changelog_url&.split("#")&.first
@new_version = T.let(nil, T.nilable(String))
@changelog_from_suggested_url = T.let(nil, T.untyped)
end
sig { returns(T.nilable(String)) }
def changelog_url
changelog&.html_url
end
sig { returns(T.nilable(String)) }
def changelog_text
return unless full_changelog_text
ChangelogPruner.new(
dependency: dependency,
changelog_text: full_changelog_text
).pruned_text
end
sig { returns(T.nilable(String)) }
def upgrade_guide_url
upgrade_guide&.html_url
end
sig { returns(T.nilable(String)) }
def upgrade_guide_text
return unless upgrade_guide
@upgrade_guide_text ||= T.let(
fetch_file_text(upgrade_guide),
T.nilable(String)
)
end
private
# rubocop:disable Metrics/PerceivedComplexity
sig { returns(T.untyped) }
def changelog
return unless changelog_from_suggested_url || source
return if git_source? && !ref_changed?
return changelog_from_suggested_url if changelog_from_suggested_url
# If there is a changelog, and it includes the new version, return it
if new_version && default_branch_changelog &&
fetch_file_text(default_branch_changelog)&.include?(T.must(new_version))
return default_branch_changelog
end
# Otherwise, look for a changelog at the tag for this version
if new_version && relevant_tag_changelog &&
fetch_file_text(relevant_tag_changelog)&.include?(T.must(new_version))
return relevant_tag_changelog
end
# Fall back to the changelog (or nil) from the default branch
default_branch_changelog
end
# rubocop:enable Metrics/PerceivedComplexity
sig { returns(T.nilable(Sawyer::Resource)) }
def changelog_from_suggested_url
return @changelog_from_suggested_url unless @changelog_from_suggested_url.nil?
return unless suggested_changelog_url
# TODO: Support other providers
suggested_source = Source.from_url(suggested_changelog_url)
return unless suggested_source&.provider == "github"
opts = { path: suggested_source&.directory, ref: suggested_source&.branch }.compact
suggested_source_client = github_client_for_source(T.must(suggested_source))
tmp_files = T.unsafe(suggested_source_client).contents(suggested_source&.repo, opts)
filename = T.must(T.must(suggested_changelog_url).split("/").last)
@changelog_from_suggested_url =
tmp_files.find { |f| f.name == filename }
rescue Octokit::NotFound, Octokit::UnavailableForLegalReasons
@changelog_from_suggested_url = nil
end
sig { returns(T.nilable(T.any(OpenStruct, Sawyer::Resource))) }
def default_branch_changelog
return unless source
@default_branch_changelog ||=
T.let(
changelog_from_ref(nil),
T.nilable(T.any(OpenStruct, Sawyer::Resource))
)
end
sig { returns(T.nilable(T.any(OpenStruct, Sawyer::Resource))) }
def relevant_tag_changelog
return unless source
return unless tag_for_new_version
@relevant_tag_changelog ||=
T.let(
changelog_from_ref(tag_for_new_version),
T.nilable(T.any(OpenStruct, Sawyer::Resource))
)
end
sig { params(ref: T.nilable(String)).returns(T.nilable(T.any(OpenStruct, Sawyer::Resource))) }
def changelog_from_ref(ref)
files =
dependency_file_list(ref)
.select { |f| f.type == "file" }
.reject { |f| f.name.end_with?(".sh") }
.reject { |f| f.size > 1_000_000 }
.reject { |f| f.size < 100 }
select_best_changelog(files)
end
# rubocop:disable Metrics/PerceivedComplexity
sig { params(files: T::Array[T.untyped]).returns(T.untyped) }
def select_best_changelog(files)
CHANGELOG_NAMES.each do |name|
candidates = files.select { |f| f.name =~ /#{name}/i }
file = candidates.first if candidates.one?
file ||=
candidates.find do |f|
if fetch_file_text(f).nil?
candidates -= [f]
next
end
pruner = ChangelogPruner.new(
dependency: dependency,
changelog_text: fetch_file_text(f)
)
pruner.includes_new_version? ||
pruner.includes_previous_version?
end
file ||= candidates.max_by(&:size)
return file if file
end
nil
end
# rubocop:enable Metrics/PerceivedComplexity
sig { returns(T.nilable(String)) }
def tag_for_new_version
@tag_for_new_version ||=
T.let(
CommitsFinder.new(
dependency: dependency,
source: source,
credentials: credentials
).new_tag,
T.nilable(String)
)
end
sig { returns(T.nilable(String)) }
def full_changelog_text
return unless changelog
fetch_file_text(changelog)
end
sig { params(file: T.untyped).returns(T.nilable(String)) }
def fetch_file_text(file)
@file_text ||= T.let({}, T.nilable(T::Hash[String, T.untyped]))
unless @file_text.key?(file.download_url)
file_source = T.must(Source.from_url(file.html_url))
@file_text[file.download_url] =
case file_source.provider
when "github" then fetch_github_file(file_source, file)
when "gitlab" then fetch_gitlab_file(file)
when "bitbucket" then fetch_bitbucket_file(file)
when "azure" then fetch_azure_file(file)
when "codecommit" then nil # TODO: git file from codecommit
else raise "Unsupported provider '#{file_source.provider}'"
end
end
return unless @file_text[file.download_url].valid_encoding?
@file_text[file.download_url].rstrip
end
sig { params(file_source: Dependabot::Source, file: T.untyped).returns(String) }
def fetch_github_file(file_source, file)
# Hitting the download URL directly causes encoding problems
raw_content = T.unsafe(github_client_for_source(file_source)).get(file.url).content
Base64.decode64(raw_content).force_encoding("UTF-8").encode
end
sig { params(file: T.untyped).returns(String) }
def fetch_gitlab_file(file)
Excon.get(
file.download_url,
idempotent: true,
**SharedHelpers.excon_defaults
).body.force_encoding("UTF-8").encode
end
sig { params(file: T.untyped).returns(String) }
def fetch_bitbucket_file(file)
T.unsafe(bitbucket_client).get(file.download_url).body
.force_encoding("UTF-8").encode
end
sig { params(file: T.untyped).returns(String) }
def fetch_azure_file(file)
azure_client.get(file.download_url).body
.force_encoding("UTF-8").encode
end
sig { returns(T.untyped) }
def upgrade_guide
return unless source
# Upgrade guide usually won't be relevant for bumping anything other
# than the major version
return unless major_version_upgrade?
dependency_file_list
.select { |f| f.type == "file" }
.select { |f| f.name.casecmp("upgrade.md").zero? }
.reject { |f| f.size > 1_000_000 }
.max_by(&:size)
end
sig { params(ref: T.nilable(String)).returns(T.untyped) }
def dependency_file_list(ref = nil)
@dependency_file_list ||= T.let({}, T.nilable(T::Hash[T.nilable(String), T.untyped]))
@dependency_file_list[ref] ||= fetch_dependency_file_list(ref)
end
sig { params(ref: T.nilable(String)).returns(T::Array[T.untyped,]) }
def fetch_dependency_file_list(ref)
case T.must(source).provider
when "github" then fetch_github_file_list(ref)
when "bitbucket" then fetch_bitbucket_file_list
when "gitlab" then fetch_gitlab_file_list
when "azure" then fetch_azure_file_list
when "codecommit" then [] # TODO: Fetch Files from Codecommit
else raise "Unexpected repo provider '#{T.must(source).provider}'"
end
end
# rubocop:disable Metrics/AbcSize
sig { params(ref: T.nilable(String)).returns(T::Array[T.untyped]) }
def fetch_github_file_list(ref)
files = []
if T.must(source).directory
opts = { path: T.must(source).directory, ref: ref }.compact
tmp_files = T.unsafe(github_client).contents(T.must(source).repo, opts)
files += tmp_files if tmp_files.is_a?(Array)
end
opts = { ref: ref }.compact
files += T.unsafe(github_client).contents(T.must(source).repo, opts)
files.uniq.each do |f|
next unless f.type == "dir" && f.name.match?(/docs?/o)
opts = { path: f.path, ref: ref }.compact
files += T.unsafe(github_client).contents(T.must(source).repo, opts)
end
files
rescue Octokit::NotFound, Octokit::UnavailableForLegalReasons
[]
end
# rubocop:enable Metrics/AbcSize
sig { returns(T.untyped) }
def fetch_bitbucket_file_list
branch = default_bitbucket_branch
T.unsafe(bitbucket_client).fetch_repo_contents(T.must(source).repo).map do |file|
type = case file.fetch("type")
when "commit_file" then "file"
when "commit_directory" then "dir"
else file.fetch("type")
end
OpenStruct.new(
name: file.fetch("path").split("/").last,
type: type,
size: file.fetch("size", 100),
html_url: "#{T.must(source).url}/src/#{branch}/#{file['path']}",
download_url: "#{T.must(source).url}/raw/#{branch}/#{file['path']}"
)
end
rescue Dependabot::Clients::Bitbucket::NotFound,
Dependabot::Clients::Bitbucket::Unauthorized,
Dependabot::Clients::Bitbucket::Forbidden
[]
end
sig { returns(T.untyped) }
def fetch_gitlab_file_list
branch = default_gitlab_branch
T.unsafe(gitlab_client).repo_tree(T.must(source).repo).map do |file|
type = case file.type
when "blob" then "file"
when "tree" then "dir"
else file.fetch("type")
end
OpenStruct.new(
name: file.name,
type: type,
size: 100, # GitLab doesn't return file size
html_url: "#{T.must(source).url}/blob/#{branch}/#{file.path}",
download_url: "#{T.must(source).url}/raw/#{branch}/#{file.path}"
)
end
rescue Gitlab::Error::NotFound
[]
end
sig { returns(T.untyped) }
def fetch_azure_file_list
azure_client.fetch_repo_contents.map do |entry|
type = case entry.fetch("gitObjectType")
when "blob" then "file"
when "tree" then "dir"
else entry.fetch("gitObjectType")
end
OpenStruct.new(
name: File.basename(entry.fetch("relativePath")),
type: type,
size: entry.fetch("size"),
path: entry.fetch("relativePath"),
html_url: "#{T.must(source).url}?path=/#{entry.fetch('relativePath')}",
download_url: entry.fetch("url")
)
end
rescue Dependabot::Clients::Azure::NotFound,
Dependabot::Clients::Azure::Unauthorized,
Dependabot::Clients::Azure::Forbidden
[]
end
sig { returns(T.nilable(String)) }
def new_version
return @new_version unless @new_version.nil?
new_version = git_source? && new_ref ? new_ref : dependency.version
@new_version = new_version&.gsub(/^v/, "")
end
sig { returns(T.nilable(String)) }
def previous_ref
previous_refs = dependency.previous_requirements&.filter_map do |r|
r.dig(:source, "ref") || r.dig(:source, :ref)
end&.uniq
previous_refs&.first if previous_refs&.count == 1
end
sig { returns(T.nilable(String)) }
def new_ref
new_refs = dependency.requirements.filter_map do |r|
r.dig(:source, "ref") || r.dig(:source, :ref)
end.uniq
new_refs.first if new_refs.count == 1
end
sig { returns(T::Boolean) }
def ref_changed?
# We could go from multiple previous refs (nil) to a single new ref
previous_ref != new_ref
end
# TODO: Refactor me so that Composer doesn't need to be special cased
sig { returns(T::Boolean) }
def git_source?
# Special case Composer, which uses git as a source but handles tags
# internally
return false if dependency.package_manager == "composer"
requirements = dependency.requirements
sources = requirements.map { |r| r.fetch(:source) }.uniq.compact
return false if sources.empty?
sources.all? { |s| s[:type] == "git" || s["type"] == "git" }
end
sig { returns(T::Boolean) }
def major_version_upgrade?
return false unless dependency.version&.match?(/^\d/)
return false unless dependency.previous_version&.match?(/^\d/)
T.must(dependency.version).split(".").first.to_i -
T.must(dependency.previous_version).split(".").first.to_i >= 1
end
sig { returns(Dependabot::Clients::GitlabWithRetries) }
def gitlab_client
@gitlab_client ||=
T.let(
Dependabot::Clients::GitlabWithRetries.for_gitlab_dot_com(credentials: credentials),
T.nilable(Dependabot::Clients::GitlabWithRetries)
)
end
sig { returns(Dependabot::Clients::GithubWithRetries) }
def github_client
@github_client ||=
T.let(
Dependabot::Clients::GithubWithRetries.for_source(source: T.must(source), credentials: credentials),
T.nilable(Dependabot::Clients::GithubWithRetries)
)
end
sig { returns(Dependabot::Clients::Azure) }
def azure_client
@azure_client ||=
T.let(
Dependabot::Clients::Azure.for_source(source: T.must(source), credentials: credentials),
T.nilable(Dependabot::Clients::Azure)
)
end
sig { params(client_source: Dependabot::Source).returns(Dependabot::Clients::GithubWithRetries) }
def github_client_for_source(client_source)
return github_client if client_source == source
Dependabot::Clients::GithubWithRetries.for_source(source: client_source, credentials: credentials)
end
sig { returns(Dependabot::Clients::BitbucketWithRetries) }
def bitbucket_client
@bitbucket_client ||=
T.let(
Dependabot::Clients::BitbucketWithRetries.for_bitbucket_dot_org(credentials: credentials),
T.nilable(Dependabot::Clients::BitbucketWithRetries)
)
end
sig { returns(String) }
def default_bitbucket_branch
@default_bitbucket_branch ||=
T.let(
T.unsafe(bitbucket_client).fetch_default_branch(T.must(source).repo),
T.nilable(String)
)
end
sig { returns(String) }
def default_gitlab_branch
@default_gitlab_branch ||=
T.let(
gitlab_client.fetch_default_branch(T.must(source).repo),
T.nilable(String)
)
end
end
# rubocop:enable Metrics/ClassLength
end
end
end