forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 41
/
client.go
311 lines (265 loc) · 7.49 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
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
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"fmt"
"io/ioutil"
"strings"
"time"
"golang.org/x/oauth2"
"github.com/golang/glog"
"github.com/google/go-github/github"
"github.com/spf13/cobra"
)
// Client can be used to run commands again GitHub API
type Client struct {
Token string
TokenFile string
Org string
Project string
githubClient *github.Client
}
const (
tokenLimit = 50 // We try to stop that far from the API limit
)
// AddFlags parses options for github client
func (client *Client) AddFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringVar(&client.Token, "token", "",
"The OAuth Token to use for requests.")
cmd.PersistentFlags().StringVar(&client.TokenFile, "token-file", "",
"The file containing the OAuth Token to use for requests.")
cmd.PersistentFlags().StringVar(&client.Org, "organization", "",
"The github organization to scan")
cmd.PersistentFlags().StringVar(&client.Project, "project", "",
"The github project to scan")
}
// CheckFlags looks for organization and project flags to configure the client
func (client *Client) CheckFlags() error {
if client.Org == "" {
return fmt.Errorf("organization flag must be set")
}
client.Org = strings.ToLower(client.Org)
if client.Project == "" {
return fmt.Errorf("project flag must be set")
}
client.Project = strings.ToLower(client.Project)
return nil
}
// getGitHubClient create the github client that we use to communicate with github
func (client *Client) getGitHubClient() (*github.Client, error) {
if client.githubClient != nil {
return client.githubClient, nil
}
token := client.Token
if len(token) == 0 && len(client.TokenFile) != 0 {
data, err := ioutil.ReadFile(client.TokenFile)
if err != nil {
return nil, err
}
token = strings.TrimSpace(string(data))
}
if len(token) > 0 {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
tc := oauth2.NewClient(oauth2.NoContext, ts)
client.githubClient = github.NewClient(tc)
} else {
client.githubClient = github.NewClient(nil)
}
return client.githubClient, nil
}
// limitsCheckAndWait make sure we have not reached the limit or wait
func (client *Client) limitsCheckAndWait() {
var sleep time.Duration
githubClient, err := client.getGitHubClient()
if err != nil {
glog.Error("Failed to get RateLimits: ", err)
sleep = time.Minute
} else {
limits, _, err := githubClient.RateLimits(context.Background())
if err != nil {
glog.Error("Failed to get RateLimits:", err)
sleep = time.Minute
}
if limits != nil && limits.Core != nil && limits.Core.Remaining < tokenLimit {
sleep = limits.Core.Reset.Sub(time.Now())
glog.Warning("RateLimits: reached. Sleeping for ", sleep)
}
}
time.Sleep(sleep)
}
// ClientInterface describes what a client should be able to do
type ClientInterface interface {
RepositoryName() string
FetchIssues(last time.Time, c chan *github.Issue)
FetchIssueEvents(issueID int, last *int, c chan *github.IssueEvent)
FetchIssueComments(issueID int, last time.Time, c chan *github.IssueComment)
FetchPullComments(issueID int, last time.Time, c chan *github.PullRequestComment)
}
// RepositoryName returns github's repository name in the form of org/project
func (client *Client) RepositoryName() string {
return fmt.Sprintf("%s/%s", client.Org, client.Project)
}
// FetchIssues from GitHub, until 'latest' time
func (client *Client) FetchIssues(latest time.Time, c chan *github.Issue) {
opt := &github.IssueListByRepoOptions{Since: latest, Sort: "updated", State: "all", Direction: "asc"}
githubClient, err := client.getGitHubClient()
if err != nil {
close(c)
glog.Error(err)
return
}
count := 0
for {
client.limitsCheckAndWait()
issues, resp, err := githubClient.Issues.ListByRepo(
context.Background(),
client.Org,
client.Project,
opt,
)
if err != nil {
close(c)
glog.Error(err)
return
}
for _, issue := range issues {
c <- issue
count++
}
if resp.NextPage == 0 {
break
}
opt.ListOptions.Page = resp.NextPage
}
glog.Infof("Fetched %d issues updated issue since %v.", count, latest)
close(c)
}
// hasID look for a specific id in a list of events
func hasID(events []*github.IssueEvent, id int) bool {
for _, event := range events {
if *event.ID == int64(id) {
return true
}
}
return false
}
// FetchIssueEvents from github and return the full list, until it matches 'latest'
// The entire last page will be included so you can have redundancy.
func (client *Client) FetchIssueEvents(issueID int, latest *int, c chan *github.IssueEvent) {
opt := &github.ListOptions{PerPage: 100}
githubClient, err := client.getGitHubClient()
if err != nil {
close(c)
glog.Error(err)
return
}
count := 0
for {
client.limitsCheckAndWait()
events, resp, err := githubClient.Issues.ListIssueEvents(
context.Background(),
client.Org,
client.Project,
issueID,
opt,
)
if err != nil {
glog.Errorf("ListIssueEvents failed: %s. Retrying...", err)
time.Sleep(time.Second)
continue
}
for _, event := range events {
c <- event
count++
}
if resp.NextPage == 0 || (latest != nil && hasID(events, *latest)) {
break
}
opt.Page = resp.NextPage
}
glog.Infof("Fetched %d events.", count)
close(c)
}
// FetchIssueComments fetches comments associated to given Issue (since latest)
func (client *Client) FetchIssueComments(issueID int, latest time.Time, c chan *github.IssueComment) {
opt := &github.IssueListCommentsOptions{Since: latest, Sort: "updated", Direction: "asc"}
githubClient, err := client.getGitHubClient()
if err != nil {
close(c)
glog.Error(err)
return
}
count := 0
for {
client.limitsCheckAndWait()
comments, resp, err := githubClient.Issues.ListComments(
context.Background(),
client.Org,
client.Project,
issueID,
opt,
)
if err != nil {
close(c)
glog.Error(err)
return
}
for _, comment := range comments {
c <- comment
count++
}
if resp.NextPage == 0 {
break
}
opt.ListOptions.Page = resp.NextPage
}
glog.Infof("Fetched %d issue comments updated since %v for issue #%d.", count, latest, issueID)
close(c)
}
// FetchPullComments fetches comments associated to given PullRequest (since latest)
func (client *Client) FetchPullComments(issueID int, latest time.Time, c chan *github.PullRequestComment) {
opt := &github.PullRequestListCommentsOptions{Since: latest, Sort: "updated", Direction: "asc"}
githubClient, err := client.getGitHubClient()
if err != nil {
close(c)
glog.Error(err)
return
}
count := 0
for {
client.limitsCheckAndWait()
comments, resp, err := githubClient.PullRequests.ListComments(
context.Background(),
client.Org,
client.Project,
issueID,
opt,
)
if err != nil {
close(c)
glog.Error(err)
return
}
for _, comment := range comments {
c <- comment
count++
}
if resp.NextPage == 0 {
break
}
opt.ListOptions.Page = resp.NextPage
}
glog.Infof("Fetched %d review comments updated since %v for issue #%d.", count, latest, issueID)
close(c)
}