/
github.go
39 lines (31 loc) · 948 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
// Copyright 2022 Outreach Corporation. All Rights Reserved.
// Description: Implements helpers for working with Github
// on local machines.
// Package github includes helper functions for standardized ways
// of interacting with Github across machines.
package github
import (
"context"
"net/http"
"github.com/google/go-github/v58/github"
"golang.org/x/oauth2"
)
// NewClient returns a new Github client using credentials from
// GetToken().
func NewClient(optFns ...Option) (*github.Client, error) {
opts := &Options{}
opts.apply(optFns...)
token, err := GetToken()
if err != nil {
if opts.AllowUnauthenticated {
opts.Logger.Warn("unable to get token, falling back to unauthenticated client")
return github.NewClient(http.DefaultClient), nil
}
return nil, err
}
return github.NewClient(oauth2.NewClient(context.Background(),
oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: string(token)},
)),
), nil
}