fix: extract file ids correctly for spdx-json (#1156)

Previously, extracting relationships between packages and files was not
completing correctly, as SPDXRef- ElementIDs were being compared to raw
IDs, and so never matched. This patch ensures that we always compare
ElementIDs, to ensure that the hasFiles field is correctly populated.

Signed-off-by: Justin Chadwell <me@jedevc.com>
This commit is contained in:
Justin Chadwell 2022-08-11 19:06:36 +01:00 committed by GitHub
parent 2693a8c19a
commit 3db6911865
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 8 deletions

View File

@ -112,8 +112,10 @@ func fileIDsForPackage(packageSpdxID string, relationships []artifact.Relationsh
continue
}
if string(relationship.From.ID()) == packageSpdxID {
fileIDs = append(fileIDs, string(relationship.To.ID()))
from := model.ElementID(relationship.From.ID()).String()
to := model.ElementID(relationship.To.ID()).String()
if from == packageSpdxID {
fileIDs = append(fileIDs, to)
}
}
return fileIDs

View File

@ -188,7 +188,7 @@ func Test_fileIDsForPackage(t *testing.T) {
}{
{
name: "find file IDs for packages with package-file relationships",
id: string(p.ID()),
id: model.ElementID(p.ID()).String(),
relationships: []artifact.Relationship{
{
From: p,
@ -197,12 +197,12 @@ func Test_fileIDsForPackage(t *testing.T) {
},
},
expected: []string{
string(c.ID()),
model.ElementID(c.ID()).String(),
},
},
{
name: "ignore package-to-package",
id: string(p.ID()),
id: model.ElementID(p.ID()).String(),
relationships: []artifact.Relationship{
{
From: p,
@ -214,7 +214,7 @@ func Test_fileIDsForPackage(t *testing.T) {
},
{
name: "ignore file-to-file",
id: string(p.ID()),
id: model.ElementID(p.ID()).String(),
relationships: []artifact.Relationship{
{
From: c,
@ -226,7 +226,7 @@ func Test_fileIDsForPackage(t *testing.T) {
},
{
name: "ignore file-to-package",
id: string(p.ID()),
id: model.ElementID(p.ID()).String(),
relationships: []artifact.Relationship{
{
From: c,
@ -238,7 +238,7 @@ func Test_fileIDsForPackage(t *testing.T) {
},
{
name: "filter by relationship type",
id: string(p.ID()),
id: model.ElementID(p.ID()).String(),
relationships: []artifact.Relationship{
{
From: p,