dependabot-python 0.405.0 → 0.406.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2c686baaf28bbd6b011f5cf893be556e402d2d89fb3be2fb94215fc111c397b4
4
- data.tar.gz: a681902018861b463591b7a46270d5718c0617f2fde9585d711f52bfcbca1bda
3
+ metadata.gz: ba75d61f2851ea25658ab1c8da1c3c714799af05a70c1f63da51c3b31ec8099e
4
+ data.tar.gz: eefe535abf82894898346df50b2f019572b8d875ccd0d15f6d42160ac9256ef7
5
5
  SHA512:
6
- metadata.gz: 7321f79551f821b967ae706829f3b8d8595ccc48ccc5b3ca0f43bd356623771b0f3fd051091230f108132ad68bdfa364fc703d9219dab762bb62f4612736bf3b
7
- data.tar.gz: 0afa4dd039b8e32b8cc762b09884b07ff8a0544bebdcc24f976d30015521b49e088e0c47abf388a03b80cf5dadef339b0a6d30ddec8fc0c8fa79acc583b271d2
6
+ metadata.gz: acef77211045caf5fedbf42e31c081810d1e4cfe770e9b8e9286ab453b0bf0bd46ae41721ab76ffea1802506b20998bba3ccc2312fdbfeb48a1ce565e8458b80
7
+ data.tar.gz: 05b8bc48254f13e52c2718a15776ec84b5b174319c288c0960acd3ed66053632b859554e63ab6ea9efe626f6f8534fbfd577caeaf5a9d3d3c7666fe901df4b17
@@ -8,6 +8,7 @@ require "dependabot/shared_helpers"
8
8
  require "dependabot/python/file_parser/python_requirement_parser"
9
9
  require "dependabot/python/language_version_manager"
10
10
  require "dependabot/python/poetry_plugin_installer"
11
+ require "dependabot/python/subprocess_error_context"
11
12
 
12
13
  module Dependabot
13
14
  module Python
@@ -102,6 +103,11 @@ module Dependabot
102
103
  sig { params(command: String).returns(String) }
103
104
  def run_poetry_command(command)
104
105
  SharedHelpers.run_shell_command(command, fingerprint: command)
106
+ rescue SharedHelpers::HelperSubprocessFailed => e
107
+ SubprocessErrorContext.attach(
108
+ e, command: command, dependency_files: dependency_files, credentials: credentials
109
+ )
110
+ raise
105
111
  end
106
112
 
107
113
  sig { returns(LanguageVersionManager) }
@@ -408,7 +408,7 @@ module Dependabot
408
408
  def language_version_manager
409
409
  @language_version_manager ||= T.let(
410
410
  LanguageVersionManager.new(
411
- python_requirement_parser: python_requirement_parser
411
+ python_requirement_parser: python_requirement_parser, highest_for_major_version: true
412
412
  ),
413
413
  T.nilable(LanguageVersionManager)
414
414
  )
@@ -15,6 +15,11 @@ module Dependabot
15
15
  class PipfileLockDocument
16
16
  extend T::Sig
17
17
 
18
+ class PreservedMetadata < T::ImmutableStruct
19
+ const :requires, Object
20
+ const :sources, Object
21
+ end
22
+
18
23
  sig { params(file: Dependabot::DependencyFile).returns(PipfileLockDocument) }
19
24
  def self.from_file(file)
20
25
  new(data: T.cast(JSON.parse(T.must(file.content)), Object), context: file.path)
@@ -52,8 +57,46 @@ module Dependabot
52
57
  PipfileDocument::Entry.new(name: name, value: value, context: "#{@context} #{group} entry").lookup_version
53
58
  end
54
59
 
60
+ sig { params(group: String).returns(T::Array[String]) }
61
+ def dependency_names(group)
62
+ value = @data[group]
63
+ value.nil? ? [] : object_value(value, group).keys
64
+ end
65
+
66
+ sig { returns(PreservedMetadata) }
67
+ def preserved_metadata
68
+ fields = metadata
69
+ PreservedMetadata.new(requires: fields["requires"], sources: fields["sources"])
70
+ end
71
+
72
+ sig { params(metadata: PreservedMetadata, checksum: String).returns(String) }
73
+ def restored_content(metadata:, checksum:)
74
+ fields = self.metadata
75
+ hash = object_value(fields["hash"], "_meta.hash")
76
+ hash["sha256"] = checksum
77
+ fields["hash"] = hash
78
+ fields["requires"] = metadata.requires
79
+ fields["sources"] = metadata.sources
80
+
81
+ JSON.pretty_generate(@data.merge("_meta" => fields), indent: " ")
82
+ .gsub(/\{\n\s*\}/, "{}")
83
+ .gsub(/\}\z/, "}\n")
84
+ end
85
+
55
86
  private
56
87
 
88
+ sig { returns(PyprojectValueParser::ObjectHash) }
89
+ def metadata
90
+ object_value(@data["_meta"], "_meta")
91
+ end
92
+
93
+ sig { params(value: Object, field: String).returns(PyprojectValueParser::ObjectHash) }
94
+ def object_value(value, field)
95
+ PyprojectValueParser.object_hash(value, "#{@context} #{field}")
96
+ rescue TypeError => e
97
+ raise Dependabot::DependencyFileNotParseable.new(@context, e.message)
98
+ end
99
+
57
100
  sig { params(group: String).returns(T.nilable(PyprojectValueParser::ObjectHash)) }
58
101
  def section(group)
59
102
  value = @data[group]
@@ -334,7 +334,8 @@ module Dependabot
334
334
  dependency_name: T.must(dependency).name,
335
335
  old_requirement: old_req.requirement_string,
336
336
  new_requirement: "==#{T.must(dependency).version}",
337
- index_urls: @index_urls
337
+ index_urls: @index_urls,
338
+ dependency_file_name: file.name
338
339
  ).updated_content
339
340
  end
340
341
 
@@ -354,7 +355,8 @@ module Dependabot
354
355
  dependency_name: T.must(dependency).name,
355
356
  old_requirement: old_req.requirement_string,
356
357
  new_requirement: T.must(T.must(new_req).requirement_string),
357
- index_urls: @index_urls
358
+ index_urls: @index_urls,
359
+ dependency_file_name: file.name
358
360
  ).updated_content
359
361
  end
360
362
 
@@ -5,6 +5,7 @@ require "open3"
5
5
  require "dependabot/dependency"
6
6
  require "dependabot/python/requirement_parser"
7
7
  require "dependabot/python/file_parser/python_requirement_parser"
8
+ require "dependabot/python/file_parser/pipfile_lock_document"
8
9
  require "dependabot/python/file_updater"
9
10
  require "dependabot/python/language_version_manager"
10
11
  require "dependabot/shared_helpers"
@@ -60,10 +61,10 @@ module Dependabot
60
61
  @requirements_files = T.let(nil, T.nilable(T::Array[Dependabot::DependencyFile]))
61
62
  @updated_dependency_files = T.let(nil, T.nilable(T::Array[Dependabot::DependencyFile]))
62
63
  @updated_pipfile_content = T.let(nil, T.nilable(String))
63
- @parsed_lockfile = T.let(nil, T.nilable(T::Hash[String, T::Hash[String, Object]]))
64
+ @parsed_lockfile = T.let(nil, T.nilable(FileParser::PipfileLockDocument))
64
65
  @pipenv_runner = T.let(nil, T.nilable(PipenvRunner))
65
66
  @language_version_manager = T.let(nil, T.nilable(LanguageVersionManager))
66
- @sanitized_setup_file_content = T.let({}, T.untyped)
67
+ @sanitized_setup_file_content = T.let({}, T::Hash[String, String])
67
68
  @python_requirement_parser = T.let(nil, T.nilable(FileParser::PythonRequirementParser))
68
69
  end
69
70
 
@@ -128,7 +129,7 @@ module Dependabot
128
129
  def generated_requirements_files(type)
129
130
  return [] unless lockfile
130
131
 
131
- pipfile_lock_deps = parsed_lockfile[type]&.keys&.sort || []
132
+ pipfile_lock_deps = parsed_lockfile.dependency_names(type).sort
132
133
  return [] unless pipfile_lock_deps.any?
133
134
 
134
135
  regex = RequirementParser::INSTALL_REQ_WITH_REQUIREMENT
@@ -208,7 +209,10 @@ module Dependabot
208
209
 
209
210
  sig { returns(T::Hash[Symbol, String]) }
210
211
  def updated_generated_files
211
- @updated_generated_files ||=
212
+ return @updated_generated_files if @updated_generated_files
213
+
214
+ metadata = parsed_lockfile.preserved_metadata
215
+ @updated_generated_files =
212
216
  SharedHelpers.in_a_temporary_repo_directory(T.must(dependency_files.first).directory, repo_contents_path) do
213
217
  SharedHelpers.with_git_configured(credentials: credentials) do
214
218
  write_temporary_dependency_files(prepared_pipfile_content)
@@ -217,7 +221,7 @@ module Dependabot
217
221
  pipenv_runner&.run_upgrade("==#{dependency&.version}")
218
222
 
219
223
  result = { lockfile: File.read("Pipfile.lock") }
220
- result[:lockfile] = post_process_lockfile(result[:lockfile])
224
+ result[:lockfile] = post_process_lockfile(result[:lockfile], metadata: metadata)
221
225
 
222
226
  # Generate updated requirement.txt entries, if needed.
223
227
  if generate_updated_requirements_files?
@@ -232,21 +236,18 @@ module Dependabot
232
236
  end
233
237
  end
234
238
 
235
- sig { params(updated_lockfile_content: String).returns(String) }
236
- def post_process_lockfile(updated_lockfile_content)
237
- pipfile_hash = pipfile_hash_for(updated_pipfile_content.to_s)
238
- original_reqs = T.must(parsed_lockfile["_meta"])["requires"]
239
- original_source = T.must(parsed_lockfile["_meta"])["sources"]
240
-
241
- new_lockfile = updated_lockfile_content.dup
242
- new_lockfile_json = JSON.parse(new_lockfile)
243
- new_lockfile_json["_meta"]["hash"]["sha256"] = pipfile_hash
244
- new_lockfile_json["_meta"]["requires"] = original_reqs
245
- new_lockfile_json["_meta"]["sources"] = original_source
246
-
247
- JSON.pretty_generate(new_lockfile_json, indent: " ")
248
- .gsub(/\{\n\s*\}/, "{}")
249
- .gsub(/\}\z/, "}\n")
239
+ sig do
240
+ params(
241
+ updated_lockfile_content: String,
242
+ metadata: FileParser::PipfileLockDocument::PreservedMetadata
243
+ ).returns(String)
244
+ end
245
+ def post_process_lockfile(updated_lockfile_content, metadata:)
246
+ file = updated_file(file: T.must(lockfile), content: updated_lockfile_content)
247
+ FileParser::PipfileLockDocument.from_file(file).restored_content(
248
+ metadata: metadata,
249
+ checksum: pipfile_hash_for(updated_pipfile_content.to_s)
250
+ )
250
251
  end
251
252
 
252
253
  sig { returns(Integer) }
@@ -313,8 +314,8 @@ module Dependabot
313
314
 
314
315
  sig { params(file: Dependabot::DependencyFile).returns(String) }
315
316
  def sanitized_setup_file_content(file)
316
- @sanitized_setup_file_content ||= {}
317
- return @sanitized_setup_file_content[file.name] if @sanitized_setup_file_content[file.name]
317
+ cached = @sanitized_setup_file_content[file.name]
318
+ return cached if cached
318
319
 
319
320
  @sanitized_setup_file_content[file.name] =
320
321
  SetupFileSanitizer
@@ -329,35 +330,21 @@ module Dependabot
329
330
  end
330
331
  end
331
332
 
332
- sig do
333
- params(
334
- pipfile_content: String
335
- ).returns(
336
- T.nilable(
337
- T.any(
338
- T::Hash[String, T.untyped],
339
- String,
340
- T::Array[T::Hash[String, T.untyped]]
341
- )
342
- )
343
- )
344
- end
333
+ sig { params(pipfile_content: String).returns(String) }
345
334
  def pipfile_hash_for(pipfile_content)
346
335
  SharedHelpers.in_a_temporary_directory do |dir|
347
336
  File.write(File.join(dir, "Pipfile"), pipfile_content)
348
- T.cast(
349
- SharedHelpers.run_helper_subprocess(
350
- command: "pyenv exec python3 #{NativeHelpers.python_helper_path}",
351
- function: "get_pipfile_hash",
352
- args: [T.cast(dir, Pathname).to_s]
353
- ),
354
- T.nilable(
355
- T.any(
356
- T::Hash[String, T.untyped],
357
- String,
358
- T::Array[T::Hash[String, T.untyped]]
359
- )
360
- )
337
+ result = SharedHelpers.run_helper_subprocess(
338
+ command: "pyenv exec python3 #{NativeHelpers.python_helper_path}",
339
+ function: "get_pipfile_hash",
340
+ args: [dir.to_s]
341
+ )
342
+ next result if result.is_a?(String)
343
+
344
+ raise SharedHelpers::HelperSubprocessFailed.new(
345
+ message: "get_pipfile_hash result must be a string",
346
+ error_class: "TypeError",
347
+ error_context: { function: "get_pipfile_hash" }
361
348
  )
362
349
  end
363
350
  end
@@ -381,7 +368,7 @@ module Dependabot
381
368
  def language_version_manager
382
369
  @language_version_manager ||=
383
370
  LanguageVersionManager.new(
384
- python_requirement_parser: python_requirement_parser
371
+ python_requirement_parser: python_requirement_parser, highest_for_major_version: true
385
372
  )
386
373
  end
387
374
 
@@ -391,13 +378,15 @@ module Dependabot
391
378
  PipenvRunner.new(
392
379
  dependency: T.must(dependency),
393
380
  lockfile: lockfile,
394
- language_version_manager: language_version_manager
381
+ language_version_manager: language_version_manager,
382
+ dependency_files: dependency_files,
383
+ credentials: credentials
395
384
  )
396
385
  end
397
386
 
398
- sig { returns(T::Hash[String, T::Hash[String, T.untyped]]) }
387
+ sig { returns(FileParser::PipfileLockDocument) }
399
388
  def parsed_lockfile
400
- @parsed_lockfile ||= JSON.parse(T.must(lockfile&.content))
389
+ @parsed_lockfile ||= FileParser::PipfileLockDocument.from_file(T.must(lockfile))
401
390
  end
402
391
 
403
392
  sig { returns(T.nilable(Dependabot::DependencyFile)) }
@@ -16,6 +16,7 @@ require "dependabot/python/native_helpers"
16
16
  require "dependabot/python/name_normaliser"
17
17
  require "dependabot/python/poetry_plugin_installer"
18
18
  require "dependabot/python/poetry_subdependency_pin"
19
+ require "dependabot/python/subprocess_error_context"
19
20
  require "dependabot/package/release_cooldown_options"
20
21
 
21
22
  module Dependabot
@@ -336,6 +337,11 @@ module Dependabot
336
337
  sig { params(command: String, fingerprint: T.nilable(String)).returns(String) }
337
338
  def run_poetry_command(command, fingerprint: nil)
338
339
  SharedHelpers.run_shell_command(command, fingerprint: fingerprint)
340
+ rescue SharedHelpers::HelperSubprocessFailed => e
341
+ SubprocessErrorContext.attach(
342
+ e, command: command, dependency_files: dependency_files, credentials: credentials
343
+ )
344
+ raise
339
345
  end
340
346
 
341
347
  sig { params(pyproject_content: String).returns(Integer) }
@@ -141,7 +141,8 @@ module Dependabot
141
141
  old_requirement: old_req&.requirement_string,
142
142
  new_requirement: new_requirement,
143
143
  new_hash_version: dependency.version,
144
- index_urls: @index_urls
144
+ index_urls: @index_urls,
145
+ dependency_file_name: new_req.file
145
146
  ).updated_content
146
147
  end
147
148
 
@@ -3,6 +3,7 @@
3
3
 
4
4
  require "sorbet-runtime"
5
5
  require "dependabot/dependency"
6
+ require "dependabot/error_report_context"
6
7
  require "dependabot/python/requirement_parser"
7
8
  require "dependabot/python/file_updater"
8
9
  require "dependabot/shared_helpers"
@@ -26,7 +27,8 @@ module Dependabot
26
27
  old_requirement: T.nilable(String),
27
28
  new_requirement: T.nilable(String),
28
29
  new_hash_version: T.nilable(String),
29
- index_urls: T.nilable(T::Array[T.nilable(String)])
30
+ index_urls: T.nilable(T::Array[T.nilable(String)]),
31
+ dependency_file_name: T.nilable(String)
30
32
  ).void
31
33
  end
32
34
  def initialize(
@@ -35,7 +37,8 @@ module Dependabot
35
37
  old_requirement:,
36
38
  new_requirement:,
37
39
  new_hash_version: nil,
38
- index_urls: nil
40
+ index_urls: nil,
41
+ dependency_file_name: nil
39
42
  )
40
43
  @content = content
41
44
  @dependency_name = T.let(normalise(dependency_name), String)
@@ -43,6 +46,7 @@ module Dependabot
43
46
  @new_requirement = new_requirement
44
47
  @new_hash_version = new_hash_version
45
48
  @index_urls = index_urls
49
+ @dependency_file_name = dependency_file_name
46
50
  end
47
51
 
48
52
  sig { returns(String) }
@@ -56,7 +60,9 @@ module Dependabot
56
60
  updated_matched_declaration(mtch)
57
61
  end
58
62
 
59
- raise "Expected content to change!" if old_requirement != new_requirement && content == updated_content
63
+ if old_requirement != new_requirement && content == updated_content
64
+ raise replacement_error("Expected content to change!")
65
+ end
60
66
 
61
67
  updated_content
62
68
  end
@@ -78,6 +84,26 @@ module Dependabot
78
84
  sig { returns(T.nilable(String)) }
79
85
  attr_reader :new_hash_version
80
86
 
87
+ sig { returns(T.nilable(String)) }
88
+ attr_reader :dependency_file_name
89
+
90
+ # Attaches the file and requirements being replaced, which the replacer's
91
+ # own error messages don't include, to help debug them from Sentry.
92
+ sig { params(message: String).returns(RuntimeError) }
93
+ def replacement_error(message)
94
+ error = RuntimeError.new(message)
95
+ Dependabot::ErrorReportContext.attach(
96
+ error,
97
+ {
98
+ dependency_file: dependency_file_name,
99
+ dependency_name: dependency_name,
100
+ old_requirement: old_requirement,
101
+ new_requirement: new_requirement
102
+ }
103
+ )
104
+ error
105
+ end
106
+
81
107
  sig { returns(T::Boolean) }
82
108
  def update_hashes?
83
109
  !new_hash_version.nil?
@@ -238,7 +264,7 @@ module Dependabot
238
264
  .find { |m| requirements_match(m[:requirements], old_req) }
239
265
  end
240
266
 
241
- raise "Declaration not found for #{dependency_name}!" unless dec
267
+ raise replacement_error("Declaration not found for #{dependency_name}!") unless dec
242
268
 
243
269
  dec.to_s.strip
244
270
  end
@@ -10,9 +10,12 @@ module Dependabot
10
10
  class LanguageVersionManager
11
11
  extend T::Sig
12
12
 
13
- sig { params(python_requirement_parser: T.untyped).void }
14
- def initialize(python_requirement_parser:)
13
+ # Pipenv builds its virtualenv on the newest Python that matches a major-only `python_version` (such as "3"),
14
+ # so Pipenv callers set `highest_for_major_version` to run Pipenv on that same Python.
15
+ sig { params(python_requirement_parser: T.untyped, highest_for_major_version: T::Boolean).void }
16
+ def initialize(python_requirement_parser:, highest_for_major_version: false)
15
17
  @python_requirement_parser = python_requirement_parser
18
+ @highest_for_major_version = highest_for_major_version
16
19
  end
17
20
 
18
21
  sig { returns(T.nilable(String)) }
@@ -105,7 +108,9 @@ module Dependabot
105
108
  # Try to match one of our pre-installed Python versions
106
109
  requirement = T.must(Python::Requirement.requirements_array(requirement_string).first)
107
110
 
108
- version = Language::PRE_INSTALLED_PYTHON_VERSIONS.find { |v| requirement.satisfied_by?(v) }
111
+ candidates = Language::PRE_INSTALLED_PYTHON_VERSIONS
112
+ candidates = candidates.reverse if highest_for_major_version?
113
+ version = candidates.find { |v| requirement.satisfied_by?(v) }
109
114
  return version.to_s if version
110
115
 
111
116
  # Otherwise we have to raise an error
@@ -113,6 +118,11 @@ module Dependabot
113
118
  raise ToolVersionNotSupported.new("Python", python_requirement_string, supported_versions)
114
119
  end
115
120
 
121
+ sig { returns(T::Boolean) }
122
+ def highest_for_major_version?
123
+ @highest_for_major_version && user_specified_python_version.to_s.match?(/\A\d+\z/)
124
+ end
125
+
116
126
  sig { returns(T.untyped) }
117
127
  def user_specified_python_version
118
128
  @python_requirement_parser.user_specified_requirements.first
@@ -77,12 +77,30 @@ module Dependabot
77
77
 
78
78
  Dependabot::Package::PackageDetails.new(
79
79
  dependency: dependency,
80
- releases: package_releases.reverse
80
+ releases: with_shared_release_dates(package_releases).reverse
81
81
  )
82
82
  end
83
83
 
84
84
  private
85
85
 
86
+ # An undated copy of a version (e.g. from a PEP 503 HTML-only index) would otherwise bypass cooldown.
87
+ sig do
88
+ params(releases: T::Array[Dependabot::Package::PackageRelease])
89
+ .returns(T::Array[Dependabot::Package::PackageRelease])
90
+ end
91
+ def with_shared_release_dates(releases)
92
+ # Keyed by string: Python::Version#hash ignores pre/post/dev/local, so 2.0.0rc1 and 2.0.0 collide.
93
+ earliest = releases.each_with_object(T.let({}, T::Hash[String, Time])) do |release, dates|
94
+ released_at = release.released_at
95
+ next unless released_at
96
+
97
+ known = dates[release.version.to_s]
98
+ dates[release.version.to_s] = released_at if known.nil? || released_at < known
99
+ end
100
+
101
+ releases.each { |release| release.released_at ||= earliest[release.version.to_s] }
102
+ end
103
+
86
104
  sig do
87
105
  params(index_url: String)
88
106
  .returns(T.nilable(T::Array[Dependabot::Package::PackageRelease]))
@@ -3,6 +3,7 @@
3
3
 
4
4
  require "dependabot/shared_helpers"
5
5
  require "dependabot/python/file_parser"
6
+ require "dependabot/python/subprocess_error_context"
6
7
  require "json"
7
8
  require "sorbet-runtime"
8
9
 
@@ -17,16 +18,25 @@ module Dependabot
17
18
  lockfile: T.nilable(Dependabot::DependencyFile),
18
19
  language_version_manager: LanguageVersionManager,
19
20
  dependency_files: T.nilable(T::Array[Dependabot::DependencyFile]),
20
- repo_contents_path: T.nilable(String)
21
+ repo_contents_path: T.nilable(String),
22
+ credentials: T::Array[Dependabot::Credential]
21
23
  )
22
24
  .void
23
25
  end
24
- def initialize(dependency:, lockfile:, language_version_manager:, dependency_files: nil, repo_contents_path: nil)
26
+ def initialize(
27
+ dependency:,
28
+ lockfile:,
29
+ language_version_manager:,
30
+ dependency_files: nil,
31
+ repo_contents_path: nil,
32
+ credentials: []
33
+ )
25
34
  @dependency = dependency
26
35
  @lockfile = lockfile
27
36
  @language_version_manager = language_version_manager
28
37
  @dependency_files = dependency_files
29
38
  @repo_contents_path = repo_contents_path
39
+ @credentials = credentials
30
40
  end
31
41
 
32
42
  sig { params(constraint: T.nilable(String)).returns(String) }
@@ -91,6 +101,9 @@ module Dependabot
91
101
  sig { returns(T.nilable(String)) }
92
102
  attr_reader :repo_contents_path
93
103
 
104
+ sig { returns(T::Array[Dependabot::Credential]) }
105
+ attr_reader :credentials
106
+
94
107
  sig { returns(Dependabot::Dependency) }
95
108
  def current_dependency
96
109
  T.must(dependency)
@@ -149,6 +162,14 @@ module Dependabot
149
162
  sig { params(command: String, fingerprint: T.nilable(String)).returns(String) }
150
163
  def run_command(command, fingerprint: nil)
151
164
  SharedHelpers.run_shell_command(command, env: pipenv_env_variables, fingerprint: fingerprint)
165
+ rescue SharedHelpers::HelperSubprocessFailed => e
166
+ SubprocessErrorContext.attach(
167
+ e,
168
+ command: command,
169
+ dependency_files: dependency_files || [lockfile].compact,
170
+ credentials: credentials
171
+ )
172
+ raise
152
173
  end
153
174
 
154
175
  sig { returns(T.nilable(String)) }
@@ -156,17 +177,25 @@ module Dependabot
156
177
  if current_dependency.requirements.any?
157
178
  T.must(T.must(current_dependency.requirements.first).groups).first&.to_s
158
179
  else
159
- parsed_lockfile = JSON.parse(T.must(T.must(lockfile).content))
160
- Python::FileParser::DEPENDENCY_GROUP_KEYS.each do |keys|
161
- section = keys.fetch(:lockfile)
162
- section_data = parsed_lockfile_section(parsed_lockfile, section)
163
- next unless section_data
164
-
165
- return section if section_data.key?(dependency_name)
166
- end
180
+ transitive_lockfile_section
181
+ end
182
+ end
167
183
 
168
- nil
184
+ sig { returns(T.nilable(String)) }
185
+ def transitive_lockfile_section
186
+ parsed_lockfile = JSON.parse(T.must(T.must(lockfile).content))
187
+ sections = Python::FileParser::DEPENDENCY_GROUP_KEYS.filter_map do |keys|
188
+ section = keys.fetch(:lockfile)
189
+ section_data = parsed_lockfile_section(parsed_lockfile, section)
190
+ next unless section_data
191
+
192
+ section if section_data.key?(dependency_name)
169
193
  end
194
+
195
+ original_version = current_dependency.previous_version || current_dependency.version
196
+ sections.find do |section|
197
+ parsed_lockfile.dig(section, dependency_name, "version")&.gsub(/^===?/, "") == original_version
198
+ end || sections.first
170
199
  end
171
200
 
172
201
  sig do
@@ -0,0 +1,114 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "json"
5
+ require "sorbet-runtime"
6
+ require "toml-rb"
7
+
8
+ require "dependabot/credential"
9
+ require "dependabot/dependency_file"
10
+ require "dependabot/error_report_context"
11
+ require "dependabot/shared_helpers"
12
+
13
+ module Dependabot
14
+ module Python
15
+ # Attaches debug-only `error-context` to failed Poetry and Pipenv commands. Only an allowlisted
16
+ # command name and the index hosts are reported: command lines, index URLs and helper output can
17
+ # contain credentials or private package names.
18
+ module SubprocessErrorContext
19
+ extend T::Sig
20
+
21
+ PYPI_HOST = "pypi.org"
22
+
23
+ SUBCOMMANDS = T.let(
24
+ {
25
+ "poetry" => %w(add config install lock show update),
26
+ "pipenv" => %w(graph install lock requirements sync update upgrade),
27
+ "pyenv" => %w(install local)
28
+ }.freeze,
29
+ T::Hash[String, T::Array[String]]
30
+ )
31
+
32
+ sig do
33
+ params(
34
+ error: SharedHelpers::HelperSubprocessFailed,
35
+ command: String,
36
+ dependency_files: T::Array[Dependabot::DependencyFile],
37
+ credentials: T::Array[Dependabot::Credential]
38
+ ).void
39
+ end
40
+ def self.attach(error, command:, dependency_files:, credentials:)
41
+ ErrorReportContext.attach(
42
+ error,
43
+ {
44
+ command_name: command_name(command),
45
+ index_hosts: index_hosts(dependency_files, credentials)
46
+ }
47
+ )
48
+ rescue StandardError => e
49
+ # Building diagnostics must never mask the original failure
50
+ Dependabot.logger.debug("Failed to build error context: #{e.class}")
51
+ end
52
+
53
+ sig { params(command: String).returns(T.nilable(String)) }
54
+ def self.command_name(command)
55
+ words = command.split
56
+ words.shift(2) if words.first(2) == %w(pyenv exec)
57
+ tool = words.first
58
+ subcommands = tool && SUBCOMMANDS[tool]
59
+ return unless subcommands
60
+
61
+ subcommand = words[1]
62
+ subcommand && subcommands.include?(subcommand) ? "#{tool} #{subcommand}" : tool
63
+ end
64
+
65
+ sig do
66
+ params(
67
+ dependency_files: T::Array[Dependabot::DependencyFile],
68
+ credentials: T::Array[Dependabot::Credential]
69
+ ).returns(T::Array[String])
70
+ end
71
+ def self.index_hosts(dependency_files, credentials)
72
+ urls = credentials.select { |cred| cred["type"] == "python_index" }.filter_map { |cred| cred["index-url"] }
73
+ urls += dependency_files.flat_map { |file| source_urls(file) }
74
+
75
+ hosts = urls.filter_map { |url| host(url) }.uniq
76
+ hosts.empty? ? [PYPI_HOST] : hosts
77
+ end
78
+
79
+ sig { params(file: Dependabot::DependencyFile).returns(T::Array[String]) }
80
+ def self.source_urls(file)
81
+ content = file.content
82
+ return [] unless content
83
+
84
+ sources = case file.name
85
+ when "Pipfile" then TomlRB.parse(content)["source"]
86
+ when "Pipfile.lock" then JSON.parse(content).dig("_meta", "sources")
87
+ when "pyproject.toml" then TomlRB.parse(content).dig("tool", "poetry", "source")
88
+ end
89
+ return [] unless sources.is_a?(Array)
90
+
91
+ sources.filter_map { |source| source["url"] if source.is_a?(Hash) && source["url"].is_a?(String) }
92
+ rescue TomlRB::Error, JSON::ParserError
93
+ []
94
+ end
95
+
96
+ # Userinfo is stripped from the authority first so that URLs with templated credentials
97
+ # (e.g. `https://${USER}:${TOKEN}@host/simple`) still yield their host. The authority
98
+ # ends at the first `/`, `?` or `#`, so query strings and fragments are never read.
99
+ sig { params(url: String).returns(T.nilable(String)) }
100
+ def self.host(url)
101
+ url = url.strip
102
+ scheme_end = url.index("://")
103
+ return unless scheme_end
104
+
105
+ authority_start = scheme_end + 3
106
+ authority_end = %w(/ ? #).filter_map { |char| url.index(char, authority_start) }.min || url.length
107
+ authority = T.must(url[authority_start...authority_end]).rpartition("@").last
108
+ host = ErrorReportContext.host_for("#{url[0...authority_start]}#{authority}#{url[authority_end..]}")
109
+ host == ErrorReportContext::REDACTED ? nil : host
110
+ end
111
+ private_class_method :command_name, :index_hosts, :source_urls, :host
112
+ end
113
+ end
114
+ end
@@ -408,7 +408,8 @@ module Dependabot
408
408
  content: T.must(file.content),
409
409
  dependency_name: dependency.name,
410
410
  old_requirement: req.requirement_string,
411
- new_requirement: updated_req
411
+ new_requirement: updated_req,
412
+ dependency_file_name: file.name
412
413
  ).updated_content
413
414
  end
414
415
 
@@ -369,7 +369,7 @@ module Dependabot
369
369
  def language_version_manager
370
370
  @language_version_manager ||= T.let(
371
371
  LanguageVersionManager.new(
372
- python_requirement_parser: python_requirement_parser
372
+ python_requirement_parser: python_requirement_parser, highest_for_major_version: true
373
373
  ),
374
374
  T.nilable(Dependabot::Python::LanguageVersionManager)
375
375
  )
@@ -381,7 +381,9 @@ module Dependabot
381
381
  PipenvRunner.new(
382
382
  dependency: dependency,
383
383
  lockfile: lockfile,
384
- language_version_manager: language_version_manager
384
+ language_version_manager: language_version_manager,
385
+ dependency_files: dependency_files,
386
+ credentials: credentials
385
387
  ),
386
388
  T.nilable(Dependabot::Python::PipenvRunner)
387
389
  )
@@ -20,6 +20,7 @@ require "dependabot/python/authed_url_builder"
20
20
  require "dependabot/python/name_normaliser"
21
21
  require "dependabot/python/poetry_plugin_installer"
22
22
  require "dependabot/python/poetry_subdependency_pin"
23
+ require "dependabot/python/subprocess_error_context"
23
24
 
24
25
  module Dependabot
25
26
  module Python
@@ -400,6 +401,11 @@ module Dependabot
400
401
  sig { params(command: String, fingerprint: T.nilable(String)).returns(String) }
401
402
  def run_poetry_command(command, fingerprint: nil)
402
403
  SharedHelpers.run_shell_command(command, fingerprint: fingerprint)
404
+ rescue SharedHelpers::HelperSubprocessFailed => e
405
+ SubprocessErrorContext.attach(
406
+ e, command: command, dependency_files: dependency_files, credentials: credentials
407
+ )
408
+ raise
403
409
  end
404
410
 
405
411
  sig { params(name: String).returns(String) }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dependabot-python
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.405.0
4
+ version: 0.406.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dependabot
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - '='
17
17
  - !ruby/object:Gem::Version
18
- version: 0.405.0
18
+ version: 0.406.0
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - '='
24
24
  - !ruby/object:Gem::Version
25
- version: 0.405.0
25
+ version: 0.406.0
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: debug
28
28
  requirement: !ruby/object:Gem::Requirement
@@ -323,6 +323,7 @@ files:
323
323
  - lib/dependabot/python/requirement_parser.rb
324
324
  - lib/dependabot/python/requirement_references.rb
325
325
  - lib/dependabot/python/shared_file_fetcher.rb
326
+ - lib/dependabot/python/subprocess_error_context.rb
326
327
  - lib/dependabot/python/update_checker.rb
327
328
  - lib/dependabot/python/update_checker/latest_version_finder.rb
328
329
  - lib/dependabot/python/update_checker/pip_compile_version_resolver.rb
@@ -336,7 +337,7 @@ licenses:
336
337
  - MIT
337
338
  metadata:
338
339
  bug_tracker_uri: https://github.com/dependabot/dependabot-core/issues
339
- changelog_uri: https://github.com/dependabot/dependabot-core/releases/tag/v0.405.0
340
+ changelog_uri: https://github.com/dependabot/dependabot-core/releases/tag/v0.406.0
340
341
  rdoc_options: []
341
342
  require_paths:
342
343
  - lib