-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathauth.go
86 lines (69 loc) · 3.27 KB
/
auth.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package auth
import (
"context"
"fmt"
"os"
"github.com/google/go-github/v43/github"
"github.com/gruntwork-io/git-xargs/types"
"github.com/gruntwork-io/go-commons/errors"
"golang.org/x/oauth2"
)
// The go-github package satisfies this PullRequest service's interface in production
type githubPullRequestService interface {
Create(ctx context.Context, owner string, name string, pr *github.NewPullRequest) (*github.PullRequest, *github.Response, error)
List(ctx context.Context, owner string, repo string, opts *github.PullRequestListOptions) ([]*github.PullRequest, *github.Response, error)
RequestReviewers(ctx context.Context, owner, repo string, number int, reviewers github.ReviewersRequest) (*github.PullRequest, *github.Response, error)
}
// The go-github package satisfies this Issues service's interface in production
type githubIssuesService interface {
AddAssignees(ctx context.Context, owner, repo string, number int, assignees []string) (*github.Issue, *github.Response, error)
}
// The go-github package satisfies this Repositories service's interface in production
type githubRepositoriesService interface {
Get(ctx context.Context, owner, repo string) (*github.Repository, *github.Response, error)
ListByOrg(ctx context.Context, org string, opts *github.RepositoryListByOrgOptions) ([]*github.Repository, *github.Response, error)
}
// GithubClient is the data structure that is common between production code and test code. In production code,
// go-github satisfies the PullRequests and Repositories service interfaces, whereas in test the concrete
// implementations for these same services are mocks that return a static slice of pointers to GitHub repositories,
// or a single pointer to a GitHub repository, as appropriate. This allows us to test the workflow of git-xargs
// without actually making API calls to GitHub when running tests
type GithubClient struct {
PullRequests githubPullRequestService
Issues githubIssuesService
Repositories githubRepositoriesService
}
func NewClient(client *github.Client) GithubClient {
return GithubClient{
PullRequests: client.PullRequests,
Issues: client.Issues,
Repositories: client.Repositories,
}
}
// ConfigureGithubClient creates a GitHub API client using the user-supplied GITHUB_OAUTH_TOKEN and returns the configured GitHub client
func ConfigureGithubClient() GithubClient {
// Ensure user provided a GITHUB_OAUTH_TOKEN
GithubOauthToken := os.Getenv("GITHUB_OAUTH_TOKEN")
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: GithubOauthToken},
)
tc := oauth2.NewClient(context.Background(), ts)
var githubClient *github.Client
if os.Getenv("GITHUB_HOSTNAME") != "" {
GithubHostname := os.Getenv("GITHUB_HOSTNAME")
baseUrl := fmt.Sprintf("https://%s/", GithubHostname)
githubClient, _ = github.NewEnterpriseClient(baseUrl, baseUrl, tc)
} else {
githubClient = github.NewClient(tc)
}
// Wrap the go-github client in a GithubClient struct, which is common between production and test code
client := NewClient(githubClient)
return client
}
// EnsureGithubOauthTokenSet is a sanity check that a value is exported for GITHUB_OAUTH_TOKEN
func EnsureGithubOauthTokenSet() error {
if os.Getenv("GITHUB_OAUTH_TOKEN") == "" {
return errors.WithStackTrace(types.NoGithubOauthTokenProvidedErr{})
}
return nil
}