Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions internal/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,20 +655,6 @@ func printSuccess(data any) {
}
}

func printSuccessWithLocation(location string) {
switch out.EffectiveFormat() {
case output.FormatStyled:
writeOutputString(renderHumanData(nil, location, false))
captureResponse()
case output.FormatMarkdown:
writeOutputString(renderHumanData(nil, location, true))
captureResponse()
default:
recordOutputError(out.OK(nil, output.WithContext("location", location)))
captureResponse()
}
}

// breadcrumb creates a single breadcrumb.
func breadcrumb(action, cmd, description string) Breadcrumb {
return Breadcrumb{Action: action, Cmd: cmd, Description: description}
Expand Down
162 changes: 86 additions & 76 deletions internal/commands/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"

"github.com/basecamp/fizzy-sdk/go/pkg/generated"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -49,21 +50,43 @@ var webhookListCmd = &cobra.Command{
return err
}

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

resp, err := client.GetWithPagination(path, webhookListAll)
if err != nil {
return err
switch {
case webhookListAll:
path := fmt.Sprintf("/boards/%s/webhooks.json", boardID)
Comment thread
robzolkos marked this conversation as resolved.
if webhookListPage > 0 {
path += fmt.Sprintf("?page=%d", webhookListPage)
}
pages, err := ac.GetAll(cmd.Context(), path)
if err != nil {
return convertSDKError(err)
}
items = jsonAnySlice(pages)
case webhookListPage > 0:
path := fmt.Sprintf("/boards/%s/webhooks.json?page=%d", boardID, webhookListPage)
resp, err := ac.Get(cmd.Context(), path)
if err != nil {
return convertSDKError(err)
}
var list []map[string]any
if err := resp.UnmarshalData(&list); err != nil {
return convertSDKError(err)
}
items = toSliceAny(list)
linkNext = parseSDKLinkNext(resp)
default:
data, resp, err := ac.Webhooks().List(cmd.Context(), boardID)
if err != nil {
return convertSDKError(err)
}
items = normalizeAny(data)
linkNext = parseSDKLinkNext(resp)
}

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

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

printListPaginated(resp.Data, webhookColumns, hasNext, resp.LinkNext, webhookListAll, summary, breadcrumbs)
printListPaginated(items, webhookColumns, hasNext, linkNext, webhookListAll, summary, breadcrumbs)
return nil
},
}
Expand Down Expand Up @@ -185,15 +208,17 @@ var webhookShowCmd = &cobra.Command{

webhookID := args[0]

client := getClient()
resp, err := client.Get(fmt.Sprintf("/boards/%s/webhooks/%s.json", boardID, webhookID))
ac := getSDK()
raw, _, err := ac.Webhooks().Get(cmd.Context(), boardID, webhookID)
if err != nil {
return err
return convertSDKError(err)
}

data := normalizeAny(raw)

summary := "Webhook"
if wh, ok := resp.Data.(map[string]any); ok {
if name, ok := wh["name"].(string); ok {
if wh, ok := data.(map[string]any); ok {
if name, ok := wh["name"].(string); ok && name != "" {
summary = fmt.Sprintf("Webhook: %s", name)
}
}
Expand All @@ -204,7 +229,7 @@ var webhookShowCmd = &cobra.Command{
breadcrumb("reactivate", fmt.Sprintf("fizzy webhook reactivate --board %s %s", boardID, webhookID), "Reactivate webhook"),
}

printDetail(resp.Data, summary, breadcrumbs)
printDetail(data, summary, breadcrumbs)
return nil
},
}
Expand Down Expand Up @@ -236,51 +261,37 @@ var webhookCreateCmd = &cobra.Command{
return newRequiredFlagError("url")
}

webhookParams := map[string]any{
"name": webhookCreateName,
"url": webhookCreateURL,
}

if len(webhookCreateActions) > 0 {
webhookParams["subscribed_actions"] = webhookCreateActions
ac := getSDK()
req := &generated.CreateWebhookRequest{
Name: webhookCreateName,
Url: webhookCreateURL,
SubscribedActions: webhookCreateActions,
}

body := map[string]any{
"webhook": webhookParams,
raw, resp, err := ac.Webhooks().Create(cmd.Context(), boardID, req)
if err != nil {
return convertSDKError(err)
}

client := getClient()
resp, err := client.Post(fmt.Sprintf("/boards/%s/webhooks.json", boardID), body)
if err != nil {
return err
data := normalizeAny(raw)
webhookID := ""
if wh, ok := data.(map[string]any); ok {
webhookID = getStringField(wh, "id")
}

if resp.Location != "" {
followResp, err := client.FollowLocation(resp.Location)
if err == nil && followResp != nil {
webhookID := ""
if wh, ok := followResp.Data.(map[string]any); ok {
if id, ok := wh["id"].(string); ok {
webhookID = id
}
}

var breadcrumbs []Breadcrumb
if webhookID != "" {
breadcrumbs = []Breadcrumb{
breadcrumb("show", fmt.Sprintf("fizzy webhook show --board %s %s", boardID, webhookID), "View webhook"),
breadcrumb("update", fmt.Sprintf("fizzy webhook update --board %s %s --name \"name\"", boardID, webhookID), "Update webhook"),
}
}

printMutationWithLocation(followResp.Data, resp.Location, breadcrumbs)
return nil
var breadcrumbs []Breadcrumb
if webhookID != "" {
breadcrumbs = []Breadcrumb{
breadcrumb("show", fmt.Sprintf("fizzy webhook show --board %s %s", boardID, webhookID), "View webhook"),
breadcrumb("update", fmt.Sprintf("fizzy webhook update --board %s %s --name \"name\"", boardID, webhookID), "Update webhook"),
}
printSuccessWithLocation(resp.Location)
return nil
}

printSuccess(resp.Data)
if location := resp.Headers.Get("Location"); location != "" {
printMutationWithLocation(data, location, breadcrumbs)
} else {
printMutation(data, "", breadcrumbs)
}
return nil
},
}
Expand All @@ -307,31 +318,26 @@ var webhookUpdateCmd = &cobra.Command{

webhookID := args[0]

webhookParams := make(map[string]any)

req := &generated.UpdateWebhookRequest{}
if webhookUpdateName != "" {
webhookParams["name"] = webhookUpdateName
req.Name = webhookUpdateName
}
if len(webhookUpdateActions) > 0 {
webhookParams["subscribed_actions"] = webhookUpdateActions
}

body := map[string]any{
"webhook": webhookParams,
req.SubscribedActions = webhookUpdateActions
}

client := getClient()
resp, err := client.Patch(fmt.Sprintf("/boards/%s/webhooks/%s.json", boardID, webhookID), body)
ac := getSDK()
raw, _, err := ac.Webhooks().Update(cmd.Context(), boardID, webhookID, req)
if err != nil {
return err
return convertSDKError(err)
}

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

printMutation(resp.Data, "", breadcrumbs)
printMutation(normalizeAny(raw), "", breadcrumbs)
return nil
},
}
Expand All @@ -354,10 +360,9 @@ var webhookDeleteCmd = &cobra.Command{
return err
}

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

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

webhookID := args[0]

client := getClient()
resp, err := client.Post(fmt.Sprintf("/boards/%s/webhooks/%s/activation.json", boardID, webhookID), nil)
ac := getSDK()
resp, err := ac.Webhooks().Activate(cmd.Context(), boardID, webhookID)
if err != nil {
return err
return convertSDKError(err)
}

data := normalizeAny(resp.Data)
if data == nil {
data = map[string]any{"id": webhookID, "active": true}
}

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

printMutation(resp.Data, "", breadcrumbs)
printMutation(data, "", breadcrumbs)
return nil
},
}
Expand Down
Loading
Loading