-
Notifications
You must be signed in to change notification settings - Fork 14
/
webhook.go
161 lines (128 loc) · 3.18 KB
/
webhook.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
package circleci
import (
"context"
"fmt"
)
type Webhooks interface {
Get(ctx context.Context, id string) (*Webhook, error)
List(ctx context.Context, options WebhookListOptions) (*WebhookList, error)
Create(ctx context.Context, options WebhookCreateOptions) (*Webhook, error)
}
type webhooks struct {
client *Client
}
type WebhookList struct {
Items []*Webhook `json:"items"`
NextPageToken string `json:"next_page_token"`
}
type Webhook struct {
ID string `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
SigningSecret string `json:"signing-secret"`
Scope Scope `json:"scope"`
Events []string `json:"events"`
VerifyTLS bool `json:"verify-tls"`
}
type Scope struct {
ID string `json:"id"`
Type string `json:"type"`
}
func (s *webhooks) Get(ctx context.Context, id string) (*Webhook, error) {
if !validString(&id) {
return nil, ErrRequiredWebhookID
}
u := fmt.Sprintf("webhook/%s", id)
req, err := s.client.newRequest("GET", u, nil)
if err != nil {
return nil, err
}
w := &Webhook{}
err = s.client.do(ctx, req, w)
if err != nil {
return nil, err
}
return w, nil
}
type WebhookListOptions struct {
ScopeID *string `url:"scope-id,omitempty"`
ScopeType *string `url:"scope-type,omitempty"`
}
func (o WebhookListOptions) valid() error {
if !validString(o.ScopeID) {
return ErrRequiredWebhookScopeID
}
if !validString(o.ScopeType) {
return ErrRequiredWebhookScopeType
}
return nil
}
func (w *webhooks) List(ctx context.Context, options WebhookListOptions) (*WebhookList, error) {
if err := options.valid(); err != nil {
return nil, err
}
u := "webhook"
req, err := w.client.newRequest("GET", u, options)
if err != nil {
return nil, err
}
wb := &WebhookList{}
err = w.client.do(ctx, req, wb)
if err != nil {
return nil, err
}
return wb, nil
}
type Event string
const (
EventWorkflowCompleted Event = "workflow-completed"
EventJobCompleted Event = "job-completed"
)
type WebhookCreateOptions struct {
Name *string `json:"name"`
Events []*Event `json:"events"`
URL *string `json:"url"`
VerifyTLS *bool `json:"verify-tls"`
SigningSecret *string `json:"signing-secret"`
Scope *Scope `json:"scope"`
}
func (o WebhookCreateOptions) valid() error {
if !validString(o.Name) {
return ErrRequiredWebhookName
}
if !validArrayOfEvent(o.Events) {
return ErrRequiredWebhookEvents
}
if !validString(o.URL) {
return ErrRequiredWebhookURL
}
if !validBool(o.VerifyTLS) {
return ErrRequiredWebhookVerifyTLS
}
if !validString(o.SigningSecret) {
return ErrRequiredWebhookSigningSecret
}
if !validString(&o.Scope.ID) {
return ErrRequiredWebhookScopeID
}
if !validString(&o.Scope.Type) {
return ErrRequiredWebhookScopeType
}
return nil
}
func (w *webhooks) Create(ctx context.Context, options WebhookCreateOptions) (*Webhook, error) {
if err := options.valid(); err != nil {
return nil, err
}
u := "webhook"
req, err := w.client.newRequest("POST", u, &options)
if err != nil {
return nil, err
}
wb := &Webhook{}
err = w.client.do(ctx, req, wb)
if err != nil {
return nil, err
}
return wb, nil
}