Skip to content

Commit

Permalink
Add support for Composer in purl2url
Browse files Browse the repository at this point in the history
Signed-off-by: Camille Moulin <[email protected]>
  • Loading branch information
camillem committed Feb 23, 2024
1 parent 30f497f commit e0d180d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/packageurl/contrib/purl2url.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,23 @@ def build_pypi_repo_url(purl):
return f"https://pypi.org/project/{name}/"


@repo_router.route("pkg:composer/.*")
def build_composer_repo_url(purl):
"""
Return a composer repo URL from the `purl` string.
"""
purl_data = PackageURL.from_string(purl)

name = (purl_data.name or "").replace("_", "-")
version = purl_data.version
namespace = purl_data.namespace

if name and version:
return f"https://packagist.org/packages/{namespace}/{name}#{version}"
elif name:
return f"https://packagist.org/packages/{namespace}/{name}"


@repo_router.route("pkg:nuget/.*")
def build_nuget_repo_url(purl):
"""
Expand Down
4 changes: 4 additions & 0 deletions tests/contrib/test_purl2url.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def test_purl2url_get_repo_url():
"pkg:pypi/sortedcontainers": "https://pypi.org/project/sortedcontainers/",
"pkg:pypi/[email protected]": "https://pypi.org/project/sortedcontainers/2.4.0/",
"pkg:pypi/packageurl_python": "https://pypi.org/project/packageurl-python/",
"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:nuget/System.Text.Json": "https://www.nuget.org/packages/System.Text.Json",
Expand Down Expand Up @@ -95,6 +97,7 @@ def test_purl2url_get_download_url():
"pkg:rubygems/package-name": None,
"pkg:bitbucket/birkenfeld": None,
"pkg:pypi/[email protected]": None,
"pkg:composer/psr/[email protected]": None,
"pkg:golang/xorm.io/[email protected]": None,
"pkg:golang/gopkg.in/[email protected]": None,
}
Expand Down Expand Up @@ -132,6 +135,7 @@ def test_purl2url_get_inferred_urls():
"https://gitlab.com/tg1999/firebase/-/archive/1a122122/firebase-1a122122.tar.gz",
],
"pkg:pypi/[email protected]": ["https://pypi.org/project/sortedcontainers/2.4.0/"],
"pkg:composer/psr/[email protected]": ["https://packagist.org/packages/psr/log#1.1.3"],
"pkg:rubygems/package-name": ["https://rubygems.org/gems/package-name"],
"pkg:bitbucket/birkenfeld": [],
}
Expand Down
12 changes: 12 additions & 0 deletions tests/data/test-suite-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,18 @@
"subpath": null,
"is_invalid": false
},
{
"description": "valid packagist purl",
"purl": "pkg:composer/guzzlehttp/[email protected]",
"canonical_purl": "pkg:composer/guzzlehttp/[email protected]",
"type": "composer",
"namespace": "guzzlehttp",
"name": "promises",
"version": "2.0.2",
"qualifiers": null,
"subpath": null,
"is_invalid": false
},
{
"description": "rpm often use qualifiers",
"purl": "pkg:Rpm/fedora/[email protected]?Arch=i386&Distro=fedora-25",
Expand Down

0 comments on commit e0d180d

Please sign in to comment.