Skip to content

Commit c42e618

Browse files
fafanouleletdruez
andauthored
Add support for Maven in purl2url (#177)
Co-authored-by: tdruez <[email protected]>
1 parent 48b22b5 commit c42e618

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/packageurl/contrib/purl2url.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
from packageurl.contrib.route import NoRouteAvailable
2929
from packageurl.contrib.route import Router
3030

31+
DEFAULT_MAVEN_REPOSITORY = "https://repo.maven.apache.org/maven2"
32+
3133

3234
def get_repo_download_url_by_package_type(
3335
type, namespace, name, version, archive_extension="tar.gz"
@@ -314,6 +316,24 @@ def build_cocoapods_repo_url(purl):
314316
return name and f"https://cocoapods.org/pods/{name}"
315317

316318

319+
@repo_router.route("pkg:maven/.*")
320+
def build_maven_repo_url(purl):
321+
"""
322+
Return a Maven repo URL from the `purl` string.
323+
"""
324+
purl_data = PackageURL.from_string(purl)
325+
namespace = purl_data.namespace
326+
name = purl_data.name
327+
version = purl_data.version
328+
qualifiers = purl_data.qualifiers
329+
330+
base_url = qualifiers.get("repository_url", DEFAULT_MAVEN_REPOSITORY)
331+
332+
if namespace and name and version:
333+
namespace = namespace.replace(".", "/")
334+
return f"{base_url}/{namespace}/{name}/{version}"
335+
336+
317337
# Download URLs:
318338

319339

@@ -365,6 +385,28 @@ def build_npm_download_url(purl):
365385
return f"{base_url}/{name}/-/{name}-{version}.tgz"
366386

367387

388+
@download_router.route("pkg:maven/.*")
389+
def build_maven_download_url(purl):
390+
"""
391+
Return a maven download URL from the `purl` string.
392+
"""
393+
purl_data = PackageURL.from_string(purl)
394+
395+
namespace = purl_data.namespace
396+
name = purl_data.name
397+
version = purl_data.version
398+
qualifiers = purl_data.qualifiers
399+
400+
base_url = qualifiers.get("repository_url", DEFAULT_MAVEN_REPOSITORY)
401+
maven_type = qualifiers.get("type", "jar") # default to "jar"
402+
classifier = qualifiers.get("classifier")
403+
404+
if namespace and name and version:
405+
namespace = namespace.replace(".", "/")
406+
classifier = f"-{classifier}" if classifier else ""
407+
return f"{base_url}/{namespace}/{name}/{version}/{name}-{version}{classifier}.{maven_type}"
408+
409+
368410
@download_router.route("pkg:hackage/.*")
369411
def build_hackage_download_url(purl):
370412
"""

tests/contrib/test_purl2url.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ def test_purl2url_get_repo_url():
6868
"pkg:golang/gopkg.in/[email protected]": "https://pkg.go.dev/gopkg.in/[email protected]",
6969
"pkg:cocoapods/[email protected]": "https://cocoapods.org/pods/AFNetworking",
7070
"pkg:cocoapods/[email protected]": "https://cocoapods.org/pods/MapsIndoors",
71+
"pkg:maven/org.apache.commons/[email protected]": "https://repo.maven.apache.org/maven2/org/apache/commons/commons-io/1.3.2",
72+
"pkg:maven/org.apache.commons/[email protected]?repository_url=https://repo1.maven.org/maven2": "https://repo1.maven.org/maven2/org/apache/commons/commons-io/1.3.2",
7173
}
7274

7375
for purl, url in purls_url.items():
@@ -92,6 +94,10 @@ def test_purl2url_get_download_url():
9294
"pkg:gitlab/tg1999/firebase@1a122122": "https://gitlab.com/tg1999/firebase/-/archive/1a122122/firebase-1a122122.tar.gz",
9395
"pkg:gitlab/tg1999/firebase@1a122122?version_prefix=v": "https://gitlab.com/tg1999/firebase/-/archive/v1a122122/firebase-v1a122122.tar.gz",
9496
"pkg:gitlab/hoppr/[email protected]": "https://gitlab.com/hoppr/hoppr/-/archive/v1.11.1-dev.2/hoppr-v1.11.1-dev.2.tar.gz",
97+
"pkg:maven/org.apache.commons/[email protected]": "https://repo.maven.apache.org/maven2/org/apache/commons/commons-io/1.3.2/commons-io-1.3.2.jar",
98+
"pkg:maven/org.apache.commons/[email protected]?repository_url=https://repo1.maven.org/maven2": "https://repo1.maven.org/maven2/org/apache/commons/commons-io/1.3.2/commons-io-1.3.2.jar",
99+
"pkg:maven/org.apache.commons/[email protected]?type=pom": "https://repo.maven.apache.org/maven2/org/apache/commons/commons-io/1.3.2/commons-io-1.3.2.pom",
100+
"pkg:maven/org.apache.commons/[email protected]?classifier=sources": "https://repo.maven.apache.org/maven2/org/apache/commons/commons-math3/3.6.1/commons-math3-3.6.1-sources.jar",
95101
# From `download_url` qualifier
96102
"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",
97103
"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",
@@ -150,6 +156,10 @@ def test_purl2url_get_inferred_urls():
150156
"pkg:cocoapods/[email protected]": ["https://cocoapods.org/pods/AFNetworking"],
151157
"pkg:composer/psr/[email protected]": ["https://packagist.org/packages/psr/log#1.1.3"],
152158
"pkg:rubygems/package-name": ["https://rubygems.org/gems/package-name"],
159+
"pkg:maven/org.apache.commons/[email protected]": [
160+
"https://repo.maven.apache.org/maven2/org/apache/commons/commons-io/1.3.2",
161+
"https://repo.maven.apache.org/maven2/org/apache/commons/commons-io/1.3.2/commons-io-1.3.2.jar",
162+
],
153163
"pkg:bitbucket/birkenfeld": [],
154164
}
155165

0 commit comments

Comments
 (0)