From 1ec4513a396cf1834d1b5955a34c83376fb8d04a Mon Sep 17 00:00:00 2001 From: Shingo Omura Date: Thu, 7 May 2020 02:41:45 +0900 Subject: [PATCH] support GitHub Enterprise (it enables to configure GitHub API endpoints) --- README.md | 1 + cmd/server/main.go | 6 ++++-- cmd/tester/main.go | 6 ++++-- pkg/triage/github.go | 13 +++++++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f43a5e4..07bb474 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ Triage Party is a stateless Go web application, configured via YAML. While it ha * Easily open groups of issues into browser tabs (must allow pop-ups) * Queries across multiple repositories * "Shift-Reload" for live data pull +* GitHub Enterprise support (via `--github-api-url` cli flag) ## Triage Party in production diff --git a/cmd/server/main.go b/cmd/server/main.go index 24eaae9..e285e79 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -37,7 +37,6 @@ import ( "syscall" "time" - "github.com/google/go-github/v31/github" "golang.org/x/oauth2" "k8s.io/klog/v2" @@ -48,6 +47,9 @@ import ( ) var ( + // custom GitHub API URLs + githubAPIRawURL = flag.String("github-api-url", "", "GitHub API url to connect. Please set this when you use GitHub Enterprise. This often is your GitHub Enterprise hostname. If the URL does not have the suffix \"/api/v3/\", it will be added automatically.") + // shared with tester configPath = flag.String("config", "", "configuration path") persistBackend = flag.String("persist-backend", "", "Cache persistence backend (disk, mysql, cloudsql)") @@ -78,7 +80,7 @@ func main() { ctx := context.Background() - client := github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource( + client := triage.MustCreateGithubClient(*githubAPIRawURL, oauth2.NewClient(ctx, oauth2.StaticTokenSource( &oauth2.Token{AccessToken: triage.MustReadToken(*githubTokenFile, "GITHUB_TOKEN")}, ))) diff --git a/cmd/tester/main.go b/cmd/tester/main.go index 4a53f0f..53b162d 100644 --- a/cmd/tester/main.go +++ b/cmd/tester/main.go @@ -26,12 +26,14 @@ import ( "github.com/google/triage-party/pkg/persist" "github.com/google/triage-party/pkg/triage" - "github.com/google/go-github/v31/github" "golang.org/x/oauth2" "k8s.io/klog/v2" ) var ( + // custom GitHub API URLs + githubAPIRawURL = flag.String("github-api-url", "", "base URL for GitHub API. Please set this when you use GitHub Enterprise. This often is your GitHub Enterprise hostname. If the base URL does not have the suffix \"/api/v3/\", it will be added automatically.") + // shared with server configPath = flag.String("config", "", "configuration path") persistBackend = flag.String("persist-backend", "", "Cache persistence backend (disk, mysql, cloudsql)") @@ -58,7 +60,7 @@ func main() { } ctx := context.Background() - client := github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource( + client := triage.MustCreateGithubClient(*githubAPIRawURL, oauth2.NewClient(ctx, oauth2.StaticTokenSource( &oauth2.Token{AccessToken: triage.MustReadToken(*githubTokenFile, "GITHUB_TOKEN")}, ))) diff --git a/pkg/triage/github.go b/pkg/triage/github.go index e2a39ff..acc98ff 100644 --- a/pkg/triage/github.go +++ b/pkg/triage/github.go @@ -2,7 +2,9 @@ package triage import ( "fmt" + "github.com/google/go-github/v31/github" "io/ioutil" + "net/http" "net/url" "os" "strings" @@ -45,3 +47,14 @@ func MustReadToken(path string, env string) string { } return token } + +func MustCreateGithubClient(githubAPIRawURL string, httpClient *http.Client) *github.Client { + if githubAPIRawURL != "" { + client, err := github.NewEnterpriseClient(githubAPIRawURL, githubAPIRawURL, httpClient) + if err != nil { + klog.Exitf("unable to create GitHub client: %v", err) + } + return client + } + return github.NewClient(httpClient) +}