Skip to content

Add Maven support for purl2url #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
35 changes: 35 additions & 0 deletions src/packageurl/contrib/purl2url.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,23 @@ def build_cocoapods_repo_url(purl):
return name and f"https://cocoapods.org/pods/{name}"


@repo_router.route("pkg:maven/.*")
def build_maven_repo_url(purl):
"""
Return a Maven repo URL from the `purl` string.
"""
purl_data = PackageURL.from_string(purl)
namespace = purl_data.namespace
name = purl_data.name
version = purl_data.version

base_url = "https://repo1.maven.org/maven2"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you handle the case with a repository_url qualifier that would override the default?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added support of repository_url cf. 2bc0cb9


if namespace and name and version:
maven_namespace = namespace.replace(".", "/")
return f"{base_url}/{maven_namespace}/{name}/{version}"


# Download URLs:


Expand Down Expand Up @@ -365,6 +382,24 @@ def build_npm_download_url(purl):
return f"{base_url}/{name}/-/{name}-{version}.tgz"


@download_router.route("pkg:maven/.*")
def build_maven_download_url(purl):
"""
Return a maven download URL from the `purl` string.
"""
purl_data = PackageURL.from_string(purl)

namespace = purl_data.namespace
name = purl_data.name
version = purl_data.version

base_url = "https://repo1.maven.org/maven2"

if namespace and name and version:
maven_namespace = namespace.replace(".", "/")
return f"{base_url}/{maven_namespace}/{name}/{version}/{name}-{version}.jar"


@download_router.route("pkg:hackage/.*")
def build_hackage_download_url(purl):
"""
Expand Down
6 changes: 6 additions & 0 deletions tests/contrib/test_purl2url.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def test_purl2url_get_repo_url():
"pkg:golang/gopkg.in/[email protected]": "https://pkg.go.dev/gopkg.in/[email protected]",
"pkg:cocoapods/[email protected]": "https://cocoapods.org/pods/AFNetworking",
"pkg:cocoapods/[email protected]": "https://cocoapods.org/pods/MapsIndoors",
"pkg:maven/org.apache.commons/[email protected]": "https://repo1.maven.org/maven2/org/apache/commons/commons-io/1.3.2",
}

for purl, url in purls_url.items():
Expand All @@ -92,6 +93,7 @@ def test_purl2url_get_download_url():
"pkg:gitlab/tg1999/firebase@1a122122": "https://gitlab.com/tg1999/firebase/-/archive/1a122122/firebase-1a122122.tar.gz",
"pkg:gitlab/tg1999/firebase@1a122122?version_prefix=v": "https://gitlab.com/tg1999/firebase/-/archive/v1a122122/firebase-v1a122122.tar.gz",
"pkg:gitlab/hoppr/[email protected]": "https://gitlab.com/hoppr/hoppr/-/archive/v1.11.1-dev.2/hoppr-v1.11.1-dev.2.tar.gz",
"pkg:maven/org.apache.commons/[email protected]": "https://repo1.maven.org/maven2/org/apache/commons/commons-io/1.3.2/commons-io-1.3.2.jar",
# From `download_url` qualifier
"pkg:github/yarnpkg/[email protected]?download_url=https://github.com/yarnpkg/yarn/releases/download/v1.3.2/yarn-v1.3.2.tar.gz&version_prefix=v": "https://github.com/yarnpkg/yarn/releases/download/v1.3.2/yarn-v1.3.2.tar.gz",
"pkg:generic/lxc-master.tar.gz?download_url=https://salsa.debian.org/lxc-team/lxc/-/archive/master/lxc-master.tar.gz": "https://salsa.debian.org/lxc-team/lxc/-/archive/master/lxc-master.tar.gz",
Expand Down Expand Up @@ -150,6 +152,10 @@ def test_purl2url_get_inferred_urls():
"pkg:cocoapods/[email protected]": ["https://cocoapods.org/pods/AFNetworking"],
"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:maven/org.apache.commons/[email protected]": [
"https://repo1.maven.org/maven2/org/apache/commons/commons-io/1.3.2",
"https://repo1.maven.org/maven2/org/apache/commons/commons-io/1.3.2/commons-io-1.3.2.jar",
],
"pkg:bitbucket/birkenfeld": [],
}

Expand Down
Loading