Skip to content

Commit 84c78a3

Browse files
feat(webhooks): Add List call for webhooks (ktrysmt#219)
Added List function for webhooks within a repository. It has the same logic as Gets, but is easier to read and will return a slice of webhooks instead of an interface. Co-authored-by: Thomas O'Neill <[email protected]>
1 parent 2472d71 commit 84c78a3

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

tests/webhooks_test.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func TestWebhook(t *testing.T) {
140140
}
141141
})
142142

143-
t.Run("gets", func(t *testing.T) {
143+
t.Run("list/gets", func(t *testing.T) {
144144
const expectedNumberOfWebhooks = 20
145145
var webhookUUIDs []string
146146
defer func() {
@@ -178,7 +178,7 @@ func TestWebhook(t *testing.T) {
178178
// Use a page length of 5 to ensure the auto paging is working
179179
c.Pagelen = 5
180180

181-
response, err := c.Repositories.Webhooks.Gets(&bitbucket.WebhooksOptions{
181+
getsResponse, err := c.Repositories.Webhooks.Gets(&bitbucket.WebhooksOptions{
182182
Owner: owner,
183183
RepoSlug: repo,
184184
})
@@ -187,15 +187,30 @@ func TestWebhook(t *testing.T) {
187187
return
188188
}
189189

190-
responseMap, ok := response.(map[string]interface{})
190+
responseMap, ok := getsResponse.(map[string]interface{})
191191
if !ok {
192192
t.Error(errors.New("response could not be decoded"))
193193
return
194194
}
195195

196196
values := responseMap["values"].([]interface{})
197197
if len(values) != expectedNumberOfWebhooks {
198-
t.Error(fmt.Errorf("Expected %d webhooks but got %d. Response: %v", expectedNumberOfWebhooks, len(values), response))
198+
t.Error(fmt.Errorf("Expected %d webhooks but got %d. Response: %v", expectedNumberOfWebhooks, len(values), getsResponse))
199+
return
200+
}
201+
202+
listResponse, err := c.Repositories.Webhooks.List(&bitbucket.WebhooksOptions{
203+
Owner: owner,
204+
RepoSlug: repo,
205+
})
206+
207+
if err != nil {
208+
t.Errorf("Failed to list webhooks: %v", err)
209+
return
210+
}
211+
212+
if len(listResponse) != expectedNumberOfWebhooks {
213+
t.Error(fmt.Errorf("Expected %d webhooks but got %d. Response: %v", expectedNumberOfWebhooks, len(values), listResponse))
199214
return
200215
}
201216
})

webhooks.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ func decodeWebhook(response interface{}) (*Webhook, error) {
3636
return webhook, nil
3737
}
3838

39+
func decodeWebhooks(response interface{}) ([]Webhook, error) {
40+
webhooks := make([]Webhook, 0)
41+
resMap := response.(map[string]interface{})
42+
for _, v := range resMap["values"].([]interface{}) {
43+
wh, err := decodeWebhook(v)
44+
if err != nil {
45+
return nil, err
46+
}
47+
webhooks = append(webhooks, *wh)
48+
}
49+
return webhooks, nil
50+
}
51+
3952
func (r *Webhooks) buildWebhooksBody(ro *WebhooksOptions) (string, error) {
4053
body := map[string]interface{}{}
4154

@@ -59,6 +72,16 @@ func (r *Webhooks) buildWebhooksBody(ro *WebhooksOptions) (string, error) {
5972
return string(data), nil
6073
}
6174

75+
func (r *Webhooks) List(ro *WebhooksOptions) ([]Webhook, error) {
76+
urlStr := r.c.requestUrl("/repositories/%s/%s/hooks/", ro.Owner, ro.RepoSlug)
77+
res, err := r.c.executePaginated("GET", urlStr, "")
78+
if err != nil {
79+
return nil, err
80+
}
81+
return decodeWebhooks(res)
82+
}
83+
84+
// Deprecate Gets for List call
6285
func (r *Webhooks) Gets(ro *WebhooksOptions) (interface{}, error) {
6386
urlStr := r.c.requestUrl("/repositories/%s/%s/hooks/", ro.Owner, ro.RepoSlug)
6487
return r.c.executePaginated("GET", urlStr, "")

0 commit comments

Comments
 (0)