Skip to content

Commit

Permalink
fix: golang purl resolution (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
ninjamast3r authored and garethr committed Aug 13, 2023
1 parent e762d1f commit 4e33b08
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ dist
# Dependency directories (remove the comment below to include it)
# vendor/

# IDE configs
.idea

/parlay
23 changes: 11 additions & 12 deletions lib/ecosystems/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,21 @@ func purlToEcosystemsRegistry(purl packageurl.PackageURL) string {
}

func purlToEcosystemsName(purl packageurl.PackageURL) string {
if purl.Namespace == "" {
return purl.Name
}

var name string
// npm names in the ecosyste.ms API include the purl namespace
// followed by a / and are url encoded. Other package managers
// appear to separate the purl namespace and name with a :
if purl.Type == "npm" {
if purl.Namespace != "" {
name = url.QueryEscape(fmt.Sprintf("%s/%s", purl.Namespace, purl.Name))
} else {
name = purl.Name
}
} else {
if purl.Namespace != "" {
name = fmt.Sprintf("%s:%s", purl.Namespace, purl.Name)
} else {
name = purl.Name
}
switch purl.Type {
case "npm":
name = url.QueryEscape(fmt.Sprintf("%s/%s", purl.Namespace, purl.Name))
case "golang":
name = fmt.Sprintf("%s/%s", purl.Namespace, purl.Name)
default:
name = fmt.Sprintf("%s:%s", purl.Namespace, purl.Name)
}
return name
}
14 changes: 14 additions & 0 deletions lib/ecosystems/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,20 @@ func TestPurlToEcosystemsName(t *testing.T) {
purlStr: "pkg:maven/my-artifact",
expectedName: "my-artifact",
},
{
// Test case 5: When the package manager type is "golang"
// and the namespace is not empty, the function should return
// a string in the form of "<namespace>/<name>"
purlStr: "pkg:golang/example.com/foo/[email protected]",
expectedName: "example.com/foo/bar",
},
{
// Test case 6: When the package manager type is "golang"
// and the namespace has lots of weird characters, make sure
// they get filtered properly
purlStr: "pkg:golang/example.com/f.o_o/ba~r",
expectedName: "example.com/f.o_o/ba~r",
},
}

for _, testCase := range testCases {
Expand Down

0 comments on commit 4e33b08

Please sign in to comment.