X Tutup
Skip to content

Commit 616d6c2

Browse files
authored
Merge pull request cli#4431 from cli/jg/get-skus-public
codespace machine: switch `API.GetCodespacesMachines` to use new API
2 parents 3652307 + e0db10e commit 616d6c2

File tree

4 files changed

+79
-86
lines changed

4 files changed

+79
-86
lines changed

internal/codespaces/api/api.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"fmt"
3535
"io/ioutil"
3636
"net/http"
37-
"strconv"
3837
"strings"
3938
"time"
4039

@@ -392,26 +391,26 @@ func (a *API) GetCodespaceRegionLocation(ctx context.Context) (string, error) {
392391
return response.Current, nil
393392
}
394393

395-
type SKU struct {
394+
type Machine struct {
396395
Name string `json:"name"`
397396
DisplayName string `json:"display_name"`
398397
}
399398

400-
// GetCodespacesSKUs returns the available SKUs for the user for a given repo, branch and location.
401-
func (a *API) GetCodespacesSKUs(ctx context.Context, user *User, repository *Repository, branch, location string) ([]*SKU, error) {
402-
req, err := http.NewRequest(http.MethodGet, a.githubAPI+"/vscs_internal/user/"+user.Login+"/skus", nil)
399+
// GetCodespacesMachines returns the codespaces machines for the given repo, branch and location.
400+
func (a *API) GetCodespacesMachines(ctx context.Context, repoID int, branch, location string) ([]*Machine, error) {
401+
reqURL := fmt.Sprintf("%s/repositories/%d/codespaces/machines", a.githubAPI, repoID)
402+
req, err := http.NewRequest(http.MethodGet, reqURL, nil)
403403
if err != nil {
404404
return nil, fmt.Errorf("error creating request: %w", err)
405405
}
406406

407407
q := req.URL.Query()
408408
q.Add("location", location)
409409
q.Add("ref", branch)
410-
q.Add("repository_id", strconv.Itoa(repository.ID))
411410
req.URL.RawQuery = q.Encode()
412411

413412
a.setHeaders(req)
414-
resp, err := a.do(ctx, req, "/vscs_internal/user/*/skus")
413+
resp, err := a.do(ctx, req, "/repositories/*/codespaces/machines")
415414
if err != nil {
416415
return nil, fmt.Errorf("error making request: %w", err)
417416
}
@@ -427,13 +426,13 @@ func (a *API) GetCodespacesSKUs(ctx context.Context, user *User, repository *Rep
427426
}
428427

429428
var response struct {
430-
SKUs []*SKU `json:"skus"`
429+
Machines []*Machine `json:"machines"`
431430
}
432431
if err := json.Unmarshal(b, &response); err != nil {
433432
return nil, fmt.Errorf("error unmarshaling response: %w", err)
434433
}
435434

436-
return response.SKUs, nil
435+
return response.Machines, nil
437436
}
438437

439438
// CreateCodespaceParams are the required parameters for provisioning a Codespace.

pkg/cmd/codespace/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type apiClient interface {
4242
GetRepository(ctx context.Context, nwo string) (*api.Repository, error)
4343
AuthorizedKeys(ctx context.Context, user string) ([]byte, error)
4444
GetCodespaceRegionLocation(ctx context.Context) (string, error)
45-
GetCodespacesSKUs(ctx context.Context, user *api.User, repository *api.Repository, branch, location string) ([]*api.SKU, error)
45+
GetCodespacesMachines(ctx context.Context, repoID int, branch, location string) ([]*api.Machine, error)
4646
GetCodespaceRepositoryContents(ctx context.Context, codespace *api.Codespace, path string) ([]byte, error)
4747
}
4848

pkg/cmd/codespace/create.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (a *App) Create(ctx context.Context, opts createOptions) error {
7171
return fmt.Errorf("error getting codespace user: %w", userResult.Err)
7272
}
7373

74-
machine, err := getMachineName(ctx, opts.machine, userResult.User, repository, branch, locationResult.Location, a.apiClient)
74+
machine, err := getMachineName(ctx, a.apiClient, repository.ID, opts.machine, branch, locationResult.Location)
7575
if err != nil {
7676
return fmt.Errorf("error getting machine type: %w", err)
7777
}
@@ -234,64 +234,64 @@ func getBranchName(branch string) (string, error) {
234234
}
235235

236236
// getMachineName prompts the user to select the machine type, or validates the machine if non-empty.
237-
func getMachineName(ctx context.Context, machine string, user *api.User, repo *api.Repository, branch, location string, apiClient apiClient) (string, error) {
238-
skus, err := apiClient.GetCodespacesSKUs(ctx, user, repo, branch, location)
237+
func getMachineName(ctx context.Context, apiClient apiClient, repoID int, machine, branch, location string) (string, error) {
238+
machines, err := apiClient.GetCodespacesMachines(ctx, repoID, branch, location)
239239
if err != nil {
240240
return "", fmt.Errorf("error requesting machine instance types: %w", err)
241241
}
242242

243243
// if user supplied a machine type, it must be valid
244244
// if no machine type was supplied, we don't error if there are no machine types for the current repo
245245
if machine != "" {
246-
for _, sku := range skus {
247-
if machine == sku.Name {
246+
for _, m := range machines {
247+
if machine == m.Name {
248248
return machine, nil
249249
}
250250
}
251251

252-
availableSKUs := make([]string, len(skus))
253-
for i := 0; i < len(skus); i++ {
254-
availableSKUs[i] = skus[i].Name
252+
availableMachines := make([]string, len(machines))
253+
for i := 0; i < len(machines); i++ {
254+
availableMachines[i] = machines[i].Name
255255
}
256256

257-
return "", fmt.Errorf("there is no such machine for the repository: %s\nAvailable machines: %v", machine, availableSKUs)
258-
} else if len(skus) == 0 {
257+
return "", fmt.Errorf("there is no such machine for the repository: %s\nAvailable machines: %v", machine, availableMachines)
258+
} else if len(machines) == 0 {
259259
return "", nil
260260
}
261261

262-
if len(skus) == 1 {
263-
return skus[0].Name, nil // VS Code does not prompt for SKU if there is only one, this makes us consistent with that behavior
262+
if len(machines) == 1 {
263+
// VS Code does not prompt for machine if there is only one, this makes us consistent with that behavior
264+
return machines[0].Name, nil
264265
}
265266

266-
skuNames := make([]string, 0, len(skus))
267-
skuByName := make(map[string]*api.SKU)
268-
for _, sku := range skus {
269-
nameParts := camelcase.Split(sku.Name)
267+
machineNames := make([]string, 0, len(machines))
268+
machineByName := make(map[string]*api.Machine)
269+
for _, m := range machines {
270+
nameParts := camelcase.Split(m.Name)
270271
machineName := strings.Title(strings.ToLower(nameParts[0]))
271-
skuName := fmt.Sprintf("%s - %s", machineName, sku.DisplayName)
272-
skuNames = append(skuNames, skuName)
273-
skuByName[skuName] = sku
272+
machineName = fmt.Sprintf("%s - %s", machineName, m.DisplayName)
273+
machineNames = append(machineNames, machineName)
274+
machineByName[machineName] = m
274275
}
275276

276-
skuSurvey := []*survey.Question{
277+
machineSurvey := []*survey.Question{
277278
{
278-
Name: "sku",
279+
Name: "machine",
279280
Prompt: &survey.Select{
280281
Message: "Choose Machine Type:",
281-
Options: skuNames,
282-
Default: skuNames[0],
282+
Options: machineNames,
283+
Default: machineNames[0],
283284
},
284285
Validate: survey.Required,
285286
},
286287
}
287288

288-
var skuAnswers struct{ SKU string }
289-
if err := ask(skuSurvey, &skuAnswers); err != nil {
290-
return "", fmt.Errorf("error getting SKU: %w", err)
289+
var machineAnswers struct{ Machine string }
290+
if err := ask(machineSurvey, &machineAnswers); err != nil {
291+
return "", fmt.Errorf("error getting machine: %w", err)
291292
}
292293

293-
sku := skuByName[skuAnswers.SKU]
294-
machine = sku.Name
294+
selectedMachine := machineByName[machineAnswers.Machine]
295295

296-
return machine, nil
296+
return selectedMachine.Name, nil
297297
}

pkg/cmd/codespace/mock_api.go

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

0 commit comments

Comments
 (0)
X Tutup