-
Notifications
You must be signed in to change notification settings - Fork 175
/
search.go
129 lines (114 loc) · 3.12 KB
/
search.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
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//!+
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"strings"
)
// 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
}
//!-
// For long-term stability, instead of http.Get, use the
// variant below which adds an HTTP request header indicating
// that only version 3 of the GitHub API is acceptable.
//
// req, err := http.NewRequest("GET", IssuesURL+"?q="+q, nil)
// if err != nil {
// return nil, err
// }
// req.Header.Set(
// "Accept", "application/vnd.github.v3.text-match+json")
// resp, err := http.DefaultClient.Do(req)
//!+
// We must close resp.Body on all execution paths.
// (Chapter 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
}
// remember to close the response body.
func get(url string) (*http.Response, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
return nil, fmt.Errorf("can't get %s: %s", url, resp.Status)
}
return resp, nil
}
func GetIssue(owner string, repo string, number string) (*Issue, error) {
url := strings.Join([]string{APIURL, "repos", owner, repo, "issues", number}, "/")
resp, err := get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var issue Issue
if err := json.NewDecoder(resp.Body).Decode(&issue); err != nil {
return nil, err
}
return &issue, nil
}
func GetIssues(owner string, repo string) ([]Issue, error) {
url := strings.Join([]string{APIURL, "repos", owner, repo, "issues"}, "/")
resp, err := get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var issues []Issue
if err := json.NewDecoder(resp.Body).Decode(&issues); err != nil {
return nil, err
}
return issues, nil
}
func EditIssue(owner, repo, number string, fields map[string]string) (*Issue, error) {
buf := &bytes.Buffer{}
encoder := json.NewEncoder(buf)
err := encoder.Encode(fields)
if err != nil {
return nil, err
}
client := &http.Client{}
url := strings.Join([]string{APIURL, "repos", owner, repo, "issues", number}, "/")
req, err := http.NewRequest("PATCH", url, buf)
req.SetBasicAuth(os.Getenv("GITHUB_USER"), os.Getenv("GITHUB_PASS"))
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to edit issue: %s", resp.Status)
}
var issue Issue
if err = json.NewDecoder(resp.Body).Decode(&issue); err != nil {
return nil, err
}
return &issue, nil
}
//!-