From a75839577dd094df6c3804b2f2003425c84ea086 Mon Sep 17 00:00:00 2001 From: Dhananjay Mishra Date: Sat, 20 Dec 2025 15:32:20 +0000 Subject: [PATCH 1/5] add support for Codespace Machines APIs --- github/codespaces_machines.go | 89 ++++++++++++++++++ github/codespaces_machines_test.go | 141 +++++++++++++++++++++++++++++ github/github-accessors.go | 24 +++++ github/github-accessors_test.go | 33 +++++++ 4 files changed, 287 insertions(+) create mode 100644 github/codespaces_machines.go create mode 100644 github/codespaces_machines_test.go diff --git a/github/codespaces_machines.go b/github/codespaces_machines.go new file mode 100644 index 00000000000..e1d1bbb77e1 --- /dev/null +++ b/github/codespaces_machines.go @@ -0,0 +1,89 @@ +// Copyright 2025 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import ( + "context" + "fmt" +) + +// Machine represent a description of the machine powering a codespace. +type Machine struct { + Name string `json:"name"` + DisplayName string `json:"display_name"` + OperatingSystem string `json:"operating_system"` + StorageInBytes int64 `json:"storage_in_bytes"` + MemoryInBytes int64 `json:"memory_in_bytes"` + CPUs int64 `json:"cpus"` + // PrebuildAvailability represents whether a prebuild is currently available when creating a codespace for this machine and repository. + // Value will be "null" if prebuilds are not supported or prebuild availability could not be determined. + // Value will be "none" if no prebuild is available. + // Latest values "ready" and "in_progress" indicate the prebuild availability status. + PrebuildAvailability string `json:"prebuild_availability"` +} + +// CodespaceMachines represent a list of machines. +type CodespaceMachines struct { + TotalCount int64 `json:"total_count"` + Machines []*Machine `json:"machines"` +} + +// ListMachinesOptions represent options for ListMachinesTypesForRepository. +type ListMachinesOptions struct { + // Ref represent the branch or commit to check for prebuild availability and devcontainer restrictions. + Ref *string `json:"ref,omitempty"` + // Location represent the location to check for available machines. Assigned by IP if not provided. + Location *string `json:"location,omitempty"` + // ClientIP represent the IP for location auto-detection when proxying a request + ClientIP *string `json:"client_ip,omitempty"` +} + +// ListMachinesTypesForRepository lists the machine types available for a given repository based on its configuration. +// +// GitHub API docs: https://docs.github.com/rest/codespaces/machines#list-available-machine-types-for-a-repository +// +//meta:operation GET /repos/{owner}/{repo}/codespaces/machines +func (s *CodespacesService) ListMachinesTypesForRepository(ctx context.Context, owner, repo string, opts *ListMachinesOptions) (*CodespaceMachines, *Response, error) { + u := fmt.Sprintf("repos/%v/%v/codespaces/machines", owner, repo) + u, err := addOptions(u, opts) + if err != nil { + return nil, nil, err + } + + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + var machines *CodespaceMachines + resp, err := s.client.Do(ctx, req, &machines) + if err != nil { + return nil, resp, err + } + + return machines, resp, nil +} + +// ListMachinesTypesForCodespace lists the machine types a codespace can transition to use. +// +// GitHub API docs: https://docs.github.com/rest/codespaces/machines#list-machine-types-for-a-codespace +// +//meta:operation GET /user/codespaces/{codespace_name}/machines +func (s *CodespacesService) ListMachinesTypesForCodespace(ctx context.Context, codespaceName string) (*CodespaceMachines, *Response, error) { + u := fmt.Sprintf("user/codespaces/%v/machines", codespaceName) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + var machines *CodespaceMachines + resp, err := s.client.Do(ctx, req, &machines) + if err != nil { + return nil, resp, err + } + + return machines, resp, nil +} diff --git a/github/codespaces_machines_test.go b/github/codespaces_machines_test.go new file mode 100644 index 00000000000..e0b7708177d --- /dev/null +++ b/github/codespaces_machines_test.go @@ -0,0 +1,141 @@ +// Copyright 2025 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import ( + "fmt" + "net/http" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestCodespacesService_ListMachinesTypesForRepository(t *testing.T) { + t.Parallel() + client, mux, _ := setup(t) + + mux.HandleFunc("/repos/owner/repo/codespaces/machines", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + + fmt.Fprint(w, `{ + "total_count": 1, + "machines": [ + { + "name": "standardLinux", + "display_name": "4 cores, 8 GB RAM, 64 GB storage", + "operating_system": "linux", + "storage_in_bytes": 68719476736, + "memory_in_bytes": 17179869184, + "cpus": 4, + "prebuild_availability": "ready" + } + ] + }`) + }) + + ctx := t.Context() + opt := &ListMachinesOptions{ + Ref: Ptr("main"), + Location: Ptr("WestUs2"), + ClientIP: Ptr("1.2.3.4"), + } + + got, _, err := client.Codespaces.ListMachinesTypesForRepository( + ctx, + "owner", + "repo", + opt, + ) + if err != nil { + t.Fatalf("Codespaces.ListMachinesTypesForRepository returned error: %v", err) + } + + want := &CodespaceMachines{ + TotalCount: 1, + Machines: []*Machine{ + { + Name: "standardLinux", + DisplayName: "4 cores, 8 GB RAM, 64 GB storage", + OperatingSystem: "linux", + StorageInBytes: 68719476736, + MemoryInBytes: 17179869184, + CPUs: 4, + PrebuildAvailability: "ready", + }, + }, + } + + if !cmp.Equal(got, want) { + t.Errorf("Codespaces.ListMachinesTypesForRepository returned %+v, want %+v", got, want) + } + + const methodName = "ListMachinesTypesForRepository" + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Codespaces.ListMachinesTypesForRepository(ctx, "/n", "/n", opt) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestCodespacesService_ListMachinesTypesForCodespace(t *testing.T) { + t.Parallel() + client, mux, _ := setup(t) + + mux.HandleFunc("/user/codespaces/codespace_1/machines", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + + fmt.Fprint(w, `{ + "total_count": 1, + "machines": [ + { + "name": "standardLinux", + "display_name": "4 cores, 8 GB RAM, 64 GB storage", + "operating_system": "linux", + "storage_in_bytes": 68719476736, + "memory_in_bytes": 17179869184, + "cpus": 4, + "prebuild_availability": "ready" + } + ] + }`) + }) + + ctx := t.Context() + got, _, err := client.Codespaces.ListMachinesTypesForCodespace(ctx, "codespace_1") + if err != nil { + t.Fatalf("Codespaces.ListMachinesTypesForCodespace returned error: %v", err) + } + + want := &CodespaceMachines{ + TotalCount: 1, + Machines: []*Machine{ + { + Name: "standardLinux", + DisplayName: "4 cores, 8 GB RAM, 64 GB storage", + OperatingSystem: "linux", + StorageInBytes: 68719476736, + MemoryInBytes: 17179869184, + CPUs: 4, + PrebuildAvailability: "ready", + }, + }, + } + + if !cmp.Equal(got, want) { + t.Errorf("Codespaces.ListMachinesTypesForCodespace returned %+v, want %+v", got, want) + } + + const methodName = "ListMachinesTypesForCodespace" + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Codespaces.ListMachinesTypesForCodespace(ctx, "/n") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} diff --git a/github/github-accessors.go b/github/github-accessors.go index b907c2af56a..33b6cec4f5c 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -14550,6 +14550,30 @@ func (l *ListGlobalSecurityAdvisoriesOptions) GetUpdated() string { return *l.Updated } +// GetClientIP returns the ClientIP field if it's non-nil, zero value otherwise. +func (l *ListMachinesOptions) GetClientIP() string { + if l == nil || l.ClientIP == nil { + return "" + } + return *l.ClientIP +} + +// GetLocation returns the Location field if it's non-nil, zero value otherwise. +func (l *ListMachinesOptions) GetLocation() string { + if l == nil || l.Location == nil { + return "" + } + return *l.Location +} + +// GetRef returns the Ref field if it's non-nil, zero value otherwise. +func (l *ListMachinesOptions) GetRef() string { + if l == nil || l.Ref == nil { + return "" + } + return *l.Ref +} + // GetTotalCount returns the TotalCount field if it's non-nil, zero value otherwise. func (l *ListOrganizations) GetTotalCount() int { if l == nil || l.TotalCount == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index b12c405d2bf..1d56fc3bedd 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -18919,6 +18919,39 @@ func TestListGlobalSecurityAdvisoriesOptions_GetUpdated(tt *testing.T) { l.GetUpdated() } +func TestListMachinesOptions_GetClientIP(tt *testing.T) { + tt.Parallel() + var zeroValue string + l := &ListMachinesOptions{ClientIP: &zeroValue} + l.GetClientIP() + l = &ListMachinesOptions{} + l.GetClientIP() + l = nil + l.GetClientIP() +} + +func TestListMachinesOptions_GetLocation(tt *testing.T) { + tt.Parallel() + var zeroValue string + l := &ListMachinesOptions{Location: &zeroValue} + l.GetLocation() + l = &ListMachinesOptions{} + l.GetLocation() + l = nil + l.GetLocation() +} + +func TestListMachinesOptions_GetRef(tt *testing.T) { + tt.Parallel() + var zeroValue string + l := &ListMachinesOptions{Ref: &zeroValue} + l.GetRef() + l = &ListMachinesOptions{} + l.GetRef() + l = nil + l.GetRef() +} + func TestListOrganizations_GetTotalCount(tt *testing.T) { tt.Parallel() var zeroValue int From cf46ffb42211c054a0701ebe485ddee21c326063 Mon Sep 17 00:00:00 2001 From: Dhananjay Mishra Date: Sat, 20 Dec 2025 16:39:59 +0000 Subject: [PATCH 2/5] feedback --- ...aces_machines.go => codespace_machines.go} | 32 ++++---- ...nes_test.go => codespace_machines_test.go} | 64 +++++++-------- github/github-accessors.go | 56 ++++++++++++++ github/github-accessors_test.go | 77 +++++++++++++++++++ 4 files changed, 183 insertions(+), 46 deletions(-) rename github/{codespaces_machines.go => codespace_machines.go} (64%) rename github/{codespaces_machines_test.go => codespace_machines_test.go} (54%) diff --git a/github/codespaces_machines.go b/github/codespace_machines.go similarity index 64% rename from github/codespaces_machines.go rename to github/codespace_machines.go index e1d1bbb77e1..9071a127820 100644 --- a/github/codespaces_machines.go +++ b/github/codespace_machines.go @@ -10,28 +10,28 @@ import ( "fmt" ) -// Machine represent a description of the machine powering a codespace. -type Machine struct { - Name string `json:"name"` - DisplayName string `json:"display_name"` - OperatingSystem string `json:"operating_system"` - StorageInBytes int64 `json:"storage_in_bytes"` - MemoryInBytes int64 `json:"memory_in_bytes"` - CPUs int64 `json:"cpus"` +// CodespaceMachine represent a description of the machine powering a codespace. +type CodespaceMachine struct { + Name *string `json:"name,omitempty"` + DisplayName *string `json:"display_name,omitempty"` + OperatingSystem *string `json:"operating_system,omitempty"` + StorageInBytes *int64 `json:"storage_in_bytes,omitempty"` + MemoryInBytes *int64 `json:"memory_in_bytes,omitempty"` + CPUs *int `json:"cpus,omitempty"` // PrebuildAvailability represents whether a prebuild is currently available when creating a codespace for this machine and repository. // Value will be "null" if prebuilds are not supported or prebuild availability could not be determined. // Value will be "none" if no prebuild is available. // Latest values "ready" and "in_progress" indicate the prebuild availability status. - PrebuildAvailability string `json:"prebuild_availability"` + PrebuildAvailability *string `json:"prebuild_availability,omitempty"` } // CodespaceMachines represent a list of machines. type CodespaceMachines struct { - TotalCount int64 `json:"total_count"` - Machines []*Machine `json:"machines"` + TotalCount int64 `json:"total_count"` + Machines []*CodespaceMachine `json:"machines"` } -// ListMachinesOptions represent options for ListMachinesTypesForRepository. +// ListMachinesOptions represent options for ListMachineTypesForRepository. type ListMachinesOptions struct { // Ref represent the branch or commit to check for prebuild availability and devcontainer restrictions. Ref *string `json:"ref,omitempty"` @@ -41,12 +41,12 @@ type ListMachinesOptions struct { ClientIP *string `json:"client_ip,omitempty"` } -// ListMachinesTypesForRepository lists the machine types available for a given repository based on its configuration. +// ListMachineTypesForRepository lists the machine types available for a given repository based on its configuration. // // GitHub API docs: https://docs.github.com/rest/codespaces/machines#list-available-machine-types-for-a-repository // //meta:operation GET /repos/{owner}/{repo}/codespaces/machines -func (s *CodespacesService) ListMachinesTypesForRepository(ctx context.Context, owner, repo string, opts *ListMachinesOptions) (*CodespaceMachines, *Response, error) { +func (s *CodespacesService) ListMachineTypesForRepository(ctx context.Context, owner, repo string, opts *ListMachinesOptions) (*CodespaceMachines, *Response, error) { u := fmt.Sprintf("repos/%v/%v/codespaces/machines", owner, repo) u, err := addOptions(u, opts) if err != nil { @@ -67,12 +67,12 @@ func (s *CodespacesService) ListMachinesTypesForRepository(ctx context.Context, return machines, resp, nil } -// ListMachinesTypesForCodespace lists the machine types a codespace can transition to use. +// ListMachineTypesForCodespace lists the machine types a codespace can transition to use. // // GitHub API docs: https://docs.github.com/rest/codespaces/machines#list-machine-types-for-a-codespace // //meta:operation GET /user/codespaces/{codespace_name}/machines -func (s *CodespacesService) ListMachinesTypesForCodespace(ctx context.Context, codespaceName string) (*CodespaceMachines, *Response, error) { +func (s *CodespacesService) ListMachineTypesForCodespace(ctx context.Context, codespaceName string) (*CodespaceMachines, *Response, error) { u := fmt.Sprintf("user/codespaces/%v/machines", codespaceName) req, err := s.client.NewRequest("GET", u, nil) if err != nil { diff --git a/github/codespaces_machines_test.go b/github/codespace_machines_test.go similarity index 54% rename from github/codespaces_machines_test.go rename to github/codespace_machines_test.go index e0b7708177d..c21c8f344ff 100644 --- a/github/codespaces_machines_test.go +++ b/github/codespace_machines_test.go @@ -13,7 +13,7 @@ import ( "github.com/google/go-cmp/cmp" ) -func TestCodespacesService_ListMachinesTypesForRepository(t *testing.T) { +func TestCodespacesService_ListMachineTypesForRepository(t *testing.T) { t.Parallel() client, mux, _ := setup(t) @@ -37,44 +37,48 @@ func TestCodespacesService_ListMachinesTypesForRepository(t *testing.T) { }) ctx := t.Context() - opt := &ListMachinesOptions{ + opts := &ListMachinesOptions{ Ref: Ptr("main"), Location: Ptr("WestUs2"), ClientIP: Ptr("1.2.3.4"), } - got, _, err := client.Codespaces.ListMachinesTypesForRepository( + got, _, err := client.Codespaces.ListMachineTypesForRepository( ctx, "owner", "repo", - opt, + opts, ) if err != nil { - t.Fatalf("Codespaces.ListMachinesTypesForRepository returned error: %v", err) + t.Fatalf("Codespaces.ListMachineTypesForRepository returned error: %v", err) } want := &CodespaceMachines{ TotalCount: 1, - Machines: []*Machine{ + Machines: []*CodespaceMachine{ { - Name: "standardLinux", - DisplayName: "4 cores, 8 GB RAM, 64 GB storage", - OperatingSystem: "linux", - StorageInBytes: 68719476736, - MemoryInBytes: 17179869184, - CPUs: 4, - PrebuildAvailability: "ready", + Name: Ptr("standardLinux"), + DisplayName: Ptr("4 cores, 8 GB RAM, 64 GB storage"), + OperatingSystem: Ptr("linux"), + StorageInBytes: Ptr(int64(68719476736)), + MemoryInBytes: Ptr(int64(17179869184)), + CPUs: Ptr(4), + PrebuildAvailability: Ptr("ready"), }, }, } if !cmp.Equal(got, want) { - t.Errorf("Codespaces.ListMachinesTypesForRepository returned %+v, want %+v", got, want) + t.Errorf("Codespaces.ListMachineTypesForRepository returned %+v, want %+v", got, want) } - const methodName = "ListMachinesTypesForRepository" + const methodName = "ListMachineTypesForRepository" + testBadOptions(t, methodName, func() error { + _, _, err := client.Codespaces.ListMachineTypesForRepository(ctx, "\n", "/n", opts) + return err + }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Codespaces.ListMachinesTypesForRepository(ctx, "/n", "/n", opt) + got, resp, err := client.Codespaces.ListMachineTypesForRepository(ctx, "/n", "/n", opts) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -82,7 +86,7 @@ func TestCodespacesService_ListMachinesTypesForRepository(t *testing.T) { }) } -func TestCodespacesService_ListMachinesTypesForCodespace(t *testing.T) { +func TestCodespacesService_ListMachineTypesForCodespace(t *testing.T) { t.Parallel() client, mux, _ := setup(t) @@ -106,33 +110,33 @@ func TestCodespacesService_ListMachinesTypesForCodespace(t *testing.T) { }) ctx := t.Context() - got, _, err := client.Codespaces.ListMachinesTypesForCodespace(ctx, "codespace_1") + got, _, err := client.Codespaces.ListMachineTypesForCodespace(ctx, "codespace_1") if err != nil { - t.Fatalf("Codespaces.ListMachinesTypesForCodespace returned error: %v", err) + t.Fatalf("Codespaces.ListMachineTypesForCodespace returned error: %v", err) } want := &CodespaceMachines{ TotalCount: 1, - Machines: []*Machine{ + Machines: []*CodespaceMachine{ { - Name: "standardLinux", - DisplayName: "4 cores, 8 GB RAM, 64 GB storage", - OperatingSystem: "linux", - StorageInBytes: 68719476736, - MemoryInBytes: 17179869184, - CPUs: 4, - PrebuildAvailability: "ready", + Name: Ptr("standardLinux"), + DisplayName: Ptr("4 cores, 8 GB RAM, 64 GB storage"), + OperatingSystem: Ptr("linux"), + StorageInBytes: Ptr(int64(68719476736)), + MemoryInBytes: Ptr(int64(17179869184)), + CPUs: Ptr(4), + PrebuildAvailability: Ptr("ready"), }, }, } if !cmp.Equal(got, want) { - t.Errorf("Codespaces.ListMachinesTypesForCodespace returned %+v, want %+v", got, want) + t.Errorf("Codespaces.ListMachineTypesForCodespace returned %+v, want %+v", got, want) } - const methodName = "ListMachinesTypesForCodespace" + const methodName = "ListMachineTypesForCodespace" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Codespaces.ListMachinesTypesForCodespace(ctx, "/n") + got, resp, err := client.Codespaces.ListMachineTypesForCodespace(ctx, "/n") if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } diff --git a/github/github-accessors.go b/github/github-accessors.go index 33b6cec4f5c..ce5263800ac 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -3942,6 +3942,62 @@ func (c *CodespaceGetDefaultAttributesOptions) GetRef() string { return *c.Ref } +// GetCPUs returns the CPUs field if it's non-nil, zero value otherwise. +func (c *CodespaceMachine) GetCPUs() int { + if c == nil || c.CPUs == nil { + return 0 + } + return *c.CPUs +} + +// GetDisplayName returns the DisplayName field if it's non-nil, zero value otherwise. +func (c *CodespaceMachine) GetDisplayName() string { + if c == nil || c.DisplayName == nil { + return "" + } + return *c.DisplayName +} + +// GetMemoryInBytes returns the MemoryInBytes field if it's non-nil, zero value otherwise. +func (c *CodespaceMachine) GetMemoryInBytes() int64 { + if c == nil || c.MemoryInBytes == nil { + return 0 + } + return *c.MemoryInBytes +} + +// GetName returns the Name field if it's non-nil, zero value otherwise. +func (c *CodespaceMachine) GetName() string { + if c == nil || c.Name == nil { + return "" + } + return *c.Name +} + +// GetOperatingSystem returns the OperatingSystem field if it's non-nil, zero value otherwise. +func (c *CodespaceMachine) GetOperatingSystem() string { + if c == nil || c.OperatingSystem == nil { + return "" + } + return *c.OperatingSystem +} + +// GetPrebuildAvailability returns the PrebuildAvailability field if it's non-nil, zero value otherwise. +func (c *CodespaceMachine) GetPrebuildAvailability() string { + if c == nil || c.PrebuildAvailability == nil { + return "" + } + return *c.PrebuildAvailability +} + +// GetStorageInBytes returns the StorageInBytes field if it's non-nil, zero value otherwise. +func (c *CodespaceMachine) GetStorageInBytes() int64 { + if c == nil || c.StorageInBytes == nil { + return 0 + } + return *c.StorageInBytes +} + // GetAhead returns the Ahead field if it's non-nil, zero value otherwise. func (c *CodespacesGitStatus) GetAhead() int { if c == nil || c.Ahead == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 1d56fc3bedd..a047c19c6d8 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -5146,6 +5146,83 @@ func TestCodespaceGetDefaultAttributesOptions_GetRef(tt *testing.T) { c.GetRef() } +func TestCodespaceMachine_GetCPUs(tt *testing.T) { + tt.Parallel() + var zeroValue int + c := &CodespaceMachine{CPUs: &zeroValue} + c.GetCPUs() + c = &CodespaceMachine{} + c.GetCPUs() + c = nil + c.GetCPUs() +} + +func TestCodespaceMachine_GetDisplayName(tt *testing.T) { + tt.Parallel() + var zeroValue string + c := &CodespaceMachine{DisplayName: &zeroValue} + c.GetDisplayName() + c = &CodespaceMachine{} + c.GetDisplayName() + c = nil + c.GetDisplayName() +} + +func TestCodespaceMachine_GetMemoryInBytes(tt *testing.T) { + tt.Parallel() + var zeroValue int64 + c := &CodespaceMachine{MemoryInBytes: &zeroValue} + c.GetMemoryInBytes() + c = &CodespaceMachine{} + c.GetMemoryInBytes() + c = nil + c.GetMemoryInBytes() +} + +func TestCodespaceMachine_GetName(tt *testing.T) { + tt.Parallel() + var zeroValue string + c := &CodespaceMachine{Name: &zeroValue} + c.GetName() + c = &CodespaceMachine{} + c.GetName() + c = nil + c.GetName() +} + +func TestCodespaceMachine_GetOperatingSystem(tt *testing.T) { + tt.Parallel() + var zeroValue string + c := &CodespaceMachine{OperatingSystem: &zeroValue} + c.GetOperatingSystem() + c = &CodespaceMachine{} + c.GetOperatingSystem() + c = nil + c.GetOperatingSystem() +} + +func TestCodespaceMachine_GetPrebuildAvailability(tt *testing.T) { + tt.Parallel() + var zeroValue string + c := &CodespaceMachine{PrebuildAvailability: &zeroValue} + c.GetPrebuildAvailability() + c = &CodespaceMachine{} + c.GetPrebuildAvailability() + c = nil + c.GetPrebuildAvailability() +} + +func TestCodespaceMachine_GetStorageInBytes(tt *testing.T) { + tt.Parallel() + var zeroValue int64 + c := &CodespaceMachine{StorageInBytes: &zeroValue} + c.GetStorageInBytes() + c = &CodespaceMachine{} + c.GetStorageInBytes() + c = nil + c.GetStorageInBytes() +} + func TestCodespacesGitStatus_GetAhead(tt *testing.T) { tt.Parallel() var zeroValue int From 1ae9c18f5dfc9c8ac6880ee0988be019aea05251 Mon Sep 17 00:00:00 2001 From: Dhananjay Mishra Date: Sat, 20 Dec 2025 16:43:06 +0000 Subject: [PATCH 3/5] remove a extra space --- github/codespace_machines.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/codespace_machines.go b/github/codespace_machines.go index 9071a127820..c3f253f1c7f 100644 --- a/github/codespace_machines.go +++ b/github/codespace_machines.go @@ -10,7 +10,7 @@ import ( "fmt" ) -// CodespaceMachine represent a description of the machine powering a codespace. +// CodespaceMachine represent a description of the machine powering a codespace. type CodespaceMachine struct { Name *string `json:"name,omitempty"` DisplayName *string `json:"display_name,omitempty"` From 6c1bb0da6cc7af48b945cdd22863bfccfe6eea7f Mon Sep 17 00:00:00 2001 From: Dhananjay Mishra Date: Sun, 21 Dec 2025 03:04:13 +0000 Subject: [PATCH 4/5] rename file and reuse struct --- ...ace_machines.go => codespaces_machines.go} | 31 ++------ ...es_test.go => codespaces_machines_test.go} | 8 +- github/github-accessors.go | 56 -------------- github/github-accessors_test.go | 77 ------------------- 4 files changed, 12 insertions(+), 160 deletions(-) rename github/{codespace_machines.go => codespaces_machines.go} (62%) rename github/{codespace_machines_test.go => codespaces_machines_test.go} (96%) diff --git a/github/codespace_machines.go b/github/codespaces_machines.go similarity index 62% rename from github/codespace_machines.go rename to github/codespaces_machines.go index c3f253f1c7f..7d10c12f5c2 100644 --- a/github/codespace_machines.go +++ b/github/codespaces_machines.go @@ -10,25 +10,10 @@ import ( "fmt" ) -// CodespaceMachine represent a description of the machine powering a codespace. -type CodespaceMachine struct { - Name *string `json:"name,omitempty"` - DisplayName *string `json:"display_name,omitempty"` - OperatingSystem *string `json:"operating_system,omitempty"` - StorageInBytes *int64 `json:"storage_in_bytes,omitempty"` - MemoryInBytes *int64 `json:"memory_in_bytes,omitempty"` - CPUs *int `json:"cpus,omitempty"` - // PrebuildAvailability represents whether a prebuild is currently available when creating a codespace for this machine and repository. - // Value will be "null" if prebuilds are not supported or prebuild availability could not be determined. - // Value will be "none" if no prebuild is available. - // Latest values "ready" and "in_progress" indicate the prebuild availability status. - PrebuildAvailability *string `json:"prebuild_availability,omitempty"` -} - -// CodespaceMachines represent a list of machines. -type CodespaceMachines struct { - TotalCount int64 `json:"total_count"` - Machines []*CodespaceMachine `json:"machines"` +// CodespacesMachines represent a list of machines. +type CodespacesMachines struct { + TotalCount int64 `json:"total_count"` + Machines []*CodespacesMachine `json:"machines"` } // ListMachinesOptions represent options for ListMachineTypesForRepository. @@ -46,7 +31,7 @@ type ListMachinesOptions struct { // GitHub API docs: https://docs.github.com/rest/codespaces/machines#list-available-machine-types-for-a-repository // //meta:operation GET /repos/{owner}/{repo}/codespaces/machines -func (s *CodespacesService) ListMachineTypesForRepository(ctx context.Context, owner, repo string, opts *ListMachinesOptions) (*CodespaceMachines, *Response, error) { +func (s *CodespacesService) ListMachineTypesForRepository(ctx context.Context, owner, repo string, opts *ListMachinesOptions) (*CodespacesMachines, *Response, error) { u := fmt.Sprintf("repos/%v/%v/codespaces/machines", owner, repo) u, err := addOptions(u, opts) if err != nil { @@ -58,7 +43,7 @@ func (s *CodespacesService) ListMachineTypesForRepository(ctx context.Context, o return nil, nil, err } - var machines *CodespaceMachines + var machines *CodespacesMachines resp, err := s.client.Do(ctx, req, &machines) if err != nil { return nil, resp, err @@ -72,14 +57,14 @@ func (s *CodespacesService) ListMachineTypesForRepository(ctx context.Context, o // GitHub API docs: https://docs.github.com/rest/codespaces/machines#list-machine-types-for-a-codespace // //meta:operation GET /user/codespaces/{codespace_name}/machines -func (s *CodespacesService) ListMachineTypesForCodespace(ctx context.Context, codespaceName string) (*CodespaceMachines, *Response, error) { +func (s *CodespacesService) ListMachineTypesForCodespace(ctx context.Context, codespaceName string) (*CodespacesMachines, *Response, error) { u := fmt.Sprintf("user/codespaces/%v/machines", codespaceName) req, err := s.client.NewRequest("GET", u, nil) if err != nil { return nil, nil, err } - var machines *CodespaceMachines + var machines *CodespacesMachines resp, err := s.client.Do(ctx, req, &machines) if err != nil { return nil, resp, err diff --git a/github/codespace_machines_test.go b/github/codespaces_machines_test.go similarity index 96% rename from github/codespace_machines_test.go rename to github/codespaces_machines_test.go index c21c8f344ff..35696faa066 100644 --- a/github/codespace_machines_test.go +++ b/github/codespaces_machines_test.go @@ -53,9 +53,9 @@ func TestCodespacesService_ListMachineTypesForRepository(t *testing.T) { t.Fatalf("Codespaces.ListMachineTypesForRepository returned error: %v", err) } - want := &CodespaceMachines{ + want := &CodespacesMachines{ TotalCount: 1, - Machines: []*CodespaceMachine{ + Machines: []*CodespacesMachine{ { Name: Ptr("standardLinux"), DisplayName: Ptr("4 cores, 8 GB RAM, 64 GB storage"), @@ -115,9 +115,9 @@ func TestCodespacesService_ListMachineTypesForCodespace(t *testing.T) { t.Fatalf("Codespaces.ListMachineTypesForCodespace returned error: %v", err) } - want := &CodespaceMachines{ + want := &CodespacesMachines{ TotalCount: 1, - Machines: []*CodespaceMachine{ + Machines: []*CodespacesMachine{ { Name: Ptr("standardLinux"), DisplayName: Ptr("4 cores, 8 GB RAM, 64 GB storage"), diff --git a/github/github-accessors.go b/github/github-accessors.go index ce5263800ac..33b6cec4f5c 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -3942,62 +3942,6 @@ func (c *CodespaceGetDefaultAttributesOptions) GetRef() string { return *c.Ref } -// GetCPUs returns the CPUs field if it's non-nil, zero value otherwise. -func (c *CodespaceMachine) GetCPUs() int { - if c == nil || c.CPUs == nil { - return 0 - } - return *c.CPUs -} - -// GetDisplayName returns the DisplayName field if it's non-nil, zero value otherwise. -func (c *CodespaceMachine) GetDisplayName() string { - if c == nil || c.DisplayName == nil { - return "" - } - return *c.DisplayName -} - -// GetMemoryInBytes returns the MemoryInBytes field if it's non-nil, zero value otherwise. -func (c *CodespaceMachine) GetMemoryInBytes() int64 { - if c == nil || c.MemoryInBytes == nil { - return 0 - } - return *c.MemoryInBytes -} - -// GetName returns the Name field if it's non-nil, zero value otherwise. -func (c *CodespaceMachine) GetName() string { - if c == nil || c.Name == nil { - return "" - } - return *c.Name -} - -// GetOperatingSystem returns the OperatingSystem field if it's non-nil, zero value otherwise. -func (c *CodespaceMachine) GetOperatingSystem() string { - if c == nil || c.OperatingSystem == nil { - return "" - } - return *c.OperatingSystem -} - -// GetPrebuildAvailability returns the PrebuildAvailability field if it's non-nil, zero value otherwise. -func (c *CodespaceMachine) GetPrebuildAvailability() string { - if c == nil || c.PrebuildAvailability == nil { - return "" - } - return *c.PrebuildAvailability -} - -// GetStorageInBytes returns the StorageInBytes field if it's non-nil, zero value otherwise. -func (c *CodespaceMachine) GetStorageInBytes() int64 { - if c == nil || c.StorageInBytes == nil { - return 0 - } - return *c.StorageInBytes -} - // GetAhead returns the Ahead field if it's non-nil, zero value otherwise. func (c *CodespacesGitStatus) GetAhead() int { if c == nil || c.Ahead == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index a047c19c6d8..1d56fc3bedd 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -5146,83 +5146,6 @@ func TestCodespaceGetDefaultAttributesOptions_GetRef(tt *testing.T) { c.GetRef() } -func TestCodespaceMachine_GetCPUs(tt *testing.T) { - tt.Parallel() - var zeroValue int - c := &CodespaceMachine{CPUs: &zeroValue} - c.GetCPUs() - c = &CodespaceMachine{} - c.GetCPUs() - c = nil - c.GetCPUs() -} - -func TestCodespaceMachine_GetDisplayName(tt *testing.T) { - tt.Parallel() - var zeroValue string - c := &CodespaceMachine{DisplayName: &zeroValue} - c.GetDisplayName() - c = &CodespaceMachine{} - c.GetDisplayName() - c = nil - c.GetDisplayName() -} - -func TestCodespaceMachine_GetMemoryInBytes(tt *testing.T) { - tt.Parallel() - var zeroValue int64 - c := &CodespaceMachine{MemoryInBytes: &zeroValue} - c.GetMemoryInBytes() - c = &CodespaceMachine{} - c.GetMemoryInBytes() - c = nil - c.GetMemoryInBytes() -} - -func TestCodespaceMachine_GetName(tt *testing.T) { - tt.Parallel() - var zeroValue string - c := &CodespaceMachine{Name: &zeroValue} - c.GetName() - c = &CodespaceMachine{} - c.GetName() - c = nil - c.GetName() -} - -func TestCodespaceMachine_GetOperatingSystem(tt *testing.T) { - tt.Parallel() - var zeroValue string - c := &CodespaceMachine{OperatingSystem: &zeroValue} - c.GetOperatingSystem() - c = &CodespaceMachine{} - c.GetOperatingSystem() - c = nil - c.GetOperatingSystem() -} - -func TestCodespaceMachine_GetPrebuildAvailability(tt *testing.T) { - tt.Parallel() - var zeroValue string - c := &CodespaceMachine{PrebuildAvailability: &zeroValue} - c.GetPrebuildAvailability() - c = &CodespaceMachine{} - c.GetPrebuildAvailability() - c = nil - c.GetPrebuildAvailability() -} - -func TestCodespaceMachine_GetStorageInBytes(tt *testing.T) { - tt.Parallel() - var zeroValue int64 - c := &CodespaceMachine{StorageInBytes: &zeroValue} - c.GetStorageInBytes() - c = &CodespaceMachine{} - c.GetStorageInBytes() - c = nil - c.GetStorageInBytes() -} - func TestCodespacesGitStatus_GetAhead(tt *testing.T) { tt.Parallel() var zeroValue int From ece434b96ceeda65daab1ac45b09edc16d951a74 Mon Sep 17 00:00:00 2001 From: Dhananjay Mishra Date: Mon, 22 Dec 2025 12:55:33 +0000 Subject: [PATCH 5/5] feedback --- github/codespaces_machines.go | 18 ++++---- github/codespaces_machines_test.go | 34 ++++++++------- github/github-accessors.go | 48 +++++++++++----------- github/github-accessors_test.go | 66 +++++++++++++++--------------- 4 files changed, 85 insertions(+), 81 deletions(-) diff --git a/github/codespaces_machines.go b/github/codespaces_machines.go index 7d10c12f5c2..5277b1f4098 100644 --- a/github/codespaces_machines.go +++ b/github/codespaces_machines.go @@ -16,22 +16,22 @@ type CodespacesMachines struct { Machines []*CodespacesMachine `json:"machines"` } -// ListMachinesOptions represent options for ListMachineTypesForRepository. -type ListMachinesOptions struct { +// ListRepoMachineTypesOptions represent options for ListMachineTypesForRepository. +type ListRepoMachineTypesOptions struct { // Ref represent the branch or commit to check for prebuild availability and devcontainer restrictions. - Ref *string `json:"ref,omitempty"` + Ref *string `url:"ref,omitempty"` // Location represent the location to check for available machines. Assigned by IP if not provided. - Location *string `json:"location,omitempty"` + Location *string `url:"location,omitempty"` // ClientIP represent the IP for location auto-detection when proxying a request - ClientIP *string `json:"client_ip,omitempty"` + ClientIP *string `url:"client_ip,omitempty"` } -// ListMachineTypesForRepository lists the machine types available for a given repository based on its configuration. +// ListRepositoryMachineTypes lists the machine types available for a given repository based on its configuration. // // GitHub API docs: https://docs.github.com/rest/codespaces/machines#list-available-machine-types-for-a-repository // //meta:operation GET /repos/{owner}/{repo}/codespaces/machines -func (s *CodespacesService) ListMachineTypesForRepository(ctx context.Context, owner, repo string, opts *ListMachinesOptions) (*CodespacesMachines, *Response, error) { +func (s *CodespacesService) ListRepositoryMachineTypes(ctx context.Context, owner, repo string, opts *ListRepoMachineTypesOptions) (*CodespacesMachines, *Response, error) { u := fmt.Sprintf("repos/%v/%v/codespaces/machines", owner, repo) u, err := addOptions(u, opts) if err != nil { @@ -52,12 +52,12 @@ func (s *CodespacesService) ListMachineTypesForRepository(ctx context.Context, o return machines, resp, nil } -// ListMachineTypesForCodespace lists the machine types a codespace can transition to use. +// ListCodespaceMachineTypes lists the machine types a codespace can transition to use. // // GitHub API docs: https://docs.github.com/rest/codespaces/machines#list-machine-types-for-a-codespace // //meta:operation GET /user/codespaces/{codespace_name}/machines -func (s *CodespacesService) ListMachineTypesForCodespace(ctx context.Context, codespaceName string) (*CodespacesMachines, *Response, error) { +func (s *CodespacesService) ListCodespaceMachineTypes(ctx context.Context, codespaceName string) (*CodespacesMachines, *Response, error) { u := fmt.Sprintf("user/codespaces/%v/machines", codespaceName) req, err := s.client.NewRequest("GET", u, nil) if err != nil { diff --git a/github/codespaces_machines_test.go b/github/codespaces_machines_test.go index 35696faa066..d413d168b93 100644 --- a/github/codespaces_machines_test.go +++ b/github/codespaces_machines_test.go @@ -13,13 +13,17 @@ import ( "github.com/google/go-cmp/cmp" ) -func TestCodespacesService_ListMachineTypesForRepository(t *testing.T) { +func TestCodespacesService_ListRepositoryMachineTypes(t *testing.T) { t.Parallel() client, mux, _ := setup(t) mux.HandleFunc("/repos/owner/repo/codespaces/machines", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") - + testFormValues(t, r, values{ + "ref": "main", + "location": "WestUs2", + "client_ip": "1.2.3.4", + }) fmt.Fprint(w, `{ "total_count": 1, "machines": [ @@ -37,20 +41,20 @@ func TestCodespacesService_ListMachineTypesForRepository(t *testing.T) { }) ctx := t.Context() - opts := &ListMachinesOptions{ + opts := &ListRepoMachineTypesOptions{ Ref: Ptr("main"), Location: Ptr("WestUs2"), ClientIP: Ptr("1.2.3.4"), } - got, _, err := client.Codespaces.ListMachineTypesForRepository( + got, _, err := client.Codespaces.ListRepositoryMachineTypes( ctx, "owner", "repo", opts, ) if err != nil { - t.Fatalf("Codespaces.ListMachineTypesForRepository returned error: %v", err) + t.Fatalf("Codespaces.ListRepositoryMachineTypes returned error: %v", err) } want := &CodespacesMachines{ @@ -69,16 +73,16 @@ func TestCodespacesService_ListMachineTypesForRepository(t *testing.T) { } if !cmp.Equal(got, want) { - t.Errorf("Codespaces.ListMachineTypesForRepository returned %+v, want %+v", got, want) + t.Errorf("Codespaces.ListRepositoryMachineTypes returned %+v, want %+v", got, want) } - const methodName = "ListMachineTypesForRepository" + const methodName = "ListRepositoryMachineTypes" testBadOptions(t, methodName, func() error { - _, _, err := client.Codespaces.ListMachineTypesForRepository(ctx, "\n", "/n", opts) + _, _, err := client.Codespaces.ListRepositoryMachineTypes(ctx, "\n", "/n", opts) return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Codespaces.ListMachineTypesForRepository(ctx, "/n", "/n", opts) + got, resp, err := client.Codespaces.ListRepositoryMachineTypes(ctx, "/n", "/n", opts) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -86,7 +90,7 @@ func TestCodespacesService_ListMachineTypesForRepository(t *testing.T) { }) } -func TestCodespacesService_ListMachineTypesForCodespace(t *testing.T) { +func TestCodespacesService_ListCodespaceMachineTypes(t *testing.T) { t.Parallel() client, mux, _ := setup(t) @@ -110,9 +114,9 @@ func TestCodespacesService_ListMachineTypesForCodespace(t *testing.T) { }) ctx := t.Context() - got, _, err := client.Codespaces.ListMachineTypesForCodespace(ctx, "codespace_1") + got, _, err := client.Codespaces.ListCodespaceMachineTypes(ctx, "codespace_1") if err != nil { - t.Fatalf("Codespaces.ListMachineTypesForCodespace returned error: %v", err) + t.Fatalf("Codespaces.ListCodespaceMachineTypes returned error: %v", err) } want := &CodespacesMachines{ @@ -131,12 +135,12 @@ func TestCodespacesService_ListMachineTypesForCodespace(t *testing.T) { } if !cmp.Equal(got, want) { - t.Errorf("Codespaces.ListMachineTypesForCodespace returned %+v, want %+v", got, want) + t.Errorf("Codespaces.ListCodespaceMachineTypes returned %+v, want %+v", got, want) } - const methodName = "ListMachineTypesForCodespace" + const methodName = "ListCodespaceMachineTypes" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Codespaces.ListMachineTypesForCodespace(ctx, "/n") + got, resp, err := client.Codespaces.ListCodespaceMachineTypes(ctx, "/n") if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } diff --git a/github/github-accessors.go b/github/github-accessors.go index 33b6cec4f5c..8b24b058ed5 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -14550,30 +14550,6 @@ func (l *ListGlobalSecurityAdvisoriesOptions) GetUpdated() string { return *l.Updated } -// GetClientIP returns the ClientIP field if it's non-nil, zero value otherwise. -func (l *ListMachinesOptions) GetClientIP() string { - if l == nil || l.ClientIP == nil { - return "" - } - return *l.ClientIP -} - -// GetLocation returns the Location field if it's non-nil, zero value otherwise. -func (l *ListMachinesOptions) GetLocation() string { - if l == nil || l.Location == nil { - return "" - } - return *l.Location -} - -// GetRef returns the Ref field if it's non-nil, zero value otherwise. -func (l *ListMachinesOptions) GetRef() string { - if l == nil || l.Ref == nil { - return "" - } - return *l.Ref -} - // GetTotalCount returns the TotalCount field if it's non-nil, zero value otherwise. func (l *ListOrganizations) GetTotalCount() int { if l == nil || l.TotalCount == nil { @@ -14702,6 +14678,30 @@ func (l *ListProvisionedSCIMUsersEnterpriseOptions) GetStartIndex() int { return *l.StartIndex } +// GetClientIP returns the ClientIP field if it's non-nil, zero value otherwise. +func (l *ListRepoMachineTypesOptions) GetClientIP() string { + if l == nil || l.ClientIP == nil { + return "" + } + return *l.ClientIP +} + +// GetLocation returns the Location field if it's non-nil, zero value otherwise. +func (l *ListRepoMachineTypesOptions) GetLocation() string { + if l == nil || l.Location == nil { + return "" + } + return *l.Location +} + +// GetRef returns the Ref field if it's non-nil, zero value otherwise. +func (l *ListRepoMachineTypesOptions) GetRef() string { + if l == nil || l.Ref == nil { + return "" + } + return *l.Ref +} + // GetTotalCount returns the TotalCount field if it's non-nil, zero value otherwise. func (l *ListRepositories) GetTotalCount() int { if l == nil || l.TotalCount == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 1d56fc3bedd..dbd85d0dc7d 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -18919,39 +18919,6 @@ func TestListGlobalSecurityAdvisoriesOptions_GetUpdated(tt *testing.T) { l.GetUpdated() } -func TestListMachinesOptions_GetClientIP(tt *testing.T) { - tt.Parallel() - var zeroValue string - l := &ListMachinesOptions{ClientIP: &zeroValue} - l.GetClientIP() - l = &ListMachinesOptions{} - l.GetClientIP() - l = nil - l.GetClientIP() -} - -func TestListMachinesOptions_GetLocation(tt *testing.T) { - tt.Parallel() - var zeroValue string - l := &ListMachinesOptions{Location: &zeroValue} - l.GetLocation() - l = &ListMachinesOptions{} - l.GetLocation() - l = nil - l.GetLocation() -} - -func TestListMachinesOptions_GetRef(tt *testing.T) { - tt.Parallel() - var zeroValue string - l := &ListMachinesOptions{Ref: &zeroValue} - l.GetRef() - l = &ListMachinesOptions{} - l.GetRef() - l = nil - l.GetRef() -} - func TestListOrganizations_GetTotalCount(tt *testing.T) { tt.Parallel() var zeroValue int @@ -19128,6 +19095,39 @@ func TestListProvisionedSCIMUsersEnterpriseOptions_GetStartIndex(tt *testing.T) l.GetStartIndex() } +func TestListRepoMachineTypesOptions_GetClientIP(tt *testing.T) { + tt.Parallel() + var zeroValue string + l := &ListRepoMachineTypesOptions{ClientIP: &zeroValue} + l.GetClientIP() + l = &ListRepoMachineTypesOptions{} + l.GetClientIP() + l = nil + l.GetClientIP() +} + +func TestListRepoMachineTypesOptions_GetLocation(tt *testing.T) { + tt.Parallel() + var zeroValue string + l := &ListRepoMachineTypesOptions{Location: &zeroValue} + l.GetLocation() + l = &ListRepoMachineTypesOptions{} + l.GetLocation() + l = nil + l.GetLocation() +} + +func TestListRepoMachineTypesOptions_GetRef(tt *testing.T) { + tt.Parallel() + var zeroValue string + l := &ListRepoMachineTypesOptions{Ref: &zeroValue} + l.GetRef() + l = &ListRepoMachineTypesOptions{} + l.GetRef() + l = nil + l.GetRef() +} + func TestListRepositories_GetTotalCount(tt *testing.T) { tt.Parallel() var zeroValue int