Skip to content

Commit 87673ef

Browse files
huydxhuydx
authored andcommitted
Add function for PullRequestService to download raw content of pull request
1 parent dccec7b commit 87673ef

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ const (
5252
// https://developer.github.com/changes/2014-12-09-new-attributes-for-stars-api/
5353
mediaTypeStarringPreview = "application/vnd.github.v3.star+json"
5454

55+
//https://developer.github.com/v3/media/
56+
mediaTypeDiff = "application/vnd.github.v3.diff"
57+
mediaTypePatch = "application/vnd.github.v3.patch"
58+
mediaTypeSha = "application/vnd.github.v3.sha"
59+
5560
// https://developer.github.com/changes/2015-11-11-protected-branches-api/
5661
mediaTypeProtectedBranchesPreview = "application/vnd.github.loki-preview+json"
5762

@@ -154,6 +159,17 @@ type UploadOptions struct {
154159
Name string `url:"name,omitempty"`
155160
}
156161

162+
type MediaRawType int
163+
164+
const (
165+
Diff MediaRawType = 1 + iota
166+
Patch
167+
)
168+
169+
type MediaRawTypeOption struct {
170+
Type MediaRawType
171+
}
172+
157173
// addOptions adds the parameters in opt as URL query parameters to s. opt
158174
// must be a struct whose fields may contain "url" tags.
159175
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
@@ -8,6 +8,7 @@ package github
88
import (
99
"fmt"
1010
"time"
11+
"bytes"
1112
)
1213

1314
// PullRequestsService handles communication with the pull request related
@@ -134,6 +135,29 @@ func (s *PullRequestsService) Get(owner string, repo string, number int) (*PullR
134135
return pull, resp, err
135136
}
136137

138+
// Get pull request raw format (diff or patch)
139+
func (s *PullRequestsService) GetRaw(owner string, repo string, number int, opt *MediaRawTypeOption) (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+
if opt.Type == Diff {
147+
req.Header.Set("Accept", mediaTypeDiff)
148+
} else if opt.Type == Patch {
149+
req.Header.Set("Accept", mediaTypePatch)
150+
}
151+
152+
ret := new(bytes.Buffer)
153+
resp, err := s.client.Do(req, ret)
154+
if err != nil {
155+
return "", resp, err
156+
}
157+
158+
return string(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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,50 @@ 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", mediaTypePatch)
78+
fmt.Fprint(w, rawStr)
79+
})
80+
81+
ret, _, err := client.PullRequests.GetRaw("o", "r", 1, &MediaRawTypeOption{Patch})
82+
if err != nil {
83+
t.Errorf("PullRequests.GetRaw return error: %v", err)
84+
}
85+
86+
if ret != rawStr {
87+
t.Errorf("PullRequests.GetRaw returned %s want %s", ret, rawStr)
88+
}
89+
}
90+
91+
92+
func TestPullRequestService_GetRawDiff(t *testing.T) {
93+
setup()
94+
defer teardown()
95+
var rawStr = "@@diff 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", mediaTypeDiff)
100+
fmt.Fprint(w, rawStr)
101+
})
102+
103+
ret, _, err := client.PullRequests.GetRaw("o", "r", 1, &MediaRawTypeOption{Diff})
104+
if err != nil {
105+
t.Errorf("PullRequests.GetRaw return error: %v", err)
106+
}
107+
108+
if ret != rawStr {
109+
t.Errorf("PullRequests.GetRaw returned %s want %s", ret, rawStr)
110+
}
111+
}
112+
113+
70114
func TestPullRequestsService_Get_headAndBase(t *testing.T) {
71115
setup()
72116
defer teardown()

0 commit comments

Comments
 (0)