forked from DependencyTrack/client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.go
178 lines (147 loc) · 5.2 KB
/
project.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package dtrack
import (
"context"
"fmt"
"net/http"
"strconv"
"github.com/google/uuid"
)
type Project struct {
UUID uuid.UUID `json:"uuid,omitempty"`
Author string `json:"author,omitempty"`
Publisher string `json:"publisher,omitempty"`
Group string `json:"group,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Version string `json:"version,omitempty"`
Classifier string `json:"classifier,omitempty"`
CPE string `json:"cpe,omitempty"`
PURL string `json:"purl,omitempty"`
SWIDTagID string `json:"swidTagId,omitempty"`
DirectDependencies string `json:"directDependencies,omitempty"`
Properties []ProjectProperty `json:"properties,omitempty"`
Tags []Tag `json:"tags,omitempty"`
Active bool `json:"active"`
Metrics ProjectMetrics `json:"metrics"`
ParentRef *ParentRef `json:"parent,omitempty"`
LastBOMImport int `json:"lastBomImport"`
}
type ParentRef struct {
UUID uuid.UUID `json:"uuid,omitempty"`
}
type ProjectService struct {
client *Client
}
func (ps ProjectService) Get(ctx context.Context, projectUUID uuid.UUID) (p Project, err error) {
req, err := ps.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/project/%s", projectUUID))
if err != nil {
return
}
_, err = ps.client.doRequest(req, &p)
return
}
func (ps ProjectService) GetAll(ctx context.Context, po PageOptions) (p Page[Project], err error) {
req, err := ps.client.newRequest(ctx, http.MethodGet, "/api/v1/project", withPageOptions(po))
if err != nil {
return
}
res, err := ps.client.doRequest(req, &p.Items)
if err != nil {
return
}
p.TotalCount = res.TotalCount
return
}
func (ps ProjectService) GetProjectsForName(ctx context.Context, name string, excludeInactive, onlyRoot bool) (p []Project, err error) {
params := map[string]string{
"name": name,
"excludeInactive": strconv.FormatBool(excludeInactive),
"onlyRoot": strconv.FormatBool(onlyRoot),
}
req, err := ps.client.newRequest(ctx, http.MethodGet, "/api/v1/project", withParams(params))
if err != nil {
return
}
_, err = ps.client.doRequest(req, &p)
return
}
func (ps ProjectService) Create(ctx context.Context, project Project) (p Project, err error) {
req, err := ps.client.newRequest(ctx, http.MethodPut, "/api/v1/project", withBody(project))
if err != nil {
return
}
_, err = ps.client.doRequest(req, &p)
return
}
func (ps ProjectService) Patch(ctx context.Context, projectUUID uuid.UUID, project Project) (p Project, err error) {
req, err := ps.client.newRequest(ctx, http.MethodPatch, fmt.Sprintf("/api/v1/project/%s", projectUUID), withBody(project))
if err != nil {
return
}
_, err = ps.client.doRequest(req, &p)
return
}
func (ps ProjectService) Update(ctx context.Context, project Project) (p Project, err error) {
req, err := ps.client.newRequest(ctx, http.MethodPost, "/api/v1/project", withBody(project))
if err != nil {
return
}
_, err = ps.client.doRequest(req, &p)
return
}
func (ps ProjectService) Delete(ctx context.Context, projectUUID uuid.UUID) (err error) {
req, err := ps.client.newRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/project/%s", projectUUID))
if err != nil {
return
}
_, err = ps.client.doRequest(req, nil)
return
}
func (ps ProjectService) Lookup(ctx context.Context, name, version string) (p Project, err error) {
params := map[string]string{
"name": name,
"version": version,
}
req, err := ps.client.newRequest(ctx, http.MethodGet, "/api/v1/project/lookup", withParams(params))
if err != nil {
return
}
_, err = ps.client.doRequest(req, &p)
return
}
func (ps ProjectService) GetAllByTag(ctx context.Context, tag string, excludeInactive, onlyRoot bool, po PageOptions) (p Page[Project], err error) {
pathParams := map[string]string{
"tag": tag,
}
params := map[string]string{
"excludeInactive": strconv.FormatBool(excludeInactive),
"onlyRoot": strconv.FormatBool(onlyRoot),
}
req, err := ps.client.newRequest(ctx, http.MethodGet, "/api/v1/project/tag/{tag}", withPathParams(pathParams), withParams(params), withPageOptions(po))
if err != nil {
return
}
res, err := ps.client.doRequest(req, &p.Items)
if err != nil {
return
}
p.TotalCount = res.TotalCount
return
}
type ProjectCloneRequest struct {
ProjectUUID uuid.UUID `json:"project"`
Version string `json:"version"`
IncludeAuditHistory bool `json:"includeAuditHistory"`
IncludeComponents bool `json:"includeComponents"`
IncludeProperties bool `json:"includeProperties"`
IncludeServices bool `json:"includeServices"`
IncludeTags bool `json:"includeTags"`
}
func (ps ProjectService) Clone(ctx context.Context, cloneReq ProjectCloneRequest) (err error) {
req, err := ps.client.newRequest(ctx, http.MethodPut, "/api/v1/project/clone", withBody(cloneReq))
if err != nil {
return
}
_, err = ps.client.doRequest(req, nil)
return
}