-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathgithub.go
40 lines (36 loc) · 949 Bytes
/
github.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
package tagpr
import (
"context"
"fmt"
"net/url"
"github.com/Songmu/gitconfig"
"github.com/gofri/go-github-ratelimit/github_ratelimit"
"github.com/google/go-github/v66/github"
"golang.org/x/oauth2"
)
func ghClient(ctx context.Context, token, host string) (*github.Client, error) {
if token == "" {
var err error
token, err = gitconfig.GitHubToken(host)
if err != nil {
return nil, err
}
}
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
oauthClient := oauth2.NewClient(ctx, ts)
rateLimiter, err := github_ratelimit.NewRateLimitWaiterClient(oauthClient.Transport)
if err != nil {
return nil, err
}
client := github.NewClient(rateLimiter)
if host != "" && host != "github.com" {
// ref. https://github.com/google/go-github/issues/958
host = fmt.Sprintf("https://%s/api/v3/", host)
u, err := url.Parse(host)
if err != nil {
return nil, err
}
client.BaseURL = u
}
return client, nil
}