This repository has been archived by the owner on Dec 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
bom.go
113 lines (88 loc) · 2.68 KB
/
bom.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package dtrack
import (
"context"
"fmt"
"net/http"
"github.com/google/uuid"
)
type BOMService struct {
client *Client
}
type BOMUploadRequest struct {
ProjectUUID *uuid.UUID `json:"project,omitempty"`
ProjectName string `json:"projectName,omitempty"`
ProjectVersion string `json:"projectVersion,omitempty"`
AutoCreate bool `json:"autoCreate"`
BOM string `json:"bom"`
}
type bomUploadResponse struct {
Token BOMUploadToken `json:"token"`
}
type BOMUploadToken string
type BOMFormat string
const (
BOMFormatJSON BOMFormat = "JSON"
BOMFormatXML BOMFormat = "XML"
)
type BOMVariant string
const (
BOMVariantInventory BOMVariant = "inventory"
BOMVariantWithVulnerabilities BOMVariant = "withVulnerabilities"
)
func (bs BOMService) ExportComponent(ctx context.Context, componentUUID uuid.UUID, format BOMFormat) (bom string, err error) {
params := make(map[string]string)
if format != "" {
params["format"] = string(format)
}
req, err := bs.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/bom/cyclonedx/component/%s", componentUUID), withParams(params))
if err != nil {
return
}
req.Header.Set("Accept", "application/vnd.cyclonedx+json")
_, err = bs.client.doRequest(req, &bom)
return
}
func (bs BOMService) ExportProject(ctx context.Context, projectUUID uuid.UUID, format BOMFormat, variant BOMVariant) (bom string, err error) {
params := make(map[string]string)
if format != "" {
params["format"] = string(format)
}
if variant != "" {
params["variant"] = string(variant)
}
req, err := bs.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/bom/cyclonedx/project/%s", projectUUID), withParams(params))
if err != nil {
return
}
req.Header.Set("Accept", "application/vnd.cyclonedx+json")
_, err = bs.client.doRequest(req, &bom)
return
}
func (bs BOMService) Upload(ctx context.Context, uploadReq BOMUploadRequest) (token BOMUploadToken, err error) {
req, err := bs.client.newRequest(ctx, http.MethodPut, "/api/v1/bom", withBody(uploadReq))
if err != nil {
return
}
var uploadRes bomUploadResponse
_, err = bs.client.doRequest(req, &uploadRes)
if err != nil {
return
}
token = uploadRes.Token
return
}
type bomProcessingResponse struct {
Processing bool `json:"processing"`
}
func (bs BOMService) IsBeingProcessed(ctx context.Context, token BOMUploadToken) (bool, error) {
req, err := bs.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/bom/token/%s", token))
if err != nil {
return false, err
}
var processingResponse bomProcessingResponse
_, err = bs.client.doRequest(req, &processingResponse)
if err != nil {
return false, err
}
return processingResponse.Processing, nil
}