Skip to content

Commit 15c8f29

Browse files
authored
Add prebuild availability status to create codespaces (cli#4737)
1 parent 57215ec commit 15c8f29

3 files changed

Lines changed: 58 additions & 3 deletions

File tree

internal/codespaces/api/api.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,9 @@ func (a *API) GetCodespaceRegionLocation(ctx context.Context) (string, error) {
415415
}
416416

417417
type Machine struct {
418-
Name string `json:"name"`
419-
DisplayName string `json:"display_name"`
418+
Name string `json:"name"`
419+
DisplayName string `json:"display_name"`
420+
PrebuildAvailability string `json:"prebuild_availability"`
420421
}
421422

422423
// GetCodespacesMachines returns the codespaces machines for the given repo, branch and location.

pkg/cmd/codespace/create.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func getMachineName(ctx context.Context, apiClient apiClient, repoID int, machin
234234
machineNames := make([]string, 0, len(machines))
235235
machineByName := make(map[string]*api.Machine)
236236
for _, m := range machines {
237-
machineName := m.DisplayName
237+
machineName := buildDisplayName(m.DisplayName, m.PrebuildAvailability)
238238
machineNames = append(machineNames, machineName)
239239
machineByName[machineName] = m
240240
}
@@ -260,3 +260,14 @@ func getMachineName(ctx context.Context, apiClient apiClient, repoID int, machin
260260

261261
return selectedMachine.Name, nil
262262
}
263+
264+
// buildDisplayName returns display name to be used in the machine survey prompt.
265+
func buildDisplayName(displayName string, prebuildAvailability string) string {
266+
prebuildText := ""
267+
268+
if prebuildAvailability == "blob" || prebuildAvailability == "pool" {
269+
prebuildText = " (Prebuild ready)"
270+
}
271+
272+
return fmt.Sprintf("%s%s", displayName, prebuildText)
273+
}

pkg/cmd/codespace/create_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package codespace
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestBuildDisplayName(t *testing.T) {
8+
tests := []struct {
9+
name string
10+
prebuildAvailability string
11+
expectedDisplayName string
12+
}{
13+
{
14+
name: "prebuild availability is pool",
15+
prebuildAvailability: "pool",
16+
expectedDisplayName: "4 cores, 8 GB RAM, 32 GB storage (Prebuild ready)",
17+
},
18+
{
19+
name: "prebuild availability is blob",
20+
prebuildAvailability: "blob",
21+
expectedDisplayName: "4 cores, 8 GB RAM, 32 GB storage (Prebuild ready)",
22+
},
23+
{
24+
name: "prebuild availability is none",
25+
prebuildAvailability: "none",
26+
expectedDisplayName: "4 cores, 8 GB RAM, 32 GB storage",
27+
},
28+
{
29+
name: "prebuild availability is empty",
30+
prebuildAvailability: "",
31+
expectedDisplayName: "4 cores, 8 GB RAM, 32 GB storage",
32+
},
33+
}
34+
for _, tt := range tests {
35+
t.Run(tt.name, func(t *testing.T) {
36+
displayName := buildDisplayName("4 cores, 8 GB RAM, 32 GB storage", tt.prebuildAvailability)
37+
38+
if displayName != tt.expectedDisplayName {
39+
t.Errorf("displayName = %q, expectedDisplayName %q", displayName, tt.expectedDisplayName)
40+
}
41+
})
42+
}
43+
}

0 commit comments

Comments
 (0)