Skip to content

Commit

Permalink
Add sorbet typing to maven requirements updater (#10778)
Browse files Browse the repository at this point in the history
  • Loading branch information
amazimbe authored Oct 14, 2024
1 parent e7f6031 commit 6cf42a1
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions maven/lib/dependabot/maven/update_checker/requirements_updater.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# typed: true
# typed: strict
# frozen_string_literal: true

#######################################################
# For more details on Maven version constraints, see: #
# https://maven.apache.org/pom.html#Dependencies #
#######################################################

require "dependabot/requirements_updater/base"
require "dependabot/maven/update_checker"
require "dependabot/maven/version"
require "dependabot/maven/requirement"
Expand All @@ -14,16 +15,33 @@ module Dependabot
module Maven
class UpdateChecker
class RequirementsUpdater
extend T::Sig
extend T::Generic

Version = type_member { { fixed: Dependabot::Maven::Version } }
Requirement = type_member { { fixed: Dependabot::Maven::Requirement } }

include Dependabot::RequirementsUpdater::Base

sig do
params(
requirements: T::Array[T::Hash[Symbol, T.untyped]],
latest_version: T.nilable(T.any(Version, String)),
source_url: String,
properties_to_update: T::Array[String]
).void
end
def initialize(requirements:, latest_version:, source_url:,
properties_to_update:)
@requirements = requirements
@source_url = source_url
@properties_to_update = properties_to_update
return unless latest_version

@latest_version = version_class.new(latest_version)
@latest_version = T.let(version_class.new(latest_version), Version)
end

sig { override.returns(T::Array[T::Hash[Symbol, T.untyped]]) }
def updated_requirements
return requirements unless latest_version

Expand All @@ -44,30 +62,42 @@ def updated_requirements

private

sig { returns(T::Array[T::Hash[Symbol, T.untyped]]) }
attr_reader :requirements

sig { returns(T.nilable(Version)) }
attr_reader :latest_version

sig { returns(String) }
attr_reader :source_url

sig { returns(T::Array[String]) }
attr_reader :properties_to_update

sig { params(req_string: String).returns(String) }
def update_requirement(req_string)
# Since range requirements are excluded this must be exact
update_exact_requirement(req_string)
end

sig { params(req_string: String).returns(String) }
def update_exact_requirement(req_string)
old_version = requirement_class.new(req_string)
.requirements.first.last
req_string.gsub(old_version.to_s, latest_version.to_s)
end

sig { override.returns(T::Class[Version]) }
def version_class
Maven::Version
end

sig { override.returns(T::Class[Requirement]) }
def requirement_class
Maven::Requirement
end

sig { returns(T::Hash[Symbol, String]) }
def updated_source
{ type: "maven_repo", url: source_url }
end
Expand Down

0 comments on commit 6cf42a1

Please sign in to comment.