diff --git a/src/packageurl/utils.py b/src/packageurl/utils.py index c8a528f..9b44433 100644 --- a/src/packageurl/utils.py +++ b/src/packageurl/utils.py @@ -30,12 +30,17 @@ def get_golang_purl(go_package: str): """ Return a PackageURL object given an imported ``go_package`` + >>> 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") """ if not go_package: return + version = None + 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) diff --git a/tests/contrib/test_utils.py b/tests/contrib/test_utils.py index 6f92d7f..6baac9e 100644 --- a/tests/contrib/test_utils.py +++ b/tests/contrib/test_utils.py @@ -66,3 +66,7 @@ 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 + )