Skip to content

Commit 941585a

Browse files
huydxdmitshur
authored andcommitted
Add PullRequests.GetRaw to download raw content of pull request.
Closes google#481. Updates google#6.
1 parent d52c8c4 commit 941585a

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

github/github.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ const (
4242
mediaTypeV3 = "application/vnd.github.v3+json"
4343
defaultMediaType = "application/octet-stream"
4444
mediaTypeV3SHA = "application/vnd.github.v3.sha"
45+
mediaTypeV3Diff = "application/vnd.github.v3.diff"
46+
mediaTypeV3Patch = "application/vnd.github.v3.patch"
4547
mediaTypeOrgPermissionRepo = "application/vnd.github.v3.repository+json"
4648

4749
// Media Type values to access preview APIs
@@ -154,6 +156,22 @@ type UploadOptions struct {
154156
Name string `url:"name,omitempty"`
155157
}
156158

159+
// RawType represents type of raw format of a request instead of JSON.
160+
type RawType uint8
161+
162+
const (
163+
// Diff format.
164+
Diff RawType = 1 + iota
165+
// Patch format.
166+
Patch
167+
)
168+
169+
// RawOptions specifies parameters when user wants to get raw format of
170+
// a response instead of JSON.
171+
type RawOptions struct {
172+
Type RawType
173+
}
174+
157175
// addOptions adds the parameters in opt as URL query parameters to s. opt
158176
// must be a struct whose fields may contain "url" tags.
159177
func addOptions(s string, opt interface{}) (string, error) {

github/pulls.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package github
77

88
import (
9+
"bytes"
910
"fmt"
1011
"time"
1112
)
@@ -134,6 +135,32 @@ func (s *PullRequestsService) Get(owner string, repo string, number int) (*PullR
134135
return pull, resp, err
135136
}
136137

138+
// GetRaw gets raw (diff or patch) format of a pull request.
139+
func (s *PullRequestsService) GetRaw(owner string, repo string, number int, opt RawOptions) (string, *Response, error) {
140+
u := fmt.Sprintf("repos/%v/%v/pulls/%d", owner, repo, number)
141+
req, err := s.client.NewRequest("GET", u, nil)
142+
if err != nil {
143+
return "", nil, err
144+
}
145+
146+
switch opt.Type {
147+
case Diff:
148+
req.Header.Set("Accept", mediaTypeV3Diff)
149+
case Patch:
150+
req.Header.Set("Accept", mediaTypeV3Patch)
151+
default:
152+
return "", nil, fmt.Errorf("unsupported raw type %d", opt.Type)
153+
}
154+
155+
ret := new(bytes.Buffer)
156+
resp, err := s.client.Do(req, ret)
157+
if err != nil {
158+
return "", resp, err
159+
}
160+
161+
return ret.String(), resp, err
162+
}
163+
137164
// NewPullRequest represents a new pull request to be created.
138165
type NewPullRequest struct {
139166
Title *string `json:"title,omitempty"`

github/pulls_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"net/http"
1212
"reflect"
13+
"strings"
1314
"testing"
1415
)
1516

@@ -67,6 +68,61 @@ func TestPullRequestsService_Get(t *testing.T) {
6768
}
6869
}
6970

71+
func TestPullRequestService_GetRawDiff(t *testing.T) {
72+
setup()
73+
defer teardown()
74+
const rawStr = "@@diff content"
75+
76+
mux.HandleFunc("/repos/o/r/pulls/1", func(w http.ResponseWriter, r *http.Request) {
77+
testMethod(t, r, "GET")
78+
testHeader(t, r, "Accept", mediaTypeV3Diff)
79+
fmt.Fprint(w, rawStr)
80+
})
81+
82+
ret, _, err := client.PullRequests.GetRaw("o", "r", 1, RawOptions{Diff})
83+
if err != nil {
84+
t.Fatalf("PullRequests.GetRaw returned error: %v", err)
85+
}
86+
87+
if ret != rawStr {
88+
t.Errorf("PullRequests.GetRaw returned %s want %s", ret, rawStr)
89+
}
90+
}
91+
92+
func TestPullRequestService_GetRawPatch(t *testing.T) {
93+
setup()
94+
defer teardown()
95+
const rawStr = "@@patch content"
96+
97+
mux.HandleFunc("/repos/o/r/pulls/1", func(w http.ResponseWriter, r *http.Request) {
98+
testMethod(t, r, "GET")
99+
testHeader(t, r, "Accept", mediaTypeV3Patch)
100+
fmt.Fprint(w, rawStr)
101+
})
102+
103+
ret, _, err := client.PullRequests.GetRaw("o", "r", 1, RawOptions{Patch})
104+
if err != nil {
105+
t.Fatalf("PullRequests.GetRaw returned error: %v", err)
106+
}
107+
108+
if ret != rawStr {
109+
t.Errorf("PullRequests.GetRaw returned %s want %s", ret, rawStr)
110+
}
111+
}
112+
113+
func TestPullRequestService_GetRawInvalid(t *testing.T) {
114+
setup()
115+
defer teardown()
116+
117+
_, _, err := client.PullRequests.GetRaw("o", "r", 1, RawOptions{100})
118+
if err == nil {
119+
t.Fatal("PullRequests.GetRaw should return error")
120+
}
121+
if !strings.Contains(err.Error(), "unsupported raw type") {
122+
t.Error("PullRequests.GetRaw should return unsupported raw type error")
123+
}
124+
}
125+
70126
func TestPullRequestsService_Get_headAndBase(t *testing.T) {
71127
setup()
72128
defer teardown()

0 commit comments

Comments
 (0)