forked from DependencyTrack/client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository.go
118 lines (97 loc) · 3.05 KB
/
repository.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
package dtrack
import (
"context"
"fmt"
"net/http"
"github.com/google/uuid"
)
const (
RepositoryTypeCargo = "CARGO"
RepositoryTypeComposer = "COMPOSER"
RepositoryTypeCpan = "CPAN"
RepositoryTypeGem = "GEM"
RepositoryTypeGoModules = "GO_MODULES"
RepositoryTypeHex = "HEX"
RepositoryTypeMaven = "MAVEN"
RepositoryTypeNpm = "NPM"
RepositoryTypeNuget = "NUGET"
RepositoryTypePypi = "PYPI"
RepositoryTypeUnsupported = "UNSUPPORTED"
)
type RepositoryType string
type Repository struct {
Type RepositoryType `json:"type"`
Identifier string `json:"identifier"`
Url string `json:"url"`
ResolutionOrder int `json:"resolutionOrder"`
Enabled bool `json:"enabled"`
Internal bool `json:"internal"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
UUID uuid.UUID `json:"uuid,omitempty"`
}
type RepositoryMetaComponent struct {
LatestVersion string `json:"latestVersion"`
}
type RepositoryService struct {
client *Client
}
func (rs RepositoryService) GetMetaComponent(ctx context.Context, purl string) (r RepositoryMetaComponent, err error) {
params := map[string]string{
"purl": purl,
}
req, err := rs.client.newRequest(ctx, http.MethodGet, "/api/v1/repository/latest", withParams(params))
if err != nil {
return
}
_, err = rs.client.doRequest(req, &r)
return
}
func (rs RepositoryService) GetAll(ctx context.Context, po PageOptions) (p Page[Repository], err error) {
req, err := rs.client.newRequest(ctx, http.MethodGet, "/api/v1/repository", withPageOptions(po))
if err != nil {
return
}
res, err := rs.client.doRequest(req, &p.Items)
if err != nil {
return
}
p.TotalCount = res.TotalCount
return
}
func (rs RepositoryService) GetByType(ctx context.Context, repoType RepositoryType, po PageOptions) (p Page[Repository], err error) {
req, err := rs.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/repository/%s", repoType), withPageOptions(po))
if err != nil {
return
}
res, err := rs.client.doRequest(req, &p.Items)
if err != nil {
return
}
p.TotalCount = res.TotalCount
return
}
func (rs RepositoryService) Create(ctx context.Context, repo Repository) (r Repository, err error) {
req, err := rs.client.newRequest(ctx, http.MethodPut, "/api/v1/repository", withBody(repo))
if err != nil {
return
}
_, err = rs.client.doRequest(req, &r)
return
}
func (rs RepositoryService) Update(ctx context.Context, repo Repository) (r Repository, err error) {
req, err := rs.client.newRequest(ctx, http.MethodPost, "/api/v1/repository", withBody(repo))
if err != nil {
return
}
_, err = rs.client.doRequest(req, &r)
return
}
func (rs RepositoryService) Delete(ctx context.Context, reposUUID uuid.UUID) (err error) {
req, err := rs.client.newRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/repository/%s", reposUUID.String()))
if err != nil {
return
}
_, err = rs.client.doRequest(req, nil)
return
}