Skip to content

Commit

Permalink
Modify from_string for golang purls (#115)
Browse files Browse the repository at this point in the history
* Modify from_string for golang purls

Signed-off-by: Tushar Goel <[email protected]>

* Fix failing tests

Signed-off-by: Tushar Goel <[email protected]>

* Address review comments

Signed-off-by: Tushar Goel <[email protected]>

---------

Signed-off-by: Tushar Goel <[email protected]>
Co-authored-by: tdruez <[email protected]>
  • Loading branch information
TG1999 and tdruez authored Dec 8, 2023
1 parent b3ed6ac commit baf0c61
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

0.11.4 (2023-12-08)
-------------------

- Modified `PackageURL.from_string` to properly handle golang purls.

0.11.3 (2023-12-08)
--------------------

Expand Down
7 changes: 6 additions & 1 deletion src/packageurl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ def from_string(cls, purl: str) -> "PackageURL":
if not type or not sep:
raise ValueError(f"purl is missing the required type component: {repr(purl)}.")

type = type.lower()

scheme, authority, path, qualifiers_str, subpath = _urlsplit(
url=remainder, scheme="", allow_fragments=True
)
Expand Down Expand Up @@ -530,7 +532,10 @@ def from_string(cls, purl: str) -> "PackageURL":
ns_name_parts = ns_name.split("/")
ns_name_parts = [seg for seg in ns_name_parts if seg and seg.strip()]
name = ""
if not namespace and len(ns_name_parts) > 1:
if type == "golang":
name = "/".join(ns_name_parts)
namespace = ""
elif not namespace and len(ns_name_parts) > 1:
name = ns_name_parts[-1]
ns = ns_name_parts[0:-1]
namespace = "/".join(ns)
Expand Down
5 changes: 2 additions & 3 deletions src/packageurl/contrib/purl2url.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,13 @@ def build_golang_repo_url(purl):
"""
purl_data = PackageURL.from_string(purl)

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

if name and version:
return f"https://pkg.go.dev/{namespace}/{name}@{version}"
return f"https://pkg.go.dev/{name}@{version}"
elif name:
return f"https://pkg.go.dev/{namespace}/{name}"
return f"https://pkg.go.dev/{name}"


# Download URLs:
Expand Down
32 changes: 28 additions & 4 deletions tests/data/test-suite-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"purl": "pkg:GOLANG/google.golang.org/genproto#/googleapis/api/annotations/",
"canonical_purl": "pkg:golang/google.golang.org/genproto#googleapis/api/annotations",
"type": "golang",
"namespace": "google.golang.org",
"name": "genproto",
"namespace": null,
"name": "google.golang.org/genproto",
"version": null,
"qualifiers": null,
"subpath": "googleapis/api/annotations",
Expand All @@ -40,13 +40,25 @@
"purl": "pkg:GOLANG/google.golang.org/genproto@abcdedf#/googleapis/api/annotations/",
"canonical_purl": "pkg:golang/google.golang.org/genproto@abcdedf#googleapis/api/annotations",
"type": "golang",
"namespace": "google.golang.org",
"name": "genproto",
"namespace": null,
"name": "google.golang.org/genproto",
"version": "abcdedf",
"qualifiers": null,
"subpath": "googleapis/api/annotations",
"is_invalid": false
},
{
"description": "valid golang purl",
"purl": "pkg:golang/github.com/nats-io/nats-server/v2/[email protected]",
"canonical_purl": "pkg:golang/github.com/nats-io/nats-server/v2/[email protected]",
"type": "golang",
"namespace": null,
"name": "github.com/nats-io/nats-server/v2/server",
"version": "v1.2.9",
"qualifiers": null,
"subpath": null,
"is_invalid": false
},
{
"description": "bitbucket namespace and name should be lowercased",
"purl": "pkg:bitbucket/birKenfeld/pyGments-main@244fd47e07d1014f0aed9c",
Expand Down Expand Up @@ -346,5 +358,17 @@
"qualifiers": null,
"subpath": "googleapis/api/annotations",
"is_invalid": false
},
{
"description": "valid npm purl without namespace, version and subpath",
"purl": "pkg:NPM/core#/googleapis/api/annotations/",
"canonical_purl": "pkg:npm/core#googleapis/api/annotations",
"type": "npm",
"namespace": null,
"name": "core",
"version": null,
"qualifiers": null,
"subpath": "googleapis/api/annotations",
"is_invalid": false
}
]

0 comments on commit baf0c61

Please sign in to comment.