/
gohub.go
352 lines (292 loc) · 8.43 KB
/
gohub.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package gohub
import (
"json"
"fmt"
"io"
"io/ioutil"
"http"
"time"
"os"
"url"
"strings"
)
const GH_API_ROOT = "https://api.github.com"
type GoHub struct {
user string
password string
client *http.Client
apiHost string
}
type Timestamp time.Time
type NullableString string
type Link struct {
Href string `json:"href"`
}
type Links struct {
Self Link `json:"self"`
Comments Link `json:"comments"`
ReviewComments Link `json:"comments"`
Html Link `json:"html"`
}
type PullRequests struct {
Url NullableString `json:"url"`
HtmlUrl NullableString `json:"html_url"`
DiffUrl NullableString `json:"diff_url"`
PatchUrl NullableString `json:"patch_url"`
IssueUrl NullableString `json:"issue_url"`
Number int `json:"number"`
State NullableString `json:"state"`
Title NullableString `json:"title"`
Body NullableString `json:"body"`
CreatedAt Timestamp `json:"created_at"`
UpdatedAt Timestamp `json:"updated_at"`
Links Links `json:"_links"`
g *GoHub
}
type PullRequest struct {
Url NullableString `json:"url"`
HtmlUrl NullableString `json:"html_url"`
DiffUrl NullableString `json:"diff_url"`
PatchUrl NullableString `json:"patch_url"`
IssueUrl NullableString `json:"issue_url"`
Number int `json:"number"`
State NullableString `json:"state"`
Title NullableString `json:"title"`
Body NullableString `json:"body"`
CreatedAt Timestamp `json:"created_at"`
UpdatedAt Timestamp `json:"updated_at"`
Merged bool `json:"merged"`
IsMergeable bool `json:"mergeable"`
Head PullRequestMarker `json:"head"`
Base PullRequestMarker `json:"base"`
Links Links `json:"_links"`
g *GoHub
}
type PullRequestMarker struct {
Label NullableString `json:"label"`
Ref NullableString `json:"ref"`
Sha NullableString `json:"sha"`
Repo Repository `json:"repo"`
User User `json:"user"`
}
type Repository struct {
Url NullableString `json:"url"`
HtmlUrl NullableString `json:"html_url"`
CloneUrl NullableString `json:"clone_url"`
GitUrl NullableString `json:"git_url"`
SshUrl NullableString `json:"ssh_url"`
SvnUrl NullableString `json:"svn_url"`
Owner User `json:"owner"`
Name NullableString `json:"name"`
Description NullableString `json:"description"`
Homepage NullableString `json:"homepage"`
Language NullableString `json:"language"`
Private bool `json:"private"`
Fork bool `json:"fork"`
Forks int `json:"forks"`
Watchers int `json:"watchers"`
Size int `json:"size"`
MasterBranch NullableString `json:"master_branch"`
OpenIssues int `json:"open_issues"`
PushedAt Timestamp `json:"pushed_at"`
CreatedAt Timestamp `json:"created_at"`
}
type User struct {
Login string `json:"login"`
Id int `json:"id"`
AvatarUrl string `json:"avatar_url"`
Url string `json:"url"`
}
type PullRequestMergeResponse struct {
Sha NullableString `json:"sha"`
Merged bool `json:"merged"`
Message string `json:"message"`
}
type Comment struct {
CreatedAt Timestamp `json:"created_at"`
UpdatedAt Timestamp `json:"updated_at"`
User User `json:"user"`
CommitId string `json:"commit_id"`
Position int `json:"position"`
Body string `json:"body"`
}
type MergeMessage struct {
CommitMessage string `json:"commit_message"`
}
func New(user, password, api_root string) *GoHub {
if api_root == "" {
return &GoHub{user, password, &http.Client{}, GH_API_ROOT}
}
return &GoHub{user, password, &http.Client{}, api_root}
}
func (g *GoHub) makeAuthRequest(method, url_ string, body io.Reader) (*http.Request, os.Error) {
req, err := http.NewRequest(method, url_, body)
req.Header.Set("User-Agent", "GoHub http://github.com/supr/gohub")
if err != nil {
return nil, err
}
if g.user != "" && g.password != "" {
req.SetBasicAuth(g.user, g.password)
}
return req, nil
}
func (g *GoHub) makeGetRequest(url_ string, params url.Values) ([]byte, os.Error) {
req, err := g.makeAuthRequest("GET", url_, strings.NewReader(params.Encode()))
if err != nil {
return nil, err
}
resp, err := g.client.Do(req)
if err != nil {
return nil, err
}
outbuf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return outbuf, err
}
func (g *GoHub) makePutRequest(url_ string, body io.Reader) ([]byte, os.Error) {
req, err := g.makeAuthRequest("PUT", url_, body)
if err != nil {
return nil, err
}
resp, err := g.client.Do(req)
if err != nil {
return nil, err
}
outbuf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return outbuf, err
}
func (g *GoHub) makePostRequest(url_ string, body io.Reader) ([]byte, os.Error) {
req, err := g.makeAuthRequest("POST", url_, body)
if err != nil {
return nil, err
}
resp, err := g.client.Do(req)
if err != nil {
return nil, err
}
outbuf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return outbuf, err
}
func (g *GoHub) PullRequest(user, repo string, id int) (*PullRequest, os.Error) {
url_ := fmt.Sprintf("%v/repos/%v/%v/pulls/%v", g.apiHost, user, repo, id)
out, err := g.makeGetRequest(url_, nil)
if err != nil {
return nil, err
}
var pr PullRequest
err = json.Unmarshal(out, &pr)
if err != nil {
return nil, err
}
pr.g = g
return &pr, nil
}
func (g *GoHub) PullRequests(user, repo string) ([]*PullRequests, os.Error) {
url_ := fmt.Sprintf("%v/repos/%v/%v/pulls", g.apiHost, user, repo)
out, err := g.makeGetRequest(url_, nil)
if err != nil {
return nil, err
}
var prs []*PullRequests
err = json.Unmarshal(out, &prs)
if err != nil {
return nil, err
}
for _, v := range prs {
v.g = g
}
return prs, nil
}
func (p *PullRequest) Merge(commit_message string) (*PullRequestMergeResponse, os.Error) {
url_ := fmt.Sprintf("%v/repos/%v/%v/pulls/%v/merge", p.g.apiHost, p.Base.Repo.Owner.Login, p.Base.Repo.Name, p.Number)
z := MergeMessage{commit_message}
buf, _ := json.Marshal(z)
out, err := p.g.makePutRequest(url_, strings.NewReader(string(buf)))
if err != nil {
return nil, err
}
var pullRequestMergeResponse PullRequestMergeResponse
err = json.Unmarshal(out, &pullRequestMergeResponse)
if err != nil {
return nil, err
}
if !pullRequestMergeResponse.Merged {
return nil, os.NewError(pullRequestMergeResponse.Message)
}
return &pullRequestMergeResponse, nil
}
func (p *PullRequest) Comments() ([]Comment, os.Error) {
url_ := fmt.Sprintf("%v/repos/%v/%v/pulls/%v/comments", p.g.apiHost, p.Base.Repo.Owner.Login, p.Base.Repo.Name, p.Number)
out, err := p.g.makeGetRequest(url_, nil)
if err != nil {
return nil, err
}
var comments []Comment
err = json.Unmarshal(out, &comments)
if err != nil {
return nil, err
}
return comments, nil
}
func (p *PullRequest) IssueComments() ([]Comment, os.Error) {
url_ := fmt.Sprintf("%v/repos/%v/%v/issues/%v/comments", p.g.apiHost, p.Base.Repo.Owner.Login, p.Base.Repo.Name, p.Number)
out, err := p.g.makeGetRequest(url_, nil)
if err != nil {
return nil, err
}
var comments []Comment
err = json.Unmarshal(out, &comments)
if err != nil {
return nil, err
}
return comments, nil
}
func (p *PullRequest) NewIssueComment(body string) (*Comment, os.Error) {
url_ := fmt.Sprintf("%v/repos/%v/%v/issues/%v/comments", p.g.apiHost, p.Head.Repo.Name, p.Head.Repo.Owner.Login, p.Number)
params := url.Values{}
params.Add("body", body)
out, err := p.g.makePostRequest(url_, strings.NewReader(params.Encode()))
if err != nil {
return nil, err
}
var comment Comment
err = json.Unmarshal(out, &comment)
if err != nil {
return nil, err
}
return &comment, nil
}
func (ts *Timestamp) UnmarshalJSON(data []byte) os.Error {
if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' {
//fail Gracefully
return os.NewError("cannot un-marshal non-string to timestamp")
}
data = data[1 : len(data)-1]
t, err := time.Parse(time.RFC3339, string(data))
if err != nil {
return err
}
*ts = Timestamp(*t)
return nil
}
func (s *NullableString) UnmarshalJSON(data []byte) os.Error {
if len(data) < 2 || string(data) == "null" {
*s = ""
return nil
}
var out string
err := json.Unmarshal(data, &out)
if err != nil {
return err
}
*s = NullableString(out)
return nil
}