Class: Dependabot::Maven::FileParser::PomFetcher

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

Instance Method Summary collapse

Constructor Details

#initialize(dependency_files:) ⇒ PomFetcher

Returns a new instance of PomFetcher.



18
19
20
21
# File 'lib/dependabot/maven/file_parser/pom_fetcher.rb', line 18

def initialize(dependency_files:)
  @dependency_files = dependency_files
  @poms = T.let({}, T::Hash[String, DependencyFile])
end

Instance Method Details

#fetch_remote_parent_pom(group_id, artifact_id, version, urls_to_try) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dependabot/maven/file_parser/pom_fetcher.rb', line 55

def fetch_remote_parent_pom(group_id, artifact_id, version, urls_to_try)
  pom_id = "#{group_id}:#{artifact_id}:#{version}"
  return @poms[pom_id] if @poms.key?(pom_id)

  urls_to_try.each do |base_url|
    url =
      if version.include?("SNAPSHOT")
        fetch_snapshot_pom_url(group_id, artifact_id, version, base_url)
      else
        remote_pom_url(group_id, artifact_id, version, base_url)
      end
    next if url.nil?

    response = fetch(url)
    next unless response.status == 200
    next unless pom?(response.body)

    dependency_file = DependencyFile.new(
      name: "remote_pom.xml",
      content: response.body
    )

    @poms[pom_id] = dependency_file
    return dependency_file
  rescue Excon::Error::Socket, Excon::Error::Timeout,
         Excon::Error::TooManyRedirects, URI::InvalidURIError
    nil
  end

  # If a parent POM couldn't be found, return `nil`
  nil
end

#internal_dependency_pomsObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dependabot/maven/file_parser/pom_fetcher.rb', line 24

def internal_dependency_poms
  return @internal_dependency_poms if @internal_dependency_poms

  @internal_dependency_poms = T.let({}, T.nilable(T::Hash[String, DependencyFile]))
  dependency_files.each do |pom|
    doc = Nokogiri::XML(pom.content)
    group_id = doc.at_css("project > groupId") ||
               doc.at_css("project > parent > groupId")
    artifact_id = doc.at_css("project > artifactId")

    next unless group_id && artifact_id

    dependency_name = [
      group_id.content.strip,
      artifact_id.content.strip
    ].join(":")

    T.must(@internal_dependency_poms)[dependency_name] = pom
  end

  T.must(@internal_dependency_poms)
end