Skip to content

Add RepositoriesService.GetCommitRaw method. #767

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

Merged
merged 1 commit into from
Oct 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions github/pulls.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (s *PullRequestsService) Get(ctx context.Context, owner string, repo string
return pull, resp, nil
}

// GetRaw gets raw (diff or patch) format of a pull request.
// GetRaw gets a single pull request in raw (diff or patch) format.
func (s *PullRequestsService) GetRaw(ctx context.Context, owner string, repo string, number int, opt RawOptions) (string, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls/%d", owner, repo, number)
req, err := s.client.NewRequest("GET", u, nil)
Expand All @@ -155,13 +155,13 @@ func (s *PullRequestsService) GetRaw(ctx context.Context, owner string, repo str
return "", nil, fmt.Errorf("unsupported raw type %d", opt.Type)
}

ret := new(bytes.Buffer)
resp, err := s.client.Do(ctx, req, ret)
var buf bytes.Buffer
resp, err := s.client.Do(ctx, req, &buf)
if err != nil {
return "", resp, err
}

return ret.String(), resp, nil
return buf.String(), resp, nil
}

// NewPullRequest represents a new pull request to be created.
Expand Down
24 changes: 13 additions & 11 deletions github/pulls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ func TestPullRequestsService_Get(t *testing.T) {
}
}

func TestPullRequestsService_GetRawDiff(t *testing.T) {
func TestPullRequestsService_GetRaw_diff(t *testing.T) {
setup()
defer teardown()

const rawStr = "@@diff content"

mux.HandleFunc("/repos/o/r/pulls/1", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -81,19 +82,20 @@ func TestPullRequestsService_GetRawDiff(t *testing.T) {
fmt.Fprint(w, rawStr)
})

ret, _, err := client.PullRequests.GetRaw(context.Background(), "o", "r", 1, RawOptions{Diff})
got, _, err := client.PullRequests.GetRaw(context.Background(), "o", "r", 1, RawOptions{Diff})
if err != nil {
t.Fatalf("PullRequests.GetRaw returned error: %v", err)
}

if ret != rawStr {
t.Errorf("PullRequests.GetRaw returned %s want %s", ret, rawStr)
want := rawStr
if got != want {
t.Errorf("PullRequests.GetRaw returned %s want %s", got, want)
}
}

func TestPullRequestsService_GetRawPatch(t *testing.T) {
func TestPullRequestsService_GetRaw_patch(t *testing.T) {
setup()
defer teardown()

const rawStr = "@@patch content"

mux.HandleFunc("/repos/o/r/pulls/1", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -102,17 +104,17 @@ func TestPullRequestsService_GetRawPatch(t *testing.T) {
fmt.Fprint(w, rawStr)
})

ret, _, err := client.PullRequests.GetRaw(context.Background(), "o", "r", 1, RawOptions{Patch})
got, _, err := client.PullRequests.GetRaw(context.Background(), "o", "r", 1, RawOptions{Patch})
if err != nil {
t.Fatalf("PullRequests.GetRaw returned error: %v", err)
}

if ret != rawStr {
t.Errorf("PullRequests.GetRaw returned %s want %s", ret, rawStr)
want := rawStr
if got != want {
t.Errorf("PullRequests.GetRaw returned %s want %s", got, want)
}
}

func TestPullRequestsService_GetRawInvalid(t *testing.T) {
func TestPullRequestsService_GetRaw_invalid(t *testing.T) {
setup()
defer teardown()

Expand Down
29 changes: 27 additions & 2 deletions github/repos_commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,9 @@ func (s *RepositoriesService) ListCommits(ctx context.Context, owner, repo strin
}

// GetCommit fetches the specified commit, including all details about it.
// todo: support media formats - https://github.com/google/go-github/issues/6
//
// GitHub API docs: https://developer.github.com/v3/repos/commits/#get-a-single-commit
// See also: https://developer.github.com//v3/git/commits/#get-a-single-commit provides the same functionality
// See also: https://developer.github.com/v3/git/commits/#get-a-single-commit provides the same functionality
func (s *RepositoriesService) GetCommit(ctx context.Context, owner, repo, sha string) (*RepositoryCommit, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/commits/%v", owner, repo, sha)

Expand All @@ -164,6 +163,32 @@ func (s *RepositoriesService) GetCommit(ctx context.Context, owner, repo, sha st
return commit, resp, nil
}

// GetCommitRaw fetches the specified commit in raw (diff or patch) format.
func (s *RepositoriesService) GetCommitRaw(ctx context.Context, owner string, repo string, sha string, opt RawOptions) (string, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/commits/%v", owner, repo, sha)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return "", nil, err
}

switch opt.Type {
case Diff:
req.Header.Set("Accept", mediaTypeV3Diff)
case Patch:
req.Header.Set("Accept", mediaTypeV3Patch)
default:
return "", nil, fmt.Errorf("unsupported raw type %d", opt.Type)
}

var buf bytes.Buffer
resp, err := s.client.Do(ctx, req, &buf)
if err != nil {
return "", resp, err
}

return buf.String(), resp, nil
}

// GetCommitSHA1 gets the SHA-1 of a commit reference. If a last-known SHA1 is
// supplied and no new commits have occurred, a 304 Unmodified response is returned.
//
Expand Down
58 changes: 58 additions & 0 deletions github/repos_commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"net/http"
"reflect"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -126,6 +127,63 @@ func TestRepositoriesService_GetCommit(t *testing.T) {
}
}

func TestRepositoriesService_GetCommitRaw_diff(t *testing.T) {
setup()
defer teardown()

const rawStr = "@@diff content"

mux.HandleFunc("/repos/o/r/commits/s", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", mediaTypeV3Diff)
fmt.Fprint(w, rawStr)
})

got, _, err := client.Repositories.GetCommitRaw(context.Background(), "o", "r", "s", RawOptions{Type: Diff})
if err != nil {
t.Fatalf("Repositories.GetCommitRaw returned error: %v", err)
}
want := rawStr
if got != want {
t.Errorf("Repositories.GetCommitRaw returned %s want %s", got, want)
}
}

func TestRepositoriesService_GetCommitRaw_patch(t *testing.T) {
setup()
defer teardown()

const rawStr = "@@patch content"

mux.HandleFunc("/repos/o/r/commits/s", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", mediaTypeV3Patch)
fmt.Fprint(w, rawStr)
})

got, _, err := client.Repositories.GetCommitRaw(context.Background(), "o", "r", "s", RawOptions{Type: Patch})
if err != nil {
t.Fatalf("Repositories.GetCommitRaw returned error: %v", err)
}
want := rawStr
if got != want {
t.Errorf("Repositories.GetCommitRaw returned %s want %s", got, want)
}
}

func TestRepositoriesService_GetCommitRaw_invalid(t *testing.T) {
setup()
defer teardown()

_, _, err := client.Repositories.GetCommitRaw(context.Background(), "o", "r", "s", RawOptions{100})
if err == nil {
t.Fatal("Repositories.GetCommitRaw should return error")
}
if !strings.Contains(err.Error(), "unsupported raw type") {
t.Error("Repositories.GetCommitRaw should return unsupported raw type error")
}
}

func TestRepositoriesService_GetCommitSHA1(t *testing.T) {
setup()
defer teardown()
Expand Down