| 1078 | } |
| 1079 | |
| 1080 | func (a *API) EditCodespace(ctx context.Context, codespaceName string, params *EditCodespaceParams) (*Codespace, error) { |
| 1081 | requestBody, err := json.Marshal(params) |
| 1082 | if err != nil { |
| 1083 | return nil, fmt.Errorf("error marshaling request: %w", err) |
| 1084 | } |
| 1085 | |
| 1086 | req, err := http.NewRequest(http.MethodPatch, a.githubAPI+"/user/codespaces/"+codespaceName, bytes.NewBuffer(requestBody)) |
| 1087 | if err != nil { |
| 1088 | return nil, fmt.Errorf("error creating request: %w", err) |
| 1089 | } |
| 1090 | |
| 1091 | a.setHeaders(req) |
| 1092 | resp, err := a.do(ctx, req, "/user/codespaces/*") |
| 1093 | if err != nil { |
| 1094 | return nil, fmt.Errorf("error making request: %w", err) |
| 1095 | } |
| 1096 | defer resp.Body.Close() |
| 1097 | |
| 1098 | if resp.StatusCode != http.StatusOK { |
| 1099 | // 422 (unprocessable entity) is likely caused by the codespace having a |
| 1100 | // pending op, so we'll fetch the codespace to see if that's the case |
| 1101 | // and return a more understandable error message. |
| 1102 | if resp.StatusCode == http.StatusUnprocessableEntity { |
| 1103 | pendingOp, reason, err := a.checkForPendingOperation(ctx, codespaceName) |
| 1104 | // If there's an error or there's not a pending op, we want to let |
| 1105 | // this fall through to the normal api.HandleHTTPError flow |
| 1106 | if err == nil && pendingOp { |
| 1107 | return nil, fmt.Errorf( |
| 1108 | "codespace is disabled while it has a pending operation: %s", |
| 1109 | reason, |
| 1110 | ) |
| 1111 | } |
| 1112 | } |
| 1113 | return nil, api.HandleHTTPError(resp) |
| 1114 | } |
| 1115 | |
| 1116 | b, err := io.ReadAll(resp.Body) |
| 1117 | if err != nil { |
| 1118 | return nil, fmt.Errorf("error reading response body: %w", err) |
| 1119 | } |
| 1120 | |
| 1121 | var response Codespace |
| 1122 | if err := json.Unmarshal(b, &response); err != nil { |
| 1123 | return nil, fmt.Errorf("error unmarshalling response: %w", err) |
| 1124 | } |
| 1125 | |
| 1126 | return &response, nil |
| 1127 | } |
| 1128 | |
| 1129 | func (a *API) checkForPendingOperation(ctx context.Context, codespaceName string) (bool, string, error) { |
| 1130 | codespace, err := a.GetCodespace(ctx, codespaceName, false) |