-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Maven: implement parent snapshot lookup #5924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
42f3833
maven: implement parent snapshot lookup and refactor
jakecoffman 6a8d6a4
add tests
jakecoffman ba56381
pass in missing credentials
jakecoffman 5c72577
oops
jakecoffman 34522c7
handle malformed metadata responses
jakecoffman 974b007
Merge branch 'main' into jakecoffman/maven-parent-snapshots
jakecoffman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "nokogiri" | ||
|
|
||
| require "dependabot/dependency_file" | ||
| require "dependabot/maven/file_parser" | ||
| require "dependabot/registry_client" | ||
|
|
||
| module Dependabot | ||
| module Maven | ||
| class FileParser | ||
| class PomFetcher | ||
| def initialize(dependency_files:) | ||
| @dependency_files = dependency_files | ||
| @poms = {} | ||
| end | ||
|
|
||
| def internal_dependency_poms | ||
| return @internal_dependency_poms if @internal_dependency_poms | ||
|
|
||
| @internal_dependency_poms = {} | ||
| 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(":") | ||
|
|
||
| @internal_dependency_poms[dependency_name] = pom | ||
| end | ||
|
|
||
| @internal_dependency_poms | ||
| end | ||
|
|
||
| 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 | ||
|
|
||
| private | ||
|
|
||
| def remote_pom_url(group_id, artifact_id, version, base_repo_url) | ||
| "#{base_repo_url}/" \ | ||
| "#{group_id.tr('.', '/')}/#{artifact_id}/#{version}/" \ | ||
| "#{artifact_id}-#{version}.pom" | ||
| end | ||
|
|
||
| def remote_pom_snapshot_url(group_id, artifact_id, version, snapshot_version, base_repo_url) | ||
| "#{base_repo_url}/" \ | ||
| "#{group_id.tr('.', '/')}/#{artifact_id}/#{version}/" \ | ||
| "#{artifact_id}-#{snapshot_version}.pom" | ||
| end | ||
|
|
||
| def remote_pom_snapshot_metadata_url(group_id, artifact_id, version, base_repo_url) | ||
| "#{base_repo_url}/" \ | ||
| "#{group_id.tr('.', '/')}/#{artifact_id}/#{version}/" \ | ||
| "maven-metadata.xml" | ||
| end | ||
|
|
||
| def fetch_snapshot_pom_url(group_id, artifact_id, version, base_url) | ||
| url = remote_pom_snapshot_metadata_url(group_id, artifact_id, version, base_url) | ||
| response = fetch(url) | ||
| return nil unless response.status == 200 | ||
|
|
||
| snapshot = Nokogiri::XML(response.body). | ||
| css("snapshotVersion"). | ||
| find { |node| node.at_css("extension").content == "pom" }&. | ||
| at_css("value")&. | ||
| content | ||
| return nil unless snapshot | ||
|
|
||
| remote_pom_snapshot_url(group_id, artifact_id, version, snapshot, base_url) | ||
| end | ||
|
|
||
| def fetch(url) | ||
| @maven_responses ||= {} | ||
| @maven_responses[url] ||= Dependabot::RegistryClient.get(url: url, options: { retry_limit: 1 }) | ||
jakecoffman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| def pom?(content) | ||
| !Nokogiri::XML(content).at_css("project > artifactId").nil? | ||
| end | ||
|
|
||
| attr_reader :dependency_files | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -198,10 +198,18 @@ def repositories | |
| @repositories | ||
| end | ||
|
|
||
| def repository_finder | ||
| @repository_finder ||= | ||
| Maven::FileParser::RepositoriesFinder.new( | ||
| pom_fetcher: Maven::FileParser::PomFetcher.new(dependency_files: dependency_files), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💯 |
||
| dependency_files: dependency_files, | ||
| credentials: credentials | ||
| ) | ||
| end | ||
|
|
||
| def pom_repository_details | ||
| @pom_repository_details ||= | ||
| Maven::FileParser::RepositoriesFinder. | ||
| new(dependency_files: dependency_files, credentials: credentials). | ||
| repository_finder. | ||
| repository_urls(pom: pom). | ||
| map do |url| | ||
| { "url" => url, "auth_headers" => {} } | ||
|
|
@@ -271,9 +279,7 @@ def version_class | |
| end | ||
|
|
||
| def central_repo_urls | ||
| central_url_without_protocol = | ||
| Maven::FileParser::RepositoriesFinder.new(credentials: credentials).central_repo_url. | ||
| gsub(%r{^.*://}, "") | ||
| central_url_without_protocol = repository_finder.central_repo_url.gsub(%r{^.*://}, "") | ||
|
|
||
| %w(http:// https://).map { |p| p + central_url_without_protocol } | ||
| end | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the new detection of snapshots.