Skip to content

Commit 5ffe838

Browse files
committed
Disallow any port operations when codespace has pending operation
Since all of the port operations require the codespace to be running, we need to disallow these operations when there's a pending op since we can't start the codespace in this state. Since the API already disallows this, this is basically cleaning up the error messages that the user sees in this state Old error message: ``` $ gh cs ports forward 80:80 ? Choose codespace: redacted Starting codespace ⣻error connecting to codespace: error starting codespace: HTTP 422: your codespace has an operation pending: updating to a sku with a different amount of storage; please wait until this operation is complete (https://api.github.com/user/codespaces/cwndrws-redacted/start) ``` New error message: ``` $ gh cs ports forward 80:80 ? Choose codespace: redacted codespace is disabled while it has a pending operation: Changing machine types... exit status 1 ```
1 parent da99a1b commit 5ffe838

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

pkg/cmd/codespace/ports.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,9 @@ func newPortsCmd(app *App) *cobra.Command {
4646

4747
// ListPorts lists known ports in a codespace.
4848
func (a *App) ListPorts(ctx context.Context, codespaceName string, exporter cmdutil.Exporter) (err error) {
49-
codespace, err := getOrChooseCodespace(ctx, a.apiClient, codespaceName)
49+
codespace, err := getCodespaceForPorts(ctx, a.apiClient, codespaceName)
5050
if err != nil {
51-
// TODO(josebalius): remove special handling of this error here and it other places
52-
if err == errNoCodespaces {
53-
return err
54-
}
55-
return fmt.Errorf("error choosing codespace: %w", err)
51+
return err
5652
}
5753

5854
devContainerCh := getDevContainer(ctx, a.apiClient, codespace)
@@ -235,12 +231,9 @@ func (a *App) UpdatePortVisibility(ctx context.Context, codespaceName string, ar
235231
return fmt.Errorf("error parsing port arguments: %w", err)
236232
}
237233

238-
codespace, err := getOrChooseCodespace(ctx, a.apiClient, codespaceName)
234+
codespace, err := getCodespaceForPorts(ctx, a.apiClient, codespaceName)
239235
if err != nil {
240-
if err == errNoCodespaces {
241-
return err
242-
}
243-
return fmt.Errorf("error getting codespace: %w", err)
236+
return err
244237
}
245238

246239
session, err := codespaces.ConnectToLiveshare(ctx, a, noopLogger(), a.apiClient, codespace)
@@ -311,12 +304,9 @@ func (a *App) ForwardPorts(ctx context.Context, codespaceName string, ports []st
311304
return fmt.Errorf("get port pairs: %w", err)
312305
}
313306

314-
codespace, err := getOrChooseCodespace(ctx, a.apiClient, codespaceName)
307+
codespace, err := getCodespaceForPorts(ctx, a.apiClient, codespaceName)
315308
if err != nil {
316-
if err == errNoCodespaces {
317-
return err
318-
}
319-
return fmt.Errorf("error getting codespace: %w", err)
309+
return err
320310
}
321311

322312
session, err := codespaces.ConnectToLiveshare(ctx, a, noopLogger(), a.apiClient, codespace)
@@ -380,3 +370,23 @@ func normalizeJSON(j []byte) []byte {
380370
// remove trailing commas
381371
return bytes.ReplaceAll(j, []byte("},}"), []byte("}}"))
382372
}
373+
374+
func getCodespaceForPorts(ctx context.Context, apiClient apiClient, codespaceName string) (*api.Codespace, error) {
375+
codespace, err := getOrChooseCodespace(ctx, apiClient, codespaceName)
376+
if err != nil {
377+
// TODO(josebalius): remove special handling of this error here and it other places
378+
if err == errNoCodespaces {
379+
return nil, err
380+
}
381+
return nil, fmt.Errorf("error choosing codespace: %w", err)
382+
}
383+
384+
if codespace.PendingOperation {
385+
return nil, fmt.Errorf(
386+
"codespace is disabled while it has a pending operation: %s",
387+
codespace.PendingOperationDisabledReason,
388+
)
389+
}
390+
391+
return codespace, nil
392+
}

0 commit comments

Comments
 (0)