Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ListOptions to GetCommit and CompareCommits to support pagination #1960

Merged
merged 8 commits into from
Jul 13, 2021
Merged
6 changes: 5 additions & 1 deletion example/commitpr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ func getFileContent(fileArg string) (targetName string, b []byte, err error) {
// pushCommit creates the commit in the given reference using the given tree.
func pushCommit(ref *github.Reference, tree *github.Tree) (err error) {
// Get the parent commit to attach the commit to.
parent, _, err := client.Repositories.GetCommit(ctx, *sourceOwner, *sourceRepo, *ref.Object.SHA)
opt := &github.ListOptions{
PerPage: 10,
Page: 1,
}
parent, _, err := client.Repositories.GetCommit(ctx, *sourceOwner, *sourceRepo, *ref.Object.SHA, opt)
Saaarah marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
Expand Down
12 changes: 10 additions & 2 deletions github/repos_commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,12 @@ func (s *RepositoriesService) ListCommits(ctx context.Context, owner, repo strin
//
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#get-a-single-commit
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#get-a-commit
func (s *RepositoriesService) GetCommit(ctx context.Context, owner, repo, sha string) (*RepositoryCommit, *Response, error) {
func (s *RepositoriesService) GetCommit(ctx context.Context, owner, repo, sha string, opts *ListOptions) (*RepositoryCommit, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/commits/%v", owner, repo, sha)
u, err := addOptions(u, opts)
if err != nil {
return nil, nil, err
}

req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
Expand Down Expand Up @@ -224,11 +228,15 @@ func (s *RepositoriesService) GetCommitSHA1(ctx context.Context, owner, repo, re
// CompareCommits compares a range of commits with each other.
//
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#compare-two-commits
func (s *RepositoriesService) CompareCommits(ctx context.Context, owner, repo string, base, head string) (*CommitsComparison, *Response, error) {
func (s *RepositoriesService) CompareCommits(ctx context.Context, owner, repo string, base, head string, opts *ListOptions) (*CommitsComparison, *Response, error) {
escapedBase := url.QueryEscape(base)
escapedHead := url.QueryEscape(head)

u := fmt.Sprintf("repos/%v/%v/compare/%v...%v", owner, repo, escapedBase, escapedHead)
u, err := addOptions(u, opts)
if err != nil {
return nil, nil, err
}
Saaarah marked this conversation as resolved.
Show resolved Hide resolved

req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
Expand Down
16 changes: 10 additions & 6 deletions github/repos_commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func TestRepositoriesService_GetCommit(t *testing.T) {

mux.HandleFunc("/repos/o/r/commits/s", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{"per_page": "2", "page": "2"})
fmt.Fprintf(w, `{
"sha": "s",
"commit": { "message": "m" },
Expand All @@ -97,8 +98,9 @@ func TestRepositoriesService_GetCommit(t *testing.T) {
}`)
})

opts := &ListOptions{Page: 2, PerPage: 2}
ctx := context.Background()
commit, _, err := client.Repositories.GetCommit(ctx, "o", "r", "s")
commit, _, err := client.Repositories.GetCommit(ctx, "o", "r", "s", opts)
if err != nil {
t.Errorf("Repositories.GetCommit returned error: %v", err)
}
Expand Down Expand Up @@ -144,12 +146,12 @@ func TestRepositoriesService_GetCommit(t *testing.T) {

const methodName = "GetCommit"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Repositories.GetCommit(ctx, "\n", "\n", "\n")
_, _, err = client.Repositories.GetCommit(ctx, "\n", "\n", "\n", opts)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Repositories.GetCommit(ctx, "o", "r", "s")
got, resp, err := client.Repositories.GetCommit(ctx, "o", "r", "s", opts)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
Expand Down Expand Up @@ -389,6 +391,7 @@ func TestRepositoriesService_CompareCommits(t *testing.T) {

mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{"per_page": "2", "page": "2"})
fmt.Fprintf(w, `{
"base_commit": {
"sha": "s",
Expand Down Expand Up @@ -424,8 +427,9 @@ func TestRepositoriesService_CompareCommits(t *testing.T) {
}`, escapedBase, escapedHead)
})

opts := &ListOptions{Page: 2, PerPage: 2}
ctx := context.Background()
got, _, err := client.Repositories.CompareCommits(ctx, "o", "r", base, head)
got, _, err := client.Repositories.CompareCommits(ctx, "o", "r", base, head, opts)
if err != nil {
t.Errorf("Repositories.CompareCommits returned error: %v", err)
}
Expand Down Expand Up @@ -484,12 +488,12 @@ func TestRepositoriesService_CompareCommits(t *testing.T) {

const methodName = "CompareCommits"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Repositories.CompareCommits(ctx, "\n", "\n", "\n", "\n")
_, _, err = client.Repositories.CompareCommits(ctx, "\n", "\n", "\n", "\n", opts)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Repositories.CompareCommits(ctx, "o", "r", base, head)
got, resp, err := client.Repositories.CompareCommits(ctx, "o", "r", base, head, opts)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
Expand Down