-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathclient.go
More file actions
536 lines (436 loc) · 12.4 KB
/
client.go
File metadata and controls
536 lines (436 loc) · 12.4 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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
package github
import (
"context"
"fmt"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/src-d/lookout"
"github.com/src-d/lookout/service/git"
"github.com/src-d/lookout/util/cache"
"github.com/src-d/lookout/util/ctxlog"
"github.com/google/go-github/github"
"gopkg.in/src-d/go-git.v4/plumbing/transport"
log "gopkg.in/src-d/go-log.v1"
)
// ClientPoolEventType type of the change in ClientPool
type ClientPoolEventType string
const (
// ClientPoolEventAdd happens when new client is added in the pool
ClientPoolEventAdd ClientPoolEventType = "add"
// ClientPoolEventRemove happens when client is removed from the pool
ClientPoolEventRemove ClientPoolEventType = "remove"
)
// keep default into our package to be able to override them in tests
var (
defaultBaseURL = "https://api.github.com/"
defaultUploadBaseURL = "https://uploads.github.com/"
)
// ClientPoolEvent defines change in ClientPool
type ClientPoolEvent struct {
Type ClientPoolEventType
Client *Client
}
// repositoryInfo wraps a lookout.RepositoryInfo adding an Organization ID
type repositoryInfo struct {
lookout.RepositoryInfo
// OrganizationID is this repository's organization
OrganizationID string
}
// ClientPool holds mapping of repositories to clients
type ClientPool struct {
byClients map[*Client][]*repositoryInfo
byRepo map[string]*Client
mutex sync.Mutex
subs map[chan ClientPoolEvent]bool
subsMutex sync.Mutex
}
// NewClientPool creates new pool of clients with repositories
func NewClientPool() *ClientPool {
return &ClientPool{
byClients: make(map[*Client][]*repositoryInfo),
byRepo: make(map[string]*Client),
subs: make(map[chan ClientPoolEvent]bool),
}
}
// newClientPoolFromClients creates a new pool of clients based on the given
// clients and repositories
func newClientPoolFromClients(
byClients map[*Client][]*repositoryInfo,
byRepo map[string]*Client) *ClientPool {
return &ClientPool{
byClients: byClients,
byRepo: byRepo,
subs: make(map[chan ClientPoolEvent]bool),
}
}
// Clients returns map[Client]RepositoryInfo
func (p *ClientPool) Clients() map[*Client][]*repositoryInfo {
p.mutex.Lock()
defer p.mutex.Unlock()
// Create the target map
copyMap := make(map[*Client][]*repositoryInfo, len(p.byClients))
// Copy from the original map to the target map
for key, value := range p.byClients {
copyMap[key] = value
}
return copyMap
}
// Client returns client, ok by username and repository name
func (p *ClientPool) Client(username, repo string) (*Client, bool) {
p.mutex.Lock()
defer p.mutex.Unlock()
c, ok := p.byRepo[username+"/"+repo]
return c, ok
}
// Repos returns list of repositories in the pool
func (p *ClientPool) Repos() []string {
p.mutex.Lock()
defer p.mutex.Unlock()
var rps []string
for r := range p.byRepo {
rps = append(rps, r)
}
return rps
}
// ReposByClient returns list of repositories by client
func (p *ClientPool) ReposByClient(c *Client) []*repositoryInfo {
p.mutex.Lock()
defer p.mutex.Unlock()
return p.byClients[c]
}
// Update updates list of repositories for a client
func (p *ClientPool) Update(c *Client, newRepos []*repositoryInfo) {
if len(newRepos) == 0 {
p.RemoveClient(c)
return
}
p.mutex.Lock()
defer p.mutex.Unlock()
repos, ok := p.byClients[c]
if !ok {
for _, r := range newRepos {
p.byRepo[r.FullName] = c
}
p.byClients[c] = newRepos
p.notify(ClientPoolEvent{
Type: ClientPoolEventAdd,
Client: c,
})
return
}
// delete old repos
var reposAfterDelete []*repositoryInfo
for _, repo := range repos {
found := false
for _, newRepo := range newRepos {
if repo == newRepo {
found = true
break
}
}
if found {
reposAfterDelete = append(reposAfterDelete, repo)
} else {
delete(p.byRepo, repo.FullName)
}
}
p.byClients[c] = reposAfterDelete
// add new repos
for _, newRepo := range newRepos {
if _, ok := p.byRepo[newRepo.FullName]; ok {
continue
}
p.byRepo[newRepo.FullName] = c
p.byClients[c] = append(p.byClients[c], newRepo)
}
}
// RemoveClient removes client from the pool and notifies about it
func (p *ClientPool) RemoveClient(c *Client) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.notify(ClientPoolEvent{
Type: ClientPoolEventRemove,
Client: c,
})
for repo, client := range p.byRepo {
if client == c {
delete(p.byRepo, repo)
}
}
delete(p.byClients, c)
}
// Subscribe allows to subscribe to changes in the pool
func (p *ClientPool) Subscribe(ch chan ClientPoolEvent) {
p.subsMutex.Lock()
defer p.subsMutex.Unlock()
p.subs[ch] = true
}
// Unsubscribe stops sending changes to the channel
func (p *ClientPool) Unsubscribe(ch chan ClientPoolEvent) {
p.subsMutex.Lock()
defer p.subsMutex.Unlock()
delete(p.subs, ch)
}
func (p *ClientPool) notify(e ClientPoolEvent) {
p.subsMutex.Lock()
defer p.subsMutex.Unlock()
for ch := range p.subs {
// use non-blocking send
select {
case ch <- e:
default:
}
}
}
var _ git.AuthProvider = &ClientPool{}
// GitAuth returns a go-git auth method for a repo
func (p *ClientPool) GitAuth(ctx context.Context, repoInfo *lookout.RepositoryInfo) transport.AuthMethod {
c, ok := p.Client(repoInfo.Owner, repoInfo.Name)
if !ok {
return nil
}
return c.gitAuth(ctx)
}
type gitAuthFn = func(ctx context.Context) transport.AuthMethod
// Client is a wrapper for github.Client that supports cache and provides rate limit information
type Client struct {
*github.Client
cache *cache.ValidableCache
limitRT *limitRoundTripper
watchMinInterval time.Duration
gitAuth gitAuthFn
mutex sync.Mutex
username string
}
// NewClient creates new Client.
// A timeout of zero means no timeout.
func NewClient(
t http.RoundTripper,
cache *cache.ValidableCache,
watchMinInterval string,
gitAuth gitAuthFn,
timeout time.Duration,
) *Client {
fixT := &fixReviewTransport{
Transport: t,
}
limitRT := &limitRoundTripper{
Base: fixT,
}
interval := minInterval
if watchMinInterval != "" {
d, err := time.ParseDuration(watchMinInterval)
if err != nil {
log.Errorf(err, "can't parse min interval %q", watchMinInterval)
} else {
interval = d
}
}
ghClient, _ := github.NewEnterpriseClient(
defaultBaseURL,
defaultUploadBaseURL,
&http.Client{
Transport: limitRT,
Timeout: timeout,
})
return &Client{
Client: ghClient,
cache: cache,
limitRT: limitRT,
watchMinInterval: interval,
gitAuth: gitAuth,
}
}
// Rate returns last github.Rate for a client by category
func (c *Client) Rate(cat rateLimitCategory) github.Rate {
return c.limitRT.Rate(cat)
}
// PollInterval returns last duration from X-Poll-Interval for a client by category
func (c *Client) PollInterval(cat pollLimitCategory) time.Duration {
return c.limitRT.PollInterval(cat)
}
// Validate validates cache by path
func (c *Client) Validate(path string) error {
return c.cache.Validate(path)
}
// Username returns name of the user for the current client
func (c *Client) Username() (string, error) {
if c.username != "" {
return c.username, nil
}
c.mutex.Lock()
defer c.mutex.Unlock()
if c.username != "" {
return c.username, nil
}
u, _, err := c.Users.Get(context.Background(), "")
if err != nil {
return "", err
}
c.username = u.GetLogin()
return c.username, nil
}
type rateLimitCategory uint8
type pollLimitCategory uint8
const (
headerRateLimit = "X-RateLimit-Limit"
headerRateRemaining = "X-RateLimit-Remaining"
headerRateReset = "X-RateLimit-Reset"
headerPollInterval = "X-Poll-Interval"
)
const (
coreCategory rateLimitCategory = iota
searchCategory
categories // An array of this length will be able to contain all rate limit categories.
)
const (
eventsCategory pollLimitCategory = iota
notificationsCategory
unknownCategory // in case some new endpoint starts return X-Poll-Interval
pollCategories
)
// category returns the rate limit category of the endpoint, determined by Request.URL.Path.
func category(path string) rateLimitCategory {
switch {
default:
return coreCategory
case strings.HasPrefix(path, "/search/"):
return searchCategory
}
}
// pollCategory returns the poll limit category of the endpoint, determined by Request.URL.Path.
// TODO(max): cover all cases
func pollCategory(path string) pollLimitCategory {
switch {
case strings.HasSuffix(path, "/events"):
return eventsCategory
case strings.HasSuffix(path, "/notifications"):
return notificationsCategory
default:
return unknownCategory
}
}
type limitRoundTripper struct {
Base http.RoundTripper
// rateLimits for the client as determined by the most recent API calls.
rateLimits [categories]github.Rate
// pollInterval for the client by endpoint as determined by the most recent API calls
pollIntervals [pollCategories]time.Duration
rateMu sync.Mutex
}
func (t *limitRoundTripper) Rate(c rateLimitCategory) github.Rate {
t.rateMu.Lock()
defer t.rateMu.Unlock()
return t.rateLimits[c]
}
func (t *limitRoundTripper) PollInterval(c pollLimitCategory) time.Duration {
t.rateMu.Lock()
defer t.rateMu.Unlock()
return t.pollIntervals[c]
}
func (t *limitRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
rt := t.Base
if rt == nil {
rt = http.DefaultTransport
}
resp, err := rt.RoundTrip(req)
if err != nil {
return resp, err
}
logFields := log.Fields{"url": req.URL}
t.rateMu.Lock()
rate := t.rateLimits[category(req.URL.Path)]
if limit := resp.Header.Get(headerRateLimit); limit != "" {
rate.Limit, _ = strconv.Atoi(limit)
logFields["rate.limit"] = rate.Limit
}
if remaining := resp.Header.Get(headerRateRemaining); remaining != "" {
rate.Remaining, _ = strconv.Atoi(remaining)
logFields["rate.remaining"] = rate.Remaining
}
if reset := resp.Header.Get(headerRateReset); reset != "" {
if v, _ := strconv.ParseInt(reset, 10, 64); v != 0 {
rate.Reset = github.Timestamp{time.Unix(v, 0)}
}
logFields["rate.reset-at"] = rate.Reset
}
if pollInterval := resp.Header.Get(headerPollInterval); pollInterval != "" {
secs, _ := strconv.Atoi(pollInterval)
duration := time.Duration(secs) * time.Second
t.pollIntervals[pollCategory(req.URL.Path)] = duration
logFields["poll-interval"] = duration
}
t.rateLimits[category(req.URL.Path)] = rate
t.rateMu.Unlock()
ctxlog.Get(req.Context()).With(logFields).Debugf("http request with GitHub rate limit")
return resp, err
}
var _ http.RoundTripper = &limitRoundTripper{}
func handleAPIError(resp *github.Response, err error, msg string) error {
if err != nil {
if e, ok := err.(*github.ErrorResponse); ok {
if e.Response == nil {
e.Response = resp.Response
} else if e.Response.Request == nil {
e.Response.Request = resp.Response.Request
}
}
return ErrGitHubAPI.Wrap(err, msg)
}
if resp.StatusCode == 200 {
return nil
}
return ErrGitHubAPI.Wrap(
fmt.Errorf("bad HTTP status: %d", resp.StatusCode),
msg,
)
}
// ValidateTokenPermissions checks that client has necessary permissions required by lookout
// returns error if any is missed
func ValidateTokenPermissions(client *Client) error {
// we need `repo` access to be able to read from private repositories
// and `repo:status` which is part of `repo` for posting statuses
required := []string{"repo"}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// authorizations api can be accessed only with username and password, not token
// read headers of any endpoint instead
_, r, err := client.Users.Get(ctx, "")
if err != nil {
return err
}
scopes := strings.Split(r.Header.Get("X-Oauth-Scopes"), ",")
scopesMap := make(map[string]bool, len(scopes))
for _, scope := range scopes {
scopesMap[strings.TrimSpace(scope)] = true
}
for _, scope := range required {
_, ok := scopesMap[scope]
if !ok {
return fmt.Errorf("token doesn't have permission scope '%s'", scope)
}
}
return nil
}
// CanPostStatus check if the client has push access to a repository
// which is required for updating status. It assumes client has correct scope.
// returns error if it permission is missed
func CanPostStatus(client *Client, repo *repositoryInfo) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
user, err := client.Username()
if err != nil {
return err
}
r, _, err := client.Repositories.GetPermissionLevel(ctx, repo.Owner, repo.Name, user)
if err != nil {
return err
}
if r.GetPermission() != "admin" && r.GetPermission() != "write" {
return fmt.Errorf("token doesn't have write access to repository %s", repo.FullName)
}
return err
}