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

Method CreateSubscription

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

Source from the content-addressed store, hash-verified

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