Skip to content
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

Add support for go packages in manifest files #148

Merged
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
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
=========

Next Release (2024-02-29)
-------------------------

- Add support to get PackageURL from ``go_package`` or
go module "name version" string as seen in a go.mod file.


0.14.0 (2024-02-29)
-------------------

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ known_django = django
sections = FUTURE,STDLIB,DJANGO,THIRDPARTY,FIRSTPARTY,LOCALFOLDER

[mypy]
python_version = 3.7
python_version = 3.8

files = src/packageurl/__init__.py
show_error_codes = True
Expand Down
14 changes: 13 additions & 1 deletion src/packageurl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,24 @@
def get_golang_purl(go_package: str):
"""
Return a PackageURL object given an imported ``go_package``
TG1999 marked this conversation as resolved.
Show resolved Hide resolved
or go module "name version" string as seen in a go.mod file.
>>> get_golang_purl(go_package="github.com/gorilla/mux v1.8.1")
PackageURL(type='golang', namespace='github.com/gorilla', name='mux', version='v1.8.1', qualifiers={}, subpath=None)
"""
if not go_package:
return
version = None
# Go package in *.mod files is represented like this
# package version
# github.com/gorilla/mux v1.8.1
# https://github.com/moby/moby/blob/6c10086976d07d4746e03dcfd188972a2f07e1c9/vendor.mod#L51
if "@" in go_package:
raise Exception(f"{go_package} should not contain ``@``")
if " " in go_package:
go_package, _, version = go_package.rpartition(" ")
parts = go_package.split("/")
if not parts:
return
name = parts[-1]
namespace = "/".join(parts[:-1])
return PackageURL(type="golang", namespace=namespace, name=name)
return PackageURL(type="golang", namespace=namespace, name=name, version=version)
8 changes: 8 additions & 0 deletions tests/contrib/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
# Visit https://github.com/package-url/packageurl-python for support and
# download.

import pytest

from packageurl.contrib.django.utils import purl_to_lookups
from packageurl.utils import get_golang_purl

Expand Down Expand Up @@ -66,3 +68,9 @@ def test_get_golang_purl():
)
assert golang_purl_1.name == "v3"
assert golang_purl_1.namespace == "github.com/envoyproxy/go-control-plane/envoy/config/listener"
golang_purl_2 = get_golang_purl(
go_package="github.com/grpc-ecosystem/go-grpc-middleware v1.3.0"
)
assert "pkg:golang/github.com/grpc-ecosystem/go-grpc-middleware@v1.3.0" == str(golang_purl_2)
with pytest.raises(Exception):
get_golang_purl("github.com/envoyproxy/go-control-plane/envoy/config/listener@v3.1")
Loading