-
Notifications
You must be signed in to change notification settings - Fork 4
/
github.go
59 lines (50 loc) · 1.29 KB
/
github.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
// Package github provides a GO API for the GitHub issue tracker.
// See https://developer.github.com/v3/search/#search-issues.
package github
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
)
const IssuesURL = "https://api.github.com/search/issues"
type IssuesSearchResult struct {
TotalCount int `json: "total_count"`
Items []*Issue
}
type Issue struct {
Number int
HTMLURL string `json:"html_url`
Title string
State string
User *User
CreatedAt time.Time `json:"created_at"`
Body string // In Markdown format
}
type User struct {
Login string
HTMLURL string `json:"html_url`
}
//SearchIssues queries the GitHub issue tracker
func SearchIssues(terms []string) (*IssuesSearchResult, error) {
q := url.QueryEscape(strings.Join(terms, " "))
resp, err := http.Get(IssuesURL + "?q=" + q)
if err != nil {
return nil, err
}
// We must close resp.Body on all execution paths.
// (Chapater 5 presents 'defer', which makes this simpler.)
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
return nil, fmt.Errorf("search query failed: %s", resp.Status)
}
var result IssuesSearchResult
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
resp.Body.Close()
return nil, err
}
resp.Body.Close()
return &result, nil
}