From bdab5da84c2d903d391491b05e228d31eea66c6c Mon Sep 17 00:00:00 2001 From: Zvi Grinberg Date: Thu, 14 Sep 2023 17:00:24 +0300 Subject: [PATCH 1/2] fix: sbom golang purl parsing Signed-off-by: Zvi Grinberg --- src/main/java/com/redhat/exhort/api/PackageRef.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/redhat/exhort/api/PackageRef.java b/src/main/java/com/redhat/exhort/api/PackageRef.java index b646377f..ab762a0e 100644 --- a/src/main/java/com/redhat/exhort/api/PackageRef.java +++ b/src/main/java/com/redhat/exhort/api/PackageRef.java @@ -54,7 +54,13 @@ public PackageURL purl() { public String name() { switch (purl.getType()) { case Constants.GOLANG_PKG_MANAGER: - return new StringBuffer(purl.getNamespace()).append("/").append(purl.getName()).toString(); + if (purl().getNamespace() != null) { + return new StringBuffer(purl.getNamespace()) + .append("/") + .append(purl.getName()) + .toString(); + } + return purl.getName(); default: if (purl.getNamespace() == null) { return purl.getName(); From cd8288aa3b6b86013bc556869a4c609f0732856f Mon Sep 17 00:00:00 2001 From: Ruben Romero Montes Date: Fri, 15 Sep 2023 11:20:54 +0200 Subject: [PATCH 2/2] chore: add tests and refactor null check Signed-off-by: Ruben Romero Montes --- .../com/redhat/exhort/api/PackageRef.java | 14 ++--- .../com/redhat/exhort/api/PackageRefTest.java | 53 +++++++++++++++++++ 2 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 src/test/java/com/redhat/exhort/api/PackageRefTest.java diff --git a/src/main/java/com/redhat/exhort/api/PackageRef.java b/src/main/java/com/redhat/exhort/api/PackageRef.java index ab762a0e..a1c2d945 100644 --- a/src/main/java/com/redhat/exhort/api/PackageRef.java +++ b/src/main/java/com/redhat/exhort/api/PackageRef.java @@ -52,19 +52,13 @@ public PackageURL purl() { } public String name() { + if (purl.getNamespace() == null) { + return purl.getName(); + } switch (purl.getType()) { case Constants.GOLANG_PKG_MANAGER: - if (purl().getNamespace() != null) { - return new StringBuffer(purl.getNamespace()) - .append("/") - .append(purl.getName()) - .toString(); - } - return purl.getName(); + return new StringBuffer(purl.getNamespace()).append("/").append(purl.getName()).toString(); default: - if (purl.getNamespace() == null) { - return purl.getName(); - } return new StringBuilder(purl.getNamespace()).append(":").append(purl.getName()).toString(); } } diff --git a/src/test/java/com/redhat/exhort/api/PackageRefTest.java b/src/test/java/com/redhat/exhort/api/PackageRefTest.java new file mode 100644 index 00000000..bb08a538 --- /dev/null +++ b/src/test/java/com/redhat/exhort/api/PackageRefTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2023 Red Hat, Inc. and/or its affiliates + * and other contributors as indicated by the @author tags. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.redhat.exhort.api; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.junit.jupiter.api.Test; + +public class PackageRefTest { + + @Test + public void testNamespace() { + PackageRef ref = + new PackageRef("pkg:golang/google.golang.org/genproto#googleapis/api/annotations"); + assertEquals("google.golang.org/genproto", ref.name()); + + ref = new PackageRef("pkg:golang/go.opencensus.io@v0.21.0"); + assertEquals("go.opencensus.io", ref.name()); + + ref = new PackageRef("pkg:npm/foobar@12.3.1"); + assertEquals("foobar", ref.name()); + + ref = new PackageRef("pkg:maven/org.apache.xmlgraphics/batik-anim@1.9.1?packaging=sources"); + assertEquals("org.apache.xmlgraphics:batik-anim", ref.name()); + } + + @Test + public void testVersion() { + PackageRef ref = + new PackageRef("pkg:golang/google.golang.org/genproto#googleapis/api/annotations"); + assertNull(ref.version()); + + ref = new PackageRef("pkg:golang/go.opencensus.io@v0.21.0"); + assertEquals("v0.21.0", ref.version()); + } +}