-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.go
More file actions
433 lines (347 loc) · 10.1 KB
/
github.go
File metadata and controls
433 lines (347 loc) · 10.1 KB
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
)
/**
* Github implementation for the generic repository
*
* Copyright (C) 2018 Arthur M
*/
/* Structures that represent the used fields from the JSON returned by the
* Github API.
*
* TGitHubOwner is the repository owner information
* TGitHubRepo is the repository information
*/
type TGitHubUser struct {
Login string
ID uint
}
type TGitHubRepo struct {
ID uint
Name string
Full_name string
Owner TGitHubUser
Description string
Issues_url string
Issue_comment_url string
Has_issues bool
}
type TGitHubIssueLabel struct {
ID uint
Name string
Color string
}
type TGitHubIssue struct {
ID uint
Number uint
Title string
User TGitHubUser
Assignees []TGitHubUser
Html_url string
State string
Created_at time.Time
Body string
Labels []TGitHubIssueLabel
}
type TGitHubIssueComment struct {
ID uint
Html_url string
Body string
User TGitHubUser
Created_at time.Time
}
func (gh *TGitHubRepo) Initialize(auth *TAuthentication, repo *TRepository) (string, error) {
// We need to get the api URL from the repository URL
// URL is https://github.com/arthurmco/clinancial
// API url is https://api.github.com/repos/arthurmco/clinancial
username := repo.author
reponame := repo.name
api_url := "https://api.github.com/repos/" + username + "/" + reponame
// Now we need to download this.
resp, err := gh.buildGetRequest(api_url, auth, "")
if err != nil {
return "", err
}
if resp.StatusCode == 404 {
return "", &RepoConnectError{"Repository not found!", 404}
}
if resp.StatusCode == 403 {
if resp.Header.Get("X-RateLimit-Remaining") == "0" {
return "", &RepoConnectError{"Github API rate limit exceeded", 403}
} else {
return "", &RepoConnectError{"Permission error!", 403}
}
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(body, gh)
if err != nil {
return "", err
}
repo.name = gh.Name
repo.desc = gh.Description
return api_url, nil
}
/* Build and send a common GET request to the API.
* 'params' is an "HTTP GET"-like parameter string, without the '?'
* Return the response object on success, or an error.
*/
func (gh *TGitHubRepo) buildGetRequest(url string, auth *TAuthentication, params string) (*http.Response, error) {
client := http.Client{}
// Replace spaces with HTTP-allowed spaces and + with HTTP-blessed ones
params = strings.Replace(params, " ", "%20", -1)
params = strings.Replace(params, "%", "%2B", -1)
// Build the request, and then do it
req, err := http.NewRequest("GET", url+"?per_page=100&"+params, nil)
if err != nil {
return nil, err
}
// Only send authorization data when we have an username
if auth != nil && auth.username != "" {
req.SetBasicAuth(auth.username, auth.password)
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode == 401 {
return nil, &RepoConnectError{"Authentication failed: wrong username and/or password", 401}
}
return resp, nil
}
/* Download a range of issues based on a certain filter, return the amount of what are really issues
* Because Github API doesn't differentiate issues from pull requests, this is needed
*
* It fills the 'issuelist' with the found issues
*
* The maximum allowed by the API is 100, and it downloads 100 by 199, so count+start needs to be less
* than 100.
*/
func (gh *TGitHubRepo) downloadIssueRange(start, count, page int,
auth *TAuthentication, url, params string,
issuelist *[]TGitHubIssue) (int, error) {
params = params + "&page=" + strconv.Itoa(page)
resp, err := gh.buildGetRequest(url, auth, params)
if err != nil {
return 0, err
}
// Read the result and build the JSON
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, err
}
var ghissues []TGitHubIssue
err = json.Unmarshal(body, &ghissues)
if len(ghissues) < start {
// No issues to return
return 0, nil
}
// TODO: Differentiate issues from pull requests
count = len(ghissues)
icount := 0
for _, iss := range ghissues[start:(count - start)] {
icount += 1
*issuelist = append(*issuelist, iss)
}
return icount, nil
}
/*
* Download all issues from this github repository
* You can use the TAuthentication struct to pass authentication info
* Send it nil for no authentication, but take note that the host
* might not send everything to unauthenticated users
*
* Return nil on the issue list and on the error if no issues exist.
* Return an issue list on success, or nil on issue list and an error
* on error
*/
func (gh *TGitHubRepo) DownloadAllIssues(auth *TAuthentication, filter TIssueFilter) ([]TIssue, error) {
if !gh.Has_issues {
// Return a nil list, since this repository doesn't has issues
// Return no errors too, since no error has been found
return nil, nil
}
issue_url := strings.Replace(gh.Issues_url, "{/number}", "", 1)
// Build filters
paramstr := make([]string, 0, 4)
if filter.labels != nil {
// Build label parameter filter
// They need to be comma-separated
labelarr := make([]string, 0)
for _, l := range *filter.labels {
labelarr = append(labelarr, l.name)
}
paramstr = append(paramstr, "labels="+strings.Join(
labelarr, ","))
}
if filter.assignee != nil {
paramstr = append(paramstr, "assignee="+*filter.assignee)
}
if filter.getOpen && filter.getClosed {
paramstr = append(paramstr, "state=all")
} else if !filter.getOpen && filter.getClosed {
paramstr = append(paramstr, "state=closed")
} else {
paramstr = append(paramstr, "state=open")
}
if filter.creator != nil {
paramstr = append(paramstr, "creator="+*filter.creator)
}
var ghissues []TGitHubIssue
imin, imax := 0, 1000
icurr := imin
ioff := icurr
pagen := 1
const pagecount = 100
params := strings.Join(paramstr, "&")
for icurr < imax {
var pageissues []TGitHubIssue
iret, err := gh.downloadIssueRange(ioff, pagecount, pagen, auth,
issue_url, params, &pageissues)
if err != nil {
return nil, err
}
// No pages
if iret == 0 {
break
}
ghissues = append(ghissues, pageissues...)
// Didn't reached the page count
if iret < pagecount {
break
}
pagen += 1
icurr += pagecount
ioff = 0
}
issues := make([]TIssue, len(ghissues))
for idx, ghissue := range ghissues {
issues[idx].id = ghissue.ID
issues[idx].number = ghissue.Number
issues[idx].name = ghissue.Title
issues[idx].url = ghissue.Html_url
issues[idx].author = ghissue.User.Login
assignees := make([]string, 0)
for _, assignee := range ghissue.Assignees {
assignees = append(assignees, assignee.Login)
}
issues[idx].assignees = assignees
labels := make([]TIssueLabel, 0)
for _, ghlabel := range ghissue.Labels {
colorR, _ := strconv.ParseUint(ghlabel.Color[0:2], 16, 8)
colorG, _ := strconv.ParseUint(ghlabel.Color[2:4], 16, 8)
colorB, _ := strconv.ParseUint(ghlabel.Color[4:6], 16, 8)
labels = append(labels, TIssueLabel{
name: ghlabel.Name,
colorR: uint8(colorR),
colorG: uint8(colorG),
colorB: uint8(colorB),
})
}
issues[idx].labels = labels
issues[idx].creation = ghissue.Created_at
issues[idx].content = ghissue.Body
issues[idx].is_closed = (ghissue.State == "closed")
}
return issues, nil
}
/*
* Download an specific issue from this github repository
*/
func (gh *TGitHubRepo) DownloadIssue(auth *TAuthentication, id uint) (*TIssue, error) {
if !gh.Has_issues {
// Return a nil list, since this repository doesn't has issues
// Return no errors too, since no error has been found
return nil, nil
}
issue_url := strings.Replace(gh.Issues_url, "{/number}",
"/"+strconv.Itoa(int(id)), 1)
resp, err := gh.buildGetRequest(issue_url, auth, "")
if err != nil {
return nil, err
}
/* No issues found. This isn't an error per se, only mean that this repo
* doesn't have any issues
*/
if resp.StatusCode == 404 {
return nil, nil
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
var ghissue TGitHubIssue
err = json.Unmarshal(body, &ghissue)
if err != nil {
return nil, err
}
issue := new(TIssue)
issue.id = ghissue.ID
issue.number = ghissue.Number
issue.name = ghissue.Title
issue.url = ghissue.Html_url
issue.author = ghissue.User.Login
assignees := make([]string, 0)
for _, assignee := range ghissue.Assignees {
assignees = append(assignees, assignee.Login)
}
issue.assignees = assignees
labels := make([]TIssueLabel, 0)
for _, ghlabel := range ghissue.Labels {
colorR, _ := strconv.ParseUint(ghlabel.Color[0:2], 16, 8)
colorG, _ := strconv.ParseUint(ghlabel.Color[2:4], 16, 8)
colorB, _ := strconv.ParseUint(ghlabel.Color[4:6], 16, 8)
labels = append(labels, TIssueLabel{
name: ghlabel.Name,
colorR: uint8(colorR),
colorG: uint8(colorG),
colorB: uint8(colorB),
})
}
issue.labels = labels
issue.creation = ghissue.Created_at
issue.content = ghissue.Body
issue.is_closed = (ghissue.State == "closed")
return issue, nil
}
/* Download all comments from that issue */
func (gh *TGitHubRepo) DownloadIssueComments(auth *TAuthentication, issue_id uint) ([]TIssueComment, error) {
if !gh.Has_issues {
// Return a nil list, since this repository doesn't has issues
// Return no errors too, since no error has been found
return nil, nil
}
comment_url := strings.Replace(gh.Issues_url, "{/number}",
"/"+strconv.Itoa(int(issue_id))+"/comments", 1)
resp, err := gh.buildGetRequest(comment_url, auth, "")
if err != nil {
return nil, err
}
/* No issue comments. This isn't an error, only means that this issue
* doesn't have comments
*/
if resp.StatusCode == 404 {
return nil, nil
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
var ghcomments []TGitHubIssueComment
err = json.Unmarshal(body, &ghcomments)
if err != nil {
return nil, err
}
comments := make([]TIssueComment, len(ghcomments))
for idx, ghissue := range ghcomments {
comments[idx].id = ghissue.ID
comments[idx].url = ghissue.Html_url
comments[idx].author = ghissue.User.Login
comments[idx].creation = ghissue.Created_at
comments[idx].content = ghissue.Body
}
return comments, nil
}