Files
gitea-tea/cmd/webhooks.go
techknowlogick b05e03416b replace log.Fatal/os.Exit with error returns (#941)
* Use stdlib encoders
* Reduce some duplication
* Remove global pagination state
* Dedupe JSON detail types
* Bump golangci-lint

Reviewed-on: https://gitea.com/gitea/tea/pulls/941
Co-authored-by: techknowlogick <techknowlogick@gitea.com>
Co-committed-by: techknowlogick <techknowlogick@gitea.com>
2026-03-27 03:36:44 +00:00

93 lines
2.2 KiB
Go

// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package cmd
import (
stdctx "context"
"fmt"
"code.gitea.io/tea/cmd/webhooks"
"code.gitea.io/tea/modules/context"
"code.gitea.io/tea/modules/print"
"code.gitea.io/tea/modules/utils"
"code.gitea.io/sdk/gitea"
"github.com/urfave/cli/v3"
)
// CmdWebhooks represents the webhooks command
var CmdWebhooks = cli.Command{
Name: "webhooks",
Aliases: []string{"webhook", "hooks", "hook"},
Category: catEntities,
Usage: "Manage webhooks",
Description: "List, create, update, and delete repository, organization, or global webhooks",
ArgsUsage: "[webhook-id]",
Action: runWebhooksDefault,
Commands: []*cli.Command{
&webhooks.CmdWebhooksList,
&webhooks.CmdWebhooksCreate,
&webhooks.CmdWebhooksDelete,
&webhooks.CmdWebhooksUpdate,
},
Flags: append([]cli.Flag{
&cli.StringFlag{
Name: "repo",
Usage: "repository to operate on",
},
&cli.StringFlag{
Name: "org",
Usage: "organization to operate on",
},
&cli.BoolFlag{
Name: "global",
Usage: "operate on global webhooks",
},
&cli.StringFlag{
Name: "login",
Usage: "gitea login instance to use",
},
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "output format [table, csv, simple, tsv, yaml, json]",
},
}, webhooks.CmdWebhooksList.Flags...),
}
func runWebhooksDefault(ctx stdctx.Context, cmd *cli.Command) error {
if cmd.Args().Len() == 1 {
return runWebhookDetail(ctx, cmd)
}
return webhooks.RunWebhooksList(ctx, cmd)
}
func runWebhookDetail(_ stdctx.Context, cmd *cli.Command) error {
ctx, err := context.InitCommand(cmd)
if err != nil {
return err
}
client := ctx.Login.Client()
webhookID, err := utils.ArgToIndex(cmd.Args().First())
if err != nil {
return err
}
var hook *gitea.Hook
if ctx.IsGlobal {
return fmt.Errorf("global webhooks not yet supported in this version")
} else if len(ctx.Org) > 0 {
hook, _, err = client.GetOrgHook(ctx.Org, int64(webhookID))
} else {
hook, _, err = client.GetRepoHook(ctx.Owner, ctx.Repo, int64(webhookID))
}
if err != nil {
return err
}
print.WebhookDetails(hook)
return nil
}