Skip to content

Commit

Permalink
Strict type Dependabot::NpmAndYarn::FileUpdater (#10651)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMagee authored Oct 8, 2024
1 parent ce21ef4 commit 3c99100
Showing 1 changed file with 63 additions and 27 deletions.
90 changes: 63 additions & 27 deletions npm_and_yarn/lib/dependabot/npm_and_yarn/file_updater.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# typed: true
# typed: strict
# frozen_string_literal: true

require "dependabot/file_updaters"
Expand All @@ -20,11 +20,15 @@ class FileUpdater < Dependabot::FileUpdaters::Base
require_relative "file_updater/pnpm_lockfile_updater"

class NoChangeError < StandardError
extend T::Sig

sig { params(message: String, error_context: T::Hash[Symbol, T.untyped]).void }
def initialize(message:, error_context:)
super(message)
@error_context = error_context
end

sig { returns(T::Hash[Symbol, T.untyped]) }
def sentry_context
{ extra: @error_context }
end
Expand Down Expand Up @@ -70,8 +74,9 @@ def updated_dependency_files

private

sig { params(updated_files: T::Array[Dependabot::DependencyFile]).returns(T::Array[Dependabot::DependencyFile]) }
def vendor_updated_files(updated_files)
base_dir = updated_files.first.directory
base_dir = T.must(updated_files.first).directory
pnp_updater.updated_files(base_directory: base_dir, only_paths: [".pnp.cjs", ".pnp.data.json"]).each do |file|
updated_files << file
end
Expand All @@ -86,16 +91,20 @@ def vendor_updated_files(updated_files)
end

# Dynamically fetch the vendor cache folder from yarn
sig { returns(String) }
def vendor_cache_dir
return @vendor_cache_dir if defined?(@vendor_cache_dir)

@vendor_cache_dir = Helpers.fetch_yarnrc_yml_value("cacheFolder", "./.yarn/cache")
@vendor_cache_dir ||= T.let(
Helpers.fetch_yarnrc_yml_value("cacheFolder", "./.yarn/cache"),
T.nilable(String)
)
end

sig { returns(String) }
def install_state_path
return @install_state_path if defined?(@install_state_path)

@install_state_path = Helpers.fetch_yarnrc_yml_value("installStatePath", "./.yarn/install-state.gz")
@install_state_path ||= T.let(
Helpers.fetch_yarnrc_yml_value("installStatePath", "./.yarn/install-state.gz"),
T.nilable(String)
)
end

sig { returns(Dependabot::FileUpdaters::VendorUpdater) }
Expand Down Expand Up @@ -153,28 +162,40 @@ def error_context(updated_files:)
}
end

sig { returns(T::Array[Dependabot::DependencyFile]) }
def package_locks
@package_locks ||=
@package_locks ||= T.let(
filtered_dependency_files
.select { |f| f.name.end_with?("package-lock.json") }
.select { |f| f.name.end_with?("package-lock.json") },
T.nilable(T::Array[Dependabot::DependencyFile])
)
end

sig { returns(T::Array[Dependabot::DependencyFile]) }
def yarn_locks
@yarn_locks ||=
@yarn_locks ||= T.let(
filtered_dependency_files
.select { |f| f.name.end_with?("yarn.lock") }
.select { |f| f.name.end_with?("yarn.lock") },
T.nilable(T::Array[Dependabot::DependencyFile])
)
end

sig { returns(T::Array[Dependabot::DependencyFile]) }
def pnpm_locks
@pnpm_locks ||=
@pnpm_locks ||= T.let(
filtered_dependency_files
.select { |f| f.name.end_with?("pnpm-lock.yaml") }
.select { |f| f.name.end_with?("pnpm-lock.yaml") },
T.nilable(T::Array[Dependabot::DependencyFile])
)
end

sig { returns(T::Array[Dependabot::DependencyFile]) }
def shrinkwraps
@shrinkwraps ||=
@shrinkwraps ||= T.let(
filtered_dependency_files
.select { |f| f.name.end_with?("npm-shrinkwrap.json") }
.select { |f| f.name.end_with?("npm-shrinkwrap.json") },
T.nilable(T::Array[Dependabot::DependencyFile])
)
end

sig { returns(T::Array[Dependabot::DependencyFile]) }
Expand All @@ -186,18 +207,22 @@ def package_files
)
end

sig { params(yarn_lock: Dependabot::DependencyFile).returns(T::Boolean) }
def yarn_lock_changed?(yarn_lock)
yarn_lock.content != updated_yarn_lock_content(yarn_lock)
end

sig { params(pnpm_lock: Dependabot::DependencyFile).returns(T::Boolean) }
def pnpm_lock_changed?(pnpm_lock)
pnpm_lock.content != updated_pnpm_lock_content(pnpm_lock)
end

sig { params(package_lock: Dependabot::DependencyFile).returns(T::Boolean) }
def package_lock_changed?(package_lock)
package_lock.content != updated_lockfile_content(package_lock)
end

sig { params(shrinkwrap: Dependabot::DependencyFile).returns(T::Boolean) }
def shrinkwrap_changed?(shrinkwrap)
shrinkwrap.content != updated_lockfile_content(shrinkwrap)
end
Expand All @@ -208,10 +233,11 @@ def updated_manifest_files
updated_content = updated_package_json_content(file)
next if updated_content == file.content

updated_file(file: file, content: updated_content)
updated_file(file: file, content: T.must(updated_content))
end
end

sig { returns(T::Array[Dependabot::DependencyFile]) }
def updated_lockfiles
updated_files = []

Expand All @@ -238,7 +264,7 @@ def updated_lockfiles

updated_files << updated_file(
file: package_lock,
content: updated_lockfile_content(package_lock)
content: T.must(updated_lockfile_content(package_lock))
)
end

Expand All @@ -247,47 +273,56 @@ def updated_lockfiles

updated_files << updated_file(
file: shrinkwrap,
content: updated_lockfile_content(shrinkwrap)
content: T.must(updated_lockfile_content(shrinkwrap))
)
end

updated_files
end

sig { params(yarn_lock: Dependabot::DependencyFile).returns(String) }
def updated_yarn_lock_content(yarn_lock)
@updated_yarn_lock_content ||= {}
@updated_yarn_lock_content ||= T.let({}, T.nilable(T::Hash[String, T.nilable(String)]))
@updated_yarn_lock_content[yarn_lock.name] ||=
yarn_lockfile_updater.updated_yarn_lock_content(yarn_lock)
end

sig { params(pnpm_lock: Dependabot::DependencyFile).returns(String) }
def updated_pnpm_lock_content(pnpm_lock)
@updated_pnpm_lock_content ||= {}
@updated_pnpm_lock_content ||= T.let({}, T.nilable(T::Hash[String, T.nilable(String)]))
@updated_pnpm_lock_content[pnpm_lock.name] ||=
pnpm_lockfile_updater.updated_pnpm_lock_content(pnpm_lock)
end

sig { returns(Dependabot::NpmAndYarn::FileUpdater::YarnLockfileUpdater) }
def yarn_lockfile_updater
@yarn_lockfile_updater ||=
@yarn_lockfile_updater ||= T.let(
YarnLockfileUpdater.new(
dependencies: dependencies,
dependency_files: dependency_files,
repo_contents_path: repo_contents_path,
credentials: credentials
)
),
T.nilable(Dependabot::NpmAndYarn::FileUpdater::YarnLockfileUpdater)
)
end

sig { returns(Dependabot::NpmAndYarn::FileUpdater::PnpmLockfileUpdater) }
def pnpm_lockfile_updater
@pnpm_lockfile_updater ||=
@pnpm_lockfile_updater ||= T.let(
PnpmLockfileUpdater.new(
dependencies: dependencies,
dependency_files: dependency_files,
repo_contents_path: repo_contents_path,
credentials: credentials
)
),
T.nilable(Dependabot::NpmAndYarn::FileUpdater::PnpmLockfileUpdater)
)
end

sig { params(file: Dependabot::DependencyFile).returns(T.nilable(String)) }
def updated_lockfile_content(file)
@updated_lockfile_content ||= {}
@updated_lockfile_content ||= T.let({}, T.nilable(T::Hash[String, T.nilable(String)]))
@updated_lockfile_content[file.name] ||=
NpmLockfileUpdater.new(
lockfile: file,
Expand All @@ -297,8 +332,9 @@ def updated_lockfile_content(file)
).updated_lockfile.content
end

sig { params(file: Dependabot::DependencyFile).returns(T.nilable(String)) }
def updated_package_json_content(file)
@updated_package_json_content ||= {}
@updated_package_json_content ||= T.let({}, T.nilable(T::Hash[String, T.nilable(String)]))
@updated_package_json_content[file.name] ||=
PackageJsonUpdater.new(
package_json: file,
Expand Down

0 comments on commit 3c99100

Please sign in to comment.