-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
58 lines (48 loc) · 1.57 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
package gh
import (
"context"
"net/http"
"time"
"github.com/google/go-github/v66/github"
"golang.org/x/oauth2"
)
// NewRESTClient returns GitHub REST API client for the given token (that may be empty)
// and debug logging function (that may be nil).
func NewRESTClient(token string, debugf Printf) (*github.Client, error) {
// don't use http.DefaultClient and oauth2.NewClient to avoid data races
httpTransport := http.DefaultTransport
if debugf != nil {
httpTransport = NewTransport(http.DefaultTransport, debugf)
}
var httpClient *http.Client
if token == "" {
httpClient = &http.Client{
Transport: httpTransport,
}
} else {
httpClient = &http.Client{
Transport: &oauth2.Transport{
Base: httpTransport,
Source: oauth2.ReuseTokenSource(nil, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)),
},
}
}
c := github.NewClient(httpClient)
c.UserAgent = "FerretDB-gh/1.0 (+https://github.com/FerretDB/gh)"
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Query rate limit to check that the client is able to make queries.
// See https://docs.github.com/en/rest/rate-limit.
// We can't use https://docs.github.com/en/rest/users/users#get-the-authenticated-user API,
// because short-lived automatic GITHUB_TOKEN is provided by GitHub Actions App that can't access this API.
rl, _, err := c.RateLimit.Get(ctx)
if rl != nil && debugf != nil {
debugf(
"Rate limit: %d/%d, resets at: %s.",
rl.Core.Remaining, rl.Core.Limit, rl.Core.Reset.Format(time.RFC3339),
)
}
return c, err
}