Skip to content

Commit 2e4650d

Browse files
committed
fix maven inability to overwrite repository urls by id
1 parent 7b19233 commit 2e4650d

File tree

4 files changed

+85
-15
lines changed

4 files changed

+85
-15
lines changed

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

+26-13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class RepositoriesFinder
2424
# The Central Repository is included in the Super POM, which is
2525
# always inherited from.
2626
CENTRAL_REPO_URL = "https://repo.maven.apache.org/maven2"
27+
SUPER_POM = { url: CENTRAL_REPO_URL, id: "central" }
2728

2829
def initialize(dependency_files:, evaluate_properties: true)
2930
@dependency_files = dependency_files
@@ -36,27 +37,39 @@ def initialize(dependency_files:, evaluate_properties: true)
3637

3738
# Collect all repository URLs from this POM and its parents
3839
def repository_urls(pom:, exclude_inherited: false)
39-
repo_urls_in_pom =
40+
entries = gather_repository_urls(pom: pom, exclude_inherited: exclude_inherited)
41+
ids = Set.new
42+
entries.map do |entry|
43+
next if entry[:id] && ids.include?(entry[:id])
44+
45+
ids.add(entry[:id]) unless entry[:id].nil?
46+
entry[:url]
47+
end.uniq.compact
48+
end
49+
50+
private
51+
52+
attr_reader :dependency_files
53+
54+
def gather_repository_urls(pom:, exclude_inherited: false)
55+
repos_in_pom =
4056
Nokogiri::XML(pom.content).
4157
css(REPOSITORY_SELECTOR).
42-
map { |node| node.at_css("url").content.strip.gsub(%r{/$}, "") }.
43-
reject { |url| contains_property?(url) && !evaluate_properties? }.
44-
select { |url| url.start_with?("http") }.
45-
map { |url| evaluated_value(url, pom) }
58+
map { |node| { url: node.at_css("url").content.strip, id: node.at_css("id").content.strip } }.
59+
reject { |entry| contains_property?(entry[:url]) && !evaluate_properties? }.
60+
select { |entry| entry[:url].start_with?("http") }.
61+
map { |entry| { url: evaluated_value(entry[:url], pom).gsub(%r{/$}, ""), id: entry[:id] } }
4662

47-
return repo_urls_in_pom + [CENTRAL_REPO_URL] if exclude_inherited
63+
return repos_in_pom + [SUPER_POM] if exclude_inherited
4864

49-
unless (parent = parent_pom(pom, repo_urls_in_pom))
50-
return repo_urls_in_pom + [CENTRAL_REPO_URL]
65+
urls_in_pom = repos_in_pom.map { |repo| repo[:url] }
66+
unless (parent = parent_pom(pom, urls_in_pom))
67+
return repos_in_pom + [SUPER_POM]
5168
end
5269

53-
repo_urls_in_pom + repository_urls(pom: parent)
70+
repos_in_pom + gather_repository_urls(pom: parent)
5471
end
5572

56-
private
57-
58-
attr_reader :dependency_files
59-
6073
def evaluate_properties?
6174
@evaluate_properties
6275
end

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

+12
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@
3939
)
4040
end
4141

42+
context "that overwrites central" do
43+
let(:base_pom_fixture_name) { "overwrite_central_pom.xml" }
44+
45+
it "does not include central" do
46+
expect(repository_urls).to eq(
47+
%w(
48+
https://example.com
49+
)
50+
)
51+
end
52+
end
53+
4254
context "that use properties" do
4355
let(:base_pom_fixture_name) { "property_repo_pom.xml" }
4456

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.dependabot</groupId>
7+
<artifactId>basic-pom</artifactId>
8+
<version>0.0.1-RELEASE</version>
9+
<name>Dependabot Basic POM</name>
10+
11+
<packaging>pom</packaging>
12+
13+
<repositories>
14+
<repository>
15+
<id>central</id>
16+
<url>https://example.com</url>
17+
<snapshots>
18+
<enabled>false</enabled>
19+
</snapshots>
20+
</repository>
21+
</repositories>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>com.google.guava</groupId>
26+
<artifactId>guava</artifactId>
27+
<version>23.3-jre</version>
28+
<scope>compile</scope>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.apache.httpcomponents</groupId>
33+
<artifactId>httpclient</artifactId>
34+
<version>4.5.3</version>
35+
<scope>test</scope>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>io.mockk</groupId>
40+
<artifactId>mockk</artifactId>
41+
<version>1.0.0</version>
42+
<classifier>sources</classifier>
43+
</dependency>
44+
</dependencies>
45+
</project>

maven/spec/fixtures/projects/invalid_repository_url/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212

1313
<repositories>
1414
<repository>
15-
<id>central</id>
15+
<id>wrong</id>
1616
<url>http://host:port/content/groups/public</url>
1717
</repository>
1818
</repositories>
1919
<pluginRepositories>
2020
<pluginRepository>
21-
<id>central</id>
21+
<id>wrong</id>
2222
<url>http://host:port/content/groups/public</url>
2323
</pluginRepository>
2424
</pluginRepositories>

0 commit comments

Comments
 (0)