forked from Gitlink/gitlink-cli
177 lines
5.1 KiB
Go
177 lines
5.1 KiB
Go
package webhook
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/gitlink-org/gitlink-cli/shortcuts/common"
|
|
)
|
|
|
|
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",
|
|
"/v1"+ctx.RepoPath()+"/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: "Payload URL", Required: true},
|
|
{Name: "events", Short: "e", Usage: "Trigger events (comma-separated: push,create,delete,etc.)", Required: true},
|
|
{Name: "content-type", Usage: "Content type: json or form", Default: "json"},
|
|
{Name: "secret", Usage: "Webhook secret"},
|
|
{Name: "active", Usage: "Active (true/false)", Default: "true"},
|
|
{Name: "branch-filter", Usage: "Branch filter pattern", Default: "*"},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
webhookURL, _ := ctx.RequireArg("url")
|
|
eventsStr, _ := ctx.RequireArg("events")
|
|
events := strings.Split(eventsStr, ",")
|
|
payload := map[string]interface{}{
|
|
"url": webhookURL,
|
|
"events": events,
|
|
"content_type": ctx.Arg("content-type"),
|
|
"http_method": "POST",
|
|
"branch_filter": ctx.Arg("branch-filter"),
|
|
"active": ctx.Arg("active") == "true",
|
|
}
|
|
if s := ctx.Arg("secret"); s != "" {
|
|
payload["secret"] = s
|
|
}
|
|
env, err := ctx.CallAPI("POST",
|
|
"/v1"+ctx.RepoPath()+"/webhooks", payload)
|
|
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, _ := ctx.RequireArg("id")
|
|
env, err := ctx.CallAPI("GET",
|
|
fmt.Sprintf("/v1%s/webhooks/%s", ctx.RepoPath(), 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: "New payload URL"},
|
|
{Name: "events", Short: "e", Usage: "New events (comma-separated)"},
|
|
{Name: "active", Usage: "Active (true/false)"},
|
|
{Name: "content-type", Usage: "Content type: json or form"},
|
|
{Name: "secret", Usage: "New webhook secret"},
|
|
{Name: "branch-filter", Usage: "New branch filter"},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
id, _ := ctx.RequireArg("id")
|
|
|
|
// GitLink PUT requires full body — fetch current values first, then merge
|
|
viewEnv, err := ctx.CallAPI("GET",
|
|
fmt.Sprintf("/v1%s/webhooks/%s", ctx.RepoPath(), id), nil)
|
|
if err != nil {
|
|
return fmt.Errorf("获取 webhook 失败: %w", err)
|
|
}
|
|
current, ok := viewEnv.Data.(map[string]interface{})
|
|
if !ok {
|
|
return fmt.Errorf("unexpected webhook response format")
|
|
}
|
|
|
|
payload := map[string]interface{}{
|
|
"url": current["url"],
|
|
"content_type": current["content_type"],
|
|
"http_method": current["http_method"],
|
|
"secret": current["secret"],
|
|
"branch_filter": current["branch_filter"],
|
|
"active": current["active"],
|
|
"events": current["events"],
|
|
}
|
|
if u := ctx.Arg("url"); u != "" {
|
|
payload["url"] = u
|
|
}
|
|
if e := ctx.Arg("events"); e != "" {
|
|
payload["events"] = strings.Split(e, ",")
|
|
}
|
|
if a := ctx.Arg("active"); a != "" {
|
|
payload["active"] = a == "true"
|
|
}
|
|
if ct := ctx.Arg("content-type"); ct != "" {
|
|
payload["content_type"] = ct
|
|
}
|
|
if s := ctx.Arg("secret"); s != "" {
|
|
payload["secret"] = s
|
|
}
|
|
if bf := ctx.Arg("branch-filter"); bf != "" {
|
|
payload["branch_filter"] = bf
|
|
}
|
|
|
|
env, err := ctx.CallAPI("PUT",
|
|
fmt.Sprintf("/v1%s/webhooks/%s", ctx.RepoPath(), id), payload)
|
|
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, _ := ctx.RequireArg("id")
|
|
env, err := ctx.CallAPI("DELETE",
|
|
fmt.Sprintf("/v1%s/webhooks/%s", ctx.RepoPath(), id), nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
}
|
|
}
|