diff --git a/.golangci.yml b/.golangci.yml index 9a7fd32877b..2224d1c1ab6 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -230,8 +230,6 @@ linters: - ExternalGroup - GenerateJITConfigRequest - GenerateNotesOptions - - Gist - - GistComment - Hook - HookConfig - ImpersonateUserOptions diff --git a/github/gists.go b/github/gists.go index bd8553f9745..a339286c062 100644 --- a/github/gists.go +++ b/github/gists.go @@ -37,6 +37,22 @@ func (g Gist) String() string { return Stringify(g) } +// CreateGistRequest represents the input for creating a gist. +type CreateGistRequest struct { + Description *string `json:"description,omitempty"` + Public *bool `json:"public,omitempty"` + // Files is the set of files that make up the gist, keyed by filename. + Files map[GistFilename]*CreateGistFile `json:"files"` +} + +// UpdateGistRequest represents the input for updating a gist. +type UpdateGistRequest struct { + Description *string `json:"description,omitempty"` + // Files is the set of files to add, change or rename, keyed by filename. + // Mapping a filename to a nil value deletes that file from the gist. + Files map[GistFilename]*UpdateGistFile `json:"files,omitempty"` +} + // GistFilename represents filename on a gist. type GistFilename string @@ -54,6 +70,23 @@ func (g GistFile) String() string { return Stringify(g) } +// CreateGistFile represents a file within a CreateGistRequest, keyed by filename +// in the request's Files map. +type CreateGistFile struct { + // Content is the contents of the file. + Content string `json:"content"` +} + +// UpdateGistFile represents a file within an UpdateGistRequest, keyed by filename +// in the request's Files map. Mapping a filename to a nil *UpdateGistFile deletes +// that file from the gist. +type UpdateGistFile struct { + // Content is the new contents of the file. + Content *string `json:"content,omitempty"` + // Filename, if set, renames the file. + Filename *string `json:"filename,omitempty"` +} + // GistCommit represents a commit on a gist. type GistCommit struct { URL *string `json:"url,omitempty"` @@ -225,7 +258,7 @@ func (s *GistsService) GetRevision(ctx context.Context, id, sha string) (*Gist, // GitHub API docs: https://docs.github.com/rest/gists/gists?apiVersion=2022-11-28#create-a-gist // //meta:operation POST /gists -func (s *GistsService) Create(ctx context.Context, body *Gist) (*Gist, *Response, error) { +func (s *GistsService) Create(ctx context.Context, body CreateGistRequest) (*Gist, *Response, error) { u := "gists" req, err := s.client.NewRequest(ctx, "POST", u, body) if err != nil { @@ -241,12 +274,12 @@ func (s *GistsService) Create(ctx context.Context, body *Gist) (*Gist, *Response return g, resp, nil } -// Edit a gist. +// Update a gist. // // GitHub API docs: https://docs.github.com/rest/gists/gists?apiVersion=2022-11-28#update-a-gist // //meta:operation PATCH /gists/{gist_id} -func (s *GistsService) Edit(ctx context.Context, id string, body *Gist) (*Gist, *Response, error) { +func (s *GistsService) Update(ctx context.Context, id string, body UpdateGistRequest) (*Gist, *Response, error) { u := fmt.Sprintf("gists/%v", id) req, err := s.client.NewRequest(ctx, "PATCH", u, body) if err != nil { diff --git a/github/gists_comments.go b/github/gists_comments.go index 27f0fa1d7a3..d2e769604fa 100644 --- a/github/gists_comments.go +++ b/github/gists_comments.go @@ -23,6 +23,18 @@ func (g GistComment) String() string { return Stringify(g) } +// CreateGistCommentRequest represents the input for creating a gist comment. +type CreateGistCommentRequest struct { + // Body is the comment text. + Body string `json:"body"` +} + +// UpdateGistCommentRequest represents the input for updating a gist comment. +type UpdateGistCommentRequest struct { + // Body is the comment text. + Body string `json:"body"` +} + // ListComments lists all comments for a gist. // // GitHub API docs: https://docs.github.com/rest/gists/comments?apiVersion=2022-11-28#list-gist-comments @@ -75,7 +87,7 @@ func (s *GistsService) GetComment(ctx context.Context, gistID string, commentID // GitHub API docs: https://docs.github.com/rest/gists/comments?apiVersion=2022-11-28#create-a-gist-comment // //meta:operation POST /gists/{gist_id}/comments -func (s *GistsService) CreateComment(ctx context.Context, gistID string, body *GistComment) (*GistComment, *Response, error) { +func (s *GistsService) CreateComment(ctx context.Context, gistID string, body CreateGistCommentRequest) (*GistComment, *Response, error) { u := fmt.Sprintf("gists/%v/comments", gistID) req, err := s.client.NewRequest(ctx, "POST", u, body) if err != nil { @@ -91,12 +103,12 @@ func (s *GistsService) CreateComment(ctx context.Context, gistID string, body *G return c, resp, nil } -// EditComment edits an existing gist comment. +// UpdateComment updates an existing gist comment. // // GitHub API docs: https://docs.github.com/rest/gists/comments?apiVersion=2022-11-28#update-a-gist-comment // //meta:operation PATCH /gists/{gist_id}/comments/{comment_id} -func (s *GistsService) EditComment(ctx context.Context, gistID string, commentID int64, body *GistComment) (*GistComment, *Response, error) { +func (s *GistsService) UpdateComment(ctx context.Context, gistID string, commentID int64, body UpdateGistCommentRequest) (*GistComment, *Response, error) { u := fmt.Sprintf("gists/%v/comments/%v", gistID, commentID) req, err := s.client.NewRequest(ctx, "PATCH", u, body) if err != nil { diff --git a/github/gists_comments_test.go b/github/gists_comments_test.go index 9db219e64fb..a18022cbb93 100644 --- a/github/gists_comments_test.go +++ b/github/gists_comments_test.go @@ -107,7 +107,7 @@ func TestGistsService_CreateComment(t *testing.T) { t.Parallel() client, mux, _ := setup(t) - input := &GistComment{ID: Ptr(int64(1)), Body: Ptr("b")} + input := CreateGistCommentRequest{Body: "b"} mux.HandleFunc("/gists/1/comments", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") @@ -146,15 +146,15 @@ func TestGistsService_CreateComment_invalidID(t *testing.T) { client, _, _ := setup(t) ctx := t.Context() - _, _, err := client.Gists.CreateComment(ctx, "%", nil) + _, _, err := client.Gists.CreateComment(ctx, "%", CreateGistCommentRequest{}) testURLParseError(t, err) } -func TestGistsService_EditComment(t *testing.T) { +func TestGistsService_UpdateComment(t *testing.T) { t.Parallel() client, mux, _ := setup(t) - input := &GistComment{ID: Ptr(int64(1)), Body: Ptr("b")} + input := UpdateGistCommentRequest{Body: "b"} mux.HandleFunc("/gists/1/comments/2", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") @@ -163,24 +163,24 @@ func TestGistsService_EditComment(t *testing.T) { }) ctx := t.Context() - comment, _, err := client.Gists.EditComment(ctx, "1", 2, input) + comment, _, err := client.Gists.UpdateComment(ctx, "1", 2, input) if err != nil { - t.Errorf("Gists.EditComment returned error: %v", err) + t.Errorf("Gists.UpdateComment 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.UpdateComment returned %+v, want %+v", comment, want) } - const methodName = "EditComment" + const methodName = "UpdateComment" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Gists.EditComment(ctx, "\n", -2, input) + _, _, err = client.Gists.UpdateComment(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.UpdateComment(ctx, "1", 2, input) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -188,12 +188,12 @@ func TestGistsService_EditComment(t *testing.T) { }) } -func TestGistsService_EditComment_invalidID(t *testing.T) { +func TestGistsService_UpdateComment_invalidID(t *testing.T) { t.Parallel() client, _, _ := setup(t) ctx := t.Context() - _, _, err := client.Gists.EditComment(ctx, "%", 1, nil) + _, _, err := client.Gists.UpdateComment(ctx, "%", 1, UpdateGistCommentRequest{}) testURLParseError(t, err) } diff --git a/github/gists_test.go b/github/gists_test.go index 39928c0d2bc..3a3f08668bb 100644 --- a/github/gists_test.go +++ b/github/gists_test.go @@ -263,11 +263,11 @@ func TestGistsService_Create(t *testing.T) { t.Parallel() client, mux, _ := setup(t) - input := &Gist{ + input := CreateGistRequest{ Description: Ptr("Gist description"), Public: Ptr(false), - Files: map[GistFilename]GistFile{ - "test.txt": {Content: Ptr("Gist file content")}, + Files: map[GistFilename]*CreateGistFile{ + "test.txt": {Content: "Gist file content"}, }, } @@ -317,14 +317,16 @@ func TestGistsService_Create(t *testing.T) { }) } -func TestGistsService_Edit(t *testing.T) { +func TestGistsService_Update(t *testing.T) { t.Parallel() client, mux, _ := setup(t) - input := &Gist{ + input := UpdateGistRequest{ Description: Ptr("New description"), - Files: map[GistFilename]GistFile{ + Files: map[GistFilename]*UpdateGistFile{ "new.txt": {Content: Ptr("new file content")}, + // A nil value deletes the file from the gist. + "old.txt": nil, }, } @@ -350,9 +352,9 @@ func TestGistsService_Edit(t *testing.T) { }) ctx := t.Context() - gist, _, err := client.Gists.Edit(ctx, "1", input) + gist, _, err := client.Gists.Update(ctx, "1", input) if err != nil { - t.Errorf("Gists.Edit returned error: %v", err) + t.Errorf("Gists.Update returned error: %v", err) } want := &Gist{ @@ -365,17 +367,17 @@ func TestGistsService_Edit(t *testing.T) { }, } if !cmp.Equal(gist, want) { - t.Errorf("Gists.Edit returned %+v, want %+v", gist, want) + t.Errorf("Gists.Update returned %+v, want %+v", gist, want) } - const methodName = "Edit" + const methodName = "Update" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Gists.Edit(ctx, "\n", input) + _, _, err = client.Gists.Update(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.Update(ctx, "1", input) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -383,12 +385,12 @@ func TestGistsService_Edit(t *testing.T) { }) } -func TestGistsService_Edit_invalidID(t *testing.T) { +func TestGistsService_Update_invalidID(t *testing.T) { t.Parallel() client, _, _ := setup(t) ctx := t.Context() - _, _, err := client.Gists.Edit(ctx, "%", nil) + _, _, err := client.Gists.Update(ctx, "%", UpdateGistRequest{}) testURLParseError(t, err) } diff --git a/github/github-accessors.go b/github/github-accessors.go index 0a6cc1e6b53..b5c01e9989b 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -10766,6 +10766,38 @@ func (c *CreateEvent) GetSender() *User { return c.Sender } +// GetBody returns the Body field. +func (c *CreateGistCommentRequest) GetBody() string { + if c == nil { + return "" + } + return c.Body +} + +// GetContent returns the Content field. +func (c *CreateGistFile) GetContent() string { + if c == nil { + return "" + } + return c.Content +} + +// 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 +} + +// 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 +} + // GetEnableStaticIP returns the EnableStaticIP field if it's non-nil, zero value otherwise. func (c *CreateHostedRunnerRequest) GetEnableStaticIP() bool { if c == nil || c.EnableStaticIP == nil { @@ -41870,6 +41902,38 @@ func (u *UpdateEnterpriseRunnerGroupRequest) GetVisibility() string { return *u.Visibility } +// GetBody returns the Body field. +func (u *UpdateGistCommentRequest) GetBody() string { + if u == nil { + return "" + } + return u.Body +} + +// GetContent returns the Content field if it's non-nil, zero value otherwise. +func (u *UpdateGistFile) GetContent() string { + if u == nil || u.Content == nil { + return "" + } + return *u.Content +} + +// GetFilename returns the Filename field if it's non-nil, zero value otherwise. +func (u *UpdateGistFile) GetFilename() string { + if u == nil || u.Filename == nil { + return "" + } + return *u.Filename +} + +// 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 +} + // GetEnableStaticIP returns the EnableStaticIP field if it's non-nil, zero value otherwise. func (u *UpdateHostedRunnerRequest) GetEnableStaticIP() bool { if u == nil || u.EnableStaticIP == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 7433e4499e8..fe11f19855f 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -13689,6 +13689,44 @@ func TestCreateEvent_GetSender(tt *testing.T) { c.GetSender() } +func TestCreateGistCommentRequest_GetBody(tt *testing.T) { + tt.Parallel() + c := &CreateGistCommentRequest{} + c.GetBody() + c = nil + c.GetBody() +} + +func TestCreateGistFile_GetContent(tt *testing.T) { + tt.Parallel() + c := &CreateGistFile{} + c.GetContent() + c = nil + c.GetContent() +} + +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_GetPublic(tt *testing.T) { + tt.Parallel() + var zeroValue bool + c := &CreateGistRequest{Public: &zeroValue} + c.GetPublic() + c = &CreateGistRequest{} + c.GetPublic() + c = nil + c.GetPublic() +} + func TestCreateHostedRunnerRequest_GetEnableStaticIP(tt *testing.T) { tt.Parallel() var zeroValue bool @@ -52563,6 +52601,47 @@ func TestUpdateEnterpriseRunnerGroupRequest_GetVisibility(tt *testing.T) { u.GetVisibility() } +func TestUpdateGistCommentRequest_GetBody(tt *testing.T) { + tt.Parallel() + u := &UpdateGistCommentRequest{} + u.GetBody() + u = nil + u.GetBody() +} + +func TestUpdateGistFile_GetContent(tt *testing.T) { + tt.Parallel() + var zeroValue string + u := &UpdateGistFile{Content: &zeroValue} + u.GetContent() + u = &UpdateGistFile{} + u.GetContent() + u = nil + u.GetContent() +} + +func TestUpdateGistFile_GetFilename(tt *testing.T) { + tt.Parallel() + var zeroValue string + u := &UpdateGistFile{Filename: &zeroValue} + u.GetFilename() + u = &UpdateGistFile{} + u.GetFilename() + u = nil + u.GetFilename() +} + +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 TestUpdateHostedRunnerRequest_GetEnableStaticIP(tt *testing.T) { tt.Parallel() var zeroValue bool