Skip to content

Commit 01a944e

Browse files
committedFeb 20, 2025·
cachito-nexus: Add compatibility toggle for versions that use orientDB
Nexus has changed the embedded database from versions 3.71.0 forward¹. The older versions used orientDB, which require a small change in the URL when searching for component data. This patch adds a configuration option so that Cachito can handle both versions Nexus. ¹https://help.sonatype.com/en/sonatype-nexus-repository-3-71-0-release-notes.html Signed-off-by: Bruno Pimentel <[email protected]>
1 parent fe2ef76 commit 01a944e

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed
 

‎cachito/workers/config.py

+2
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,15 @@ class Config(object):
5050
cachito_nexus_hoster_password: Optional[str] = None
5151
cachito_nexus_hoster_url: Optional[str] = None
5252
cachito_nexus_hoster_username: Optional[str] = None
53+
cachito_nexus_hoster_is_orient_db = False
5354
cachito_nexus_js_hosted_repo_name = "cachito-js-hosted"
5455
cachito_nexus_max_search_attempts = 5
5556
cachito_nexus_npm_proxy_url = "http://localhost:8081/repository/cachito-js/"
5657
cachito_nexus_pip_raw_repo_name = "cachito-pip-raw"
5758
cachito_nexus_rubygems_raw_repo_name = "cachito-rubygems-raw"
5859
cachito_nexus_proxy_password: Optional[str] = None
5960
cachito_nexus_proxy_username: Optional[str] = None
61+
cachito_nexus_proxy_is_orient_db = False
6062
cachito_nexus_request_repo_prefix = "cachito-"
6163
cachito_nexus_timeout = 60
6264
cachito_nexus_username = "cachito"

‎cachito/workers/nexus.py

+33-3
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,30 @@ def get_component_info_from_nexus(
267267
return None
268268

269269

270+
def _get_nexus_orient_db_configuration():
271+
config = get_worker_config()
272+
return config.cachito_nexus_hoster_is_orient_db, config.cachito_nexus_proxy_is_orient_db
273+
274+
275+
def _treat_raw_component_name(name, is_nexus_hoster):
276+
"""
277+
Treats the raw component name based on the use of orientDB by Nexus.
278+
279+
Starting from v3.71.0, Nexus has dropped the use of orientDB in favor of H2, which forces
280+
all raw component names to have a leading slash. This function works as a compatibility layer
281+
for orientDB, removing the leading slash.
282+
"""
283+
nexus_hoster_is_orient_db, nexus_proxy_is_orient_db = _get_nexus_orient_db_configuration()
284+
285+
if is_nexus_hoster and nexus_hoster_is_orient_db:
286+
return name[1:]
287+
288+
if not is_nexus_hoster and nexus_proxy_is_orient_db:
289+
return name[1:]
290+
291+
return name
292+
293+
270294
def get_raw_component_asset_url(repository, name, max_attempts=1, from_nexus_hoster=True):
271295
"""
272296
Get download URL for the asset of a raw component.
@@ -281,8 +305,14 @@ def get_raw_component_asset_url(repository, name, max_attempts=1, from_nexus_hos
281305
available
282306
:return: download URL for the asset, or None if component was not found
283307
"""
308+
treated_name = _treat_raw_component_name(name, from_nexus_hoster)
309+
284310
component = get_component_info_from_nexus(
285-
repository, "raw", name, max_attempts=max_attempts, from_nexus_hoster=from_nexus_hoster
311+
repository,
312+
"raw",
313+
treated_name,
314+
max_attempts=max_attempts,
315+
from_nexus_hoster=from_nexus_hoster,
286316
)
287317
if component is None:
288318
return None
@@ -291,10 +321,10 @@ def get_raw_component_asset_url(repository, name, max_attempts=1, from_nexus_hos
291321

292322
# Sanity checks, in practice this should not happen
293323
if not assets:
294-
raise RuntimeError(f"Component {name} has no assets")
324+
raise RuntimeError(f"Component {treated_name} has no assets")
295325
if len(assets) > 1:
296326
log.debug("All assets: %r", assets)
297-
raise RuntimeError(f"Component {name} has more than 1 asset")
327+
raise RuntimeError(f"Component {treated_name} has more than 1 asset")
298328

299329
return assets[0]["downloadUrl"]
300330

‎tests/test_workers/test_nexus.py

+26-4
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,29 @@ def test_get_component_info_from_nexus_version_vs_raw(raw, version):
411411
)
412412

413413

414-
@pytest.mark.parametrize("hoster", [True, False])
414+
@pytest.mark.parametrize(
415+
"hoster, hoster_is_orient_db, proxy_is_orient_db, treated_name",
416+
[
417+
pytest.param(True, True, False, "foo/bar/foobar-1.0.tar.gz", id="from_hoster_is_orient_db"),
418+
pytest.param(False, False, True, "foo/bar/foobar-1.0.tar.gz", id="from_proxy_is_orient_db"),
419+
pytest.param(
420+
False, False, False, "/foo/bar/foobar-1.0.tar.gz", id="from_proxy_is_not_orient_db"
421+
),
422+
pytest.param(
423+
True, False, True, "/foo/bar/foobar-1.0.tar.gz", id="from_hoster_is_not_orient_db"
424+
),
425+
],
426+
)
415427
@mock.patch("cachito.workers.nexus.get_component_info_from_nexus")
416-
def test_get_raw_component_asset_url(mock_get_component_info, hoster):
428+
@mock.patch("cachito.workers.nexus._get_nexus_orient_db_configuration")
429+
def test_get_raw_component_asset_url(
430+
mock_get_nexus_orient_db_configuration,
431+
mock_get_component_info,
432+
hoster,
433+
hoster_is_orient_db,
434+
proxy_is_orient_db,
435+
treated_name,
436+
):
417437
mock_get_component_info.return_value = {
418438
# "id": "1234",
419439
# "repository": "cachito-pip-raw",
@@ -437,15 +457,17 @@ def test_get_raw_component_asset_url(mock_get_component_info, hoster):
437457
]
438458
}
439459

460+
mock_get_nexus_orient_db_configuration.return_value = (hoster_is_orient_db, proxy_is_orient_db)
461+
440462
url = nexus.get_raw_component_asset_url(
441-
"cachito-pip-raw", "foo/bar/foobar-1.0.tar.gz", from_nexus_hoster=hoster
463+
"cachito-pip-raw", "/foo/bar/foobar-1.0.tar.gz", from_nexus_hoster=hoster
442464
)
443465
assert url == "http://nexus/repository/cachito-pip-raw/foo/bar/foobar-1.0.tar.gz"
444466

445467
mock_get_component_info.assert_called_once_with(
446468
"cachito-pip-raw",
447469
"raw",
448-
"foo/bar/foobar-1.0.tar.gz",
470+
treated_name,
449471
max_attempts=1,
450472
from_nexus_hoster=hoster,
451473
)

0 commit comments

Comments
 (0)
Please sign in to comment.