Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,6 @@ linters:
- ExternalGroup
- GenerateJITConfigRequest
- GenerateNotesOptions
- Gist
- GistComment
- Hook
- HookConfig
- ImpersonateUserOptions
Expand Down
39 changes: 36 additions & 3 deletions github/gists.go
Original file line number Diff line number Diff line change
Expand Up @@ -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. (Required.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Files is the set of files that make up the gist, keyed by filename. (Required.)
// 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

Expand All @@ -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. (Required.)
Content *string `json:"content,omitempty"`
Comment on lines +76 to +77

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Content is the contents of the file. (Required.)
Content *string `json:"content,omitempty"`
// 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"`
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
16 changes: 13 additions & 3 deletions github/gists_comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ func (g GistComment) String() string {
return Stringify(g)
}

// CreateGistCommentRequest represents the input for creating a gist comment.
type CreateGistCommentRequest struct {
Body *string `json:"body,omitempty"`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Body *string `json:"body,omitempty"`
// Body is the comment text.
Body string `json:"body"`

}

// UpdateGistCommentRequest represents the input for updating a gist comment.
type UpdateGistCommentRequest struct {
Body *string `json:"body,omitempty"`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Body *string `json:"body,omitempty"`
// 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
Expand Down Expand Up @@ -75,7 +85,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 {
Expand All @@ -91,12 +101,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 {
Expand Down
24 changes: 12 additions & 12 deletions github/gists_comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: Ptr("b")}

mux.HandleFunc("/gists/1/comments", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
Expand Down Expand Up @@ -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: Ptr("b")}

mux.HandleFunc("/gists/1/comments/2", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PATCH")
Expand All @@ -163,37 +163,37 @@ 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)
}
return resp, err
})
}

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)
}

Expand Down
28 changes: 15 additions & 13 deletions github/gists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,10 @@ 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{
Files: map[GistFilename]*CreateGistFile{
"test.txt": {Content: Ptr("Gist file content")},
},
}
Expand Down Expand Up @@ -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,
},
}

Expand All @@ -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{
Expand All @@ -365,30 +367,30 @@ 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)
}
return resp, err
})
}

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)
}

Expand Down
64 changes: 64 additions & 0 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading