Skip to content

Commit 27c22a0

Browse files
author
Marwan Sulaiman
committed
Add org support
1 parent aa5e99a commit 27c22a0

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

webhook/create_webhook.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"strconv"
78
"strings"
89

910
"github.com/cli/go-gh"
@@ -42,6 +43,10 @@ func createHook(o *hookOptions) (string, func() error, error) {
4243
return "", nil, fmt.Errorf("error creating rest client: %w", err)
4344
}
4445
path := fmt.Sprintf("repos/%s/hooks", o.Repo)
46+
if o.Org != "" {
47+
path = fmt.Sprintf("orgs/%s/hooks", o.Org)
48+
}
49+
4550
req := createHookRequest{
4651
Name: "cli",
4752
Events: o.EventTypes,
@@ -62,8 +67,10 @@ func createHook(o *hookOptions) (string, func() error, error) {
6267
return "", nil, fmt.Errorf("error creating webhook: %w", err)
6368
}
6469

70+
// reset path for activation.
71+
path += "/" + strconv.Itoa(res.ID)
72+
6573
return res.WsURL, func() error {
66-
path := fmt.Sprintf("repos/%s/hooks/%d", o.Repo, res.ID)
6774
err = apiClient.Patch(path, strings.NewReader(`{"active": true}`), nil)
6875
if err != nil {
6976
return fmt.Errorf("error activating webhook: %w", err)

webhook/forward.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type hookOptions struct {
2424
Host string
2525
EventTypes []string
2626
Repo string
27+
Org string
2728
Port int
2829
}
2930

@@ -33,22 +34,23 @@ func NewCmdForward(runF func(*hookOptions) error) *cobra.Command {
3334
Out: os.Stdout,
3435
}
3536
cmd := &cobra.Command{
36-
Use: "forward --events=<event_types> --repo=<repo> [--port=<port>] [--host=<host>]",
37+
Use: "forward --events=<event_types> --repo|org=<repo|org> [--port=<port>] [--host=<host>]",
3738
Short: "Receive test events on a server running locally",
3839
Long: heredoc.Doc(`To output event payloads to stdout instead of sending to a server,
3940
omit the --port flag. If the --host flag is not specified, webhooks will be created against github.com`),
4041
Example: heredoc.Doc(`
4142
# create a dev webhook for the 'issue_open' event in the monalisa/smile repo in GitHub running locally, and
4243
# forward payloads for the triggered event to localhost:9999
4344
44-
$ gh webhooks forward --events=issue_open --repo=monalisa/smile --port=9999 --host=api.github.localhost
45+
$ gh webhooks forward --events=issues --repo=monalisa/smile --port=9999
46+
$ gh webhooks forward --events=issues --org=github --port=9999
4547
`),
4648
RunE: func(*cobra.Command, []string) error {
4749
if opts.EventTypes == nil {
4850
return cmdutil.FlagErrorf("`--events` flag required")
4951
}
50-
if opts.Repo == "" {
51-
return cmdutil.FlagErrorf("`--repo` flag required")
52+
if opts.Repo == "" && opts.Org == "" {
53+
return cmdutil.FlagErrorf("`--repo` or `--org` flag required")
5254
}
5355
if opts.Host == "" {
5456
opts.Host = gitHubAPIProdURL
@@ -83,9 +85,10 @@ func NewCmdForward(runF func(*hookOptions) error) *cobra.Command {
8385
},
8486
}
8587
cmd.Flags().StringSliceVarP(&opts.EventTypes, "events", "E", []string{}, "(required) Names of the event types to forward")
86-
cmd.Flags().StringVarP(&opts.Repo, "repo", "R", "", "(required) Name of the repo where the webhook is installed")
88+
cmd.Flags().StringVarP(&opts.Repo, "repo", "R", "", "Name of the repo where the webhook is installed")
8789
cmd.Flags().IntVarP(&opts.Port, "port", "P", 0, "(optional) Local port where the server which will receive webhooks is running")
8890
cmd.Flags().StringVarP(&opts.Host, "host", "H", "", "(optional) Host address of GitHub API, default: api.github.com")
91+
cmd.Flags().StringVarP(&opts.Org, "org", "O", "", "Name of the org where the webhook is installed")
8992
return cmd
9093
}
9194

@@ -100,9 +103,11 @@ func runFwd(out io.Writer, port int, token, wsURL string, activateHook func() er
100103
if err != nil {
101104
// If the error is a server disconnect (1006), retry connecting
102105
if websocket.IsCloseError(err, websocket.CloseAbnormalClosure) {
106+
fmt.Println("ABNORMAL CLOSURE")
103107
time.Sleep(5 * time.Second)
104108
continue
105109
}
110+
fmt.Println("BRUH", err)
106111
return err
107112
}
108113
}

0 commit comments

Comments
 (0)