-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
27 lines (24 loc) · 877 Bytes
/
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
package actions
import (
"context"
"os"
"github.com/google/go-github/v28/github"
"golang.org/x/oauth2"
)
// CreateClient uses either of the belows to authenticate to the Github API:
// - installation toke: `"token " + os.Getenv("GITHUB_TOKEN")`
// - personal access token: `"bearer " + os.Getenv("GITHUB_TOKEN")`
func CreateClient(instToken, baseURL, uploadURL string) (*github.Client, error) {
// For installation tokens, Github uses a different token type ("token" instead of "bearer")
tokenType := "token"
if os.Getenv("GITHUB_TOKEN_TYPE") != "" {
tokenType = os.Getenv("GITHUB_TOKEN_TYPE")
}
t := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: instToken, TokenType: tokenType})
c := context.Background()
tc := oauth2.NewClient(c, t)
if baseURL != "" {
return github.NewEnterpriseClient(baseURL, uploadURL, tc)
}
return github.NewClient(tc), nil
}