Skip to content

Commit 50d8f1e

Browse files
authored
Merge pull request cli#4409 from cli/jg/delete-codespace-public
codespace delete: update DeleteCodespaces to new API endpoint
2 parents 93cea6d + 61af29b commit 50d8f1e

File tree

5 files changed

+18
-27
lines changed

5 files changed

+18
-27
lines changed

internal/codespaces/api/api.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -520,20 +520,14 @@ func (a *API) startCreate(ctx context.Context, repoID int, machine, branch, loca
520520
return &response, nil
521521
}
522522

523-
func (a *API) DeleteCodespace(ctx context.Context, user string, codespaceName string) error {
524-
token, err := a.GetCodespaceToken(ctx, user, codespaceName)
525-
if err != nil {
526-
return fmt.Errorf("error getting codespace token: %w", err)
527-
}
528-
529-
req, err := http.NewRequest(http.MethodDelete, a.githubAPI+"/vscs_internal/user/"+user+"/codespaces/"+codespaceName, nil)
523+
func (a *API) DeleteCodespace(ctx context.Context, codespaceName string) error {
524+
req, err := http.NewRequest(http.MethodDelete, a.githubAPI+"/user/codespaces/"+codespaceName, nil)
530525
if err != nil {
531526
return fmt.Errorf("error creating request: %w", err)
532527
}
533528

534-
// TODO: use a.setHeaders()
535-
req.Header.Set("Authorization", "Bearer "+token)
536-
resp, err := a.do(ctx, req, "/vscs_internal/user/*/codespaces/*")
529+
a.setHeaders(req)
530+
resp, err := a.do(ctx, req, "/user/codespaces/*")
537531
if err != nil {
538532
return fmt.Errorf("error making request: %w", err)
539533
}

pkg/cmd/codespace/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type apiClient interface {
3636
GetCodespaceToken(ctx context.Context, user, name string) (string, error)
3737
GetCodespace(ctx context.Context, token, user, name string) (*api.Codespace, error)
3838
ListCodespaces(ctx context.Context) ([]*api.Codespace, error)
39-
DeleteCodespace(ctx context.Context, user, name string) error
39+
DeleteCodespace(ctx context.Context, name string) error
4040
StartCodespace(ctx context.Context, token string, codespace *api.Codespace) error
4141
CreateCodespace(ctx context.Context, params *api.CreateCodespaceParams) (*api.Codespace, error)
4242
GetRepository(ctx context.Context, nwo string) (*api.Repository, error)

pkg/cmd/codespace/delete.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ func (a *App) Delete(ctx context.Context, opts deleteOptions) error {
8080
nameFilter = c.Name
8181
}
8282
} else {
83-
// TODO: this token is discarded and then re-requested later in DeleteCodespace
8483
token, err := a.apiClient.GetCodespaceToken(ctx, user.Login, nameFilter)
8584
if err != nil {
8685
return fmt.Errorf("error getting codespace token: %w", err)
@@ -132,7 +131,7 @@ func (a *App) Delete(ctx context.Context, opts deleteOptions) error {
132131
for _, c := range codespacesToDelete {
133132
codespaceName := c.Name
134133
g.Go(func() error {
135-
if err := a.apiClient.DeleteCodespace(ctx, user.Login, codespaceName); err != nil {
134+
if err := a.apiClient.DeleteCodespace(ctx, codespaceName); err != nil {
136135
_, _ = a.logger.Errorf("error deleting codespace %q: %v\n", codespaceName, err)
137136
return err
138137
}
@@ -143,6 +142,13 @@ func (a *App) Delete(ctx context.Context, opts deleteOptions) error {
143142
if err := g.Wait(); err != nil {
144143
return errors.New("some codespaces failed to delete")
145144
}
145+
146+
noun := "Codespace"
147+
if len(codespacesToDelete) > 1 {
148+
noun = noun + "s"
149+
}
150+
a.logger.Println(noun + " deleted.")
151+
146152
return nil
147153
}
148154

pkg/cmd/codespace/delete_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,7 @@ func TestDelete(t *testing.T) {
156156
GetUserFunc: func(_ context.Context) (*api.User, error) {
157157
return user, nil
158158
},
159-
DeleteCodespaceFunc: func(_ context.Context, userLogin, name string) error {
160-
if userLogin != user.Login {
161-
return fmt.Errorf("unexpected user %q", userLogin)
162-
}
159+
DeleteCodespaceFunc: func(_ context.Context, name string) error {
163160
if tt.deleteErr != nil {
164161
return tt.deleteErr
165162
}

pkg/cmd/codespace/mock_api.go

Lines changed: 4 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)