Skip to content

Commit 6dcc781

Browse files
huydxhuydx
authored and
huydx
committed
Add function for PullRequestService to download raw content of pull request
1 parent dccec7b commit 6dcc781

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
*.test
2+
.idea

github/github.go

Lines changed: 15 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,19 @@ type UploadOptions struct {
154156
Name string `url:"name,omitempty"`
155157
}
156158

159+
// MediaRawType represents raw return of a request instead of json format
160+
type MediaRawType int
161+
162+
const (
163+
Diff MediaRawType = 1 + iota
164+
Patch
165+
)
166+
167+
// GetRawOptions specifies the optional parameter when user want to get raw presentation of a response instead of json format
168+
type GetRawOptions struct {
169+
Type MediaRawType
170+
}
171+
157172
// addOptions adds the parameters in opt as URL query parameters to s. opt
158173
// must be a struct whose fields may contain "url" tags.
159174
func addOptions(s string, opt interface{}) (string, error) {

github/pulls.go

Lines changed: 24 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,29 @@ 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 GetRawOptions) ([]byte, *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 []byte{}, nil, err
144+
}
145+
146+
if opt.Type == Diff {
147+
req.Header.Set("Accept", mediaTypeV3Diff)
148+
} else if opt.Type == Patch {
149+
req.Header.Set("Accept", mediaTypeV3Patch)
150+
}
151+
152+
ret := new(bytes.Buffer)
153+
resp, err := s.client.Do(req, ret)
154+
if err != nil {
155+
return []byte{}, resp, err
156+
}
157+
158+
return ret.Bytes(), resp, err
159+
}
160+
137161
// NewPullRequest represents a new pull request to be created.
138162
type NewPullRequest struct {
139163
Title *string `json:"title,omitempty"`

github/pulls_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,48 @@ func TestPullRequestsService_Get(t *testing.T) {
6767
}
6868
}
6969

70+
func TestPullRequestService_GetRawPatch(t *testing.T) {
71+
setup()
72+
defer teardown()
73+
var rawStr = "@@patch content"
74+
75+
mux.HandleFunc("/repos/o/r/pulls/1", func(w http.ResponseWriter, r *http.Request) {
76+
testMethod(t, r, "GET")
77+
testHeader(t, r, "Accept", mediaTypeV3Patch)
78+
fmt.Fprint(w, rawStr)
79+
})
80+
81+
ret, _, err := client.PullRequests.GetRaw("o", "r", 1, GetRawOptions{Patch})
82+
if err != nil {
83+
t.Errorf("PullRequests.GetRaw return error: %v", err)
84+
}
85+
86+
if string(ret) != rawStr {
87+
t.Errorf("PullRequests.GetRaw returned %s want %s", ret, rawStr)
88+
}
89+
}
90+
91+
func TestPullRequestService_GetRawDiff(t *testing.T) {
92+
setup()
93+
defer teardown()
94+
var rawStr = "@@diff content"
95+
96+
mux.HandleFunc("/repos/o/r/pulls/1", func(w http.ResponseWriter, r *http.Request) {
97+
testMethod(t, r, "GET")
98+
testHeader(t, r, "Accept", mediaTypeV3Diff)
99+
fmt.Fprint(w, rawStr)
100+
})
101+
102+
ret, _, err := client.PullRequests.GetRaw("o", "r", 1, GetRawOptions{Diff})
103+
if err != nil {
104+
t.Errorf("PullRequests.GetRaw return error: %v", err)
105+
}
106+
107+
if string(ret) != rawStr {
108+
t.Errorf("PullRequests.GetRaw returned %s want %s", ret, rawStr)
109+
}
110+
}
111+
70112
func TestPullRequestsService_Get_headAndBase(t *testing.T) {
71113
setup()
72114
defer teardown()

0 commit comments

Comments
 (0)