From 2163aba3d5ae5f9643e295038a0710bc57b78cc7 Mon Sep 17 00:00:00 2001 From: Jose Garcia Date: Wed, 1 Sep 2021 13:54:45 -0400 Subject: [PATCH] pass branch for sku selection, pre-select if only one is returned --- api/api.go | 3 ++- cmd/ghcs/create.go | 10 +++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/api/api.go b/api/api.go index faa71d253..7686d8f81 100644 --- a/api/api.go +++ b/api/api.go @@ -327,7 +327,7 @@ type SKU struct { DisplayName string `json:"display_name"` } -func (a *API) GetCodespacesSKUs(ctx context.Context, user *User, repository *Repository, location string) ([]*SKU, error) { +func (a *API) GetCodespacesSKUs(ctx context.Context, user *User, repository *Repository, branch, location string) ([]*SKU, error) { req, err := http.NewRequest(http.MethodGet, githubAPI+"/vscs_internal/user/"+user.Login+"/skus", nil) if err != nil { return nil, fmt.Errorf("err creating request: %v", err) @@ -335,6 +335,7 @@ func (a *API) GetCodespacesSKUs(ctx context.Context, user *User, repository *Rep q := req.URL.Query() q.Add("location", location) + q.Add("ref", branch) q.Add("repository_id", strconv.Itoa(repository.ID)) req.URL.RawQuery = q.Encode() diff --git a/cmd/ghcs/create.go b/cmd/ghcs/create.go index 71e31f298..f8276bc5e 100644 --- a/cmd/ghcs/create.go +++ b/cmd/ghcs/create.go @@ -77,7 +77,7 @@ func create(opts *createOptions) error { return fmt.Errorf("error getting Codespace user: %v", userResult.Err) } - machine, err := getMachineName(ctx, opts.machine, userResult.User, repository, locationResult.Location, apiClient) + machine, err := getMachineName(ctx, opts.machine, userResult.User, repository, branch, locationResult.Location, apiClient) if err != nil { return fmt.Errorf("error getting machine type: %v", err) } @@ -225,8 +225,8 @@ func getBranchName(branch string) (string, error) { } // getMachineName prompts the user to select the machine type, or validates the machine if non-empty. -func getMachineName(ctx context.Context, machine string, user *api.User, repo *api.Repository, location string, apiClient *api.API) (string, error) { - skus, err := apiClient.GetCodespacesSKUs(ctx, user, repo, location) +func getMachineName(ctx context.Context, machine string, user *api.User, repo *api.Repository, branch, location string, apiClient *api.API) (string, error) { + skus, err := apiClient.GetCodespacesSKUs(ctx, user, repo, branch, location) if err != nil { return "", fmt.Errorf("error getting Codespace SKUs: %v", err) } @@ -250,6 +250,10 @@ func getMachineName(ctx context.Context, machine string, user *api.User, repo *a return "", nil } + if len(skus) == 1 { + return skus[0].Name, nil // VS Code does not prompt for SKU if there is only one, this makes us consistent with that behavior + } + skuNames := make([]string, 0, len(skus)) skuByName := make(map[string]*api.SKU) for _, sku := range skus {