Skip to content

Commit

Permalink
Add support for namespace in build_npm_download_url (#158)
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez authored Jul 9, 2024
1 parent aa217f1 commit be73771
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
-------------------

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/packageurl/contrib/purl2url.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/.*")
Expand Down
13 changes: 10 additions & 3 deletions tests/contrib/test_purl2url.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def test_purl2url_get_repo_url():
"pkg:composer/psr/log": "https://packagist.org/packages/psr/log",
"pkg:composer/psr/[email protected]": "https://packagist.org/packages/psr/log#1.1.3",
"pkg:npm/is-npm": "https://www.npmjs.com/package/is-npm",
"pkg:npm/[email protected]": "https://www.npmjs.com/package/is-npm/v/1.0.0",
"pkg:npm/@clayui/[email protected]": "https://www.npmjs.com/package/@clayui/tooltip/v/3.1.0",
"pkg:npm/%40clayui/[email protected]": "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/[email protected]": "https://www.nuget.org/packages/System.Text.Json/6.0.6",
"pkg:hackage/cli-extras": "https://hackage.haskell.org/package/cli-extras",
Expand All @@ -75,7 +76,9 @@ def test_purl2url_get_download_url():
# Generated
"pkg:cargo/[email protected]": "https://crates.io/api/v1/crates/rand/0.7.2/download",
"pkg:gem/[email protected]": "https://rubygems.org/downloads/bundler-2.3.23.gem",
"pkg:npm/[email protected]": "http://registry.npmjs.org/is-npm/-/is-npm-1.0.0.tgz",
"pkg:npm/[email protected]": "https://registry.npmjs.org/is-npm/-/is-npm-1.0.0.tgz",
"pkg:npm/@clayui/[email protected]": "https://registry.npmjs.org/@clayui/tooltip/-/tooltip-3.1.0.tgz",
"pkg:npm/%40clayui/[email protected]": "https://registry.npmjs.org/@clayui/tooltip/-/tooltip-3.1.0.tgz",
"pkg:hackage/[email protected]": "https://hackage.haskell.org/package/cli-extras-0.2.0.0/cli-extras-0.2.0.0.tar.gz",
"pkg:nuget/[email protected]": "https://www.nuget.org/api/v2/package/System.Text.Json/6.0.6",
"pkg:github/nexb/[email protected]?version_prefix=v": "https://github.com/nexb/scancode-toolkit/archive/v3.1.1.tar.gz",
Expand Down Expand Up @@ -119,7 +122,11 @@ def test_purl2url_get_inferred_urls():
],
"pkg:npm/[email protected]": [
"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/[email protected]": [
"https://www.npmjs.com/package/@clayui/tooltip/v/3.1.0",
"https://registry.npmjs.org/@clayui/tooltip/-/tooltip-3.1.0.tgz",
],
"pkg:hackage/[email protected]": [
"https://hackage.haskell.org/package/cli-extras-0.2.0.0",
Expand Down

0 comments on commit be73771

Please sign in to comment.