Skip to content

Commit

Permalink
Improve support for 3 path segments NPM URLs in url2purl #167 (#170)
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez authored Oct 22, 2024
1 parent d89a160 commit 883041e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Changelog
- Drop support for Python 3.7.
https://github.com/package-url/packageurl-python/issues/160

- Improve support for 3 path segments NPM URLs in ``url2purl``.
https://github.com/package-url/packageurl-python/issues/167

0.15.6 (2024-07-25)
-------------------

Expand Down
15 changes: 7 additions & 8 deletions src/packageurl/contrib/url2purl.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,19 @@ def build_npm_api_purl(uri):
path = unquote_plus(urlparse(uri).path)
segments = [seg for seg in path.split("/") if seg]

if len(segments) != 2:
if len(segments) < 2:
return

# /@esbuild/freebsd-arm64/0.21.5
if len(segments) == 3:
return PackageURL("npm", namespace=segments[0], name=segments[1], version=segments[2])

# /@invisionag/eslint-config-ivx
if segments[0].startswith("@"):
namespace = segments[0]
name = segments[1]
return PackageURL("npm", namespace, name)
return PackageURL("npm", namespace=segments[0], name=segments[1])

# /angular/1.6.6
else:
name = segments[0]
version = segments[1]
return PackageURL("npm", name=name, version=version)
return PackageURL("npm", name=segments[0], version=segments[1])


def build_npm_download_purl(uri):
Expand Down
1 change: 1 addition & 0 deletions tests/contrib/data/url2purl.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"https://registry.npmjs.org/xdg-basedir/-/xdg-basedir-3.0.0.tgz": "pkg:npm/[email protected]",
"https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz": "pkg:npm/[email protected]",
"https://registry.npmjs.org/@theia/plugin-ext/-/plugin-ext-0.9.0-next.007f41ae.tgz": "pkg:npm/%40theia/[email protected]",
"https://registry.npmjs.org/@esbuild/freebsd-arm64/0.21.5": "pkg:npm/%40esbuild/[email protected]",
"https://npmjs.com/package/abbrev": "pkg:npm/abbrev",
"https://npmjs.com/package/accepts/v/1.3.7": "pkg:npm/[email protected]",
"https://npmjs.com/package/@angular/cli": "pkg:npm/%40angular/cli",
Expand Down
2 changes: 2 additions & 0 deletions tests/contrib/test_purl2url.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def test_purl2url_get_repo_url():
"pkg:npm/is-npm": "https://www.npmjs.com/package/is-npm",
"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:npm/%40esbuild/[email protected]": "https://www.npmjs.com/package/@esbuild/freebsd-arm64/v/0.21.5",
"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 @@ -81,6 +82,7 @@ def test_purl2url_get_download_url():
"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:npm/%40esbuild/[email protected]": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.21.5.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

0 comments on commit 883041e

Please sign in to comment.