Browse Source

Added basic Repository Comments API

Billy Lynch 13 years ago
committed by Will Norris
parent
commit
d5115b45d0
2 changed files with 258 additions and 0 deletions
  1. +112
    -0
      github/repos_comments.go
  2. +146
    -0
      github/repos_comments_test.go

+ 112
- 0
github/repos_comments.go View File

@ -0,0 +1,112 @@
// 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"
)
// RepositoryComment represents a comment for a commit, file, or line in a repository.
type RepositoryComment struct {
HTMLURL string `json:"html_url,omitempty"`
URL string `json:"url,omitempty"`
ID int `json:"id,omitempty"`
CommitID string `json:"commit_id,omitempty"`
User User `json:"user,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
// User-mutable fields
Body string `json:"body"`
Path string `json:"path,omitempty"`
Position int `json:"position,omitempty"`
}
// ListComments lists all the comments for the repository.
//
// GitHub API docs: http://developer.github.com/v3/repos/comments/#list-commit-comments-for-a-repository
func (s *RepositoriesService) ListComments(owner, repo string) ([]RepositoryComment, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/comments", owner, repo)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
comments := new([]RepositoryComment)
resp, err := s.client.Do(req, comments)
return *comments, resp, err
}
// ListCommitComments lists all the comments for a given commit SHA.
//
// GitHub API docs: http://developer.github.com/v3/repos/comments/#list-comments-for-a-single-commit
func (s *RepositoriesService) ListCommitComments(owner, repo, sha string) ([]RepositoryComment, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/commits/%v/comments", owner, repo, sha)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
comments := new([]RepositoryComment)
resp, err := s.client.Do(req, comments)
return *comments, resp, err
}
// CreateComment creates a comment for the given commit.
// Note: GitHub allows for comments to be created for non-existing files and positions.
//
// GitHub API docs: http://developer.github.com/v3/repos/comments/#create-a-commit-comment
func (s *RepositoriesService) CreateComment(owner, repo, sha string, comment *RepositoryComment) (*RepositoryComment, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/commits/%v/comments", owner, repo, sha)
req, err := s.client.NewRequest("POST", u, comment)
if err != nil {
return nil, nil, err
}
c := new(RepositoryComment)
resp, err := s.client.Do(req, c)
return c, resp, err
}
// GetComment gets a single comment from a repository.
//
// GitHub API docs: http://developer.github.com/v3/repos/comments/#get-a-single-commit-comment
func (s *RepositoriesService) GetComment(owner, repo string, id int) (*RepositoryComment, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/comments/%v", owner, repo, id)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
c := new(RepositoryComment)
resp, err := s.client.Do(req, c)
return c, resp, err
}
// UpdateComment updates the body of a single comment.
//
// GitHub API docs: http://developer.github.com/v3/repos/comments/#update-a-commit-comment
func (s *RepositoriesService) UpdateComment(owner, repo string, id int, body string) (*RepositoryComment, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/comments/%v", owner, repo, id)
comment := RepositoryComment{Body: body}
req, err := s.client.NewRequest("PATCH", u, &comment)
if err != nil {
return nil, nil, err
}
c := new(RepositoryComment)
resp, err := s.client.Do(req, c)
return c, resp, err
}
// DeleteComment deletes a single comment from a repository.
//
// GitHub API docs: http://developer.github.com/v3/repos/comments/#delete-a-commit-comment
func (s *RepositoriesService) DeleteComment(owner, repo string, id int) (*Response, error) {
u := fmt.Sprintf("repos/%v/%v/comments/%v", owner, repo, id)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
resp, err := s.client.Do(req, nil)
return resp, err
}

+ 146
- 0
github/repos_comments_test.go View File

@ -0,0 +1,146 @@
// 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 TestRepositoriesService_ListComments(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/repos/o/r/comments", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id":1}, {"id":2}]`)
})
comments, _, err := client.Repositories.ListComments("o", "r")
if err != nil {
t.Errorf("Repositories.ListComments returned error: %v", err)
}
want := []RepositoryComment{{ID: 1}, {ID: 2}}
if !reflect.DeepEqual(comments, want) {
t.Errorf("Repositories.ListComments returned %+v, want %+v", comments, want)
}
}
func TestRepositoriesService_ListCommitComments(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/repos/o/r/commits/s/comments", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id":1}, {"id":2}]`)
})
comments, _, err := client.Repositories.ListCommitComments("o", "r", "s")
if err != nil {
t.Errorf("Repositories.ListCommitComments returned error: %v", err)
}
want := []RepositoryComment{{ID: 1}, {ID: 2}}
if !reflect.DeepEqual(comments, want) {
t.Errorf("Repositories.ListCommitComments returned %+v, want %+v", comments, want)
}
}
func TestRepositoriesService_CreateComment(t *testing.T) {
setup()
defer teardown()
input := &RepositoryComment{Body: "asdf"}
mux.HandleFunc("/repos/o/r/commits/s/comments", func(w http.ResponseWriter, r *http.Request) {
v := new(RepositoryComment)
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}`)
})
comment, _, err := client.Repositories.CreateComment("o", "r", "s", input)
if err != nil {
t.Errorf("Repositories.CreateComment returned error: %v", err)
}
want := &RepositoryComment{ID: 1}
if !reflect.DeepEqual(comment, want) {
t.Errorf("Repositories.CreateComment returned %+v, want %+v", comment, want)
}
}
func TestRepositoriesService_GetComment(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/repos/o/r/comments/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"id":1}`)
})
comment, _, err := client.Repositories.GetComment("o", "r", 1)
if err != nil {
t.Errorf("Repositories.GetComment returned error: %v", err)
}
want := &RepositoryComment{ID: 1}
if !reflect.DeepEqual(comment, want) {
t.Errorf("Repositories.GetComment returned %+v, want %+v", comment, want)
}
}
func TestRepositoriesService_UpdateComment(t *testing.T) {
setup()
defer teardown()
input := &RepositoryComment{Body: "asdf"}
mux.HandleFunc("/repos/o/r/comments/1", func(w http.ResponseWriter, r *http.Request) {
v := new(RepositoryComment)
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}`)
})
comment, _, err := client.Repositories.UpdateComment("o", "r", 1, "asdf")
if err != nil {
t.Errorf("Repositories.UpdateComment returned error: %v", err)
}
want := &RepositoryComment{ID: 1}
if !reflect.DeepEqual(comment, want) {
t.Errorf("Repositories.UpdateComment returned %+v, want %+v", comment, want)
}
}
func TestRepositoriesService_DeleteComment(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/repos/o/r/comments/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
})
_, err := client.Repositories.DeleteComment("o", "r", 1)
if err != nil {
t.Errorf("Repositories.DeleteComment returned error: %v", err)
}
}

Loading…
Cancel
Save