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

Method EditCodespace

internal/codespaces/api/api.go:1162–1213  ·  view source on GitHub ↗
(ctx context.Context, codespaceName string, params *EditCodespaceParams)

Source from the content-addressed store, hash-verified

1160}
1161
1162func (a *API) EditCodespace(ctx context.Context, codespaceName string, params *EditCodespaceParams) (*Codespace, error) {
1163 requestBody, err := json.Marshal(params)
1164 if err != nil {
1165 return nil, fmt.Errorf("error marshaling request: %w", err)
1166 }
1167
1168 u, err := safeurl.JoinPathWithHostPrefix(a.githubAPI, "user", "codespaces", codespaceName)
1169 if err != nil {
1170 return nil, err
1171 }
1172 req, err := http.NewRequest(http.MethodPatch, u.String(), bytes.NewBuffer(requestBody))
1173 if err != nil {
1174 return nil, fmt.Errorf("error creating request: %w", err)
1175 }
1176
1177 a.setHeaders(req)
1178 resp, err := a.do(ctx, req, "/user/codespaces/*")
1179 if err != nil {
1180 return nil, fmt.Errorf("error making request: %w", err)
1181 }
1182 defer resp.Body.Close()
1183
1184 if resp.StatusCode != http.StatusOK {
1185 // 422 (unprocessable entity) is likely caused by the codespace having a
1186 // pending op, so we'll fetch the codespace to see if that's the case
1187 // and return a more understandable error message.
1188 if resp.StatusCode == http.StatusUnprocessableEntity {
1189 pendingOp, reason, err := a.checkForPendingOperation(ctx, codespaceName)
1190 // If there's an error or there's not a pending op, we want to let
1191 // this fall through to the normal api.HandleHTTPError flow
1192 if err == nil && pendingOp {
1193 return nil, fmt.Errorf(
1194 "codespace is disabled while it has a pending operation: %s",
1195 reason,
1196 )
1197 }
1198 }
1199 return nil, api.HandleHTTPError(resp)
1200 }
1201
1202 b, err := io.ReadAll(resp.Body)
1203 if err != nil {
1204 return nil, fmt.Errorf("error reading response body: %w", err)
1205 }
1206
1207 var response Codespace
1208 if err := json.Unmarshal(b, &response); err != nil {
1209 return nil, fmt.Errorf("error unmarshalling response: %w", err)
1210 }
1211
1212 return &response, nil
1213}
1214
1215func (a *API) checkForPendingOperation(ctx context.Context, codespaceName string) (bool, string, error) {
1216 codespace, err := a.GetCodespace(ctx, codespaceName, false)

Callers 2

TestAPI_EditCodespaceFunction · 0.95

Calls 8

setHeadersMethod · 0.95
doMethod · 0.95
JoinPathWithHostPrefixFunction · 0.92
HandleHTTPErrorFunction · 0.92
ErrorfMethod · 0.65
StringMethod · 0.65
CloseMethod · 0.65

Tested by 2

TestAPI_EditCodespaceFunction · 0.76