Skip to content

Commit

Permalink
fix: spdx element id cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
manifestori committed Feb 25, 2024
1 parent ee68c08 commit 5a51626
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion parser/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"os"
"slices"
"strings"

spdx_json "github.com/spdx/tools-golang/json"
"github.com/spdx/tools-golang/spdx"
Expand Down Expand Up @@ -124,6 +125,15 @@ func getRootPackageIndex(doc *Document) (int, int) {
return i, j
}

func sanitizeDocumentName(name string) string {
var str = strings.ToLower(name)
str = strings.ReplaceAll(str, " ", "-")
str = strings.ReplaceAll(str, "/", "-")
str = strings.ReplaceAll(str, "\\", "-")
str = strings.ReplaceAll(str, "_", "-")
return str
}

func updateRelationships(doc *Document, composableDocs []*Document) (*Document, []*Document) {
for _, cdoc := range composableDocs {
i, j := getRootPackageIndex(cdoc)
Expand All @@ -134,7 +144,7 @@ func updateRelationships(doc *Document, composableDocs []*Document) (*Document,
if rootPkg == nil {
rootPkg = &spdx.Package{
PackageName: cdoc.SPDXDocRef.DocumentName,
PackageSPDXIdentifier: spdx_common.MakeDocElementID("", cdoc.SPDXDocRef.DocumentName).ElementRefID,
PackageSPDXIdentifier: spdx_common.MakeDocElementID("", sanitizeDocumentName(cdoc.SPDXDocRef.DocumentName)).ElementRefID,
}
cdoc.SPDXDocRef.Packages = append([]*spdx.Package{rootPkg}, cdoc.SPDXDocRef.Packages...)

Expand Down

0 comments on commit 5a51626

Please sign in to comment.