From 09300aa5e26f11732598dbc383ac7e75562ed46d Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Sun, 29 Oct 2017 01:32:37 -0400 Subject: [PATCH] Add RepositoriesService.GetCommitRaw method. Follows the same high-level pattern as set out in PR #481 with PullRequestsService.GetRaw method. Improve documentation wording. Improve style of PullRequestsService.GetRaw and its tests to be more consistent. Fix minor typo in GitHub documentation URL. --- github/pulls.go | 8 ++--- github/pulls_test.go | 24 ++++++++------- github/repos_commits.go | 29 ++++++++++++++++-- github/repos_commits_test.go | 58 ++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 17 deletions(-) diff --git a/github/pulls.go b/github/pulls.go index bc46081fca6..6681c8b21b0 100644 --- a/github/pulls.go +++ b/github/pulls.go @@ -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) @@ -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. diff --git a/github/pulls_test.go b/github/pulls_test.go index ea31e521651..89adf239463 100644 --- a/github/pulls_test.go +++ b/github/pulls_test.go @@ -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) { @@ -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) { @@ -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() diff --git a/github/repos_commits.go b/github/repos_commits.go index 4451b6bb465..0484737342c 100644 --- a/github/repos_commits.go +++ b/github/repos_commits.go @@ -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) @@ -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. // diff --git a/github/repos_commits_test.go b/github/repos_commits_test.go index 98eadabb3fd..1d5f5ec936a 100644 --- a/github/repos_commits_test.go +++ b/github/repos_commits_test.go @@ -10,6 +10,7 @@ import ( "fmt" "net/http" "reflect" + "strings" "testing" "time" ) @@ -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()