forked from ktrysmt/go-bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhooks.go
131 lines (108 loc) · 3.18 KB
/
webhooks.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
package bitbucket
import (
"encoding/json"
"github.com/mitchellh/mapstructure"
)
type Webhooks struct {
c *Client
}
type Webhook struct {
Owner string `json:"owner"`
RepoSlug string `json:"repo_slug"`
Uuid string `json:"uuid"`
Description string `json:"description"`
Url string `json:"url"`
Active bool `json:"active"`
Events []string `json:"events"` // EX: {'repo:push','issue:created',..} REF: https://bit.ly/3FjRHHu
}
func decodeWebhook(response interface{}) (*Webhook, error) {
respMap := response.(map[string]interface{})
if respMap["type"] == "error" {
return nil, DecodeError(respMap)
}
var webhook = new(Webhook)
err := mapstructure.Decode(respMap, webhook)
if err != nil {
return nil, err
}
return webhook, nil
}
func decodeWebhooks(response interface{}) ([]Webhook, error) {
webhooks := make([]Webhook, 0)
resMap := response.(map[string]interface{})
for _, v := range resMap["values"].([]interface{}) {
wh, err := decodeWebhook(v)
if err != nil {
return nil, err
}
webhooks = append(webhooks, *wh)
}
return webhooks, nil
}
func (r *Webhooks) buildWebhooksBody(ro *WebhooksOptions) (string, error) {
body := map[string]interface{}{}
if ro.Description != "" {
body["description"] = ro.Description
}
if ro.Url != "" {
body["url"] = ro.Url
}
if ro.Active == true || ro.Active == false {
body["active"] = ro.Active
}
body["events"] = ro.Events
data, err := json.Marshal(body)
if err != nil {
return "", err
}
return string(data), nil
}
func (r *Webhooks) List(ro *WebhooksOptions) ([]Webhook, error) {
urlStr := r.c.requestUrl("/repositories/%s/%s/hooks/", ro.Owner, ro.RepoSlug)
res, err := r.c.executePaginated("GET", urlStr, "", nil)
if err != nil {
return nil, err
}
return decodeWebhooks(res)
}
// Deprecate Gets for List call
func (r *Webhooks) Gets(ro *WebhooksOptions) (interface{}, error) {
urlStr := r.c.requestUrl("/repositories/%s/%s/hooks/", ro.Owner, ro.RepoSlug)
return r.c.executePaginated("GET", urlStr, "", nil)
}
func (r *Webhooks) Create(ro *WebhooksOptions) (*Webhook, error) {
data, err := r.buildWebhooksBody(ro)
if err != nil {
return nil, err
}
urlStr := r.c.requestUrl("/repositories/%s/%s/hooks", ro.Owner, ro.RepoSlug)
response, err := r.c.execute("POST", urlStr, data)
if err != nil {
return nil, err
}
return decodeWebhook(response)
}
func (r *Webhooks) Get(ro *WebhooksOptions) (*Webhook, error) {
urlStr := r.c.requestUrl("/repositories/%s/%s/hooks/%s", ro.Owner, ro.RepoSlug, ro.Uuid)
response, err := r.c.execute("GET", urlStr, "")
if err != nil {
return nil, err
}
return decodeWebhook(response)
}
func (r *Webhooks) Update(ro *WebhooksOptions) (*Webhook, error) {
data, err := r.buildWebhooksBody(ro)
if err != nil {
return nil, err
}
urlStr := r.c.requestUrl("/repositories/%s/%s/hooks/%s", ro.Owner, ro.RepoSlug, ro.Uuid)
response, err := r.c.execute("PUT", urlStr, data)
if err != nil {
return nil, err
}
return decodeWebhook(response)
}
func (r *Webhooks) Delete(ro *WebhooksOptions) (interface{}, error) {
urlStr := r.c.requestUrl("/repositories/%s/%s/hooks/%s", ro.Owner, ro.RepoSlug, ro.Uuid)
return r.c.execute("DELETE", urlStr, "")
}