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

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

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(dependency_files:, credentials: []) ⇒ PropertyValueFinder

Returns a new instance of PropertyValueFinder.



25
26
27
28
29
30
# File 'lib/dependabot/maven/file_parser/property_value_finder.rb', line 25

def initialize(dependency_files:, credentials: [])
  @dependency_files = dependency_files
  @credentials = credentials
  @pom_fetcher = T.let(PomFetcher.new(dependency_files: dependency_files),
                       Dependabot::Maven::FileParser::PomFetcher)
end

Instance Method Details

#property_details(property_name:, callsite_pom:) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/dependabot/maven/file_parser/property_value_finder.rb', line 35

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.xpath("/project/#{nm}").last ||
        doc.xpath("/project/properties/#{property_name}").last ||
        doc.xpath("/project/profiles/profile/properties/#{property_name}").last

      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

  # and value is an expression
  if node && /\$\{(?<expression>.+)\}/.match(node.content.strip)
    return extract_value_from_expression(
      expression: node.content.strip,
      property_name: property_name,
      callsite_pom: callsite_pom
    )
  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