MCPcopy Create free account
hub / github.com/cli/cli / CreateSubscription

Method CreateSubscription

pkg/cmd/event/client/client.go:70–113  ·  view source on GitHub ↗
(ctx context.Context, resourceURL string, eventTypes []string)

Source from the content-addressed store, hash-verified

68}
69
70func (c *eventClient) CreateSubscription(ctx context.Context, resourceURL string, eventTypes []string) (*Subscription, bool, error) {
71 if !SupportedHost(c.host) {
72 return nil, false, ErrUnsupportedHost
73 }
74
75 body, err := json.Marshal(struct {
76 ResourceURL string `json:"resource_url"`
77 EventTypes []string `json:"event_types"`
78 }{
79 ResourceURL: resourceURL,
80 EventTypes: eventTypes,
81 })
82 if err != nil {
83 return nil, false, err
84 }
85
86 path, err := safeurl.JoinPath("event-subscriptions")
87 if err != nil {
88 return nil, false, err
89 }
90
91 resp, err := c.api.RequestWithContext(ctx, c.host, http.MethodPost, path.String(), bytes.NewReader(body))
92 if err != nil {
93 if httpErr, ok := errors.AsType[api.HTTPError](err); ok {
94 switch {
95 case httpErr.StatusCode == http.StatusNotFound:
96 return nil, false, fmt.Errorf("%w: %w", ErrResourceNotFound, err)
97 case httpErr.StatusCode == http.StatusForbidden:
98 return nil, false, forbiddenError(httpErr, err)
99 case httpErr.StatusCode == http.StatusUnprocessableEntity && len(httpErr.Errors) > 0:
100 return nil, false, fmt.Errorf("invalid subscription request: %w", err)
101 case httpErr.StatusCode == http.StatusUnprocessableEntity:
102 return nil, false, fmt.Errorf("%w: %w", ErrSubscriptionLimit, err)
103 }
104 }
105 return nil, false, err
106 }
107 created := resp.StatusCode == http.StatusCreated
108 var sub Subscription
109 if err := decodeJSON(resp, &sub); err != nil {
110 return nil, false, err
111 }
112 return &sub, created, nil
113}
114
115func (c *eventClient) GetSubscription(ctx context.Context, id string) (*Subscription, error) {
116 if !SupportedHost(c.host) {

Callers

nothing calls this directly

Calls 7

JoinPathFunction · 0.92
SupportedHostFunction · 0.85
forbiddenErrorFunction · 0.85
RequestWithContextMethod · 0.80
decodeJSONFunction · 0.70
StringMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected