From be737715df229109897c6aac66da97dd720c5eac Mon Sep 17 00:00:00 2001 From: tdruez <489057+tdruez@users.noreply.github.com> Date: Tue, 9 Jul 2024 12:33:13 +0400 Subject: [PATCH] Add support for namespace in build_npm_download_url (#158) Signed-off-by: tdruez --- CHANGELOG.rst | 5 +++++ setup.cfg | 2 +- src/packageurl/contrib/purl2url.py | 8 +++++++- tests/contrib/test_purl2url.py | 13 ++++++++++--- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index f7a259f..a2ffa68 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,6 +4,11 @@ Changelog 0.16.0 (unreleased) ------------------- +0.15.3 (2024-07-09) +------------------- + +- Add support for namespace in ``build_npm_download_url``. + 0.15.2 (2024-07-04) ------------------- diff --git a/setup.cfg b/setup.cfg index 6051281..4938ed3 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = packageurl-python -version = 0.15.2 +version = 0.15.3 license = MIT description = A purl aka. Package URL parser and builder long_description = file:README.rst diff --git a/src/packageurl/contrib/purl2url.py b/src/packageurl/contrib/purl2url.py index 0b9336b..08a404b 100644 --- a/src/packageurl/contrib/purl2url.py +++ b/src/packageurl/contrib/purl2url.py @@ -342,11 +342,17 @@ def build_npm_download_url(purl): """ purl_data = PackageURL.from_string(purl) + namespace = purl_data.namespace name = purl_data.name version = purl_data.version + base_url = "https://registry.npmjs.org" + + if namespace: + base_url += f"/{namespace}" + if name and version: - return f"http://registry.npmjs.org/{name}/-/{name}-{version}.tgz" + return f"{base_url}/{name}/-/{name}-{version}.tgz" @download_router.route("pkg:hackage/.*") diff --git a/tests/contrib/test_purl2url.py b/tests/contrib/test_purl2url.py index 5ca30dc..a37cbcd 100644 --- a/tests/contrib/test_purl2url.py +++ b/tests/contrib/test_purl2url.py @@ -56,7 +56,8 @@ def test_purl2url_get_repo_url(): "pkg:composer/psr/log": "https://packagist.org/packages/psr/log", "pkg:composer/psr/log@1.1.3": "https://packagist.org/packages/psr/log#1.1.3", "pkg:npm/is-npm": "https://www.npmjs.com/package/is-npm", - "pkg:npm/is-npm@1.0.0": "https://www.npmjs.com/package/is-npm/v/1.0.0", + "pkg:npm/@clayui/tooltip@3.1.0": "https://www.npmjs.com/package/@clayui/tooltip/v/3.1.0", + "pkg:npm/%40clayui/tooltip@3.1.0": "https://www.npmjs.com/package/@clayui/tooltip/v/3.1.0", "pkg:nuget/System.Text.Json": "https://www.nuget.org/packages/System.Text.Json", "pkg:nuget/System.Text.Json@6.0.6": "https://www.nuget.org/packages/System.Text.Json/6.0.6", "pkg:hackage/cli-extras": "https://hackage.haskell.org/package/cli-extras", @@ -75,7 +76,9 @@ def test_purl2url_get_download_url(): # Generated "pkg:cargo/rand@0.7.2": "https://crates.io/api/v1/crates/rand/0.7.2/download", "pkg:gem/bundler@2.3.23": "https://rubygems.org/downloads/bundler-2.3.23.gem", - "pkg:npm/is-npm@1.0.0": "http://registry.npmjs.org/is-npm/-/is-npm-1.0.0.tgz", + "pkg:npm/is-npm@1.0.0": "https://registry.npmjs.org/is-npm/-/is-npm-1.0.0.tgz", + "pkg:npm/@clayui/tooltip@3.1.0": "https://registry.npmjs.org/@clayui/tooltip/-/tooltip-3.1.0.tgz", + "pkg:npm/%40clayui/tooltip@3.1.0": "https://registry.npmjs.org/@clayui/tooltip/-/tooltip-3.1.0.tgz", "pkg:hackage/cli-extras@0.2.0.0": "https://hackage.haskell.org/package/cli-extras-0.2.0.0/cli-extras-0.2.0.0.tar.gz", "pkg:nuget/System.Text.Json@6.0.6": "https://www.nuget.org/api/v2/package/System.Text.Json/6.0.6", "pkg:github/nexb/scancode-toolkit@3.1.1?version_prefix=v": "https://github.com/nexb/scancode-toolkit/archive/v3.1.1.tar.gz", @@ -119,7 +122,11 @@ def test_purl2url_get_inferred_urls(): ], "pkg:npm/is-npm@1.0.0": [ "https://www.npmjs.com/package/is-npm/v/1.0.0", - "http://registry.npmjs.org/is-npm/-/is-npm-1.0.0.tgz", + "https://registry.npmjs.org/is-npm/-/is-npm-1.0.0.tgz", + ], + "pkg:npm/%40clayui/tooltip@3.1.0": [ + "https://www.npmjs.com/package/@clayui/tooltip/v/3.1.0", + "https://registry.npmjs.org/@clayui/tooltip/-/tooltip-3.1.0.tgz", ], "pkg:hackage/cli-extras@0.2.0.0": [ "https://hackage.haskell.org/package/cli-extras-0.2.0.0",