Skip to content

Commit cd13197

Browse files
committed
Migrate webhook commands to SDK
Replaces the legacy HTTP client (getClient()) with the typed WebhooksService for show/create/update/delete/reactivate. The list command uses the typed service for the no-flag case, falling back to ac.Get / ac.GetAll for --page and --all (the SDK's Webhooks().List doesn't accept a path). Behavior, flags, and CLI surface are unchanged. Unit tests updated to the SetTestModeWithSDK pattern; assertions now match the SDK's request shape (no `webhook` body wrapper) and URL paths (no .json suffix on get/update/delete).
1 parent b15efd1 commit cd13197

2 files changed

Lines changed: 133 additions & 143 deletions

File tree

internal/commands/webhook.go

Lines changed: 74 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"strings"
66

7+
"github.com/basecamp/fizzy-sdk/go/pkg/generated"
78
"github.com/spf13/cobra"
89
)
910

@@ -49,21 +50,40 @@ var webhookListCmd = &cobra.Command{
4950
return err
5051
}
5152

52-
client := getClient()
53-
path := fmt.Sprintf("/boards/%s/webhooks.json", boardID)
54-
if webhookListPage > 0 {
55-
path += fmt.Sprintf("?page=%d", webhookListPage)
56-
}
53+
ac := getSDK()
54+
var items any
55+
var linkNext string
5756

58-
resp, err := client.GetWithPagination(path, webhookListAll)
59-
if err != nil {
60-
return err
57+
switch {
58+
case webhookListAll:
59+
path := fmt.Sprintf("/boards/%s/webhooks.json", boardID)
60+
pages, err := ac.GetAll(cmd.Context(), path)
61+
if err != nil {
62+
return convertSDKError(err)
63+
}
64+
items = jsonAnySlice(pages)
65+
case webhookListPage > 0:
66+
path := fmt.Sprintf("/boards/%s/webhooks.json?page=%d", boardID, webhookListPage)
67+
resp, err := ac.Get(cmd.Context(), path)
68+
if err != nil {
69+
return convertSDKError(err)
70+
}
71+
var list []map[string]any
72+
if err := resp.UnmarshalData(&list); err != nil {
73+
return convertSDKError(err)
74+
}
75+
items = toSliceAny(list)
76+
linkNext = parseSDKLinkNext(resp)
77+
default:
78+
data, resp, err := ac.Webhooks().List(cmd.Context(), boardID)
79+
if err != nil {
80+
return convertSDKError(err)
81+
}
82+
items = normalizeAny(data)
83+
linkNext = parseSDKLinkNext(resp)
6184
}
6285

63-
count := 0
64-
if arr, ok := resp.Data.([]any); ok {
65-
count = len(arr)
66-
}
86+
count := dataCount(items)
6787
summary := fmt.Sprintf("%d webhooks", count)
6888
if webhookListAll {
6989
summary += " (all)"
@@ -76,7 +96,7 @@ var webhookListCmd = &cobra.Command{
7696
breadcrumb("create", fmt.Sprintf("fizzy webhook create --board %s --name \"name\" --url \"url\"", boardID), "Create webhook"),
7797
}
7898

79-
hasNext := resp.LinkNext != ""
99+
hasNext := linkNext != ""
80100
if hasNext {
81101
nextPage := webhookListPage + 1
82102
if webhookListPage == 0 {
@@ -85,7 +105,7 @@ var webhookListCmd = &cobra.Command{
85105
breadcrumbs = append(breadcrumbs, breadcrumb("next", fmt.Sprintf("fizzy webhook list --board %s --page %d", boardID, nextPage), "Next page"))
86106
}
87107

88-
printListPaginated(resp.Data, webhookColumns, hasNext, resp.LinkNext, webhookListAll, summary, breadcrumbs)
108+
printListPaginated(items, webhookColumns, hasNext, linkNext, webhookListAll, summary, breadcrumbs)
89109
return nil
90110
},
91111
}
@@ -185,15 +205,17 @@ var webhookShowCmd = &cobra.Command{
185205

186206
webhookID := args[0]
187207

188-
client := getClient()
189-
resp, err := client.Get(fmt.Sprintf("/boards/%s/webhooks/%s.json", boardID, webhookID))
208+
ac := getSDK()
209+
raw, _, err := ac.Webhooks().Get(cmd.Context(), boardID, webhookID)
190210
if err != nil {
191-
return err
211+
return convertSDKError(err)
192212
}
193213

214+
data := normalizeAny(raw)
215+
194216
summary := "Webhook"
195-
if wh, ok := resp.Data.(map[string]any); ok {
196-
if name, ok := wh["name"].(string); ok {
217+
if wh, ok := data.(map[string]any); ok {
218+
if name, ok := wh["name"].(string); ok && name != "" {
197219
summary = fmt.Sprintf("Webhook: %s", name)
198220
}
199221
}
@@ -204,7 +226,7 @@ var webhookShowCmd = &cobra.Command{
204226
breadcrumb("reactivate", fmt.Sprintf("fizzy webhook reactivate --board %s %s", boardID, webhookID), "Reactivate webhook"),
205227
}
206228

207-
printDetail(resp.Data, summary, breadcrumbs)
229+
printDetail(data, summary, breadcrumbs)
208230
return nil
209231
},
210232
}
@@ -236,51 +258,33 @@ var webhookCreateCmd = &cobra.Command{
236258
return newRequiredFlagError("url")
237259
}
238260

239-
webhookParams := map[string]any{
240-
"name": webhookCreateName,
241-
"url": webhookCreateURL,
242-
}
243-
244-
if len(webhookCreateActions) > 0 {
245-
webhookParams["subscribed_actions"] = webhookCreateActions
261+
ac := getSDK()
262+
req := &generated.CreateWebhookRequest{
263+
Name: webhookCreateName,
264+
Url: webhookCreateURL,
265+
SubscribedActions: webhookCreateActions,
246266
}
247267

248-
body := map[string]any{
249-
"webhook": webhookParams,
268+
raw, _, err := ac.Webhooks().Create(cmd.Context(), boardID, req)
269+
if err != nil {
270+
return convertSDKError(err)
250271
}
251272

252-
client := getClient()
253-
resp, err := client.Post(fmt.Sprintf("/boards/%s/webhooks.json", boardID), body)
254-
if err != nil {
255-
return err
273+
data := normalizeAny(raw)
274+
webhookID := ""
275+
if wh, ok := data.(map[string]any); ok {
276+
webhookID = getStringField(wh, "id")
256277
}
257278

258-
if resp.Location != "" {
259-
followResp, err := client.FollowLocation(resp.Location)
260-
if err == nil && followResp != nil {
261-
webhookID := ""
262-
if wh, ok := followResp.Data.(map[string]any); ok {
263-
if id, ok := wh["id"].(string); ok {
264-
webhookID = id
265-
}
266-
}
267-
268-
var breadcrumbs []Breadcrumb
269-
if webhookID != "" {
270-
breadcrumbs = []Breadcrumb{
271-
breadcrumb("show", fmt.Sprintf("fizzy webhook show --board %s %s", boardID, webhookID), "View webhook"),
272-
breadcrumb("update", fmt.Sprintf("fizzy webhook update --board %s %s --name \"name\"", boardID, webhookID), "Update webhook"),
273-
}
274-
}
275-
276-
printMutationWithLocation(followResp.Data, resp.Location, breadcrumbs)
277-
return nil
279+
var breadcrumbs []Breadcrumb
280+
if webhookID != "" {
281+
breadcrumbs = []Breadcrumb{
282+
breadcrumb("show", fmt.Sprintf("fizzy webhook show --board %s %s", boardID, webhookID), "View webhook"),
283+
breadcrumb("update", fmt.Sprintf("fizzy webhook update --board %s %s --name \"name\"", boardID, webhookID), "Update webhook"),
278284
}
279-
printSuccessWithLocation(resp.Location)
280-
return nil
281285
}
282286

283-
printSuccess(resp.Data)
287+
printMutation(data, "", breadcrumbs)
284288
return nil
285289
},
286290
}
@@ -307,31 +311,26 @@ var webhookUpdateCmd = &cobra.Command{
307311

308312
webhookID := args[0]
309313

310-
webhookParams := make(map[string]any)
311-
314+
req := &generated.UpdateWebhookRequest{}
312315
if webhookUpdateName != "" {
313-
webhookParams["name"] = webhookUpdateName
316+
req.Name = webhookUpdateName
314317
}
315318
if len(webhookUpdateActions) > 0 {
316-
webhookParams["subscribed_actions"] = webhookUpdateActions
317-
}
318-
319-
body := map[string]any{
320-
"webhook": webhookParams,
319+
req.SubscribedActions = webhookUpdateActions
321320
}
322321

323-
client := getClient()
324-
resp, err := client.Patch(fmt.Sprintf("/boards/%s/webhooks/%s.json", boardID, webhookID), body)
322+
ac := getSDK()
323+
raw, _, err := ac.Webhooks().Update(cmd.Context(), boardID, webhookID, req)
325324
if err != nil {
326-
return err
325+
return convertSDKError(err)
327326
}
328327

329328
breadcrumbs := []Breadcrumb{
330329
breadcrumb("show", fmt.Sprintf("fizzy webhook show --board %s %s", boardID, webhookID), "View webhook"),
331330
breadcrumb("delete", fmt.Sprintf("fizzy webhook delete --board %s %s", boardID, webhookID), "Delete webhook"),
332331
}
333332

334-
printMutation(resp.Data, "", breadcrumbs)
333+
printMutation(normalizeAny(raw), "", breadcrumbs)
335334
return nil
336335
},
337336
}
@@ -354,10 +353,9 @@ var webhookDeleteCmd = &cobra.Command{
354353
return err
355354
}
356355

357-
client := getClient()
358-
_, err = client.Delete(fmt.Sprintf("/boards/%s/webhooks/%s.json", boardID, args[0]))
359-
if err != nil {
360-
return err
356+
ac := getSDK()
357+
if _, err := ac.Webhooks().Delete(cmd.Context(), boardID, args[0]); err != nil {
358+
return convertSDKError(err)
361359
}
362360

363361
breadcrumbs := []Breadcrumb{
@@ -392,18 +390,17 @@ var webhookReactivateCmd = &cobra.Command{
392390

393391
webhookID := args[0]
394392

395-
client := getClient()
396-
resp, err := client.Post(fmt.Sprintf("/boards/%s/webhooks/%s/activation.json", boardID, webhookID), nil)
397-
if err != nil {
398-
return err
393+
ac := getSDK()
394+
if _, err := ac.Webhooks().Activate(cmd.Context(), boardID, webhookID); err != nil {
395+
return convertSDKError(err)
399396
}
400397

401398
breadcrumbs := []Breadcrumb{
402399
breadcrumb("show", fmt.Sprintf("fizzy webhook show --board %s %s", boardID, webhookID), "View webhook"),
403400
breadcrumb("webhooks", fmt.Sprintf("fizzy webhook list --board %s", boardID), "List webhooks"),
404401
}
405402

406-
printMutation(resp.Data, "", breadcrumbs)
403+
printMutation(map[string]any{}, "", breadcrumbs)
407404
return nil
408405
},
409406
}

0 commit comments

Comments
 (0)