@@ -167,18 +167,22 @@ func (a *API) GetRepository(ctx context.Context, nwo string) (*Repository, error
167167}
168168
169169// Codespace represents a codespace.
170+ // You can see more about the fields in this type in the codespaces api docs:
171+ // https://docs.github.com/en/rest/reference/codespaces
170172type Codespace struct {
171- Name string `json:"name"`
172- CreatedAt string `json:"created_at"`
173- DisplayName string `json:"display_name"`
174- LastUsedAt string `json:"last_used_at"`
175- Owner User `json:"owner"`
176- Repository Repository `json:"repository"`
177- State string `json:"state"`
178- GitStatus CodespaceGitStatus `json:"git_status"`
179- Connection CodespaceConnection `json:"connection"`
180- Machine CodespaceMachine `json:"machine"`
181- VSCSTarget string `json:"vscs_target"`
173+ Name string `json:"name"`
174+ CreatedAt string `json:"created_at"`
175+ DisplayName string `json:"display_name"`
176+ LastUsedAt string `json:"last_used_at"`
177+ Owner User `json:"owner"`
178+ Repository Repository `json:"repository"`
179+ State string `json:"state"`
180+ GitStatus CodespaceGitStatus `json:"git_status"`
181+ Connection CodespaceConnection `json:"connection"`
182+ Machine CodespaceMachine `json:"machine"`
183+ VSCSTarget string `json:"vscs_target"`
184+ PendingOperation bool `json:"pending_operation"`
185+ PendingOperationDisabledReason string `json:"pending_operation_disabled_reason"`
182186}
183187
184188type CodespaceGitStatus struct {
@@ -781,6 +785,20 @@ func (a *API) EditCodespace(ctx context.Context, codespaceName string, params *E
781785 defer resp .Body .Close ()
782786
783787 if resp .StatusCode != http .StatusOK {
788+ // 422 (unprocessable entity) is likely caused by the codespace having a
789+ // pending op, so we'll fetch the codespace to see if that's the case
790+ // and return a more understandable error message.
791+ if resp .StatusCode == http .StatusUnprocessableEntity {
792+ pendingOp , reason , err := a .checkForPendingOperation (ctx , codespaceName )
793+ // If there's an error or there's not a pending op, we want to let
794+ // this fall through to the normal api.HandleHTTPError flow
795+ if err == nil && pendingOp {
796+ return nil , fmt .Errorf (
797+ "codespace is disabled while it has a pending operation: %s" ,
798+ reason ,
799+ )
800+ }
801+ }
784802 return nil , api .HandleHTTPError (resp )
785803 }
786804
@@ -797,6 +815,14 @@ func (a *API) EditCodespace(ctx context.Context, codespaceName string, params *E
797815 return & response , nil
798816}
799817
818+ func (a * API ) checkForPendingOperation (ctx context.Context , codespaceName string ) (bool , string , error ) {
819+ codespace , err := a .GetCodespace (ctx , codespaceName , false )
820+ if err != nil {
821+ return false , "" , err
822+ }
823+ return codespace .PendingOperation , codespace .PendingOperationDisabledReason , nil
824+ }
825+
800826type getCodespaceRepositoryContentsResponse struct {
801827 Content string `json:"content"`
802828}
0 commit comments