Skip to content

Commit 1eaf383

Browse files
ktosowillnorris
authored andcommitted
implement repos_commits API
1 parent 838a7ac commit 1eaf383

5 files changed

Lines changed: 358 additions & 0 deletions

File tree

‎github/git_commits.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Commit struct {
1818
Message *string `json:"message,omitempty"`
1919
Tree *Tree `json:"tree,omitempty"`
2020
Parents []Commit `json:"parents,omitempty"`
21+
Stats *CommitStats `json:"stats,omitempty"`
2122
}
2223

2324
func (c Commit) String() string {

‎github/repos_collaborators.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func (s *RepositoriesService) IsCollaborator(owner, repo, user string) (bool, *R
3636
if err != nil {
3737
return false, nil, err
3838
}
39+
3940
resp, err := s.client.Do(req, nil)
4041
isCollab, err := parseBoolResponse(err)
4142
return isCollab, resp, err

‎github/repos_commits.go‎

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
// Copyright 2013 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package github
7+
8+
import (
9+
"fmt"
10+
"net/url"
11+
"time"
12+
)
13+
14+
// RepositoryCommit represents a commit in a repo.
15+
// Note that it's wrapping a Commit, so author/committer information is in two places,
16+
// but contain different details about them: in RepositoryCommit "github details", in Commit - "git details".
17+
type RepositoryCommit struct {
18+
SHA *string `json:"sha,omitempty"`
19+
Commit *Commit `json:"commit,omitempty"`
20+
Author *User `json:"author,omitempty"`
21+
Committer *User `json:"committer,omitempty"`
22+
Parents []Commit `json:"parents,omitempty"`
23+
Message *string `json:"message,omitempty"`
24+
25+
// Details about how many changes were made in this commit. Only filled in during GetCommit!
26+
Stats *CommitStats `json:"stats,omitempty"`
27+
// Details about which files, and how this commit touched. Only filled in during GetCommit!
28+
Files []CommitFile `json:"files,omitempty"`
29+
}
30+
31+
func (r RepositoryCommit) String() string {
32+
return Stringify(r)
33+
}
34+
35+
// CommitStats represents the number of additions / deletions from a file in a given RepositoryCommit.
36+
type CommitStats struct {
37+
Additions *int `json:"additions,omitempty"`
38+
Deletions *int `json:"deletions,omitempty"`
39+
Total *int `json:"total,omitempty"`
40+
}
41+
42+
func (c CommitStats) String() string {
43+
return Stringify(c)
44+
}
45+
46+
// CommitFile represents a file modified in a commit.
47+
type CommitFile struct {
48+
SHA *string `json:"sha,omitempty"`
49+
Filename *string `json:"filename,omitempty"`
50+
Additions *int `json:"additions,omitempty"`
51+
Deletions *int `json:"deletions,omitempty"`
52+
Changes *int `json:"changes,omitempty"`
53+
Status *string `json:"status,omitempty"`
54+
Patch *string `json:"patch,omitempty"`
55+
}
56+
57+
func (c CommitFile) String() string {
58+
return Stringify(c)
59+
}
60+
61+
// CommitsComparison is the result of comparing two commits.
62+
// See CompareCommits() for details.
63+
type CommitsComparison struct {
64+
BaseCommit *RepositoryCommit `json:"base_commit,omitempty"`
65+
66+
// Head can be 'behind' or 'ahead'
67+
Status *string `json:"status,omitempty"`
68+
AheadBy *int `json:"ahead_by,omitempty"`
69+
BehindBy *int `json:"behind_by,omitempty"`
70+
TotalCommits *int `json:"total_commits,omitempty"`
71+
72+
Commits []RepositoryCommit `json:"commits,omitempty"`
73+
74+
Files []CommitFile `json:"files,omitempty"`
75+
}
76+
77+
func (c CommitsComparison) String() string {
78+
return Stringify(c)
79+
}
80+
81+
// CommitsListOptions specifies the optional parameters to the
82+
// RepositoriesService.ListCommits method.
83+
type CommitsListOptions struct {
84+
// SHA or branch to start listing Commits from.
85+
SHA string
86+
// Path that should be touched by the returned Commits.
87+
Path string
88+
// Author of by which to filter Commits.
89+
Author string
90+
// Since when should Commits be included in the response.
91+
Since time.Time
92+
// Until when should Commits be included in the response.
93+
Until time.Time
94+
}
95+
96+
// ListCommits lists the commits of a repository.
97+
//
98+
// GitHub API docs: http://developer.github.com/v3/repos/commits/#list
99+
func (s *RepositoriesService) ListCommits(owner, repo string, opts *CommitsListOptions) ([]RepositoryCommit, *Response, error) {
100+
u := fmt.Sprintf("repos/%v/%v/commits", owner, repo)
101+
102+
if opts != nil {
103+
params := url.Values{
104+
"sha": {opts.SHA},
105+
"path": {opts.Path},
106+
"author": {opts.Author},
107+
}
108+
if !opts.Since.IsZero() {
109+
params.Add("since", opts.Since.Format(time.RFC3339))
110+
}
111+
if !opts.Until.IsZero() {
112+
params.Add("until", opts.Until.Format(time.RFC3339))
113+
}
114+
115+
u += "?" + params.Encode()
116+
}
117+
118+
req, err := s.client.NewRequest("GET", u, nil)
119+
if err != nil {
120+
return nil, nil, err
121+
}
122+
123+
commits := new([]RepositoryCommit)
124+
resp, err := s.client.Do(req, commits)
125+
return *commits, resp, err
126+
}
127+
128+
// GetCommit fetches the specified commit, including all details about it.
129+
// todo: support media formats - https://github.com/google/go-github/issues/6
130+
//
131+
// GitHub API docs: http://developer.github.com/v3/repos/commits/#get-a-single-commit
132+
// See also: http://developer.github.com//v3/git/commits/#get-a-single-commit provides the same functionality
133+
func (s *RepositoriesService) GetCommit(owner, repo, sha string) (*RepositoryCommit, *Response, error) {
134+
u := fmt.Sprintf("repos/%v/%v/commits/%v", owner, repo, sha)
135+
136+
req, err := s.client.NewRequest("GET", u, nil)
137+
if err != nil {
138+
return nil, nil, err
139+
}
140+
141+
commit := new(RepositoryCommit)
142+
resp, err := s.client.Do(req, commit)
143+
return commit, resp, err
144+
}
145+
146+
// CompareCommits compares a range of commits with each other.
147+
// todo: support media formats - https://github.com/google/go-github/issues/6
148+
//
149+
// GitHub API docs: http://developer.github.com/v3/repos/commits/index.html#compare-two-commits
150+
func (s *RepositoriesService) CompareCommits(owner, repo string, base, head string) (*CommitsComparison, *Response, error) {
151+
u := fmt.Sprintf("repos/%v/%v/compare/%v...%v", owner, repo, base, head)
152+
153+
req, err := s.client.NewRequest("GET", u, nil)
154+
if err != nil {
155+
return nil, nil, err
156+
}
157+
158+
comp := new(CommitsComparison)
159+
resp, err := s.client.Do(req, comp)
160+
return comp, resp, err
161+
}

‎github/repos_commits_test.go‎

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
// Copyright 2013 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package github
7+
8+
import (
9+
"fmt"
10+
"net/http"
11+
"reflect"
12+
"testing"
13+
"time"
14+
)
15+
16+
func TestRepositoriesService_ListCommits(t *testing.T) {
17+
setup()
18+
defer teardown()
19+
20+
// given
21+
mux.HandleFunc("/repos/o/r/commits", func(w http.ResponseWriter, r *http.Request) {
22+
testMethod(t, r, "GET")
23+
testFormValues(t, r,
24+
values{
25+
"sha": "s",
26+
"path": "p",
27+
"author": "a",
28+
"since": "2013-08-01T00:00:00Z",
29+
"until": "2013-09-03T00:00:00Z",
30+
})
31+
fmt.Fprintf(w, `[{"sha": "s"}]`)
32+
})
33+
34+
opt := &CommitsListOptions{
35+
SHA: "s",
36+
Path: "p",
37+
Author: "a",
38+
Since: time.Date(2013, time.August, 1, 0, 0, 0, 0, time.UTC),
39+
Until: time.Date(2013, time.September, 3, 0, 0, 0, 0, time.UTC),
40+
}
41+
commits, _, err := client.Repositories.ListCommits("o", "r", opt)
42+
if err != nil {
43+
t.Errorf("Repositories.ListCommits returned error: %v", err)
44+
}
45+
46+
want := []RepositoryCommit{{SHA: String("s")}}
47+
if !reflect.DeepEqual(commits, want) {
48+
t.Errorf("Repositories.ListCommits returned %+v, want %+v", commits, want)
49+
}
50+
}
51+
52+
func TestRepositoriesService_GetCommit(t *testing.T) {
53+
setup()
54+
defer teardown()
55+
56+
mux.HandleFunc("/repos/o/r/commits/s", func(w http.ResponseWriter, r *http.Request) {
57+
testMethod(t, r, "GET")
58+
fmt.Fprintf(w, `{
59+
"sha": "s",
60+
"commit": { "message": "m" },
61+
"author": { "login": "l" },
62+
"committer": { "login": "l" },
63+
"parents": [ { "sha": "s" } ],
64+
"stats": { "additions": 104, "deletions": 4, "total": 108 },
65+
"files": [
66+
{
67+
"filename": "f",
68+
"additions": 10,
69+
"deletions": 2,
70+
"changes": 12,
71+
"status": "s",
72+
"raw_url": "r",
73+
"blob_url": "b",
74+
"patch": "p"
75+
}
76+
]
77+
}`)
78+
})
79+
80+
commit, _, err := client.Repositories.GetCommit("o", "r", "s")
81+
if err != nil {
82+
t.Errorf("Repositories.GetCommit returned error: %v", err)
83+
}
84+
85+
want := &RepositoryCommit{
86+
SHA: String("s"),
87+
Commit: &Commit{
88+
Message: String("m"),
89+
},
90+
Author: &User{
91+
Login: String("l"),
92+
},
93+
Committer: &User{
94+
Login: String("l"),
95+
},
96+
Parents: []Commit{
97+
{
98+
SHA: String("s"),
99+
},
100+
},
101+
Stats: &CommitStats{
102+
Additions: Int(104),
103+
Deletions: Int(4),
104+
Total: Int(108),
105+
},
106+
Files: []CommitFile{
107+
{
108+
Filename: String("f"),
109+
Additions: Int(10),
110+
Deletions: Int(2),
111+
Changes: Int(12),
112+
Status: String("s"),
113+
Patch: String("p"),
114+
},
115+
},
116+
}
117+
if !reflect.DeepEqual(commit, want) {
118+
t.Errorf("Repositories.GetCommit returned \n%+v, want \n%+v", commit, want)
119+
}
120+
}
121+
122+
func TestRepositoriesService_CompareCommits(t *testing.T) {
123+
setup()
124+
defer teardown()
125+
126+
mux.HandleFunc("/repos/o/r/compare/b...h", func(w http.ResponseWriter, r *http.Request) {
127+
testMethod(t, r, "GET")
128+
fmt.Fprintf(w, `{
129+
"base_commit": {
130+
"sha": "s",
131+
"commit": {
132+
"author": { "name": "n" },
133+
"committer": { "name": "n" },
134+
"message": "m",
135+
"tree": { "sha": "t" }
136+
},
137+
"author": { "login": "n" },
138+
"committer": { "login": "l" },
139+
"parents": [ { "sha": "s" } ]
140+
},
141+
"status": "s",
142+
"ahead_by": 1,
143+
"behind_by": 2,
144+
"total_commits": 1,
145+
"commits": [
146+
{
147+
"sha": "s",
148+
"commit": { "author": { "name": "n" } },
149+
"author": { "login": "l" },
150+
"committer": { "login": "l" },
151+
"parents": [ { "sha": "s" } ]
152+
}
153+
],
154+
"files": [ { "filename": "f" } ]
155+
}`)
156+
})
157+
158+
got, _, err := client.Repositories.CompareCommits("o", "r", "b", "h")
159+
if err != nil {
160+
t.Errorf("Repositories.CompareCommits returned error: %v", err)
161+
}
162+
163+
want := &CommitsComparison{
164+
Status: String("s"),
165+
AheadBy: Int(1),
166+
BehindBy: Int(2),
167+
TotalCommits: Int(1),
168+
BaseCommit: &RepositoryCommit{
169+
Commit: &Commit{
170+
Author: &CommitAuthor{Name: String("n")},
171+
},
172+
Author: &User{Login: String("l")},
173+
Committer: &User{Login: String("l")},
174+
Message: String("m"),
175+
},
176+
Commits: []RepositoryCommit{
177+
{
178+
SHA: String("s"),
179+
},
180+
},
181+
Files: []CommitFile{
182+
{
183+
Filename: String("f"),
184+
},
185+
},
186+
}
187+
188+
if reflect.DeepEqual(got, want) {
189+
t.Errorf("Repositories.CompareCommits returned \n%+v, want \n%+v", got, want)
190+
}
191+
}

‎github/strings_test.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ func TestString(t *testing.T) {
108108
{PullRequestComment{ID: Int(1)}, `github.PullRequestComment{ID:1}`},
109109
{Repository{ID: Int(1)}, `github.Repository{ID:1}`},
110110
{RepositoryComment{ID: Int(1)}, `github.RepositoryComment{ID:1}`},
111+
{RepositoryCommit{SHA: String("s")}, `github.RepositoryCommit{SHA:"s"}`},
112+
{CommitStats{Total: Int(1)}, `github.CommitStats{Total:1}`},
113+
{CommitFile{SHA: String("s")}, `github.CommitFile{SHA:"s"}`},
114+
{CommitsComparison{TotalCommits: Int(1)}, `github.CommitsComparison{TotalCommits:1}`},
111115
{WebHookPayload{Ref: String("r")}, `github.WebHookPayload{Ref:"r"}`},
112116
{WebHookCommit{ID: String("1")}, `github.WebHookCommit{ID:"1"}`},
113117
{WebHookAuthor{Name: String("n")}, `github.WebHookAuthor{Name:"n"}`},

0 commit comments

Comments
 (0)