Class: Dependabot::Maven::FileParser::PropertyValueFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/dependabot/maven/file_parser/property_value_finder.rb

Constant Summary collapse

DOT_SEPARATOR_REGEX =
%r{\.(?!\d+([.\/_\-]|$)+)}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(dependency_files:) ⇒ PropertyValueFinder

Returns a new instance of PropertyValueFinder.



20
21
22
# File 'lib/dependabot/maven/file_parser/property_value_finder.rb', line 20

def initialize(dependency_files:)
  @dependency_files = dependency_files
end

Instance Method Details

#property_details(property_name:, callsite_pom:) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dependabot/maven/file_parser/property_value_finder.rb', line 24

def property_details(property_name:, callsite_pom:)
  pom = callsite_pom
  doc = Nokogiri::XML(pom.content)
  doc.remove_namespaces!

  # Loop through the paths that would satisfy this property name,
  # looking for one that exists in this POM
  nm = sanitize_property_name(property_name)
  node =
    loop do
      candidate_node =
        doc.at_xpath("/project/#{nm}") ||
        doc.at_xpath("/project/properties/#{nm}") ||
        doc.at_xpath("/project/profiles/profile/properties/#{nm}")
      break candidate_node if candidate_node
      break unless nm.match?(DOT_SEPARATOR_REGEX)

      nm = nm.sub(DOT_SEPARATOR_REGEX, "/")

    rescue Nokogiri::XML::XPath::SyntaxError => e
      raise DependencyFileNotEvaluatable, e.message
    end

  # If we found a property, return it
  return { file: pom.name, node: node, value: node.content.strip } if node

  # Otherwise, look for a value in this pom's parent
  return unless (parent = parent_pom(pom))

  property_details(
    property_name: property_name,
    callsite_pom: parent
  )
end