Skip to content

Commit 9073966

Browse files
authored
Merge pull request #5884 from dependabot/jakecoffman/maven-creds-settings-xml
add Maven credential metadata to the URLs it searches for POM files
2 parents a6a6636 + f05e712 commit 9073966

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

maven/lib/dependabot/maven/file_parser/property_value_finder.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ class PropertyValueFinder
1717

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

20-
def initialize(dependency_files:)
20+
def initialize(dependency_files:, credentials: [])
2121
@dependency_files = dependency_files
22+
@credentials = credentials
2223
end
2324

2425
def property_details(property_name:, callsite_pom:)
@@ -119,6 +120,7 @@ def repositories_finder
119120
@repositories_finder ||=
120121
RepositoriesFinder.new(
121122
dependency_files: dependency_files,
123+
credentials: @credentials,
122124
evaluate_properties: false
123125
)
124126
end

maven/lib/dependabot/maven/file_parser/repositories_finder.rb

+10-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ class RepositoriesFinder
2626
CENTRAL_REPO_URL = "https://repo.maven.apache.org/maven2"
2727
SUPER_POM = { url: CENTRAL_REPO_URL, id: "central" }
2828

29-
def initialize(dependency_files:, evaluate_properties: true)
29+
def initialize(dependency_files:, credentials: [], evaluate_properties: true)
3030
@dependency_files = dependency_files
31+
@credentials = credentials
3132

3233
# We need the option not to evaluate properties so as not to have a
3334
# circular dependency between this class and the PropertyValueFinder
@@ -39,7 +40,7 @@ def initialize(dependency_files:, evaluate_properties: true)
3940
def repository_urls(pom:, exclude_inherited: false)
4041
entries = gather_repository_urls(pom: pom, exclude_inherited: exclude_inherited)
4142
ids = Set.new
42-
entries.map do |entry|
43+
urls_from_credentials + entries.map do |entry|
4344
next if entry[:id] && ids.include?(entry[:id])
4445

4546
ids.add(entry[:id]) unless entry[:id].nil?
@@ -119,7 +120,7 @@ def internal_dependency_poms
119120
end
120121

121122
def fetch_remote_parent_pom(group_id, artifact_id, version, repo_urls)
122-
(repo_urls + [CENTRAL_REPO_URL]).uniq.each do |base_url|
123+
(urls_from_credentials + repo_urls + [CENTRAL_REPO_URL]).uniq.each do |base_url|
123124
url = remote_pom_url(group_id, artifact_id, version, base_url)
124125

125126
@maven_responses ||= {}
@@ -155,6 +156,12 @@ def remote_pom_url(group_id, artifact_id, version, base_repo_url)
155156
"#{artifact_id}-#{version}.pom"
156157
end
157158

159+
def urls_from_credentials
160+
@credentials.
161+
select { |cred| cred["type"] == "maven_repository" }.
162+
filter_map { |cred| cred["url"]&.strip&.gsub(%r{/$}, "") }
163+
end
164+
158165
def contains_property?(value)
159166
value.match?(property_regex)
160167
end

maven/spec/dependabot/maven/file_parser/repositories_finder_spec.rb

+26-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
require "dependabot/maven/file_parser/repositories_finder"
66

77
RSpec.describe Dependabot::Maven::FileParser::RepositoriesFinder do
8-
let(:finder) { described_class.new(dependency_files: dependency_files) }
9-
8+
let(:finder) do
9+
described_class.new(
10+
dependency_files: dependency_files,
11+
credentials: credentials
12+
)
13+
end
14+
let(:credentials) { [] }
1015
let(:dependency_files) { [base_pom] }
1116
let(:base_pom) do
1217
Dependabot::DependencyFile.new(
@@ -51,6 +56,25 @@
5156
end
5257
end
5358

59+
context "with credentials" do
60+
let(:base_pom_fixture_name) { "basic_pom.xml" }
61+
let(:credentials) do
62+
[
63+
{ "type" => "maven_repository", "url" => "https://example.com" },
64+
{ "type" => "git_source", "url" => "https://github.com" } # ignored since it's not maven
65+
]
66+
end
67+
68+
it "adds the credential urls first" do
69+
expect(repository_urls).to eq(
70+
%w(
71+
https://example.com
72+
https://repo.maven.apache.org/maven2
73+
)
74+
)
75+
end
76+
end
77+
5478
context "that use properties" do
5579
let(:base_pom_fixture_name) { "property_repo_pom.xml" }
5680

0 commit comments

Comments
 (0)