Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/fetchcode/package_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def get_gem_versions_from_purl(purl):
def get_npm_versions_from_purl(purl):
"""Fetch versions of npm packages from the npm registry API."""
purl = PackageURL.from_string(purl)
url = f"https://registry.npmjs.org/{purl.name}"
url = get_npm_registry_url(purl)
response = get_response(url=url, content_type="json")
if not response:
logger.error(f"Failed to fetch {url}")
Expand Down Expand Up @@ -545,3 +545,9 @@ def remove_debian_default_epoch(version):
''
"""
return version and version.replace("0:", "")


def get_npm_registry_url(purl):
"""Return registry URL for npm Package-URL."""
pkg_name = f"{purl.namespace}/{purl.name}" if purl.namespace else purl.name
return f"https://registry.npmjs.org/{pkg_name}"
16 changes: 16 additions & 0 deletions tests/test_package_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
from unittest import mock

import yaml
from packageurl import PackageURL

from fetchcode.package_versions import get_npm_registry_url
from fetchcode.package_versions import versions

FETCHCODE_REGEN_TEST_FIXTURES = os.getenv("FETCHCODE_REGEN_TEST_FIXTURES", False)
Expand Down Expand Up @@ -87,6 +89,20 @@ def test_get_gem_versions_from_purl(mock_get_response):
check_results_against_json(result, expected_file)


def test_get_npm_registry_url():
purl1 = PackageURL.from_string("pkg:npm/%40angular/animation")
purl2 = PackageURL.from_string("pkg:npm/core")

result_url1 = get_npm_registry_url(purl1)
result_url2 = get_npm_registry_url(purl2)

expected_url1 = "https://registry.npmjs.org/@angular/animation"
expected_url2 = "https://registry.npmjs.org/core"

assert result_url1 == expected_url1
assert result_url2 == expected_url2


@mock.patch("fetchcode.package_versions.get_response")
def test_get_npm_versions_from_purl(mock_get_response):
side_effect = [get_json_data(data_location / "npm_mock_data.json")]
Expand Down
Loading