Skip to content

Commit 6976c76

Browse files
authored
Merge pull request basecamp#159 from basecamp/migrate-webhook-to-sdk
Migrate webhook commands to SDK
2 parents b15efd1 + add987b commit 6976c76

3 files changed

Lines changed: 184 additions & 156 deletions

File tree

internal/commands/root.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -655,20 +655,6 @@ func printSuccess(data any) {
655655
}
656656
}
657657

658-
func printSuccessWithLocation(location string) {
659-
switch out.EffectiveFormat() {
660-
case output.FormatStyled:
661-
writeOutputString(renderHumanData(nil, location, false))
662-
captureResponse()
663-
case output.FormatMarkdown:
664-
writeOutputString(renderHumanData(nil, location, true))
665-
captureResponse()
666-
default:
667-
recordOutputError(out.OK(nil, output.WithContext("location", location)))
668-
captureResponse()
669-
}
670-
}
671-
672658
// breadcrumb creates a single breadcrumb.
673659
func breadcrumb(action, cmd, description string) Breadcrumb {
674660
return Breadcrumb{Action: action, Cmd: cmd, Description: description}

internal/commands/webhook.go

Lines changed: 86 additions & 76 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,43 @@ 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+
if webhookListPage > 0 {
61+
path += fmt.Sprintf("?page=%d", webhookListPage)
62+
}
63+
pages, err := ac.GetAll(cmd.Context(), path)
64+
if err != nil {
65+
return convertSDKError(err)
66+
}
67+
items = jsonAnySlice(pages)
68+
case webhookListPage > 0:
69+
path := fmt.Sprintf("/boards/%s/webhooks.json?page=%d", boardID, webhookListPage)
70+
resp, err := ac.Get(cmd.Context(), path)
71+
if err != nil {
72+
return convertSDKError(err)
73+
}
74+
var list []map[string]any
75+
if err := resp.UnmarshalData(&list); err != nil {
76+
return convertSDKError(err)
77+
}
78+
items = toSliceAny(list)
79+
linkNext = parseSDKLinkNext(resp)
80+
default:
81+
data, resp, err := ac.Webhooks().List(cmd.Context(), boardID)
82+
if err != nil {
83+
return convertSDKError(err)
84+
}
85+
items = normalizeAny(data)
86+
linkNext = parseSDKLinkNext(resp)
6187
}
6288

63-
count := 0
64-
if arr, ok := resp.Data.([]any); ok {
65-
count = len(arr)
66-
}
89+
count := dataCount(items)
6790
summary := fmt.Sprintf("%d webhooks", count)
6891
if webhookListAll {
6992
summary += " (all)"
@@ -76,7 +99,7 @@ var webhookListCmd = &cobra.Command{
7699
breadcrumb("create", fmt.Sprintf("fizzy webhook create --board %s --name \"name\" --url \"url\"", boardID), "Create webhook"),
77100
}
78101

79-
hasNext := resp.LinkNext != ""
102+
hasNext := linkNext != ""
80103
if hasNext {
81104
nextPage := webhookListPage + 1
82105
if webhookListPage == 0 {
@@ -85,7 +108,7 @@ var webhookListCmd = &cobra.Command{
85108
breadcrumbs = append(breadcrumbs, breadcrumb("next", fmt.Sprintf("fizzy webhook list --board %s --page %d", boardID, nextPage), "Next page"))
86109
}
87110

88-
printListPaginated(resp.Data, webhookColumns, hasNext, resp.LinkNext, webhookListAll, summary, breadcrumbs)
111+
printListPaginated(items, webhookColumns, hasNext, linkNext, webhookListAll, summary, breadcrumbs)
89112
return nil
90113
},
91114
}
@@ -185,15 +208,17 @@ var webhookShowCmd = &cobra.Command{
185208

186209
webhookID := args[0]
187210

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

217+
data := normalizeAny(raw)
218+
194219
summary := "Webhook"
195-
if wh, ok := resp.Data.(map[string]any); ok {
196-
if name, ok := wh["name"].(string); ok {
220+
if wh, ok := data.(map[string]any); ok {
221+
if name, ok := wh["name"].(string); ok && name != "" {
197222
summary = fmt.Sprintf("Webhook: %s", name)
198223
}
199224
}
@@ -204,7 +229,7 @@ var webhookShowCmd = &cobra.Command{
204229
breadcrumb("reactivate", fmt.Sprintf("fizzy webhook reactivate --board %s %s", boardID, webhookID), "Reactivate webhook"),
205230
}
206231

207-
printDetail(resp.Data, summary, breadcrumbs)
232+
printDetail(data, summary, breadcrumbs)
208233
return nil
209234
},
210235
}
@@ -236,51 +261,37 @@ var webhookCreateCmd = &cobra.Command{
236261
return newRequiredFlagError("url")
237262
}
238263

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

248-
body := map[string]any{
249-
"webhook": webhookParams,
271+
raw, resp, err := ac.Webhooks().Create(cmd.Context(), boardID, req)
272+
if err != nil {
273+
return convertSDKError(err)
250274
}
251275

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

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
282+
var breadcrumbs []Breadcrumb
283+
if webhookID != "" {
284+
breadcrumbs = []Breadcrumb{
285+
breadcrumb("show", fmt.Sprintf("fizzy webhook show --board %s %s", boardID, webhookID), "View webhook"),
286+
breadcrumb("update", fmt.Sprintf("fizzy webhook update --board %s %s --name \"name\"", boardID, webhookID), "Update webhook"),
278287
}
279-
printSuccessWithLocation(resp.Location)
280-
return nil
281288
}
282289

283-
printSuccess(resp.Data)
290+
if location := resp.Headers.Get("Location"); location != "" {
291+
printMutationWithLocation(data, location, breadcrumbs)
292+
} else {
293+
printMutation(data, "", breadcrumbs)
294+
}
284295
return nil
285296
},
286297
}
@@ -307,31 +318,26 @@ var webhookUpdateCmd = &cobra.Command{
307318

308319
webhookID := args[0]
309320

310-
webhookParams := make(map[string]any)
311-
321+
req := &generated.UpdateWebhookRequest{}
312322
if webhookUpdateName != "" {
313-
webhookParams["name"] = webhookUpdateName
323+
req.Name = webhookUpdateName
314324
}
315325
if len(webhookUpdateActions) > 0 {
316-
webhookParams["subscribed_actions"] = webhookUpdateActions
317-
}
318-
319-
body := map[string]any{
320-
"webhook": webhookParams,
326+
req.SubscribedActions = webhookUpdateActions
321327
}
322328

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

329335
breadcrumbs := []Breadcrumb{
330336
breadcrumb("show", fmt.Sprintf("fizzy webhook show --board %s %s", boardID, webhookID), "View webhook"),
331337
breadcrumb("delete", fmt.Sprintf("fizzy webhook delete --board %s %s", boardID, webhookID), "Delete webhook"),
332338
}
333339

334-
printMutation(resp.Data, "", breadcrumbs)
340+
printMutation(normalizeAny(raw), "", breadcrumbs)
335341
return nil
336342
},
337343
}
@@ -354,10 +360,9 @@ var webhookDeleteCmd = &cobra.Command{
354360
return err
355361
}
356362

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

363368
breadcrumbs := []Breadcrumb{
@@ -392,18 +397,23 @@ var webhookReactivateCmd = &cobra.Command{
392397

393398
webhookID := args[0]
394399

395-
client := getClient()
396-
resp, err := client.Post(fmt.Sprintf("/boards/%s/webhooks/%s/activation.json", boardID, webhookID), nil)
400+
ac := getSDK()
401+
resp, err := ac.Webhooks().Activate(cmd.Context(), boardID, webhookID)
397402
if err != nil {
398-
return err
403+
return convertSDKError(err)
404+
}
405+
406+
data := normalizeAny(resp.Data)
407+
if data == nil {
408+
data = map[string]any{"id": webhookID, "active": true}
399409
}
400410

401411
breadcrumbs := []Breadcrumb{
402412
breadcrumb("show", fmt.Sprintf("fizzy webhook show --board %s %s", boardID, webhookID), "View webhook"),
403413
breadcrumb("webhooks", fmt.Sprintf("fizzy webhook list --board %s", boardID), "List webhooks"),
404414
}
405415

406-
printMutation(resp.Data, "", breadcrumbs)
416+
printMutation(data, "", breadcrumbs)
407417
return nil
408418
},
409419
}

0 commit comments

Comments
 (0)