forked from Gitlink/gitlink-cli
210 lines
5.8 KiB
Go
210 lines
5.8 KiB
Go
package webhook
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/gitlink-org/gitlink-cli/shortcuts/common"
|
|
)
|
|
|
|
// Shortcuts returns all shortcuts for webhook management.
|
|
func Shortcuts() []*common.Shortcut {
|
|
return []*common.Shortcut{
|
|
{
|
|
Name: "list",
|
|
Description: "List webhooks",
|
|
Flags: []common.Flag{
|
|
{Name: "page", Short: "p", Usage: "Page number", Default: "1"},
|
|
{Name: "limit", Short: "l", Usage: "Items per page", Default: "20"},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
q := url.Values{}
|
|
q.Set("page", ctx.Arg("page"))
|
|
q.Set("limit", ctx.Arg("limit"))
|
|
env, err := ctx.CallAPIWithQuery("GET", v1Path(ctx)+"/webhooks", q)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
{
|
|
Name: "create",
|
|
Description: "Create a webhook",
|
|
Flags: []common.Flag{
|
|
{Name: "url", Short: "u", Usage: "Webhook payload URL", Required: true},
|
|
{Name: "content-type", Usage: "Content type: json, form", Default: "json"},
|
|
{Name: "secret", Short: "s", Usage: "Webhook secret"},
|
|
{Name: "events", Short: "e", Usage: "Comma-separated events (push,issues,pull_request,etc)"},
|
|
{Name: "branch-filter", Usage: "Branch filter pattern"},
|
|
{Name: "active", Usage: "Whether the webhook is active", Default: "true"},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
webhookURL, err := ctx.RequireArg("url")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
body := map[string]interface{}{
|
|
"url": webhookURL,
|
|
"content_type": ctx.Arg("content-type"),
|
|
"http_method": "POST",
|
|
"active": true,
|
|
}
|
|
if secret := ctx.Arg("secret"); secret != "" {
|
|
body["secret"] = secret
|
|
}
|
|
if events := ctx.Arg("events"); events != "" {
|
|
body["events"] = strings.Split(events, ",")
|
|
} else {
|
|
body["events"] = []string{"push"}
|
|
}
|
|
if bf := ctx.Arg("branch-filter"); bf != "" {
|
|
body["branch_filter"] = bf
|
|
}
|
|
env, err := ctx.CallAPI("POST", v1Path(ctx)+"/webhooks", body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
{
|
|
Name: "view",
|
|
Description: "View webhook details",
|
|
Flags: []common.Flag{
|
|
{Name: "id", Short: "i", Usage: "Webhook ID", Required: true},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
id, err := ctx.RequireArg("id")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
env, err := ctx.CallAPI("GET", fmt.Sprintf("%s/webhooks/%s", v1Path(ctx), id), nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
{
|
|
Name: "update",
|
|
Description: "Update a webhook",
|
|
Flags: []common.Flag{
|
|
{Name: "id", Short: "i", Usage: "Webhook ID", Required: true},
|
|
{Name: "url", Short: "u", Usage: "Webhook payload URL"},
|
|
{Name: "content-type", Usage: "Content type: json, form", Default: "json"},
|
|
{Name: "secret", Short: "s", Usage: "Webhook secret"},
|
|
{Name: "events", Short: "e", Usage: "Comma-separated events (push,issues,pull_request,etc)"},
|
|
{Name: "branch-filter", Usage: "Branch filter pattern"},
|
|
{Name: "active", Usage: "Whether the webhook is active", Default: "true"},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
id, err := ctx.RequireArg("id")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
body := map[string]interface{}{
|
|
"content_type": ctx.Arg("content-type"),
|
|
"http_method": "POST",
|
|
"active": true,
|
|
"branch_filter": ctx.Arg("branch-filter"),
|
|
"secret": ctx.Arg("secret"),
|
|
}
|
|
if webhookURL := ctx.Arg("url"); webhookURL != "" {
|
|
body["url"] = webhookURL
|
|
}
|
|
if events := ctx.Arg("events"); events != "" {
|
|
body["events"] = strings.Split(events, ",")
|
|
} else {
|
|
body["events"] = []string{"push"}
|
|
}
|
|
env, err := ctx.CallAPI("PUT", fmt.Sprintf("%s/webhooks/%s", v1Path(ctx), id), body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
{
|
|
Name: "history",
|
|
Description: "List webhook delivery history",
|
|
Flags: []common.Flag{
|
|
{Name: "id", Short: "i", Usage: "Webhook ID", Required: true},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
id, err := ctx.RequireArg("id")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
env, err := ctx.CallAPI("GET", fmt.Sprintf("%s/webhooks/%s/hooktasks", v1Path(ctx), id), nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
{
|
|
Name: "test",
|
|
Description: "Test a webhook delivery",
|
|
Flags: []common.Flag{
|
|
{Name: "id", Short: "i", Usage: "Webhook ID", Required: true},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
id, err := ctx.RequireArg("id")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
env, err := ctx.CallAPI("POST", fmt.Sprintf("%s/webhooks/%s/tests", v1Path(ctx), id), nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
{
|
|
Name: "delete",
|
|
Description: "Delete a webhook",
|
|
Flags: []common.Flag{
|
|
{Name: "id", Short: "i", Usage: "Webhook ID", Required: true},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
id, err := ctx.RequireArg("id")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
env, err := ctx.CallAPI("DELETE", fmt.Sprintf("%s/webhooks/%s", v1Path(ctx), id), nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func v1Path(ctx *common.RuntimeContext) string {
|
|
return fmt.Sprintf("/v1/%s/%s", ctx.Owner, ctx.Repo)
|
|
}
|