Skip to content

Commit 6f1a6a9

Browse files
Preserve npm scopes when fetching package metadata
Signed-off-by: Ali Zulfiqar <codewithfourtix@gmail.com>
1 parent 27c4fc9 commit 6f1a6a9

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

src/fetchcode/package.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ def get_npm_data_from_purl(purl):
122122
base_path = "http://registry.npmjs.org"
123123
name = purl.name
124124
version = purl.version
125-
api_url = f"{base_path}/{name}"
125+
full_name = f"{purl.namespace}/{name}" if purl.namespace else name
126+
api_url = f"{base_path}/{full_name}"
126127

127128
response = get_response(api_url)
128129
vcs_data = response.get("repository") or {}
@@ -136,7 +137,9 @@ def get_npm_data_from_purl(purl):
136137
versions = response.get("versions", [])
137138
for num in versions:
138139
version = versions[num]
139-
version_purl = PackageURL(type=purl.type, name=name, version=version.get("version"))
140+
version_purl = PackageURL(
141+
type=purl.type, namespace=purl.namespace, name=name, version=version.get("version")
142+
)
140143
repository = version.get("repository") or {}
141144
bugs = response.get("bugs") or {}
142145
dist = version.get("dist") or {}

tests/test_npm_metadata_scope.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# fetchcode is a free software tool from nexB Inc. and others.
2+
# Visit https://github.com/aboutcode-org/fetchcode for support and download.
3+
4+
# Copyright (c) nexB Inc. and others. All rights reserved.
5+
# http://nexb.com and http://aboutcode.org
6+
7+
# This software is licensed under the Apache License version 2.0.
8+
9+
# You may not use this software except in compliance with the License.
10+
# You may obtain a copy of the License at:
11+
# http://apache.org/licenses/LICENSE-2.0
12+
# Unless required by applicable law or agreed to in writing, software distributed
13+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations under the License.
16+
17+
import pytest
18+
19+
from fetchcode import package
20+
21+
22+
@pytest.mark.parametrize(
23+
"purl,registry_name",
24+
[("pkg:npm/%40scope/example@1.0", "@scope/example"), ("pkg:npm/example@1.0", "example")],
25+
)
26+
def test_npm_metadata_preserves_scope(monkeypatch, purl, registry_name):
27+
urls = []
28+
29+
def response(url):
30+
urls.append(url)
31+
return {"versions": {"1.0": {"version": "1.0"}}}
32+
33+
monkeypatch.setattr(package, "get_response", response)
34+
result = list(package.get_npm_data_from_purl(purl))
35+
assert urls == ["http://registry.npmjs.org/" + registry_name]
36+
assert result[0].purl == purl

0 commit comments

Comments
 (0)