Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[extractor/golang] Generate valid golang purls #501

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions extractor/filesystem/language/golang/gobinary/gobinary.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,12 @@ func parseDependency(d *debug.Module) (string, string) {

// ToPURL converts an inventory created by this extractor into a PURL.
func (e Extractor) ToPURL(i *extractor.Inventory) *purl.PackageURL {
nameParts := strings.Split(i.Name, "/")
return &purl.PackageURL{
Type: purl.TypeGolang,
Name: i.Name,
Version: i.Version,
Type: purl.TypeGolang,
Name: nameParts[len(nameParts)-1],
Namespace: strings.Join(nameParts[:len(nameParts)-1], "/"),
Version: i.Version,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,15 @@ func TestExtract(t *testing.T) {
func TestToPURL(t *testing.T) {
e := gobinary.Extractor{}
i := &extractor.Inventory{
Name: "name",
Name: "github.com/google/osv-scalibr",
Version: "1.2.3",
Locations: []string{"location"},
}
want := &purl.PackageURL{
Type: purl.TypeGolang,
Name: "name",
Version: "1.2.3",
Type: purl.TypeGolang,
Name: "osv-scalibr",
Namespace: "github.com/google",
Version: "1.2.3",
}
got := e.ToPURL(i)
if diff := cmp.Diff(want, got); diff != "" {
Expand Down
8 changes: 5 additions & 3 deletions extractor/filesystem/language/golang/gomod/gomod.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,12 @@ func (e Extractor) Extract(ctx context.Context, input *filesystem.ScanInput) ([]

// ToPURL converts an inventory created by this extractor into a PURL.
func (e Extractor) ToPURL(i *extractor.Inventory) *purl.PackageURL {
nameParts := strings.Split(i.Name, "/")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're doing this in two places can you move the ToPURL logic into a separate library inside language/golang, similar to https://github.com/google/osv-scalibr/blob/main/extractor/filesystem/language/python/internal/pypipurl/pythonpurl.go?

return &purl.PackageURL{
Type: purl.TypeGolang,
Name: i.Name,
Version: i.Version,
Type: purl.TypeGolang,
Name: nameParts[len(nameParts)-1],
Namespace: strings.Join(nameParts[:len(nameParts)-1], "/"),
Version: i.Version,
}
}

Expand Down
19 changes: 19 additions & 0 deletions extractor/filesystem/language/golang/gomod/gomod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/google/osv-scalibr/extractor"
"github.com/google/osv-scalibr/extractor/filesystem/language/golang/gomod"
"github.com/google/osv-scalibr/extractor/filesystem/simplefileapi"
"github.com/google/osv-scalibr/purl"
"github.com/google/osv-scalibr/testing/extracttest"
)

Expand Down Expand Up @@ -278,3 +279,21 @@ func TestExtractor_Extract(t *testing.T) {
})
}
}

func TestExtractor_ToPURL(t *testing.T) {
e := gomod.Extractor{}
i := &extractor.Inventory{
Name: "github.com/google/osv-scalibr",
Version: "1.2.3",
}
want := &purl.PackageURL{
Type: purl.TypeGolang,
Name: "osv-scalibr",
Namespace: "github.com/google",
Version: "1.2.3",
}
got := e.ToPURL(i)
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("ToPURL(%v) (-want +got):\n%s", i, diff)
}
}
Loading