NewCmdForward returns a forward command.
()
| 17 | |
| 18 | // NewCmdForward returns a forward command. |
| 19 | func NewCmdForward() *cobra.Command { |
| 20 | var ( |
| 21 | localURL string |
| 22 | eventTypes []string |
| 23 | targetRepo string |
| 24 | targetOrg string |
| 25 | githubHost string |
| 26 | webhookSecret string |
| 27 | ) |
| 28 | |
| 29 | cmd := &cobra.Command{ |
| 30 | Use: "forward --events=<types> [--url=<url>]", |
| 31 | Short: "Receive test events locally", |
| 32 | Example: heredoc.Doc(` |
| 33 | # create a dev webhook for the 'issue_open' event in the monalisa/smile repo in GitHub running locally, and |
| 34 | # forward payloads for the triggered event to http://localhost:9999/webhooks |
| 35 | |
| 36 | $ gh webhook forward --events=issues --repo=monalisa/smile --url="http://localhost:9999/webhooks" |
| 37 | $ gh webhook forward --events=issues --org=github --url="http://localhost:9999/webhooks" |
| 38 | `), |
| 39 | RunE: func(c *cobra.Command, _ []string) error { |
| 40 | if targetRepo == "" && targetOrg == "" { |
| 41 | return errors.New("`--repo` or `--org` flag required") |
| 42 | } |
| 43 | |
| 44 | if envHost := os.Getenv("GH_HOST"); envHost != "" && !c.Flags().Changed("github-host") { |
| 45 | githubHost = envHost |
| 46 | } |
| 47 | |
| 48 | authToken, err := authTokenForHost(githubHost) |
| 49 | if err != nil { |
| 50 | return fmt.Errorf("fatal: error fetching gh token: %w", err) |
| 51 | } |
| 52 | |
| 53 | wsURL, activate, err := createHook(&hookOptions{ |
| 54 | gitHubHost: githubHost, |
| 55 | eventTypes: eventTypes, |
| 56 | authToken: authToken, |
| 57 | repo: targetRepo, |
| 58 | org: targetOrg, |
| 59 | secret: webhookSecret, |
| 60 | }) |
| 61 | if err != nil { |
| 62 | return err |
| 63 | } |
| 64 | |
| 65 | return runFwd(os.Stdout, localURL, authToken, wsURL, activate) |
| 66 | }, |
| 67 | } |
| 68 | |
| 69 | cmd.Flags().StringSliceVarP(&eventTypes, "events", "E", nil, "Names of the event `types` to forward. Use `*` to forward all events.") |
| 70 | _ = cmd.MarkFlagRequired("events") |
| 71 | cmd.Flags().StringVarP(&targetRepo, "repo", "R", "", "Name of the repo where the webhook is installed") |
| 72 | cmd.Flags().StringVarP(&githubHost, "github-host", "H", "github.com", "GitHub host name") |
| 73 | cmd.Flags().StringVarP(&localURL, "url", "U", "", "Address of the local server to receive events. If omitted, events will be printed to stdout.") |
| 74 | cmd.Flags().StringVarP(&targetOrg, "org", "O", "", "Name of the org where the webhook is installed") |
| 75 | cmd.Flags().StringVarP(&webhookSecret, "secret", "S", "", "Webhook secret for incoming events") |
| 76 |
no test coverage detected