dependabot-npm_and_yarn 0.224.0 → 0.226.0

Sign up to get free protection for your applications and to get access to all the features.
data/helpers/package.json CHANGED
@@ -14,14 +14,14 @@
14
14
  "detect-indent": "^6.1.0",
15
15
  "nock": "^13.3.2",
16
16
  "npm": "6.14.18",
17
- "@pnpm/lockfile-file": "^8.1.1",
17
+ "@pnpm/lockfile-file": "^8.1.2",
18
18
  "@pnpm/dependency-path": "^2.1.1",
19
19
  "semver": "^7.4.0"
20
20
  },
21
21
  "devDependencies": {
22
- "eslint": "^8.39.0",
23
- "eslint-config-prettier": "^8.8.0",
24
- "jest": "^29.5.0",
25
- "prettier": "^2.8.8"
22
+ "eslint": "^8.46.0",
23
+ "eslint-config-prettier": "^9.0.0",
24
+ "jest": "^29.6.2",
25
+ "prettier": "^3.0.1"
26
26
  }
27
27
  }
@@ -217,8 +217,9 @@ module Dependabot
217
217
  end
218
218
 
219
219
  def npmrc
220
- @npmrc ||= fetch_file_if_present(".npmrc")&.
221
- tap { |f| f.support_file = true }
220
+ return @npmrc if defined?(@npmrc)
221
+
222
+ @npmrc = fetch_support_file(".npmrc")
222
223
 
223
224
  return @npmrc if @npmrc || directory == "/"
224
225
 
@@ -236,8 +237,9 @@ module Dependabot
236
237
  end
237
238
 
238
239
  def yarnrc
239
- @yarnrc ||= fetch_file_if_present(".yarnrc")&.
240
- tap { |f| f.support_file = true }
240
+ return @yarnrc if defined?(@yarnrc)
241
+
242
+ @yarnrc = fetch_support_file(".yarnrc")
241
243
 
242
244
  return @yarnrc if @yarnrc || directory == "/"
243
245
 
@@ -255,18 +257,21 @@ module Dependabot
255
257
  end
256
258
 
257
259
  def yarnrc_yml
258
- @yarnrc_yml ||= fetch_file_if_present(".yarnrc.yml")&.
259
- tap { |f| f.support_file = true }
260
+ return @yarnrc_yml if defined?(@yarnrc_yml)
261
+
262
+ @yarnrc_yml = fetch_support_file(".yarnrc.yml")
260
263
  end
261
264
 
262
265
  def pnpm_workspace_yaml
263
- @pnpm_workspace_yaml ||= fetch_file_if_present("pnpm-workspace.yaml")&.
264
- tap { |f| f.support_file = true }
266
+ return @pnpm_workspace_yaml if defined?(@pnpm_workspace_yaml)
267
+
268
+ @pnpm_workspace_yaml = fetch_support_file("pnpm-workspace.yaml")
265
269
  end
266
270
 
267
271
  def lerna_json
268
- @lerna_json ||= fetch_file_if_present("lerna.json")&.
269
- tap { |f| f.support_file = true }
272
+ return @lerna_json if defined?(@lerna_json)
273
+
274
+ @lerna_json = fetch_support_file("lerna.json")
270
275
  end
271
276
 
272
277
  def workspace_package_jsons
@@ -513,16 +513,18 @@ module Dependabot
513
513
  file.content
514
514
  end
515
515
 
516
+ package_json_preparer = package_json_preparer(updated_content)
517
+
516
518
  # TODO: Figure out if we need to lock git deps for npm 7 and can
517
519
  # start deprecating this hornets nest
518
520
  #
519
521
  # NOTE: When updating a package-lock.json we have to manually lock
520
522
  # all git dependencies, otherwise npm will (unhelpfully) update them
521
523
  updated_content = lock_git_deps(updated_content)
522
- updated_content = replace_ssh_sources(updated_content)
524
+ updated_content = package_json_preparer.replace_ssh_sources(updated_content)
523
525
  updated_content = lock_deps_with_latest_reqs(updated_content)
524
526
 
525
- updated_content = sanitized_package_json_content(updated_content)
527
+ updated_content = package_json_preparer.remove_invalid_characters(updated_content)
526
528
 
527
529
  File.write(file.name, updated_content)
528
530
  end
@@ -614,35 +616,12 @@ module Dependabot
614
616
  JSON.pretty_generate(json, indent: indent)
615
617
  end
616
618
 
617
- def replace_ssh_sources(content)
618
- updated_content = content
619
-
620
- git_ssh_requirements_to_swap.each do |req|
621
- new_req = req.gsub(%r{git\+ssh://git@(.*?)[:/]}, 'https://\1/')
622
- updated_content = updated_content.gsub(req, new_req)
623
- end
624
-
625
- updated_content
626
- end
627
-
628
619
  def git_ssh_requirements_to_swap
629
620
  return @git_ssh_requirements_to_swap if @git_ssh_requirements_to_swap
630
621
 
631
- @git_ssh_requirements_to_swap = []
632
-
633
- package_files.each do |file|
634
- NpmAndYarn::FileParser::DEPENDENCY_TYPES.each do |t|
635
- JSON.parse(file.content).fetch(t, {}).each do |_, requirement|
636
- next unless requirement.is_a?(String)
637
- next unless requirement.start_with?("git+ssh:")
638
-
639
- req = requirement.split("#").first
640
- @git_ssh_requirements_to_swap << req
641
- end
642
- end
622
+ @git_ssh_requirements_to_swap = package_files.flat_map do |file|
623
+ package_json_preparer(file.content).swapped_ssh_requirements
643
624
  end
644
-
645
- @git_ssh_requirements_to_swap
646
625
  end
647
626
 
648
627
  def post_process_npm_lockfile(updated_lockfile_content)
@@ -841,6 +820,14 @@ module Dependabot
841
820
  ).updated_package_json.content
842
821
  end
843
822
 
823
+ def package_json_preparer(content)
824
+ @package_json_preparer ||= {}
825
+ @package_json_preparer[content] ||=
826
+ PackageJsonPreparer.new(
827
+ package_json_content: content
828
+ )
829
+ end
830
+
844
831
  def npmrc_disables_lockfile?
845
832
  npmrc_content.match?(/^package-lock\s*=\s*false/)
846
833
  end
@@ -851,13 +838,6 @@ module Dependabot
851
838
  @npm8 = Dependabot::NpmAndYarn::Helpers.npm_version(lockfile.content) == "npm8"
852
839
  end
853
840
 
854
- def sanitized_package_json_content(content)
855
- content.
856
- gsub(/\{\{[^\}]*?\}\}/, "something"). # {{ nm }} syntax not allowed
857
- gsub(/(?<!\\)\\ /, " "). # escaped whitespace not allowed
858
- gsub(%r{^\s*//.*}, " ") # comments are not allowed
859
- end
860
-
861
841
  def sanitize_package_name(package_name)
862
842
  package_name.gsub("%2f", "/").gsub("%2F", "/")
863
843
  end
@@ -48,7 +48,7 @@ module Dependabot
48
48
 
49
49
  paths_array.each { |path| path.gsub!(%r{^\./}, "") }
50
50
 
51
- json.to_json
51
+ JSON.pretty_generate(json)
52
52
  end
53
53
 
54
54
  def remove_invalid_characters(content)
@@ -17,6 +17,7 @@ module Dependabot
17
17
  class YarnLockfileUpdater
18
18
  require_relative "npmrc_builder"
19
19
  require_relative "package_json_updater"
20
+ require_relative "package_json_preparer"
20
21
 
21
22
  def initialize(dependencies:, dependency_files:, repo_contents_path:, credentials:)
22
23
  @dependencies = dependencies
@@ -357,13 +358,7 @@ module Dependabot
357
358
  file.content
358
359
  end
359
360
 
360
- updated_content = replace_ssh_sources(updated_content)
361
-
362
- # A bug prevents Yarn recognising that a directory is part of a
363
- # workspace if it is specified with a `./` prefix.
364
- updated_content = remove_workspace_path_prefixes(updated_content)
365
-
366
- updated_content = sanitized_package_json_content(updated_content)
361
+ updated_content = package_json_preparer(updated_content).prepared_content
367
362
  File.write(file.name, updated_content)
368
363
  end
369
364
 
@@ -380,9 +375,10 @@ module Dependabot
380
375
  dirs.pop
381
376
  while dirs.any?
382
377
  npmrc = dirs.join("/") + "/.npmrc"
383
- break unless File.exist?(npmrc)
384
-
385
- File.write(npmrc, File.read(npmrc).gsub(/\$\{.*\}/, ""))
378
+ if File.exist?(npmrc)
379
+ # If the .npmrc file exists, clean it
380
+ File.write(npmrc, File.read(npmrc).gsub(/\$\{.*?\}/, ""))
381
+ end
386
382
  dirs.pop
387
383
  end
388
384
  end
@@ -394,60 +390,12 @@ module Dependabot
394
390
  end
395
391
  end
396
392
 
397
- def replace_ssh_sources(content)
398
- updated_content = content
399
-
400
- git_ssh_requirements_to_swap.each do |req|
401
- new_req = req.gsub(%r{git\+ssh://git@(.*?)[:/]}, 'https://\1/')
402
- updated_content = updated_content.gsub(req, new_req)
403
- end
404
-
405
- updated_content
406
- end
407
-
408
- def remove_workspace_path_prefixes(content)
409
- json = JSON.parse(content)
410
- return content unless json.key?("workspaces")
411
-
412
- workspace_object = json.fetch("workspaces")
413
- paths_array =
414
- if workspace_object.is_a?(Hash)
415
- workspace_object.values_at("packages", "nohoist").
416
- flatten.compact
417
- elsif workspace_object.is_a?(Array) then workspace_object
418
- else
419
- raise "Unexpected workspace object"
420
- end
421
-
422
- paths_array.each { |path| path.gsub!(%r{^\./}, "") }
423
-
424
- json.to_json
425
- end
426
-
427
393
  def git_ssh_requirements_to_swap
428
394
  return @git_ssh_requirements_to_swap if @git_ssh_requirements_to_swap
429
395
 
430
- git_dependencies =
431
- dependencies.
432
- select do |dep|
433
- dep.requirements.any? { |r| r.dig(:source, :type) == "git" }
434
- end
435
-
436
- @git_ssh_requirements_to_swap = []
437
-
438
- package_files.each do |file|
439
- NpmAndYarn::FileParser::DEPENDENCY_TYPES.each do |t|
440
- JSON.parse(file.content).fetch(t, {}).each do |nm, requirement|
441
- next unless git_dependencies.map(&:name).include?(nm)
442
- next unless requirement.start_with?("git+ssh:")
443
-
444
- req = requirement.split("#").first
445
- @git_ssh_requirements_to_swap << req
446
- end
447
- end
396
+ @git_ssh_requirements_to_swap = package_files.flat_map do |file|
397
+ package_json_preparer(file.content).swapped_ssh_requirements
448
398
  end
449
-
450
- @git_ssh_requirements_to_swap
451
399
  end
452
400
 
453
401
  def post_process_yarn_lockfile(lockfile_content)
@@ -537,12 +485,18 @@ module Dependabot
537
485
  end
538
486
 
539
487
  def updated_package_json_content(file)
540
- @updated_package_json_content ||= {}
541
- @updated_package_json_content[file.name] ||=
542
- PackageJsonUpdater.new(
543
- package_json: file,
544
- dependencies: top_level_dependencies
545
- ).updated_package_json.content
488
+ PackageJsonUpdater.new(
489
+ package_json: file,
490
+ dependencies: top_level_dependencies
491
+ ).updated_package_json.content
492
+ end
493
+
494
+ def package_json_preparer(content)
495
+ @package_json_preparer ||= {}
496
+ @package_json_preparer[content] ||=
497
+ PackageJsonPreparer.new(
498
+ package_json_content: content
499
+ )
546
500
  end
547
501
 
548
502
  def npmrc_disables_lockfile?
@@ -574,13 +528,6 @@ module Dependabot
574
528
  ).yarnrc_content
575
529
  end
576
530
 
577
- def sanitized_package_json_content(content)
578
- content.
579
- gsub(/\{\{[^\}]*?\}\}/, "something"). # {{ nm }} syntax not allowed
580
- gsub(/(?<!\\)\\ /, " "). # escaped whitespace not allowed
581
- gsub(%r{^\s*//.*}, " ") # comments are not allowed
582
- end
583
-
584
531
  def sanitize_package_name(package_name)
585
532
  package_name.gsub("%2f", "/").gsub("%2F", "/")
586
533
  end
@@ -261,25 +261,25 @@ module Dependabot
261
261
  end
262
262
 
263
263
  def npm_details
264
- return @npm_details if @npm_details_lookup_attempted
264
+ return @npm_details if defined?(@npm_details)
265
265
 
266
- @npm_details_lookup_attempted = true
267
- @npm_details ||=
268
- begin
269
- npm_response = fetch_npm_response
270
-
271
- check_npm_response(npm_response)
272
- JSON.parse(npm_response.body)
273
- rescue JSON::ParserError,
274
- Excon::Error::Timeout,
275
- Excon::Error::Socket,
276
- RegistryError => e
277
- if git_dependency?
278
- nil
279
- else
280
- raise_npm_details_error(e)
281
- end
282
- end
266
+ @npm_details = fetch_npm_details
267
+ end
268
+
269
+ def fetch_npm_details
270
+ npm_response = fetch_npm_response
271
+
272
+ check_npm_response(npm_response)
273
+ JSON.parse(npm_response.body)
274
+ rescue JSON::ParserError,
275
+ Excon::Error::Timeout,
276
+ Excon::Error::Socket,
277
+ RegistryError => e
278
+ if git_dependency?
279
+ nil
280
+ else
281
+ raise_npm_details_error(e)
282
+ end
283
283
  end
284
284
 
285
285
  def fetch_npm_response
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dependabot-npm_and_yarn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.224.0
4
+ version: 0.226.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dependabot
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-27 00:00:00.000000000 Z
11
+ date: 2023-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dependabot-common
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 0.224.0
19
+ version: 0.226.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 0.224.0
26
+ version: 0.226.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: debug
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -128,14 +128,14 @@ dependencies:
128
128
  requirements:
129
129
  - - "~>"
130
130
  - !ruby/object:Gem::Version
131
- version: 1.17.1
131
+ version: 1.18.0
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
136
  - - "~>"
137
137
  - !ruby/object:Gem::Version
138
- version: 1.17.1
138
+ version: 1.18.0
139
139
  - !ruby/object:Gem::Dependency
140
140
  name: stackprof
141
141
  requirement: !ruby/object:Gem::Requirement
@@ -280,7 +280,7 @@ licenses:
280
280
  - Nonstandard
281
281
  metadata:
282
282
  bug_tracker_uri: https://github.com/dependabot/dependabot-core/issues
283
- changelog_uri: https://github.com/dependabot/dependabot-core/releases/tag/v0.224.0
283
+ changelog_uri: https://github.com/dependabot/dependabot-core/releases/tag/v0.226.0
284
284
  post_install_message:
285
285
  rdoc_options: []
286
286
  require_paths: