diff --git a/github/gists_comments.go b/github/gists_comments.go new file mode 100644 index 0000000..6b956c6 --- /dev/null +++ b/github/gists_comments.go @@ -0,0 +1,93 @@ +// Copyright 2013 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" + "time" +) + +// GistComment represents a Gist comment. +type GistComment struct { + ID int `json:"id,omitempty"` + URL string `json:"url,omitempty"` + Body string `json:"body,omitempty"` + User *User `json:"user,omitempty"` + CreatedAt *time.Time `json:"created_at,omitempty"` +} + +// ListComments lists all comments for a gist. +// +// GitHub API docs: http://developer.github.com/v3/gists/comments/#list-comments-on-a-gist +func (s *GistsService) ListComments(gistID string) ([]GistComment, *Response, error) { + u := fmt.Sprintf("gists/%v/comments", gistID) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + comments := new([]GistComment) + resp, err := s.client.Do(req, comments) + return *comments, resp, err +} + +// GetComment retrieves a single comment from a gist. +// +// GitHub API docs: http://developer.github.com/v3/gists/comments/#get-a-single-comment +func (s *GistsService) GetComment(gistID string, commentID int) (*GistComment, *Response, error) { + u := fmt.Sprintf("gists/%v/comments/%v", gistID, commentID) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + c := new(GistComment) + resp, err := s.client.Do(req, c) + return c, resp, err +} + +// CreateComment creates a comment for a gist. +// +// GitHub API docs: http://developer.github.com/v3/gists/comments/#create-a-comment +func (s *GistsService) CreateComment(gistID string, comment *GistComment) (*GistComment, *Response, error) { + u := fmt.Sprintf("gists/%v/comments", gistID) + req, err := s.client.NewRequest("POST", u, comment) + if err != nil { + return nil, nil, err + } + + c := new(GistComment) + resp, err := s.client.Do(req, c) + return c, resp, err +} + +// EditComment edits an existing gist comment. +// +// GitHub API docs: http://developer.github.com/v3/gists/comments/#edit-a-comment +func (s *GistsService) EditComment(gistID string, commentID int, comment *GistComment) (*GistComment, *Response, error) { + u := fmt.Sprintf("gists/%v/comments/%v", gistID, commentID) + req, err := s.client.NewRequest("PATCH", u, comment) + if err != nil { + return nil, nil, err + } + + c := new(GistComment) + resp, err := s.client.Do(req, c) + return c, resp, err +} + +// DeleteComment deletes a gist comment. +// +// GitHub API docs: http://developer.github.com/v3/gists/comments/#delete-a-comment +func (s *GistsService) DeleteComment(gistID string, commentID int) (*Response, error) { + u := fmt.Sprintf("gists/%v/comments/%v", gistID, commentID) + req, err := s.client.NewRequest("DELETE", u, nil) + if err != nil { + return nil, err + } + + return s.client.Do(req, nil) +} diff --git a/github/gists_comments_test.go b/github/gists_comments_test.go new file mode 100644 index 0000000..c441036 --- /dev/null +++ b/github/gists_comments_test.go @@ -0,0 +1,134 @@ +// Copyright 2013 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 ( + "encoding/json" + "fmt" + "net/http" + "reflect" + "testing" +) + +func TestGistsService_ListComments(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/gists/1/comments", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `[{"id": 1}]`) + }) + + comments, _, err := client.Gists.ListComments("1") + + if err != nil { + t.Errorf("Gists.Comments returned error: %v", err) + } + + want := []GistComment{{ID: 1}} + if !reflect.DeepEqual(comments, want) { + t.Errorf("Gists.ListComments returned %+v, want %+v", comments, want) + } +} + +func TestGistsService_GetComment(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/gists/1/comments/2", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"id": 1}`) + }) + + comment, _, err := client.Gists.GetComment("1", 2) + + if err != nil { + t.Errorf("Gists.GetComment returned error: %v", err) + } + + want := &GistComment{ID: 1} + if !reflect.DeepEqual(comment, want) { + t.Errorf("Gists.GetComment returned %+v, want %+v", comment, want) + } +} + +func TestGistsService_CreateComment(t *testing.T) { + setup() + defer teardown() + + input := &GistComment{ + ID: 1, + Body: "This is the comment body.", + } + + mux.HandleFunc("/gists/1/comments", func(w http.ResponseWriter, r *http.Request) { + v := new(GistComment) + json.NewDecoder(r.Body).Decode(v) + + testMethod(t, r, "POST") + if !reflect.DeepEqual(v, input) { + t.Errorf("Request body = %+v, want %+v", v, input) + } + + fmt.Fprint(w, `{"id":1, "body":"b", "url":"u", "user":{"id":2}}`) + }) + + comment, _, err := client.Gists.CreateComment("1", input) + if err != nil { + t.Errorf("Gists.CreateComment returned error: %v", err) + } + + want := &GistComment{ID: 1, Body: "b", URL: "u", User: &User{ID: 2}} + if !reflect.DeepEqual(comment, want) { + t.Errorf("Gists.CreateComment returned %+v, want %+v", comment, want) + } +} + +func TestGistsService_EditComment(t *testing.T) { + setup() + defer teardown() + + input := &GistComment{ + ID: 1, + Body: "New comment.", + } + + mux.HandleFunc("/gists/1/comments/2", func(w http.ResponseWriter, r *http.Request) { + v := new(GistComment) + json.NewDecoder(r.Body).Decode(v) + + testMethod(t, r, "PATCH") + if !reflect.DeepEqual(v, input) { + t.Errorf("Request body = %+v, want %+v", v, input) + } + + fmt.Fprint(w, `{"id":1, "body":"b", "url":"u", "user":{"id":2}}`) + }) + + comment, _, err := client.Gists.EditComment("1", 2, input) + if err != nil { + t.Errorf("Gists.EditComment returned error: %v", err) + } + + want := &GistComment{ID: 1, Body: "b", URL: "u", User: &User{ID: 2}} + if !reflect.DeepEqual(comment, want) { + t.Errorf("Gists.EditComment returned %+v, want %+v", comment, want) + } +} + +func TestGistsService_DeleteComment(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/gists/1/comments/2", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + }) + + _, err := client.Gists.DeleteComment("1", 2) + if err != nil { + t.Errorf("Gists.Delete returned error: %v", err) + } +}