-
Notifications
You must be signed in to change notification settings - Fork 21
/
client.go
250 lines (228 loc) · 6.25 KB
/
client.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
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package github provides idiomatic Go APIs for accessing basic GitHub issue operations.
//
// The entire GitHub API can be accessed by using the [Client] with GraphQL schema from
// [rsc.io/github/schema].
package github
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
"rsc.io/github/schema"
)
// A Client is an authenticated client for accessing the GitHub GraphQL API.
// Client provides convenient methods for common operations.
// To build others, see the [GraphQLQuery] and [GraphQLMutation] methods.
type Client struct {
token string
}
// Dial returns a Client authenticating as user.
// Authentication credentials are loaded from $HOME/.netrc
// using the 'api.github.com' entry, which should contain a
// GitHub personal access token.
// If user is the empty string, Dial uses the first line in .netrc
// listed for api.github.com.
//
// For example, $HOME/.netrc might contain:
//
// machine api.github.com login ken password ghp_123456789abcdef123456789abcdef12345
func Dial(user string) (*Client, error) {
_, passwd, err := netrcAuth("api.github.com", user)
if err != nil {
return nil, err
}
return &Client{token: passwd}, nil
}
// NewClient returns a new client using the given GitHub personal access token (of the form "ghp_....").
func NewClient(token string) *Client {
return &Client{token: token}
}
// A Vars is a binding of GraphQL variables to JSON-able values (usually strings).
type Vars map[string]any
// GraphQLQuery runs a single query with the bound variables.
// For example, to look up a repository ID:
//
// func repoID(org, name string) (string, error) {
// graphql := `
// query($Org: String!, $Repo: String!) {
// repository(owner: $Org, name: $Repo) {
// id
// }
// }
// `
// vars := Vars{"Org": org, "Repo": repo}
// q, err := c.GraphQLQuery(graphql, vars)
// if err != nil {
// return "", err
// }
// return string(q.Repository.Id), nil
// }
//
// (This is roughly the implementation of the [Client.Repo] method.)
func (c *Client) GraphQLQuery(query string, vars Vars) (*schema.Query, error) {
var reply schema.Query
if err := c.graphQL(query, vars, &reply); err != nil {
return nil, err
}
return &reply, nil
}
// GraphQLMutation runs a single mutation with the bound variables.
// For example, to edit an issue comment:
//
// func editComment(commentID, body string) error {
// graphql := `
// mutation($Comment: ID!, $Body: String!) {
// updateIssueComment(input: {id: $Comment, body: $Body}) {
// clientMutationId
// }
// }
// `
// _, err := c.GraphQLMutation(graphql, Vars{"Comment": commentID, "Body": body})
// return err
// }
//
// (This is roughly the implementation of the [Client.EditIssueComment] method.)
func (c *Client) GraphQLMutation(query string, vars Vars) (*schema.Mutation, error) {
var reply schema.Mutation
if err := c.graphQL(query, vars, &reply); err != nil {
return nil, err
}
return &reply, nil
}
func (c *Client) graphQL(query string, vars Vars, reply any) error {
js, err := json.Marshal(struct {
Query string `json:"query"`
Variables any `json:"variables"`
}{
Query: query,
Variables: vars,
})
if err != nil {
return err
}
Retry:
method := "POST"
body := bytes.NewReader(js)
if query == "schema" && vars == nil {
method = "GET"
js = nil
}
req, err := http.NewRequest(method, "https://api.github.com/graphql", body)
if err != nil {
return err
}
if c.token != "" {
req.Header.Set("Authorization", "Bearer "+c.token)
}
previews := []string{
"application/vnd.github.inertia-preview+json", // projects
"application/vnd.github.starfox-preview+json", // projects events
"application/vnd.github.elektra-preview+json", // pinned issues
}
req.Header.Set("Accept", strings.Join(previews, ","))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("reading body: %v", err)
}
if resp.StatusCode != 200 {
err := fmt.Errorf("%s\n%s", resp.Status, data)
// TODO(rsc): Could do better here, but this works reasonably well.
// If we're over quota, it could be a while.
if strings.Contains(err.Error(), "wait a few minutes") {
log.Printf("github: %v", err)
time.Sleep(10 * time.Minute)
goto Retry
}
return err
}
jsreply := struct {
Data any
Errors []struct {
Message string
}
}{
Data: reply,
}
err = json.Unmarshal(data, &jsreply)
if err != nil {
return fmt.Errorf("parsing reply: %v", err)
}
if len(jsreply.Errors) > 0 {
if strings.Contains(jsreply.Errors[0].Message, "rate limit exceeded") {
log.Printf("github: %s", jsreply.Errors[0].Message)
time.Sleep(10 * time.Minute)
goto Retry
}
if strings.Contains(jsreply.Errors[0].Message, "submitted too quickly") {
log.Printf("github: %s", jsreply.Errors[0].Message)
time.Sleep(5 * time.Second)
goto Retry
}
for i, line := range strings.Split(query, "\n") {
log.Print(i+1, line)
}
return fmt.Errorf("graphql error: %s", jsreply.Errors[0].Message)
}
return nil
}
func collect[Schema, Out any](c *Client, graphql string, vars Vars, transform func(Schema) Out,
page func(*schema.Query) pager[Schema]) ([]Out, error) {
var cursor string
var list []Out
for {
if cursor != "" {
vars["Cursor"] = cursor
}
q, err := c.GraphQLQuery(graphql, vars)
if err != nil {
return list, err
}
p := page(q)
if p == nil {
break
}
list = append(list, apply(transform, p.GetNodes())...)
info := p.GetPageInfo()
cursor = info.EndCursor
if cursor == "" || !info.HasNextPage {
break
}
}
return list, nil
}
type pager[T any] interface {
GetPageInfo() *schema.PageInfo
GetNodes() []T
}
func apply[In, Out any](f func(In) Out, x []In) []Out {
var out []Out
for _, in := range x {
out = append(out, f(in))
}
return out
}
func toTime(s schema.DateTime) time.Time {
t, err := time.ParseInLocation(time.RFC3339Nano, string(s), time.UTC)
if err != nil {
return time.Time{}
}
return t
}
func toDate(s schema.Date) time.Time {
t, err := time.ParseInLocation("2006-01-02", string(s), time.UTC)
if err != nil {
return time.Time{}
}
return t
}