-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add function for PullRequestService to download raw content of pull r… #481
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
package github | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"time" | ||
) | ||
|
@@ -134,6 +135,32 @@ func (s *PullRequestsService) Get(owner string, repo string, number int) (*PullR | |
return pull, resp, err | ||
} | ||
|
||
// GetRaw gets raw (diff or patch) format of a pull request. | ||
func (s *PullRequestsService) GetRaw(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) | ||
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) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two things here.
|
||
|
||
ret := new(bytes.Buffer) | ||
resp, err := s.client.Do(req, ret) | ||
if err != nil { | ||
return "", resp, err | ||
} | ||
|
||
return ret.String(), resp, err | ||
} | ||
|
||
// NewPullRequest represents a new pull request to be created. | ||
type NewPullRequest struct { | ||
Title *string `json:"title,omitempty"` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import ( | |
"fmt" | ||
"net/http" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
|
@@ -67,6 +68,60 @@ func TestPullRequestsService_Get(t *testing.T) { | |
} | ||
} | ||
|
||
func TestPullRequestService_GetRawPatch(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
const rawStr = "@@patch content" | ||
|
||
mux.HandleFunc("/repos/o/r/pulls/1", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
testHeader(t, r, "Accept", mediaTypeV3Patch) | ||
fmt.Fprint(w, rawStr) | ||
}) | ||
|
||
ret, _, err := client.PullRequests.GetRaw("o", "r", 1, RawOptions{Patch}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Client code will not be able to write They'd have to write On the one hand, keeping the Just making an observation here. Not asking to change anything. |
||
if err != nil { | ||
t.Errorf("PullRequests.GetRaw return error: %v", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here too. |
||
} | ||
|
||
if ret != rawStr { | ||
t.Errorf("PullRequests.GetRaw returned %s want %s", ret, rawStr) | ||
} | ||
} | ||
|
||
func TestPullRequestService_GetRawDiff(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
const rawStr = "@@diff content" | ||
|
||
mux.HandleFunc("/repos/o/r/pulls/1", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
testHeader(t, r, "Accept", mediaTypeV3Diff) | ||
fmt.Fprint(w, rawStr) | ||
}) | ||
|
||
ret, _, err := client.PullRequests.GetRaw("o", "r", 1, RawOptions{Diff}) | ||
if err != nil { | ||
t.Errorf("PullRequests.GetRaw return error: %v", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here too, this should be |
||
} | ||
|
||
if ret != rawStr { | ||
t.Errorf("PullRequests.GetRaw returned %s want %s", ret, rawStr) | ||
} | ||
} | ||
|
||
func TestPullRequestService_GetRawInvalid(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
_, _, err := client.PullRequests.GetRaw("o", "r", 1, RawOptions{100}) | ||
if err == nil { | ||
t.Error("PullRequests.GetRaw should return error") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If So, change |
||
} | ||
if !strings.Contains(err.Error(), "unsupported raw type") { | ||
t.Error("PullRequests.GetRaw should return unsupported raw type error") | ||
} | ||
} | ||
|
||
func TestPullRequestsService_Get_headAndBase(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This causes 2 golint issues: