aboutsummaryrefslogtreecommitdiff
path: root/v0/builder/builder2v1/build_creation_info.go
blob: 570bb58844d32e349ef06b2fb43b594bdc121d5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package builder2v1

import (
	"fmt"
	"time"

	"github.com/spdx/tools-golang/v0/spdx"
)

// BuildCreationInfoSection2_1 creates an SPDX Package (version 2.1), returning that
// package or error if any is encountered. Arguments:
//   - packageName: name of package / directory
//   - code: verification code from Package
//   - namespacePrefix: prefix for DocumentNamespace (packageName and code will be added)
//   - creatorType: one of Person, Organization or Tool
//   - creator: creator string
//   - testValues: for testing only; call with nil when using in production
func BuildCreationInfoSection2_1(packageName string, code string, namespacePrefix string, creatorType string, creator string, testValues map[string]string) (*spdx.CreationInfo2_1, error) {
	// build creator slices
	cPersons := []string{}
	cOrganizations := []string{}
	cTools := []string{}
	// add builder as a tool
	cTools = append(cTools, "github.com/spdx/tools-golang/v0/builder")

	switch creatorType {
	case "Person":
		cPersons = append(cPersons, creator)
	case "Organization":
		cOrganizations = append(cOrganizations, creator)
	case "Tool":
		cTools = append(cTools, creator)
	default:
		cPersons = append(cPersons, creator)
	}

	// use test Created time if passing test values
	location, _ := time.LoadLocation("UTC")
	locationTime := time.Now().In(location)
	created := locationTime.Format("2006-01-02T15:04:05Z")
	if testVal := testValues["Created"]; testVal != "" {
		created = testVal
	}

	ci := &spdx.CreationInfo2_1{
		SPDXVersion:          "SPDX-2.1",
		DataLicense:          "CC0-1.0",
		SPDXIdentifier:       "SPDXRef-DOCUMENT",
		DocumentName:         packageName,
		DocumentNamespace:    fmt.Sprintf("%s%s-%s", namespacePrefix, packageName, code),
		CreatorPersons:       cPersons,
		CreatorOrganizations: cOrganizations,
		CreatorTools:         cTools,
		Created:              created,
	}
	return ci, nil
}