Skip to content

Commit 40abe9e

Browse files
authored
chore: use priority ordering in java repo finder (#641)
Signed-off-by: Ben Selwyn-Smith <[email protected]>
1 parent a07df35 commit 40abe9e

File tree

4 files changed

+66
-19
lines changed

4 files changed

+66
-19
lines changed

src/macaron/config/defaults.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ redirect_urls =
5959
[repofinder.java]
6060
# The list of maven-like repositories to attempt to retrieve artifact POMs from.
6161
artifact_repositories = https://repo.maven.apache.org/maven2
62+
# The repo_pom_paths list is a priority list. The first path that produces a valid URL will be returned as the result.
6263
repo_pom_paths =
6364
scm.url
6465
scm.connection

src/macaron/repo_finder/repo_finder_java.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2023 - 2023, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2023 - 2024, Oracle and/or its affiliates. All rights reserved.
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
33

44
"""This module contains the JavaRepoFinder class to be used for finding Java repositories."""
@@ -116,7 +116,10 @@ def _create_urls(self, group: str, artifact: str, version: str) -> list[str]:
116116
The list of created URLs.
117117
"""
118118
repositories = defaults.get_list(
119-
"repofinder.java", "artifact_repositories", fallback=["https://repo.maven.apache.org/maven2"]
119+
"repofinder.java",
120+
"artifact_repositories",
121+
fallback=["https://repo.maven.apache.org/maven2"],
122+
duplicated_ok=True,
120123
)
121124
urls = []
122125
for repo in repositories:
@@ -160,7 +163,7 @@ def _read_pom(self, pom: str) -> list[str]:
160163
The extracted contents as a list of strings.
161164
"""
162165
# Retrieve tags
163-
tags = defaults.get_list("repofinder.java", "repo_pom_paths")
166+
tags = defaults.get_list("repofinder.java", "repo_pom_paths", duplicated_ok=True)
164167
if not any(tags):
165168
logger.debug("No POM tags found for URL discovery.")
166169
return []

src/macaron/repo_finder/repo_validator.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,19 @@ def find_valid_repository_url(urls: Iterable[str]) -> str:
2121
Returns
2222
-------
2323
str
24-
A valid URL, or an empty string if none can be found.
24+
The first valid URL from the iterable, or an empty string if none can be found.
2525
"""
26-
pruned_list = []
2726
for url in urls:
2827
parsed_url = clean_url(url)
2928
if not parsed_url:
30-
# URLs that failed to parse can be rejected here.
29+
# URLs that fail to parse can be rejected here.
3130
continue
3231
redirect_url = resolve_redirects(parsed_url)
33-
# If a redirect URL is found add it, otherwise add the parsed url.
34-
pruned_list.append(redirect_url if redirect_url else parsed_url.geturl())
32+
checked_url = get_remote_vcs_url(redirect_url if redirect_url else parsed_url.geturl())
33+
if checked_url:
34+
return checked_url
3535

36-
vcs_set = {get_remote_vcs_url(value) for value in pruned_list if get_remote_vcs_url(value) != ""}
37-
38-
# To avoid non-deterministic results we sort the URLs.
39-
vcs_list = sorted(vcs_set)
40-
41-
if len(vcs_list) < 1:
42-
return ""
43-
44-
# Report the first valid URL from the end of the list.
45-
return vcs_list.pop()
36+
return ""
4637

4738

4839
def resolve_redirects(parsed_url: urllib.parse.ParseResult) -> str | None:

tests/repo_finder/test_repo_finder.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
# Copyright (c) 2023 - 2023, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2023 - 2024, Oracle and/or its affiliates. All rights reserved.
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
33

44
"""This module tests the repo finder."""
5+
import os
6+
from pathlib import Path
57

68
import pytest
79
from packageurl import PackageURL
810

11+
from macaron.config.defaults import load_defaults
912
from macaron.config.target_config import Configuration
13+
from macaron.repo_finder.repo_finder_java import JavaRepoFinder
1014
from macaron.slsa_analyzer.analyzer import Analyzer
1115

1216

@@ -69,3 +73,51 @@ def test_resolve_analysis_target(
6973
) -> None:
7074
"""Test the resolve analysis target method with valid inputs."""
7175
assert Analyzer.to_analysis_target(config, available_domains) == expect
76+
77+
78+
@pytest.mark.parametrize(
79+
("user_config_input", "expected"),
80+
[
81+
(
82+
"""
83+
[repofinder.java]
84+
repo_pom_paths =
85+
scm.connection
86+
scm.url
87+
""",
88+
["scm:git:[email protected]:oracle-samples/macaron.git", "https://github.com/oracle/macaron"],
89+
),
90+
(
91+
"""
92+
[repofinder.java]
93+
repo_pom_paths =
94+
scm.url
95+
scm.connection
96+
""",
97+
["https://github.com/oracle/macaron", "scm:git:[email protected]:oracle-samples/macaron.git"],
98+
),
99+
],
100+
)
101+
def test_pom_extraction_ordering(tmp_path: Path, user_config_input: str, expected: list[str]) -> None:
102+
"""Test the ordering of elements extracted from the POM is correct and maintained."""
103+
pom_text = """
104+
<project>
105+
<url>https://example.org</url>
106+
<scm>
107+
<connection>scm:git:[email protected]:oracle-samples/macaron.git</connection>
108+
<url>https://github.com/oracle/macaron</url>
109+
</scm>
110+
<properties>
111+
<url>1.9.15</url>
112+
</properties>
113+
</project>
114+
"""
115+
user_config_path = os.path.join(tmp_path, "config.ini")
116+
with open(user_config_path, "w", encoding="utf-8") as user_config_file:
117+
user_config_file.write(user_config_input)
118+
load_defaults(user_config_path)
119+
120+
repo_finder = JavaRepoFinder()
121+
122+
# Retrieve SCM from POM.
123+
assert expected == repo_finder._read_pom(pom_text) # pylint: disable=W0212

0 commit comments

Comments
 (0)