From eafa56901b70d2da7aea2d290d971142825585f5 Mon Sep 17 00:00:00 2001 From: tdruez Date: Tue, 22 Oct 2024 08:59:02 +0400 Subject: [PATCH] Improve support for 3 path segments NPM URLs in url2purl #167 Signed-off-by: tdruez --- CHANGELOG.rst | 3 +++ src/packageurl/contrib/url2purl.py | 15 +++++++-------- tests/contrib/data/url2purl.json | 1 + tests/contrib/test_purl2url.py | 2 ++ 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 310b59e..1a99350 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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) ------------------- diff --git a/src/packageurl/contrib/url2purl.py b/src/packageurl/contrib/url2purl.py index 0d2cfb5..4bb5274 100644 --- a/src/packageurl/contrib/url2purl.py +++ b/src/packageurl/contrib/url2purl.py @@ -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): diff --git a/tests/contrib/data/url2purl.json b/tests/contrib/data/url2purl.json index 2de615b..3b6efb8 100644 --- a/tests/contrib/data/url2purl.json +++ b/tests/contrib/data/url2purl.json @@ -88,6 +88,7 @@ "https://registry.npmjs.org/xdg-basedir/-/xdg-basedir-3.0.0.tgz": "pkg:npm/xdg-basedir@3.0.0", "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz": "pkg:npm/yallist@2.1.2", "https://registry.npmjs.org/@theia/plugin-ext/-/plugin-ext-0.9.0-next.007f41ae.tgz": "pkg:npm/%40theia/plugin-ext@0.9.0-next.007f41ae", + "https://registry.npmjs.org/@esbuild/freebsd-arm64/0.21.5": "pkg:npm/%40esbuild/freebsd-arm64@0.21.5", "https://npmjs.com/package/abbrev": "pkg:npm/abbrev", "https://npmjs.com/package/accepts/v/1.3.7": "pkg:npm/accepts@1.3.7", "https://npmjs.com/package/@angular/cli": "pkg:npm/%40angular/cli", diff --git a/tests/contrib/test_purl2url.py b/tests/contrib/test_purl2url.py index 6770641..64ea924 100644 --- a/tests/contrib/test_purl2url.py +++ b/tests/contrib/test_purl2url.py @@ -58,6 +58,7 @@ def test_purl2url_get_repo_url(): "pkg:npm/is-npm": "https://www.npmjs.com/package/is-npm", "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:npm/%40esbuild/freebsd-arm64@0.21.5": "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/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", @@ -81,6 +82,7 @@ def test_purl2url_get_download_url(): "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:npm/%40esbuild/freebsd-arm64@0.21.5": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.21.5.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",