Skip to content

Commit 5d7e416

Browse files
refactor!: Pass the Codespaces body types by value and rename them to ...Request (#4540)
BREAKING CHANGE: `CreateCodespaceOptions`, `CodespaceCreateForUserOptions`, `UpdateCodespaceOptions` and `PublishCodespaceOptions` are renamed to `CreateCodespaceRequest`, `CreateCodespaceForUserRequest`, `UpdateCodespaceRequest` and `PublishCodespaceRequest`; `CodespacesService.CreateInRepo`, `CreateFromPullRequest`, `Create`, `Update` and `Publish` now take them by value instead of by pointer; `CreateCodespaceForUserRequest.RepositoryID` is now `*int64`.
1 parent d39a9b4 commit 5d7e416

5 files changed

Lines changed: 376 additions & 341 deletions

File tree

‎.golangci.yml‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,8 @@ linters:
205205
- AddProjectItemOptions
206206
- AuthorizationRequest
207207
- CodeScanningAlertState
208-
- CodespaceCreateForUserOptions
209208
- ConfigApplyOptions
210209
- ConfigSettings
211-
- CreateCodespaceOptions
212210
- CreateOrUpdateIssueTypesOptions
213211
- CreateOrgInvitationOptions
214212
- CreateUpdateEnvironment
@@ -229,7 +227,6 @@ linters:
229227
- PagesUpdate
230228
- PagesUpdateWithoutCNAME
231229
- ProtectionRequest
232-
- PublishCodespaceOptions
233230
- PullRequestBranchUpdateOptions
234231
- PullRequestReviewRequest
235232
- PullRequestReviewsEnforcementUpdate
@@ -246,7 +243,6 @@ linters:
246243
- TeamAddTeamMembershipOptions
247244
- TeamAddTeamRepoOptions
248245
- TeamProjectOptions
249-
- UpdateCodespaceOptions
250246
- UpdateDefaultSetupConfigurationOptions
251247
- UpdateProjectItemOptions
252248
- UserSuspendOptions
@@ -256,18 +252,15 @@ linters:
256252
body-allowed-wrong-names:
257253
- AddProjectItemOptions
258254
- CheckSuitePreferenceOptions
259-
- CodespaceCreateForUserOptions
260255
- ConfigApplyOptions
261256
- CreateCheckRunOptions
262257
- CreateCheckSuiteOptions
263-
- CreateCodespaceOptions
264258
- CreateOrUpdateIssueTypesOptions
265259
- CreateOrgInvitationOptions
266260
- InstallationTokenListRepoOptions
267261
- InstallationTokenOptions
268262
- LockIssueOptions
269263
- MaintenanceOptions
270-
- PublishCodespaceOptions
271264
- PullRequestBranchUpdateOptions
272265
- RepositoryAddCollaboratorOptions
273266
- RepositoryContentFileOptions
@@ -278,7 +271,6 @@ linters:
278271
- TeamAddTeamRepoOptions
279272
- TeamProjectOptions
280273
- UpdateCheckRunOptions
281-
- UpdateCodespaceOptions
282274
- UpdateDefaultSetupConfigurationOptions
283275
- UpdateProjectItemOptions
284276
- UserSuspendOptions

‎github/codespaces.go‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ func (s *CodespacesService) List(ctx context.Context, opts *ListCodespacesOption
150150
return codespaces, resp, nil
151151
}
152152

153-
// CreateCodespaceOptions represents options for the creation of a codespace in a repository.
154-
type CreateCodespaceOptions struct {
153+
// CreateCodespaceRequest represents a request to create a codespace in a repository.
154+
type CreateCodespaceRequest struct {
155155
Ref *string `json:"ref,omitempty"`
156156
// Geo represents the geographic area for this codespace.
157157
// If not specified, the value is assigned by IP.
@@ -212,11 +212,11 @@ type CodespacePullRequestOptions struct {
212212
RepositoryID int64 `json:"repository_id"`
213213
}
214214

215-
// CodespaceCreateForUserOptions represents options for creating a codespace for the authenticated user.
216-
type CodespaceCreateForUserOptions struct {
217-
PullRequest *CodespacePullRequestOptions `json:"pull_request"`
215+
// CreateCodespaceForUserRequest represents a request to create a codespace for the authenticated user.
216+
type CreateCodespaceForUserRequest struct {
217+
PullRequest *CodespacePullRequestOptions `json:"pull_request,omitempty"`
218218
// RepositoryID represents the repository ID for this codespace.
219-
RepositoryID int64 `json:"repository_id"`
219+
RepositoryID *int64 `json:"repository_id,omitempty"`
220220
Ref *string `json:"ref,omitempty"`
221221
Geo *string `json:"geo,omitempty"`
222222
ClientIP *string `json:"client_ip,omitempty"`
@@ -230,10 +230,12 @@ type CodespaceCreateForUserOptions struct {
230230
DisplayName *string `json:"display_name,omitempty"`
231231
}
232232

233-
// UpdateCodespaceOptions represents options for updating a codespace.
234-
type UpdateCodespaceOptions struct {
233+
// UpdateCodespaceRequest represents a request to update a codespace.
234+
type UpdateCodespaceRequest struct {
235235
// Machine represents a valid machine to transition this codespace to.
236236
Machine *string `json:"machine,omitempty"`
237+
// DisplayName represents the display name for this codespace.
238+
DisplayName *string `json:"display_name,omitempty"`
237239
// RecentFolders represents the recently opened folders inside the codespace.
238240
// It is currently used by the clients to determine the folder path to load the codespace in.
239241
RecentFolders []string `json:"recent_folders,omitempty"`
@@ -251,8 +253,8 @@ type CodespaceExport struct {
251253
HTMLURL *string `json:"html_url,omitempty"`
252254
}
253255

254-
// PublishCodespaceOptions represents options for creating a repository from an unpublished codespace.
255-
type PublishCodespaceOptions struct {
256+
// PublishCodespaceRequest represents a request to create a repository from an unpublished codespace.
257+
type PublishCodespaceRequest struct {
256258
// Name represents the name of the new repository.
257259
Name *string `json:"name,omitempty"`
258260
// Private represents whether the new repository is private. Defaults to false.
@@ -273,7 +275,7 @@ type CodespacePermissions struct {
273275
// GitHub API docs: https://docs.github.com/rest/codespaces/codespaces?apiVersion=2022-11-28#create-a-codespace-in-a-repository
274276
//
275277
//meta:operation POST /repos/{owner}/{repo}/codespaces
276-
func (s *CodespacesService) CreateInRepo(ctx context.Context, owner, repo string, body *CreateCodespaceOptions) (*Codespace, *Response, error) {
278+
func (s *CodespacesService) CreateInRepo(ctx context.Context, owner, repo string, body CreateCodespaceRequest) (*Codespace, *Response, error) {
277279
u := fmt.Sprintf("repos/%v/%v/codespaces", owner, repo)
278280
req, err := s.client.NewRequest(ctx, "POST", u, body)
279281
if err != nil {
@@ -444,7 +446,7 @@ func (s *CodespacesService) CheckPermissions(ctx context.Context, owner, repo, r
444446
// GitHub API docs: https://docs.github.com/rest/codespaces/codespaces?apiVersion=2022-11-28#create-a-codespace-from-a-pull-request
445447
//
446448
//meta:operation POST /repos/{owner}/{repo}/pulls/{pull_number}/codespaces
447-
func (s *CodespacesService) CreateFromPullRequest(ctx context.Context, owner, repo string, pullNumber int, body *CreateCodespaceOptions) (*Codespace, *Response, error) {
449+
func (s *CodespacesService) CreateFromPullRequest(ctx context.Context, owner, repo string, pullNumber int, body CreateCodespaceRequest) (*Codespace, *Response, error) {
448450
u := fmt.Sprintf("repos/%v/%v/pulls/%v/codespaces", owner, repo, pullNumber)
449451
req, err := s.client.NewRequest(ctx, "POST", u, body)
450452
if err != nil {
@@ -462,12 +464,12 @@ func (s *CodespacesService) CreateFromPullRequest(ctx context.Context, owner, re
462464

463465
// Create creates a new codespace, owned by the authenticated user.
464466
//
465-
// This method requires either RepositoryId OR a PullRequest but not both.
467+
// This method requires either RepositoryID or PullRequest to be set, but not both.
466468
//
467469
// GitHub API docs: https://docs.github.com/rest/codespaces/codespaces?apiVersion=2022-11-28#create-a-codespace-for-the-authenticated-user
468470
//
469471
//meta:operation POST /user/codespaces
470-
func (s *CodespacesService) Create(ctx context.Context, body *CodespaceCreateForUserOptions) (*Codespace, *Response, error) {
472+
func (s *CodespacesService) Create(ctx context.Context, body CreateCodespaceForUserRequest) (*Codespace, *Response, error) {
471473
u := "user/codespaces"
472474
req, err := s.client.NewRequest(ctx, "POST", u, body)
473475
if err != nil {
@@ -511,7 +513,7 @@ func (s *CodespacesService) Get(ctx context.Context, codespaceName string) (*Cod
511513
// GitHub API docs: https://docs.github.com/rest/codespaces/codespaces?apiVersion=2022-11-28#update-a-codespace-for-the-authenticated-user
512514
//
513515
//meta:operation PATCH /user/codespaces/{codespace_name}
514-
func (s *CodespacesService) Update(ctx context.Context, codespaceName string, body *UpdateCodespaceOptions) (*Codespace, *Response, error) {
516+
func (s *CodespacesService) Update(ctx context.Context, codespaceName string, body UpdateCodespaceRequest) (*Codespace, *Response, error) {
515517
u := fmt.Sprintf("user/codespaces/%v", codespaceName)
516518
req, err := s.client.NewRequest(ctx, "PATCH", u, body)
517519
if err != nil {
@@ -574,7 +576,7 @@ func (s *CodespacesService) GetLatestCodespaceExport(ctx context.Context, codesp
574576
// GitHub API docs: https://docs.github.com/rest/codespaces/codespaces?apiVersion=2022-11-28#create-a-repository-from-an-unpublished-codespace
575577
//
576578
//meta:operation POST /user/codespaces/{codespace_name}/publish
577-
func (s *CodespacesService) Publish(ctx context.Context, codespaceName string, body *PublishCodespaceOptions) (*Codespace, *Response, error) {
579+
func (s *CodespacesService) Publish(ctx context.Context, codespaceName string, body PublishCodespaceRequest) (*Codespace, *Response, error) {
578580
u := fmt.Sprintf("user/codespaces/%v/publish", codespaceName)
579581
req, err := s.client.NewRequest(ctx, "POST", u, body)
580582
if err != nil {

‎github/codespaces_test.go‎

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func TestCodespacesService_CreateInRepo(t *testing.T) {
145145
t.Parallel()
146146
client, mux, _ := setup(t)
147147

148-
input := &CreateCodespaceOptions{
148+
input := CreateCodespaceRequest{
149149
Ref: new("main"),
150150
Geo: new("WestUs2"),
151151
Machine: new("standardLinux"),
@@ -451,7 +451,7 @@ func TestCodespacesService_CreateFromPullRequest(t *testing.T) {
451451
t.Parallel()
452452
client, mux, _ := setup(t)
453453

454-
input := &CreateCodespaceOptions{
454+
input := CreateCodespaceRequest{
455455
Machine: new("standardLinux"),
456456
IdleTimeoutMinutes: new(60),
457457
}
@@ -497,25 +497,25 @@ func TestCodespacesService_Create(t *testing.T) {
497497
t.Parallel()
498498
client, mux, _ := setup(t)
499499

500-
opt := &CodespaceCreateForUserOptions{
500+
input := CreateCodespaceForUserRequest{
501501
Ref: new("main"),
502502
Geo: new("WestUs2"),
503503
Machine: new("standardLinux"),
504504
IdleTimeoutMinutes: new(60),
505-
RepositoryID: int64(111),
505+
RepositoryID: new(int64(111)),
506506
PullRequest: nil,
507507
}
508508

509509
mux.HandleFunc("/user/codespaces", func(w http.ResponseWriter, r *http.Request) {
510510
testMethod(t, r, "POST")
511-
testJSONBody(t, r, opt)
511+
testJSONBody(t, r, input)
512512
fmt.Fprint(w, `{"id":1,"repository":{"id":111}}`)
513513
})
514514

515515
ctx := t.Context()
516516
codespace, _, err := client.Codespaces.Create(
517517
ctx,
518-
opt,
518+
input,
519519
)
520520
if err != nil {
521521
t.Fatalf("Codespaces.Create returned error: %v", err)
@@ -536,7 +536,7 @@ func TestCodespacesService_Create(t *testing.T) {
536536
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
537537
got, resp, err := client.Codespaces.Create(
538538
ctx,
539-
opt,
539+
input,
540540
)
541541
if got != nil {
542542
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
@@ -545,6 +545,24 @@ func TestCodespacesService_Create(t *testing.T) {
545545
})
546546
}
547547

548+
func TestCreateCodespaceForUserRequest_Marshal(t *testing.T) {
549+
t.Parallel()
550+
551+
repoInput := CreateCodespaceForUserRequest{
552+
RepositoryID: new(int64(111)),
553+
Ref: new("main"),
554+
}
555+
testJSONMarshal(t, repoInput, `{"repository_id":111,"ref":"main"}`)
556+
557+
pullRequestInput := CreateCodespaceForUserRequest{
558+
PullRequest: &CodespacePullRequestOptions{
559+
PullRequestNumber: 42,
560+
RepositoryID: 111,
561+
},
562+
}
563+
testJSONMarshal(t, pullRequestInput, `{"pull_request":{"pull_request_number":42,"repository_id":111}}`)
564+
}
565+
548566
func TestCodespacesService_Get(t *testing.T) {
549567
t.Parallel()
550568
client, mux, _ := setup(t)
@@ -585,8 +603,9 @@ func TestCodespacesService_Update(t *testing.T) {
585603
t.Parallel()
586604
client, mux, _ := setup(t)
587605

588-
opt := &UpdateCodespaceOptions{
589-
Machine: new("standardLinux"),
606+
input := UpdateCodespaceRequest{
607+
Machine: new("standardLinux"),
608+
DisplayName: new("my codespace"),
590609
RecentFolders: []string{
591610
"folder1",
592611
"folder2",
@@ -595,15 +614,15 @@ func TestCodespacesService_Update(t *testing.T) {
595614

596615
mux.HandleFunc("/user/codespaces/codespace_1", func(w http.ResponseWriter, r *http.Request) {
597616
testMethod(t, r, "PATCH")
598-
testJSONBody(t, r, opt)
617+
testJSONBody(t, r, input)
599618
fmt.Fprint(w, `{"id":1,"repository":{"id":111}}`)
600619
})
601620

602621
ctx := t.Context()
603622
codespace, _, err := client.Codespaces.Update(
604623
ctx,
605624
"codespace_1",
606-
opt,
625+
input,
607626
)
608627
if err != nil {
609628
t.Fatalf("Codespaces.Update returned error: %v", err)
@@ -625,7 +644,7 @@ func TestCodespacesService_Update(t *testing.T) {
625644
got, resp, err := client.Codespaces.Update(
626645
ctx,
627646
"codespace_1",
628-
opt,
647+
input,
629648
)
630649
if got != nil {
631650
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
@@ -720,22 +739,22 @@ func TestCodespacesService_Publish(t *testing.T) {
720739
t.Parallel()
721740
client, mux, _ := setup(t)
722741

723-
opt := &PublishCodespaceOptions{
742+
input := PublishCodespaceRequest{
724743
Name: new("repo"),
725744
Private: new(true),
726745
}
727746

728747
mux.HandleFunc("/user/codespaces/codespace_1/publish", func(w http.ResponseWriter, r *http.Request) {
729748
testMethod(t, r, "POST")
730-
testJSONBody(t, r, opt)
749+
testJSONBody(t, r, input)
731750
fmt.Fprint(w, `{"id":1,"repository":{"id":111}}`)
732751
})
733752

734753
ctx := t.Context()
735754
repo, _, err := client.Codespaces.Publish(
736755
ctx,
737756
"codespace_1",
738-
opt,
757+
input,
739758
)
740759
if err != nil {
741760
t.Fatalf("Codespaces.Publish returned error: %v", err)
@@ -756,7 +775,7 @@ func TestCodespacesService_Publish(t *testing.T) {
756775
got, resp, err := client.Codespaces.Publish(
757776
ctx,
758777
"codespace_1",
759-
opt,
778+
input,
760779
)
761780
if got != nil {
762781
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)

0 commit comments

Comments
 (0)