Browse Source

add support for list commits endpoint

Use Timestamp type for GistCommit CommitedAt field.
Add support for ListForks endpoint.
Add missing fields for GistCommit and GistFork types.

Closes #385.

Change-Id: If1379d83173d52c974bb5834a834d97aee2f0ad8
Beshr Kayali 10 years ago
committed by Glenn Lewis
parent
commit
e4b1c2870f
3 changed files with 148 additions and 1 deletions
  1. +64
    -0
      github/gists.go
  2. +83
    -0
      github/gists_test.go
  3. +1
    -1
      github/repos_commits.go

+ 64
- 0
github/gists.go View File

@ -52,6 +52,32 @@ func (g GistFile) String() string {
return Stringify(g)
}
// GistCommit represents a commit on a gist.
type GistCommit struct {
URL *string `json:"url,omitempty"`
Version *string `json:"version,omitempty"`
User *User `json:"user,omitempty"`
ChangeStatus *CommitStats `json:"change_status,omitempty"`
CommitedAt *Timestamp `json:"commited_at,omitempty"`
}
func (gc GistCommit) String() string {
return Stringify(gc)
}
// GistFork represents a fork of a gist.
type GistFork struct {
URL *string `json:"url,omitempty"`
User *User `json:"user,omitempty"`
ID *string `json:"id,omitempty"`
CreatedAt *Timestamp `json:"created_at,omitempty"`
UpdatedAt *Timestamp `json:"updated_at,omitempty"`
}
func (gf GistFork) String() string {
return Stringify(gf)
}
// GistListOptions specifies the optional parameters to the
// GistsService.List, GistsService.ListAll, and GistsService.ListStarred methods.
type GistListOptions struct {
@ -211,6 +237,25 @@ func (s *GistsService) Edit(id string, gist *Gist) (*Gist, *Response, error) {
return g, resp, err
}
// ListCommits lists commits of a gist.
//
// Github API docs: https://developer.github.com/v3/gists/#list-gist-commits
func (s *GistsService) ListCommits(id string) ([]*GistCommit, *Response, error) {
u := fmt.Sprintf("gists/%v/commits", id)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
gistCommits := new([]*GistCommit)
resp, err := s.client.Do(req, gistCommits)
if err != nil {
return nil, resp, err
}
return *gistCommits, resp, err
}
// Delete a gist.
//
// GitHub API docs: http://developer.github.com/v3/gists/#delete-a-gist
@ -279,3 +324,22 @@ func (s *GistsService) Fork(id string) (*Gist, *Response, error) {
return g, resp, err
}
// ListForks lists forks of a gist.
//
// Github API docs: https://developer.github.com/v3/gists/#list-gist-forks
func (s *GistsService) ListForks(id string) ([]*GistFork, *Response, error) {
u := fmt.Sprintf("gists/%v/forks", id)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
gistForks := new([]*GistFork)
resp, err := s.client.Do(req, gistForks)
if err != nil {
return nil, resp, err
}
return *gistForks, resp, err
}

+ 83
- 0
github/gists_test.go View File

@ -281,6 +281,53 @@ func TestGistsService_Edit_invalidID(t *testing.T) {
testURLParseError(t, err)
}
func TestGistsService_ListCommits(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/gists/1/commits", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, nil)
fmt.Fprint(w, `
[
{
"url": "https://api.github.com/gists/1/1",
"version": "1",
"user": {
"id": 1
},
"change_status": {
"deletions": 0,
"additions": 180,
"total": 180
},
"commited_at": "2010-01-01T00:00:00Z"
}
]
`)
})
gistCommits, _, err := client.Gists.ListCommits("1")
if err != nil {
t.Errorf("Gists.ListCommits returned error: %v", err)
}
want := []*GistCommit{{
URL: String("https://api.github.com/gists/1/1"),
Version: String("1"),
User: &User{ID: Int(1)},
CommitedAt: &Timestamp{time.Date(2010, 1, 1, 00, 00, 00, 0, time.UTC)},
ChangeStatus: &CommitStats{
Additions: Int(180),
Deletions: Int(0),
Total: Int(180),
}}}
if !reflect.DeepEqual(gistCommits, want) {
t.Errorf("Gists.ListCommits returned %+v, want %+v", gistCommits, want)
}
}
func TestGistsService_Delete(t *testing.T) {
setup()
defer teardown()
@ -399,6 +446,42 @@ func TestGistsService_Fork(t *testing.T) {
}
}
func TestGistsService_ListForks(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/gists/1/forks", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, nil)
fmt.Fprint(w, `
[
{"url": "https://api.github.com/gists/1",
"user": {"id": 1},
"id": "1",
"created_at": "2010-01-01T00:00:00Z",
"updated_at": "2013-01-01T00:00:00Z"
}
]
`)
})
gistForks, _, err := client.Gists.ListForks("1")
if err != nil {
t.Errorf("Gists.ListForks returned error: %v", err)
}
want := []*GistFork{{
URL: String("https://api.github.com/gists/1"),
ID: String("1"),
User: &User{ID: Int(1)},
CreatedAt: &Timestamp{time.Date(2010, 1, 1, 00, 00, 00, 0, time.UTC)},
UpdatedAt: &Timestamp{time.Date(2013, 1, 1, 00, 00, 00, 0, time.UTC)}}}
if !reflect.DeepEqual(gistForks, want) {
t.Errorf("Gists.ListForks returned %+v, want %+v", gistForks, want)
}
}
func TestGistsService_Fork_invalidID(t *testing.T) {
_, _, err := client.Gists.Fork("%")
testURLParseError(t, err)


+ 1
- 1
github/repos_commits.go View File

@ -33,7 +33,7 @@ func (r RepositoryCommit) String() string {
return Stringify(r)
}
// CommitStats represents the number of additions / deletions from a file in a given RepositoryCommit.
// CommitStats represents the number of additions / deletions from a file in a given RepositoryCommit or GistCommit.
type CommitStats struct {
Additions *int `json:"additions,omitempty"`
Deletions *int `json:"deletions,omitempty"`


Loading…
Cancel
Save