-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhooks_service.go
More file actions
141 lines (125 loc) · 5.31 KB
/
webhooks_service.go
File metadata and controls
141 lines (125 loc) · 5.31 KB
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
// Webhooks allow to notify external services when a project analysis is done
package sonarapi
import (
"github.com/google/go-querystring/query"
"net/http"
"strings"
)
type WebhooksService struct {
client *Client
}
type WebhooksCreateObject struct {
Webhook *Webhook `json:"webhook,omitempty"`
}
type Webhook struct {
Key string `json:"key,omitempty"`
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
LatestDelivery *Delivery `json:"latestDelivery,omitempty"`
}
type WebhooksDeliveriesObject struct {
Deliveries []*Delivery `json:"deliveries,omitempty"`
Paging Paging `json:"paging,omitempty"`
}
type Delivery struct {
At string `json:"at,omitempty"`
CeTaskID string `json:"ceTaskId,omitempty"`
ComponentKey string `json:"componentKey,omitempty"`
DurationMs int64 `json:"durationMs,omitempty"`
HTTPStatus int64 `json:"httpStatus,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Payload string `json:"payload,omitempty"`
Success bool `json:"success,omitempty"`
URL string `json:"url,omitempty"`
ErrorStackTrace string `json:"errorStacktrace,omitempty"`
}
type WebhooksDeliveryObject struct {
Delivery *Delivery `json:"delivery,omitempty"`
}
type WebhooksListObject struct {
Webhooks []*Webhook `json:"webhooks,omitempty"`
}
type WebhooksCreateOption struct {
Name string `url:"name,omitempty"` // Description:"Name displayed in the administration console of webhooks",ExampleValue:"My Webhook"
Url string `url:"url,omitempty"` // Description:"Server endpoint that will receive the webhook payload, for example 'http://my_server/foo'. If HTTP Basic authentication is used, HTTPS is recommended to avoid man in the middle attacks. Example: 'https://myLogin:myPassword@my_server/foo'",ExampleValue:"https://www.my-webhook-listener.com/sonar"
Secret string `url:"secret,omitempty"`
}
// Create Create a Webhook.<br>Requires 'Administer' permission on the specified project, or global 'Administer' permission.
func (s *WebhooksService) Create(opt *WebhooksCreateOption) (v *WebhooksCreateObject, resp *http.Response, err error) {
path := s.client.url + "/api/webhooks/create"
optv, _ := query.Values(opt)
req, err := http.NewRequest("POST", path, strings.NewReader(optv.Encode()))
if err != nil {
return
}
s.client.requestExtHeader(req)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
v = new(WebhooksCreateObject)
resp, err = s.client.Do(req, v)
if err != nil {
return nil, resp, err
}
return
}
type WebhooksDeleteOption struct {
Webhook string `url:"webhook,omitempty"` // Description:"The key of the webhook to be deleted,auto-generated value can be obtained through api/webhooks/create or api/webhooks/list",ExampleValue:"my_project"
}
// Delete Delete a Webhook.<br>Requires 'Administer' permission on the specified project, or global 'Administer' permission.
func (s *WebhooksService) Delete(opt *WebhooksDeleteOption) (resp *http.Response, err error) {
path := s.client.url + "/api/webhooks/delete"
optv, _ := query.Values(opt)
req, err := http.NewRequest("POST", path, strings.NewReader(optv.Encode()))
if err != nil {
return
}
s.client.requestExtHeader(req)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err = s.client.Do(req, nil)
if err != nil {
return
}
return
}
type WebhooksListOption struct {
//Project string `url:"project,omitempty"` // Description:"Project key",ExampleValue:"my_project"
}
// List Search for global webhooks or project webhooks. Webhooks are ordered by name.<br>Requires 'Administer' permission on the specified project, or global 'Administer' permission.
func (s *WebhooksService) List(opt *WebhooksListOption) (v *WebhooksListObject, resp *http.Response, err error) {
path := s.client.url + "/api/webhooks/list"
optv, _ := query.Values(opt)
req, err := http.NewRequest("GET", path, strings.NewReader(optv.Encode()))
if err != nil {
return
}
s.client.requestExtHeader(req)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
v = new(WebhooksListObject)
resp, err = s.client.Do(req, v)
if err != nil {
return nil, resp, err
}
return
}
type WebhooksUpdateOption struct {
Name string `url:"name,omitempty"` // Description:"new name of the webhook",ExampleValue:"My Webhook"
Url string `url:"url,omitempty"` // Description:"new url to be called by the webhook",ExampleValue:"https://www.my-webhook-listener.com/sonar"
Webhook string `url:"webhook,omitempty"` // Description:"The key of the webhook to be updated,auto-generated value can be obtained through api/webhooks/create or api/webhooks/list",ExampleValue:"my_project"
Secret string `url:"secret,omitempty"`
}
// Update Update a Webhook.<br>Requires 'Administer' permission on the specified project, or global 'Administer' permission.
func (s *WebhooksService) Update(opt *WebhooksUpdateOption) (resp *http.Response, err error) {
path := s.client.url + "/api/webhooks/update"
optv, _ := query.Values(opt)
req, err := http.NewRequest("POST", path, strings.NewReader(optv.Encode()))
if err != nil {
return
}
s.client.requestExtHeader(req)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err = s.client.Do(req, nil)
if err != nil {
return
}
return
}