Skip to content

Commit d06937f

Browse files
rpbeltrancopybara-github
authored andcommitted
Use package IDs from inventory in SPDX converter
PiperOrigin-RevId: 952533748
1 parent 87eb9df commit d06937f

2 files changed

Lines changed: 97 additions & 1 deletion

File tree

converter/spdx/spdx.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"time"
2222

2323
"bitbucket.org/creachadair/stringset"
24+
"github.com/google/osv-scalibr/extractor"
2425
"github.com/google/osv-scalibr/inventory"
2526
"github.com/google/osv-scalibr/log"
2627
"github.com/google/uuid"
@@ -86,7 +87,12 @@ func ToSPDX23(i inventory.Inventory, c Config) *v2_3.Document {
8687
log.Warnf("Package %v PURL name or version empty, skipping", pkg)
8788
continue
8889
}
89-
pID := SPDXRefPrefix + "Package-" + replaceSPDXIDInvalidChars(pName) + "-" + uuid.New().String()
90+
id, err := pkg.GetIDOrGenerate(&extractor.RandomIDGenerator{})
91+
if err != nil {
92+
log.Warnf("Failed to get or generate ID for package %v: %v", pkg, err)
93+
continue
94+
}
95+
pID := fmt.Sprintf("%sPackage-%s-%s", SPDXRefPrefix, replaceSPDXIDInvalidChars(pName), replaceSPDXIDInvalidChars(id))
9096
pSourceInfo := ""
9197
if len(pkg.Plugins) > 0 {
9298
pSourceInfo = fmt.Sprintf("Identified by the %s extractor", pkg.Plugins[0])

converter/spdx/spdx_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,96 @@ func TestToSPDX23(t *testing.T) {
698698
},
699699
},
700700
},
701+
{
702+
desc: "Package_with_custom_package_id",
703+
inv: inventory.Inventory{
704+
Packages: []*extractor.Package{{
705+
ID: "pkg-custom-id-123",
706+
Name: "software",
707+
Version: "1.2.3",
708+
PURLType: purl.TypePyPi,
709+
Plugins: []string{wheelegg.Name},
710+
}},
711+
},
712+
want: &v2_3.Document{
713+
SPDXVersion: "SPDX-2.3",
714+
DataLicense: "CC0-1.0",
715+
SPDXIdentifier: "DOCUMENT",
716+
DocumentName: "SCALIBR-generated SPDX",
717+
DocumentNamespace: "https://spdx.google/067d89bc-7f01-41f5-b398-1659a44ff17a",
718+
CreationInfo: &v2_3.CreationInfo{
719+
Creators: []common.Creator{
720+
{
721+
CreatorType: "Tool",
722+
Creator: "SCALIBR",
723+
},
724+
},
725+
},
726+
Packages: []*v2_3.Package{
727+
{
728+
PackageName: "main",
729+
PackageSPDXIdentifier: "SPDXRef-Package-main-3bea6f5b-3af6-4e03-b436-6c4719e43a1b",
730+
PackageVersion: "0",
731+
PackageSupplier: &common.Supplier{
732+
Supplier: spdx.NoAssertion,
733+
SupplierType: spdx.NoAssertion,
734+
},
735+
PackageDownloadLocation: spdx.NoAssertion,
736+
IsFilesAnalyzedTagPresent: false,
737+
},
738+
{
739+
PackageName: "software",
740+
PackageSPDXIdentifier: "SPDXRef-Package-software-pkg-custom-id-123",
741+
PackageVersion: "1.2.3",
742+
PackageSupplier: &common.Supplier{
743+
Supplier: spdx.NoAssertion,
744+
SupplierType: spdx.NoAssertion,
745+
},
746+
PackageDownloadLocation: spdx.NoAssertion,
747+
PackageLicenseConcluded: spdx.NoAssertion,
748+
PackageLicenseDeclared: spdx.NoAssertion,
749+
IsFilesAnalyzedTagPresent: false,
750+
PackageSourceInfo: "Identified by the python/wheelegg extractor",
751+
PackageExternalReferences: []*v2_3.PackageExternalReference{
752+
{
753+
Category: "PACKAGE-MANAGER",
754+
RefType: "purl",
755+
Locator: "pkg:pypi/software@1.2.3",
756+
},
757+
},
758+
},
759+
},
760+
Relationships: []*v2_3.Relationship{
761+
{
762+
RefA: common.DocElementID{
763+
ElementRefID: "SPDXRef-DOCUMENT",
764+
},
765+
RefB: common.DocElementID{
766+
ElementRefID: "SPDXRef-Package-main-3bea6f5b-3af6-4e03-b436-6c4719e43a1b",
767+
},
768+
Relationship: "DESCRIBES",
769+
},
770+
{
771+
RefA: common.DocElementID{
772+
ElementRefID: "SPDXRef-Package-main-3bea6f5b-3af6-4e03-b436-6c4719e43a1b",
773+
},
774+
RefB: common.DocElementID{
775+
ElementRefID: "SPDXRef-Package-software-pkg-custom-id-123",
776+
},
777+
Relationship: "CONTAINS",
778+
},
779+
{
780+
RefA: common.DocElementID{
781+
ElementRefID: "SPDXRef-Package-software-pkg-custom-id-123",
782+
},
783+
RefB: common.DocElementID{
784+
SpecialID: spdx.NoAssertion,
785+
},
786+
Relationship: "CONTAINS",
787+
},
788+
},
789+
},
790+
},
701791
}
702792

703793
for _, tc := range testCases {

0 commit comments

Comments
 (0)