/
client.go
70 lines (64 loc) · 1.99 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
package github
import (
"context"
"fmt"
"net/http"
"os"
"github.com/google/go-github/v58/github"
"github.com/gregjones/httpcache"
"github.com/int128/oauth2-github-app"
"golang.org/x/oauth2"
)
type client struct {
rest *github.Client
}
func NewClient(ctx context.Context) (Client, error) {
transport := httpcache.NewMemoryCacheTransport()
ctx = context.WithValue(ctx, oauth2.HTTPClient, transport)
oauth2Client, err := newOAuth2Client(ctx)
if err != nil {
return nil, fmt.Errorf("could not create an OAuth2 client: %w", err)
}
ghc, err := newGitHubClient(oauth2Client)
if err != nil {
return nil, fmt.Errorf("could not create a GitHub client: %w", err)
}
return &client{rest: ghc}, nil
}
func newOAuth2Client(ctx context.Context) (*http.Client, error) {
var (
token = os.Getenv("GITHUB_TOKEN")
appID = os.Getenv("GITHUB_APP_ID")
installationID = os.Getenv("GITHUB_APP_INSTALLATION_ID")
privateKey = os.Getenv("GITHUB_APP_PRIVATE_KEY")
ghesURL = os.Getenv("GITHUB_ENTERPRISE_URL")
)
if token != "" {
return oauth2.NewClient(ctx, oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})), nil
}
if appID == "" || installationID == "" || privateKey == "" {
return nil, fmt.Errorf("you need to set either GITHUB_TOKEN or GitHub App configuration")
}
k, err := oauth2githubapp.ParsePrivateKey([]byte(privateKey))
if err != nil {
return nil, fmt.Errorf("invalid GITHUB_APP_PRIVATE_KEY: %w", err)
}
cfg := oauth2githubapp.Config{
PrivateKey: k,
AppID: appID,
InstallationID: installationID,
BaseURL: ghesURL,
}
return oauth2.NewClient(ctx, cfg.TokenSource(ctx)), nil
}
func newGitHubClient(hc *http.Client) (*github.Client, error) {
ghesURL := os.Getenv("GITHUB_ENTERPRISE_URL")
if ghesURL != "" {
ghc, err := github.NewClient(hc).WithEnterpriseURLs(ghesURL, ghesURL)
if err != nil {
return nil, fmt.Errorf("could not create a GitHub Enterprise client: %w", err)
}
return ghc, nil
}
return github.NewClient(hc), nil
}