forked from DependencyTrack/client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification.go
139 lines (114 loc) · 4.35 KB
/
notification.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
package dtrack
import (
"context"
"fmt"
"net/http"
"github.com/google/uuid"
)
type NotificationPublisher struct {
UUID uuid.UUID `json:"uuid"`
Name string `json:"name"`
Description string `json:"description"`
PublisherClass string `json:"publisherClass"`
Template string `json:"template"`
TemplateMimeType string `json:"templateMimeType"`
DefaultPublisher bool `json:"defaultPublisher"`
}
type NotificationRule struct {
UUID uuid.UUID `json:"uuid"`
Name string `json:"name"`
Publisher NotificationPublisher `json:"publisher"`
Scope string `json:"scope"`
NotificationLevel string `json:"notificationLevel"`
NotifyOn []string `json:"notifyOn"`
Enabled bool `json:"enabled"`
NotifyChildren bool `json:"notifyChildren"`
LogSuccessfulPublish bool `json:"logSuccessfulPublish"`
PublisherConfig string `json:"publisherConfig,omitempty"`
Projects []Project `json:"projects,omitempty"`
}
type NotificationService struct {
client *Client
}
func (ns NotificationService) GetAllPublishers(ctx context.Context) (p []NotificationPublisher, err error) {
req, err := ns.client.newRequest(ctx, http.MethodGet, "/api/v1/notification/publisher")
if err != nil {
return
}
_, err = ns.client.doRequest(req, &p)
return
}
func (ns NotificationService) CreatePublisher(ctx context.Context, publisher NotificationPublisher) (r NotificationPublisher, err error) {
req, err := ns.client.newRequest(ctx, http.MethodPut, "/api/v1/notification/publisher", withBody(publisher))
if err != nil {
return
}
_, err = ns.client.doRequest(req, &r)
return
}
func (ns NotificationService) UpdatePublisher(ctx context.Context, publisher NotificationPublisher) (r NotificationPublisher, err error) {
req, err := ns.client.newRequest(ctx, http.MethodPost, "/api/v1/notification/publisher", withBody(publisher))
if err != nil {
return
}
_, err = ns.client.doRequest(req, &r)
return
}
func (ns NotificationService) DeletePublisher(ctx context.Context, ruleUuid uuid.UUID) (err error) {
req, err := ns.client.newRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/notification/publisher/%s", ruleUuid.String()))
if err != nil {
return
}
_, err = ns.client.doRequest(req, nil)
return
}
func (ns NotificationService) GetAllRules(ctx context.Context) (r []NotificationRule, err error) {
req, err := ns.client.newRequest(ctx, http.MethodGet, "/api/v1/notification/rule")
if err != nil {
return
}
_, err = ns.client.doRequest(req, &r)
return
}
func (ns NotificationService) CreateRule(ctx context.Context, rule NotificationRule) (r NotificationRule, err error) {
req, err := ns.client.newRequest(ctx, http.MethodPut, "/api/v1/notification/rule", withBody(rule))
if err != nil {
return
}
_, err = ns.client.doRequest(req, &r)
return
}
func (ns NotificationService) UpdateRule(ctx context.Context, rule NotificationRule) (r NotificationRule, err error) {
req, err := ns.client.newRequest(ctx, http.MethodPost, "/api/v1/notification/rule", withBody(rule))
if err != nil {
return
}
_, err = ns.client.doRequest(req, &r)
return
}
func (ns NotificationService) DeleteRule(ctx context.Context, ruleUuid uuid.UUID) (err error) {
req, err := ns.client.newRequest(ctx, http.MethodDelete, "/api/v1/notification/rule", withBody(struct {
UUID uuid.UUID `json:"uuid"`
}{UUID: ruleUuid}))
if err != nil {
return
}
_, err = ns.client.doRequest(req, nil)
return
}
func (ns NotificationService) AddProjectToRule(ctx context.Context, ruleUuid, projectUuid uuid.UUID) (r NotificationRule, err error) {
req, err := ns.client.newRequest(ctx, http.MethodPost, fmt.Sprintf("/api/v1/notification/rule/%s/project/%s", ruleUuid.String(), projectUuid.String()))
if err != nil {
return
}
_, err = ns.client.doRequest(req, &r)
return
}
func (ns NotificationService) DeleteProjectFromRule(ctx context.Context, ruleUuid, projectUuid uuid.UUID) (r NotificationRule, err error) {
req, err := ns.client.newRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/notification/rule/%s/project/%s", ruleUuid.String(), projectUuid.String()))
if err != nil {
return
}
_, err = ns.client.doRequest(req, &r)
return
}