Repository navigation
Expand file tree
/
Copy pathgithub.go
More file actions
103 lines (89 loc) · 2.76 KB
/
Copy pathgithub.go
File metadata and controls
103 lines (89 loc) · 2.76 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
package probot
import (
"net/http"
"github.com/bradleyfalzon/ghinstallation/v2"
"github.com/google/go-github/v88/github"
)
// tokenAuthTransport adds a bearer token to every request. Used for the
// GithubToken (PAT / installation token) authentication mode.
type tokenAuthTransport struct {
token string
base http.RoundTripper
}
func (t tokenAuthTransport) RoundTrip(req *http.Request) (*http.Response, error) {
r := req.Clone(req.Context())
r.Header.Set("Authorization", "Bearer "+t.token)
return t.base.RoundTrip(r)
}
func newClientOpts(p *Probot) []github.ClientOptionsFunc {
opts := []github.ClientOptionsFunc{github.WithTimeout(p.opts.RequestTimeout)}
if p.opts.BaseURL != "" {
opts = append(opts, github.WithEnterpriseURLs(p.opts.BaseURL, p.opts.BaseURL))
}
return opts
}
// newAppClient builds a GitHub API client authenticated at the app level
// (JWT), or with a static token when GithubToken is configured.
func (p *Probot) newAppClient() (*github.Client, error) {
if p.opts.GithubToken != "" {
return github.NewClient(append(newClientOpts(p),
github.WithHTTPClient(&http.Client{
Transport: tokenAuthTransport{token: p.opts.GithubToken, base: http.DefaultTransport},
}),
github.WithAuthToken(p.opts.GithubToken),
)...)
}
tr, err := ghinstallation.NewAppsTransport(http.DefaultTransport, p.appID, p.privateKey)
if err != nil {
return nil, err
}
if p.opts.BaseURL != "" {
tr.BaseURL = p.opts.BaseURL
}
p.appTransport = tr
return github.NewClient(append(newClientOpts(p),
github.WithTransport(tr),
)...)
}
// newInstallationClient builds (or reuses) a GitHub API client authenticated
// as the given installation. Clients are cached per installation; the
// underlying ghinstallation transport refreshes its access token before
// expiry, so this is safe to keep around.
func (p *Probot) newInstallationClient(installationID int64) (*github.Client, error) {
if p.opts.GithubToken != "" {
p.credMu.RLock()
defer p.credMu.RUnlock()
return p.appClient, nil
}
p.installMu.Lock()
defer p.installMu.Unlock()
if c, ok := p.installClients[installationID]; ok {
return c, nil
}
p.credMu.Lock()
if p.appTransport == nil {
tr, err := ghinstallation.NewAppsTransport(http.DefaultTransport, p.appID, p.privateKey)
if err != nil {
p.credMu.Unlock()
return nil, err
}
if p.opts.BaseURL != "" {
tr.BaseURL = p.opts.BaseURL
}
p.appTransport = tr
}
appTransport := p.appTransport
p.credMu.Unlock()
itr := ghinstallation.NewFromAppsTransport(appTransport, installationID)
if p.opts.BaseURL != "" {
itr.BaseURL = p.opts.BaseURL
}
client, err := github.NewClient(append(newClientOpts(p),
github.WithTransport(itr),
)...)
if err != nil {
return nil, err
}
p.installClients[installationID] = client
return client, nil
}