Skip to content

Commit

Permalink
Better (rather than good) error handling
Browse files Browse the repository at this point in the history
Dealing with some inconsistencies between purl and ecosyste.ms
  • Loading branch information
garethr committed Apr 22, 2023
1 parent 1551196 commit fb4355f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
28 changes: 13 additions & 15 deletions internal/commands/enrich.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
19 changes: 18 additions & 1 deletion pkg/parlay/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package parlay

import (
"context"
"fmt"
"net/url"

"github.com/snyk/parlay/pkg/ecosystems/packages"

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fb4355f

Please sign in to comment.