class Dependabot::Maven::FileUpdater::DeclarationFinder

Constants

DECLARATION_REGEX

Attributes

declaring_requirement[R]
dependency[R]
dependency_files[R]

Public Class Methods

new(dependency:, dependency_files:, declaring_requirement:) click to toggle source
# File lib/dependabot/maven/file_updater/declaration_finder.rb, line 18
def initialize(dependency:, dependency_files:, declaring_requirement:)
  @dependency            = dependency
  @dependency_files      = dependency_files
  @declaring_requirement = declaring_requirement
end

Public Instance Methods

declaration_nodes() click to toggle source
# File lib/dependabot/maven/file_updater/declaration_finder.rb, line 28
def declaration_nodes
  declaration_strings.map do |declaration_string|
    Nokogiri::XML(declaration_string)
  end
end
declaration_strings() click to toggle source
# File lib/dependabot/maven/file_updater/declaration_finder.rb, line 24
def declaration_strings
  @declaration_strings ||= fetch_pom_declaration_strings
end

Private Instance Methods

declaring_pom() click to toggle source
# File lib/dependabot/maven/file_updater/declaration_finder.rb, line 36
def declaring_pom
  filename = declaring_requirement.fetch(:file)
  declaring_pom = dependency_files.find { |f| f.name == filename }
  return declaring_pom if declaring_pom

  raise "No pom found with name #{filename}!"
end
declaring_requirement_matches?(node) click to toggle source
# File lib/dependabot/maven/file_updater/declaration_finder.rb, line 86
def declaring_requirement_matches?(node)
  node_requirement = node.at_css("version")&.content&.strip

  if declaring_requirement.dig(:metadata, :property_name)
    return false unless node_requirement

    property_name =
      node_requirement.
      match(Maven::FileParser::PROPERTY_REGEX)&.
      named_captures&.
      fetch("property")

    property_name == declaring_requirement[:metadata][:property_name]
  else
    node_requirement == declaring_requirement.fetch(:requirement)
  end
end
deep_find_declarations(string) click to toggle source
# File lib/dependabot/maven/file_updater/declaration_finder.rb, line 80
def deep_find_declarations(string)
  string.scan(DECLARATION_REGEX).flat_map do |matching_node|
    [matching_node, *deep_find_declarations(matching_node[1..-1])]
  end
end
dependency_name() click to toggle source
# File lib/dependabot/maven/file_updater/declaration_finder.rb, line 44
def dependency_name
  dependency.name
end
dependency_scope(dependency_node) click to toggle source
# File lib/dependabot/maven/file_updater/declaration_finder.rb, line 126
def dependency_scope(dependency_node)
  return "compile" unless dependency_node.at_xpath("./*/scope")

  scope_content = dependency_node.at_xpath("./*/scope").content.strip
  scope_content = evaluated_value(scope_content)

  scope_content.empty? ? "compile" : scope_content
end
evaluated_value(value) click to toggle source
# File lib/dependabot/maven/file_updater/declaration_finder.rb, line 135
def evaluated_value(value)
  return value unless value.match?(Maven::FileParser::PROPERTY_REGEX)

  property_name =
    value.match(Maven::FileParser::PROPERTY_REGEX).
    named_captures.fetch("property")

  property_value =
    property_value_finder.
    property_details(
      property_name: property_name,
      callsite_pom: declaring_pom
    )&.fetch(:value)

  return value unless property_value

  value.gsub(
    value.match(Maven::FileParser::PROPERTY_REGEX).to_s,
    property_value
  )
end
fetch_pom_declaration_strings() click to toggle source
# File lib/dependabot/maven/file_updater/declaration_finder.rb, line 48
def fetch_pom_declaration_strings
  deep_find_declarations(declaring_pom.content).select do |nd|
    node = Nokogiri::XML(nd)
    node.remove_namespaces!
    next false unless node_group_id(node)
    next false unless node.at_xpath("./*/artifactId")

    node_name = [
      node_group_id(node),
      evaluated_value(node.at_xpath("./*/artifactId").content.strip)
    ].compact.join(":")

    if node.at_xpath("./*/classifier")
      node_name += ":#{evaluated_value(node.at_xpath('./*/classifier').
        content.strip)}"
    end

    next false unless node_name == dependency_name
    next false unless packaging_type_matches?(node)
    next false unless scope_matches?(node)

    declaring_requirement_matches?(node)
  end
end
node_group_id(node) click to toggle source
# File lib/dependabot/maven/file_updater/declaration_finder.rb, line 73
def node_group_id(node)
  return unless node.at_xpath("./*/groupId") || node.at_xpath("./plugin")
  return "org.apache.maven.plugins" unless node.at_xpath("./*/groupId")

  evaluated_value(node.at_xpath("./*/groupId").content.strip)
end
packaging_type(dependency_node) click to toggle source
# File lib/dependabot/maven/file_updater/declaration_finder.rb, line 116
def packaging_type(dependency_node)
  return "pom" if dependency_node.child.node_name == "parent"
  return "jar" unless dependency_node.at_xpath("./*/type")

  packaging_type_content = dependency_node.at_xpath("./*/type").
                           content.strip

  evaluated_value(packaging_type_content)
end
packaging_type_matches?(node) click to toggle source
# File lib/dependabot/maven/file_updater/declaration_finder.rb, line 104
def packaging_type_matches?(node)
  type = declaring_requirement.dig(:metadata, :packaging_type)
  type == packaging_type(node)
end
property_value_finder() click to toggle source
# File lib/dependabot/maven/file_updater/declaration_finder.rb, line 157
def property_value_finder
  @property_value_finder ||=
    Maven::FileParser::PropertyValueFinder.
    new(dependency_files: dependency_files)
end
scope_matches?(node) click to toggle source
# File lib/dependabot/maven/file_updater/declaration_finder.rb, line 109
def scope_matches?(node)
  dependency_type = declaring_requirement.fetch(:groups)
  node_type = dependency_scope(node) == "test" ? ["test"] : []

  dependency_type == node_type
end