Skip to content

Commit 8120d8e

Browse files
authored
Add support for go packages in manifest files (#148)
* Add support for go packages in manifest files Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com> * Fix failing tests Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com> * Add documentation about go packages in mod files Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com> * Fix doctests Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com> * Fix linting errors Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com> * Raise exception when go_package contains @ Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com> * Add tests Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com> * Address review comments Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com> * Change python_version to 3.8 for mypy Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com> --------- Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent 6d37742 commit 8120d8e

4 files changed

Lines changed: 29 additions & 2 deletions

File tree

CHANGELOG.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
Next Release (2024-02-29)
5+
-------------------------
6+
7+
- Add support to get PackageURL from ``go_package`` or
8+
go module "name version" string as seen in a go.mod file.
9+
10+
411
0.14.0 (2024-02-29)
512
-------------------
613

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ known_django = django
6868
sections = FUTURE,STDLIB,DJANGO,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
6969

7070
[mypy]
71-
python_version = 3.7
71+
python_version = 3.8
7272

7373
files = src/packageurl/__init__.py
7474
show_error_codes = True

src/packageurl/utils.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,24 @@
3030
def get_golang_purl(go_package: str):
3131
"""
3232
Return a PackageURL object given an imported ``go_package``
33+
or go module "name version" string as seen in a go.mod file.
34+
>>> get_golang_purl(go_package="github.com/gorilla/mux v1.8.1")
35+
PackageURL(type='golang', namespace='github.com/gorilla', name='mux', version='v1.8.1', qualifiers={}, subpath=None)
3336
"""
3437
if not go_package:
3538
return
39+
version = None
40+
# Go package in *.mod files is represented like this
41+
# package version
42+
# github.com/gorilla/mux v1.8.1
43+
# https://github.com/moby/moby/blob/6c10086976d07d4746e03dcfd188972a2f07e1c9/vendor.mod#L51
44+
if "@" in go_package:
45+
raise Exception(f"{go_package} should not contain ``@``")
46+
if " " in go_package:
47+
go_package, _, version = go_package.rpartition(" ")
3648
parts = go_package.split("/")
3749
if not parts:
3850
return
3951
name = parts[-1]
4052
namespace = "/".join(parts[:-1])
41-
return PackageURL(type="golang", namespace=namespace, name=name)
53+
return PackageURL(type="golang", namespace=namespace, name=name, version=version)

tests/contrib/test_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
# Visit https://github.com/package-url/packageurl-python for support and
2525
# download.
2626

27+
import pytest
28+
2729
from packageurl.contrib.django.utils import purl_to_lookups
2830
from packageurl.utils import get_golang_purl
2931

@@ -66,3 +68,9 @@ def test_get_golang_purl():
6668
)
6769
assert golang_purl_1.name == "v3"
6870
assert golang_purl_1.namespace == "github.com/envoyproxy/go-control-plane/envoy/config/listener"
71+
golang_purl_2 = get_golang_purl(
72+
go_package="github.com/grpc-ecosystem/go-grpc-middleware v1.3.0"
73+
)
74+
assert "pkg:golang/github.com/grpc-ecosystem/go-grpc-middleware@v1.3.0" == str(golang_purl_2)
75+
with pytest.raises(Exception):
76+
get_golang_purl("github.com/envoyproxy/go-control-plane/envoy/config/listener@v3.1")

0 commit comments

Comments
 (0)