MCPcopy Create free account
hub / github.com/cli/gh-webhook / createHook

Function createHook

webhook/create_webhook.go:49–94  ·  view source on GitHub ↗

createHook issues a request against the GitHub API to create a dev webhook

(o *hookOptions)

Source from the content-addressed store, hash-verified

47
48// createHook issues a request against the GitHub API to create a dev webhook
49func createHook(o *hookOptions) (string, func() error, error) {
50 apiClient, err := api.NewRESTClient(api.ClientOptions{
51 Host: o.gitHubHost,
52 AuthToken: o.authToken,
53 })
54 if err != nil {
55 return "", nil, fmt.Errorf("error creating REST client: %w", err)
56 }
57 path := fmt.Sprintf("repos/%s/hooks", o.repo)
58 if o.org != "" {
59 path = fmt.Sprintf("orgs/%s/hooks", o.org)
60 }
61
62 req := createHookRequest{
63 Name: "cli",
64 Events: o.eventTypes,
65 Active: false,
66 Config: hookConfig{
67 ContentType: "json",
68 InsecureSSL: "0",
69 Secret: o.secret,
70 },
71 }
72
73 reqBytes, err := json.Marshal(req)
74 if err != nil {
75 return "", nil, err
76 }
77 var res createHookResponse
78 err = apiClient.Post(path, bytes.NewReader(reqBytes), &res)
79 if err != nil {
80 var apierr *api.HTTPError
81 if errors.As(err, &apierr) && apierr.StatusCode == http.StatusForbidden {
82 return "", nil, fmt.Errorf("you do not have access to this feature")
83 }
84 return "", nil, fmt.Errorf("error creating webhook: %w", err)
85 }
86
87 return res.WsURL, func() error {
88 err := apiClient.Patch(res.URL, strings.NewReader(`{"active": true}`), nil)
89 if err != nil {
90 return fmt.Errorf("error activating webhook: %w", err)
91 }
92 return nil
93 }, nil
94}
95
96func authTokenForHost(host string) (string, error) {
97 token, _ := auth.TokenForHost(host)

Callers 1

NewCmdForwardFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected