diff --git a/github/gists.go b/github/gists.go index ee4314b986e..900e63fdd57 100644 --- a/github/gists.go +++ b/github/gists.go @@ -33,6 +33,19 @@ type Gist struct { NodeID *string `json:"node_id,omitempty"` } +// CreateGistRequest represents the input for creating a gist. +type CreateGistRequest struct { + Description *string `json:"description,omitempty"` + Public *bool `json:"public,omitempty"` + Files map[GistFilename]GistFile `json:"files,omitempty"` +} + +// UpdateGistRequest represents the input for updating a gist. +type UpdateGistRequest struct { + Description *string `json:"description,omitempty"` + Files map[GistFilename]GistFile `json:"files,omitempty"` +} + func (g Gist) String() string { return Stringify(g) } @@ -224,7 +237,7 @@ func (s *GistsService) GetRevision(ctx context.Context, id, sha string) (*Gist, // GitHub API docs: https://docs.github.com/rest/gists/gists#create-a-gist // //meta:operation POST /gists -func (s *GistsService) Create(ctx context.Context, gist *Gist) (*Gist, *Response, error) { +func (s *GistsService) Create(ctx context.Context, gist CreateGistRequest) (*Gist, *Response, error) { u := "gists" req, err := s.client.NewRequest("POST", u, gist) if err != nil { @@ -240,12 +253,33 @@ func (s *GistsService) Create(ctx context.Context, gist *Gist) (*Gist, *Response return g, resp, nil } +// CreateFromGist creates a gist for the authenticated user using a Gist struct. +// +// Deprecated: Use Create with CreateGistRequest instead. +// +// GitHub API docs: https://docs.github.com/rest/gists/gists#create-a-gist +// +//meta:operation POST /gists +func (s *GistsService) CreateFromGist(ctx context.Context, gist *Gist) (*Gist, *Response, error) { + var req CreateGistRequest + + if gist != nil { + req = CreateGistRequest{ + Description: gist.Description, + Public: gist.Public, + Files: gist.Files, + } + } + + return s.Create(ctx, req) +} + // Edit a gist. // // GitHub API docs: https://docs.github.com/rest/gists/gists#update-a-gist // //meta:operation PATCH /gists/{gist_id} -func (s *GistsService) Edit(ctx context.Context, id string, gist *Gist) (*Gist, *Response, error) { +func (s *GistsService) Edit(ctx context.Context, id string, gist UpdateGistRequest) (*Gist, *Response, error) { u := fmt.Sprintf("gists/%v", id) req, err := s.client.NewRequest("PATCH", u, gist) if err != nil { @@ -261,6 +295,26 @@ func (s *GistsService) Edit(ctx context.Context, id string, gist *Gist) (*Gist, return g, resp, nil } +// EditFromGist updates a gist using a Gist struct. +// +// Deprecated: Use Edit with UpdateGistRequest instead. +// +// GitHub API docs: https://docs.github.com/rest/gists/gists#update-a-gist +// +//meta:operation PATCH /gists/{gist_id} +func (s *GistsService) EditFromGist(ctx context.Context, id string, gist *Gist) (*Gist, *Response, error) { + var req UpdateGistRequest + + if gist != nil { + req = UpdateGistRequest{ + Description: gist.Description, + Files: gist.Files, + } + } + + return s.Edit(ctx, id, req) +} + // ListCommits lists commits of a gist. // // GitHub API docs: https://docs.github.com/rest/gists/gists#list-gist-commits diff --git a/github/gists_comments.go b/github/gists_comments.go index 5e0614231b8..cae09089f96 100644 --- a/github/gists_comments.go +++ b/github/gists_comments.go @@ -19,6 +19,16 @@ type GistComment struct { CreatedAt *Timestamp `json:"created_at,omitempty"` } +// CreateGistCommentRequest represents the input for creating a gist comment. +type CreateGistCommentRequest struct { + Body *string `json:"body,omitempty"` +} + +// UpdateGistCommentRequest represents the input for updating a gist comment. +type UpdateGistCommentRequest struct { + Body *string `json:"body,omitempty"` +} + func (g GistComment) String() string { return Stringify(g) } @@ -75,7 +85,7 @@ func (s *GistsService) GetComment(ctx context.Context, gistID string, commentID // GitHub API docs: https://docs.github.com/rest/gists/comments#create-a-gist-comment // //meta:operation POST /gists/{gist_id}/comments -func (s *GistsService) CreateComment(ctx context.Context, gistID string, comment *GistComment) (*GistComment, *Response, error) { +func (s *GistsService) CreateComment(ctx context.Context, gistID string, comment CreateGistCommentRequest) (*GistComment, *Response, error) { u := fmt.Sprintf("gists/%v/comments", gistID) req, err := s.client.NewRequest("POST", u, comment) if err != nil { @@ -91,12 +101,31 @@ func (s *GistsService) CreateComment(ctx context.Context, gistID string, comment return c, resp, nil } +// CreateCommentFromGistComment creates a comment for a gist using a GistComment struct. +// +// Deprecated: Use CreateComment with CreateGistCommentRequest instead. +// +// GitHub API docs: https://docs.github.com/rest/gists/comments#create-a-gist-comment +// +//meta:operation POST /gists/{gist_id}/comments +func (s *GistsService) CreateCommentFromGistComment(ctx context.Context, gistID string, comment *GistComment) (*GistComment, *Response, error) { + var req CreateGistCommentRequest + + if comment != nil { + req = CreateGistCommentRequest{ + Body: comment.Body, + } + } + + return s.CreateComment(ctx, gistID, req) +} + // EditComment edits an existing gist comment. // // GitHub API docs: https://docs.github.com/rest/gists/comments#update-a-gist-comment // //meta:operation PATCH /gists/{gist_id}/comments/{comment_id} -func (s *GistsService) EditComment(ctx context.Context, gistID string, commentID int64, comment *GistComment) (*GistComment, *Response, error) { +func (s *GistsService) EditComment(ctx context.Context, gistID string, commentID int64, comment UpdateGistCommentRequest) (*GistComment, *Response, error) { u := fmt.Sprintf("gists/%v/comments/%v", gistID, commentID) req, err := s.client.NewRequest("PATCH", u, comment) if err != nil { @@ -112,6 +141,25 @@ func (s *GistsService) EditComment(ctx context.Context, gistID string, commentID return c, resp, nil } +// EditCommentFromGistComment edits an existing gist comment using a GistComment struct. +// +// Deprecated: Use EditComment with UpdateGistCommentRequest instead. +// +// GitHub API docs: https://docs.github.com/rest/gists/comments#update-a-gist-comment +// +//meta:operation PATCH /gists/{gist_id}/comments/{comment_id} +func (s *GistsService) EditCommentFromGistComment(ctx context.Context, gistID string, commentID int64, comment *GistComment) (*GistComment, *Response, error) { + var req UpdateGistCommentRequest + + if comment != nil { + req = UpdateGistCommentRequest{ + Body: comment.Body, + } + } + + return s.EditComment(ctx, gistID, commentID, req) +} + // DeleteComment deletes a gist comment. // // GitHub API docs: https://docs.github.com/rest/gists/comments#delete-a-gist-comment diff --git a/github/gists_comments_test.go b/github/gists_comments_test.go index 61c6d8f172d..c8cd81fa942 100644 --- a/github/gists_comments_test.go +++ b/github/gists_comments_test.go @@ -170,36 +170,37 @@ func TestGistsService_CreateComment(t *testing.T) { input := &GistComment{ID: Ptr(int64(1)), Body: Ptr("b")} mux.HandleFunc("/gists/1/comments", func(w http.ResponseWriter, r *http.Request) { - v := new(GistComment) + v := new(CreateGistCommentRequest) assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) + want := &CreateGistCommentRequest{Body: Ptr("b")} + if !cmp.Equal(v, want) { + t.Errorf("Request body = %+v, want %+v", v, want) } fmt.Fprint(w, `{"id":1}`) }) ctx := context.Background() - comment, _, err := client.Gists.CreateComment(ctx, "1", input) + comment, _, err := client.Gists.CreateCommentFromGistComment(ctx, "1", input) if err != nil { - t.Errorf("Gists.CreateComment returned error: %v", err) + t.Errorf("Gists.CreateCommentFromGistComment returned error: %v", err) } want := &GistComment{ID: Ptr(int64(1))} if !cmp.Equal(comment, want) { - t.Errorf("Gists.CreateComment returned %+v, want %+v", comment, want) + t.Errorf("Gists.CreateCommentFromGistComment returned %+v, want %+v", comment, want) } - const methodName = "CreateComment" + const methodName = "CreateCommentFromGistComment" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Gists.CreateComment(ctx, "\n", input) + _, _, err = client.Gists.CreateCommentFromGistComment(ctx, "\n", input) return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Gists.CreateComment(ctx, "1", input) + got, resp, err := client.Gists.CreateCommentFromGistComment(ctx, "1", input) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -212,10 +213,42 @@ func TestGistsService_CreateComment_invalidID(t *testing.T) { client, _, _ := setup(t) ctx := context.Background() - _, _, err := client.Gists.CreateComment(ctx, "%", nil) + _, _, err := client.Gists.CreateCommentFromGistComment(ctx, "%", nil) testURLParseError(t, err) } +func TestGistsService_CreateComment_ValueParameter(t *testing.T) { + t.Parallel() + client, mux, _ := setup(t) + + input := CreateGistCommentRequest{ + Body: Ptr("Test comment body"), + } + + mux.HandleFunc("/gists/1/comments", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "POST") + v := new(CreateGistCommentRequest) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) + + if !cmp.Equal(v, &input) { + t.Errorf("Request body = %+v, want %+v", v, &input) + } + + fmt.Fprint(w, `{"id": 1}`) + }) + + ctx := context.Background() + comment, _, err := client.Gists.CreateComment(ctx, "1", input) + if err != nil { + t.Errorf("Gists.CreateComment returned error: %v", err) + } + + want := &GistComment{ID: Ptr(int64(1))} + if !cmp.Equal(comment, want) { + t.Errorf("Gists.CreateComment returned %+v, want %+v", comment, want) + } +} + func TestGistsService_EditComment(t *testing.T) { t.Parallel() client, mux, _ := setup(t) @@ -223,36 +256,37 @@ func TestGistsService_EditComment(t *testing.T) { input := &GistComment{ID: Ptr(int64(1)), Body: Ptr("b")} mux.HandleFunc("/gists/1/comments/2", func(w http.ResponseWriter, r *http.Request) { - v := new(GistComment) + v := new(UpdateGistCommentRequest) assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) + want := &UpdateGistCommentRequest{Body: Ptr("b")} + if !cmp.Equal(v, want) { + t.Errorf("Request body = %+v, want %+v", v, want) } fmt.Fprint(w, `{"id":1}`) }) ctx := context.Background() - comment, _, err := client.Gists.EditComment(ctx, "1", 2, input) + comment, _, err := client.Gists.EditCommentFromGistComment(ctx, "1", 2, input) if err != nil { - t.Errorf("Gists.EditComment returned error: %v", err) + t.Errorf("Gists.EditCommentFromGistComment returned error: %v", err) } want := &GistComment{ID: Ptr(int64(1))} if !cmp.Equal(comment, want) { - t.Errorf("Gists.EditComment returned %+v, want %+v", comment, want) + t.Errorf("Gists.EditCommentFromGistComment returned %+v, want %+v", comment, want) } - const methodName = "EditComment" + const methodName = "EditCommentFromGistComment" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Gists.EditComment(ctx, "\n", -2, input) + _, _, err = client.Gists.EditCommentFromGistComment(ctx, "\n", -2, input) return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Gists.EditComment(ctx, "1", 2, input) + got, resp, err := client.Gists.EditCommentFromGistComment(ctx, "1", 2, input) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -265,10 +299,42 @@ func TestGistsService_EditComment_invalidID(t *testing.T) { client, _, _ := setup(t) ctx := context.Background() - _, _, err := client.Gists.EditComment(ctx, "%", 1, nil) + _, _, err := client.Gists.EditCommentFromGistComment(ctx, "%", 1, nil) testURLParseError(t, err) } +func TestGistsService_EditComment_ValueParameter(t *testing.T) { + t.Parallel() + client, mux, _ := setup(t) + + input := UpdateGistCommentRequest{ + Body: Ptr("Updated comment body"), + } + + mux.HandleFunc("/gists/1/comments/2", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PATCH") + v := new(UpdateGistCommentRequest) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) + + if !cmp.Equal(v, &input) { + t.Errorf("Request body = %+v, want %+v", v, &input) + } + + fmt.Fprint(w, `{"id": 1}`) + }) + + ctx := context.Background() + comment, _, err := client.Gists.EditComment(ctx, "1", 2, input) + if err != nil { + t.Errorf("Gists.EditComment returned error: %v", err) + } + + want := &GistComment{ID: Ptr(int64(1))} + if !cmp.Equal(comment, want) { + t.Errorf("Gists.EditComment returned %+v, want %+v", comment, want) + } +} + func TestGistsService_DeleteComment(t *testing.T) { t.Parallel() client, mux, _ := setup(t) diff --git a/github/gists_test.go b/github/gists_test.go index b6d2313e18a..7aa82c3a861 100644 --- a/github/gists_test.go +++ b/github/gists_test.go @@ -509,7 +509,7 @@ func TestGistsService_Create(t *testing.T) { }) ctx := context.Background() - gist, _, err := client.Gists.Create(ctx, input) + gist, _, err := client.Gists.CreateFromGist(ctx, input) if err != nil { t.Errorf("Gists.Create returned error: %v", err) } @@ -528,7 +528,7 @@ func TestGistsService_Create(t *testing.T) { const methodName = "Create" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Gists.Create(ctx, input) + got, resp, err := client.Gists.CreateFromGist(ctx, input) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -536,6 +536,42 @@ func TestGistsService_Create(t *testing.T) { }) } +func TestGistsService_Create_ValueParameter(t *testing.T) { + t.Parallel() + client, mux, _ := setup(t) + + input := CreateGistRequest{ + Description: Ptr("Gist description"), + Public: Ptr(false), + Files: map[GistFilename]GistFile{ + "test.txt": {Content: Ptr("Hello World")}, + }, + } + + mux.HandleFunc("/gists", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "POST") + v := new(CreateGistRequest) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) + + if !cmp.Equal(v, &input) { + t.Errorf("Request body = %+v, want %+v", v, &input) + } + + fmt.Fprint(w, `{"id":"1"}`) + }) + + ctx := context.Background() + gist, _, err := client.Gists.Create(ctx, input) + if err != nil { + t.Errorf("Gists.Create returned error: %v", err) + } + + want := &Gist{ID: Ptr("1")} + if !cmp.Equal(gist, want) { + t.Errorf("Gists.Create returned %+v, want %+v", gist, want) + } +} + func TestGistsService_Edit(t *testing.T) { t.Parallel() client, mux, _ := setup(t) @@ -574,7 +610,7 @@ func TestGistsService_Edit(t *testing.T) { }) ctx := context.Background() - gist, _, err := client.Gists.Edit(ctx, "1", input) + gist, _, err := client.Gists.EditFromGist(ctx, "1", input) if err != nil { t.Errorf("Gists.Edit returned error: %v", err) } @@ -594,12 +630,12 @@ func TestGistsService_Edit(t *testing.T) { const methodName = "Edit" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Gists.Edit(ctx, "\n", input) + _, _, err = client.Gists.EditFromGist(ctx, "\n", input) return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Gists.Edit(ctx, "1", input) + got, resp, err := client.Gists.EditFromGist(ctx, "1", input) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -612,10 +648,45 @@ func TestGistsService_Edit_invalidID(t *testing.T) { client, _, _ := setup(t) ctx := context.Background() - _, _, err := client.Gists.Edit(ctx, "%", nil) + _, _, err := client.Gists.EditFromGist(ctx, "%", nil) testURLParseError(t, err) } +func TestGistsService_Edit_ValueParameter(t *testing.T) { + t.Parallel() + client, mux, _ := setup(t) + + input := UpdateGistRequest{ + Description: Ptr("Updated description"), + Files: map[GistFilename]GistFile{ + "test.txt": {Content: Ptr("Updated content")}, + }, + } + + mux.HandleFunc("/gists/1", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PATCH") + v := new(UpdateGistRequest) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) + + if !cmp.Equal(v, &input) { + t.Errorf("Request body = %+v, want %+v", v, &input) + } + + fmt.Fprint(w, `{"id":"1"}`) + }) + + ctx := context.Background() + gist, _, err := client.Gists.Edit(ctx, "1", input) + if err != nil { + t.Errorf("Gists.Edit returned error: %v", err) + } + + want := &Gist{ID: Ptr("1")} + if !cmp.Equal(gist, want) { + t.Errorf("Gists.Edit returned %+v, want %+v", gist, want) + } +} + func TestGistsService_ListCommits(t *testing.T) { t.Parallel() client, mux, _ := setup(t) diff --git a/github/github-accessors.go b/github/github-accessors.go index b470ba80255..f548f1bf0ce 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -6110,6 +6110,38 @@ func (c *CreateEvent) GetSender() *User { return c.Sender } +// GetBody returns the Body field if it's non-nil, zero value otherwise. +func (c *CreateGistCommentRequest) GetBody() string { + if c == nil || c.Body == nil { + return "" + } + return *c.Body +} + +// GetDescription returns the Description field if it's non-nil, zero value otherwise. +func (c *CreateGistRequest) GetDescription() string { + if c == nil || c.Description == nil { + return "" + } + return *c.Description +} + +// GetFiles returns the Files map if it's non-nil, an empty map otherwise. +func (c *CreateGistRequest) GetFiles() map[GistFilename]GistFile { + if c == nil || c.Files == nil { + return map[GistFilename]GistFile{} + } + return c.Files +} + +// GetPublic returns the Public field if it's non-nil, zero value otherwise. +func (c *CreateGistRequest) GetPublic() bool { + if c == nil || c.Public == nil { + return false + } + return *c.Public +} + // GetEmail returns the Email field if it's non-nil, zero value otherwise. func (c *CreateOrgInvitationOptions) GetEmail() string { if c == nil || c.Email == nil { @@ -28318,6 +28350,30 @@ func (u *UpdateEnterpriseRunnerGroupRequest) GetVisibility() string { return *u.Visibility } +// GetBody returns the Body field if it's non-nil, zero value otherwise. +func (u *UpdateGistCommentRequest) GetBody() string { + if u == nil || u.Body == nil { + return "" + } + return *u.Body +} + +// GetDescription returns the Description field if it's non-nil, zero value otherwise. +func (u *UpdateGistRequest) GetDescription() string { + if u == nil || u.Description == nil { + return "" + } + return *u.Description +} + +// GetFiles returns the Files map if it's non-nil, an empty map otherwise. +func (u *UpdateGistRequest) GetFiles() map[GistFilename]GistFile { + if u == nil || u.Files == nil { + return map[GistFilename]GistFile{} + } + return u.Files +} + // GetForce returns the Force field if it's non-nil, zero value otherwise. func (u *UpdateRef) GetForce() bool { if u == nil || u.Force == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index d61b00e5f69..036f7af61db 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -7965,6 +7965,50 @@ func TestCreateEvent_GetSender(tt *testing.T) { c.GetSender() } +func TestCreateGistCommentRequest_GetBody(tt *testing.T) { + tt.Parallel() + var zeroValue string + c := &CreateGistCommentRequest{Body: &zeroValue} + c.GetBody() + c = &CreateGistCommentRequest{} + c.GetBody() + c = nil + c.GetBody() +} + +func TestCreateGistRequest_GetDescription(tt *testing.T) { + tt.Parallel() + var zeroValue string + c := &CreateGistRequest{Description: &zeroValue} + c.GetDescription() + c = &CreateGistRequest{} + c.GetDescription() + c = nil + c.GetDescription() +} + +func TestCreateGistRequest_GetFiles(tt *testing.T) { + tt.Parallel() + zeroValue := map[GistFilename]GistFile{} + c := &CreateGistRequest{Files: zeroValue} + c.GetFiles() + c = &CreateGistRequest{} + c.GetFiles() + c = nil + c.GetFiles() +} + +func TestCreateGistRequest_GetPublic(tt *testing.T) { + tt.Parallel() + var zeroValue bool + c := &CreateGistRequest{Public: &zeroValue} + c.GetPublic() + c = &CreateGistRequest{} + c.GetPublic() + c = nil + c.GetPublic() +} + func TestCreateOrgInvitationOptions_GetEmail(tt *testing.T) { tt.Parallel() var zeroValue string @@ -36455,6 +36499,39 @@ func TestUpdateEnterpriseRunnerGroupRequest_GetVisibility(tt *testing.T) { u.GetVisibility() } +func TestUpdateGistCommentRequest_GetBody(tt *testing.T) { + tt.Parallel() + var zeroValue string + u := &UpdateGistCommentRequest{Body: &zeroValue} + u.GetBody() + u = &UpdateGistCommentRequest{} + u.GetBody() + u = nil + u.GetBody() +} + +func TestUpdateGistRequest_GetDescription(tt *testing.T) { + tt.Parallel() + var zeroValue string + u := &UpdateGistRequest{Description: &zeroValue} + u.GetDescription() + u = &UpdateGistRequest{} + u.GetDescription() + u = nil + u.GetDescription() +} + +func TestUpdateGistRequest_GetFiles(tt *testing.T) { + tt.Parallel() + zeroValue := map[GistFilename]GistFile{} + u := &UpdateGistRequest{Files: zeroValue} + u.GetFiles() + u = &UpdateGistRequest{} + u.GetFiles() + u = nil + u.GetFiles() +} + func TestUpdateRef_GetForce(tt *testing.T) { tt.Parallel() var zeroValue bool