Skip to content

Commit

Permalink
Merge pull request #156 from zvigrinberg/fix-golang-purl-parsing
Browse files Browse the repository at this point in the history
fix: sbom golang purl parsing failing
  • Loading branch information
ruromero authored Sep 15, 2023
2 parents e212290 + cd8288a commit cc7b7ca
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/redhat/exhort/api/PackageRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ public PackageURL purl() {
}

public String name() {
if (purl.getNamespace() == null) {
return purl.getName();
}
switch (purl.getType()) {
case Constants.GOLANG_PKG_MANAGER:
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();
}
}
Expand Down
53 changes: 53 additions & 0 deletions src/test/java/com/redhat/exhort/api/PackageRefTest.java
Original file line number Diff line number Diff line change
@@ -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/[email protected]");
assertEquals("go.opencensus.io", ref.name());

ref = new PackageRef("pkg:npm/[email protected]");
assertEquals("foobar", ref.name());

ref = new PackageRef("pkg:maven/org.apache.xmlgraphics/[email protected]?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/[email protected]");
assertEquals("v0.21.0", ref.version());
}
}

0 comments on commit cc7b7ca

Please sign in to comment.