diff --git a/internal/commands/enrich.go b/internal/commands/enrich.go index 4cb5ea1..9744a31 100644 --- a/internal/commands/enrich.go +++ b/internal/commands/enrich.go @@ -7,7 +7,6 @@ import ( "log" "os" - "github.com/snyk/parlay/pkg/ecosystems/packages" "github.com/snyk/parlay/pkg/parlay" cdx "github.com/CycloneDX/cyclonedx-go" @@ -47,12 +46,19 @@ func NewEnrichCommand(logger *log.Logger) *cobra.Command { wg.Add() go func(component cdx.Component, i int) { purl, _ := packageurl.FromString(component.PackageURL) - packageData := query(purl) - //logger.Printf("Looking up: %s", i) - component.Description = *packageData.Description - lice := cdx.LicenseChoice{Expression: *packageData.Licenses} - component.Licenses = &cdx.Licenses{lice} - //logger.Printf("Desc for %s: %s", i, update) + resp, err := parlay.GetPackageData(purl) + if err == nil { + packageData := resp.JSON200 + if packageData != nil { + if packageData.Description != nil { + component.Description = *packageData.Description + } + if packageData.Licenses != nil { + licences := cdx.LicenseChoice{Expression: *packageData.Licenses} + component.Licenses = &cdx.Licenses{licences} + } + } + } newComponents[i] = component wg.Done() }(component, i) @@ -67,11 +73,3 @@ func NewEnrichCommand(logger *log.Logger) *cobra.Command { } return &cmd } - -func query(i packageurl.PackageURL) packages.Package { - resp, err := parlay.GetPackageData(i) - if err != nil { - panic(err) - } - return *resp.JSON200 -} diff --git a/pkg/parlay/package.go b/pkg/parlay/package.go index 7edb0e2..7221d11 100644 --- a/pkg/parlay/package.go +++ b/pkg/parlay/package.go @@ -2,6 +2,8 @@ package parlay import ( "context" + "fmt" + "net/url" "github.com/snyk/parlay/pkg/ecosystems/packages" @@ -36,7 +38,22 @@ func GetPackageData(purl packageurl.PackageURL) (*packages.GetRegistryPackageRes "apk": "alpine", } - resp, err := client.GetRegistryPackageWithResponse(context.Background(), mapping[purl.Type], purl.Name) + var name string + 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 + } + } + + resp, err := client.GetRegistryPackageWithResponse(context.Background(), mapping[purl.Type], name) if err != nil { return nil, err