Skip to content

Commit 9d7ba95

Browse files
pxp928jeffmendoza
authored andcommitted
fix golang coordinate to ensure valid coordinate
Signed-off-by: pxp928 <parth.psu@gmail.com>
1 parent 5a88b66 commit 9d7ba95

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

coordinates/coordinates.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"strings"
2121

2222
purl "github.com/package-url/packageurl-go"
23+
"golang.org/x/mod/semver"
2324
)
2425

2526
type Coordinate struct {
@@ -238,7 +239,7 @@ func ConvertPurlToCoordinate(purlUri string) (*Coordinate, error) {
238239
Provider: pkg.Type,
239240
Namespace: emptyToHyphen(strings.ReplaceAll(pkg.Namespace, "/", "%2f")),
240241
Name: pkg.Name,
241-
Revision: "v" + pkg.Version,
242+
Revision: ensureSemverPrefixGolang(pkg.Version),
242243
}, nil
243244
case "maven":
244245
// maven is a unique case where it can have 3 Providers
@@ -307,3 +308,18 @@ func emptyToHyphen(namespace string) string {
307308
return namespace
308309
}
309310
}
311+
312+
// ensureSemverPrefixGolang checks if the version string is valid SemVer and ensures it starts with "v" for golang version
313+
func ensureSemverPrefixGolang(version string) string {
314+
if semver.IsValid(version) {
315+
return version
316+
}
317+
318+
vPrefixed := "v" + version
319+
if semver.IsValid(vPrefixed) {
320+
return vPrefixed
321+
}
322+
323+
// If not a valid SemVer, return as-is
324+
return version
325+
}

coordinates/coordinates_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ func TestConvertPurlToCoordinate(t *testing.T) {
160160
Revision: "244fd47e07d1004",
161161
},
162162
wantErr: false,
163-
}, {
163+
},
164+
{
164165
Name: "golang",
165166
purlUri: "pkg:golang/cloud.google.com/go/compute@1.23.0",
166167
want: &Coordinate{
@@ -172,6 +173,18 @@ func TestConvertPurlToCoordinate(t *testing.T) {
172173
},
173174
wantErr: false,
174175
},
176+
{
177+
Name: "golang",
178+
purlUri: "pkg:golang/github.com/aws/aws-lambda-go@v1.46.0",
179+
want: &Coordinate{
180+
CoordinateType: "go",
181+
Provider: "golang",
182+
Namespace: "github.com%2faws",
183+
Name: "aws-lambda-go",
184+
Revision: "v1.46.0",
185+
},
186+
wantErr: false,
187+
},
175188
{
176189
Name: "golang",
177190
purlUri: "pkg:golang/context@234fd47e07d1004f0aed9c#api",
@@ -180,7 +193,7 @@ func TestConvertPurlToCoordinate(t *testing.T) {
180193
Provider: "golang",
181194
Namespace: "-",
182195
Name: "context",
183-
Revision: "v234fd47e07d1004f0aed9c",
196+
Revision: "234fd47e07d1004f0aed9c",
184197
},
185198
wantErr: false,
186199
}, {

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/guacsec/sw-id-core
22

33
go 1.23.3
44

5-
require github.com/package-url/packageurl-go v0.1.3 // indirect
5+
require (
6+
github.com/package-url/packageurl-go v0.1.3
7+
golang.org/x/mod v0.24.0
8+
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
github.com/package-url/packageurl-go v0.1.3 h1:4juMED3hHiz0set3Vq3KeQ75KD1avthoXLtmE3I0PLs=
22
github.com/package-url/packageurl-go v0.1.3/go.mod h1:nKAWB8E6uk1MHqiS/lQb9pYBGH2+mdJ2PJc2s50dQY0=
3+
golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
4+
golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=

0 commit comments

Comments
 (0)