Skip to content

Commit 593d04c

Browse files
erikvargacopybara-github
authored andcommitted
Enable storing PURL-less packages in PackageIndex.
PiperOrigin-RevId: 770555219
1 parent 84941b4 commit 593d04c

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

packageindex/package_index.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ type PackageIndex struct {
3030
func New(pkgs []*extractor.Package) (*PackageIndex, error) {
3131
pkgMap := make(map[string]map[string][]*extractor.Package)
3232
for _, pkg := range pkgs {
33-
p := pkg.PURL()
34-
if p == nil {
35-
continue
33+
name := pkg.Name
34+
purlType := pkg.PURLType
35+
if p := pkg.PURL(); p != nil {
36+
name = p.Name
37+
purlType = p.Type
3638
}
37-
if _, ok := pkgMap[p.Type]; !ok {
38-
pkgMap[p.Type] = make(map[string][]*extractor.Package)
39+
if _, ok := pkgMap[purlType]; !ok {
40+
pkgMap[purlType] = make(map[string][]*extractor.Package)
3941
}
40-
pkgMap[p.Type][p.Name] = append(pkgMap[p.Type][p.Name], pkg)
42+
pkgMap[purlType][name] = append(pkgMap[purlType][name], pkg)
4143
}
4244
return &PackageIndex{pkgMap: pkgMap}, nil
4345
}

packageindex/package_index_test.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ var (
3636

3737
func TestGetAll(t *testing.T) {
3838
pkgs := []*extractor.Package{
39-
{Name: "software1", Plugins: []string{packagejson.Name}, PURLType: purl.TypeNPM},
40-
{Name: "software2", Plugins: []string{wheelegg.Name}, PURLType: purl.TypePyPi},
41-
{Name: "software3", Plugins: []string{wheelegg.Name}, PURLType: purl.TypePyPi},
39+
{Name: "software1", PURLType: purl.TypeNPM},
40+
{Name: "software2", PURLType: purl.TypePyPi},
41+
{Name: "software3", PURLType: purl.TypePyPi},
42+
{Name: "software-no-purl"},
4243
}
4344
want := pkgs
4445

@@ -55,13 +56,14 @@ func TestGetAll(t *testing.T) {
5556

5657
func TestGetAllOfType(t *testing.T) {
5758
pkgs := []*extractor.Package{
58-
{Name: "software1", Plugins: []string{packagejson.Name}, PURLType: purl.TypeNPM},
59-
{Name: "software2", Plugins: []string{wheelegg.Name}, PURLType: purl.TypePyPi},
60-
{Name: "software3", Plugins: []string{wheelegg.Name}, PURLType: purl.TypePyPi},
59+
{Name: "software1", PURLType: purl.TypeNPM},
60+
{Name: "software2", PURLType: purl.TypePyPi},
61+
{Name: "software3", PURLType: purl.TypePyPi},
62+
{Name: "software-no-purl"},
6163
}
6264
want := []*extractor.Package{
63-
{Name: "software2", Plugins: []string{wheelegg.Name}, PURLType: purl.TypePyPi},
64-
{Name: "software3", Plugins: []string{wheelegg.Name}, PURLType: purl.TypePyPi},
65+
{Name: "software2", PURLType: purl.TypePyPi},
66+
{Name: "software3", PURLType: purl.TypePyPi},
6567
}
6668

6769
px, err := packageindex.New(pkgs)
@@ -76,12 +78,13 @@ func TestGetAllOfType(t *testing.T) {
7678
}
7779

7880
func TestGetSpecific(t *testing.T) {
79-
pkg1 := &extractor.Package{Name: "software1", Version: "1.2.3", Plugins: []string{packagejson.Name}, PURLType: purl.TypeNPM}
80-
pkg2 := &extractor.Package{Name: "software2", Version: "1.2.3", Plugins: []string{wheelegg.Name}, PURLType: purl.TypePyPi}
81-
pkg3 := &extractor.Package{Name: "software3", Plugins: []string{wheelegg.Name}, PURLType: purl.TypePyPi}
82-
pkg4v123 := &extractor.Package{Name: "software4", Version: "1.2.3", Plugins: []string{packagejson.Name}, PURLType: purl.TypeNPM}
83-
pkg4v456 := &extractor.Package{Name: "software4", Version: "4.5.6", Plugins: []string{packagejson.Name}, PURLType: purl.TypeNPM}
84-
pkgs := []*extractor.Package{pkg1, pkg2, pkg3, pkg4v123, pkg4v456}
81+
pkg1 := &extractor.Package{Name: "software1", Version: "1.2.3", PURLType: purl.TypeNPM}
82+
pkg2 := &extractor.Package{Name: "software2", Version: "1.2.3", PURLType: purl.TypePyPi}
83+
pkg3 := &extractor.Package{Name: "software3", PURLType: purl.TypePyPi}
84+
pkg4v123 := &extractor.Package{Name: "software4", Version: "1.2.3", PURLType: purl.TypeNPM}
85+
pkg4v456 := &extractor.Package{Name: "software4", Version: "4.5.6", PURLType: purl.TypeNPM}
86+
pkgNoPURL := &extractor.Package{Name: "software-no-purl", Version: "1.2.3"}
87+
pkgs := []*extractor.Package{pkg1, pkg2, pkg3, pkg4v123, pkg4v456, pkgNoPURL}
8588

8689
testCases := []struct {
8790
desc string
@@ -113,6 +116,12 @@ func TestGetSpecific(t *testing.T) {
113116
pkgName: "software4",
114117
want: []*extractor.Package{pkg4v123, pkg4v456},
115118
},
119+
{
120+
desc: "no purl type",
121+
pkgType: "",
122+
pkgName: "software-no-purl",
123+
want: []*extractor.Package{pkgNoPURL},
124+
},
116125
}
117126

118127
for _, tc := range testCases {

0 commit comments

Comments
 (0)