From 8948a50e8bafcb4345ade1fb0715415b344bf3d8 Mon Sep 17 00:00:00 2001 From: Shane McEwan Date: Wed, 31 Jul 2019 11:26:22 +1000 Subject: [PATCH] Add support for Github Enterprise through a GITHUB_URL environment variable. --- README.md | 3 +++ initialize/initialize.go | 16 +++++++++++++++- merge/merge.go | 8 ++++++++ push/push.go | 7 +++++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2095934..b2d17e6 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,9 @@ First, clone the repo into your `GOPATH`. Next, run `make install_deps` (this ca The `GITHUB_API_TOKEN` environment variable must be set for Github. This should be a [GitHub Token](https://github.com/settings/tokens) with `repo` scope. +Optionally: The `GITHUB_URL` environment variable can be set to use a Github Enterprise setup, otherwise it will use https://github.com. +The `GITHUB_URL` **must** be a valid URL for the API endpoint including a trailing slash: e.g. `https://git.yourcompany.com/api/v3/` + ### GitLab setup The `GITLAB_API_TOKEN` environment variable must be set for Gitlab. This should be a [GitLab access token](https://gitlab.com/profile/personal_access_tokens) diff --git a/initialize/initialize.go b/initialize/initialize.go index fed4211..60dad3c 100644 --- a/initialize/initialize.go +++ b/initialize/initialize.go @@ -6,6 +6,7 @@ import ( "log" "os" "sort" + "net/url" "github.com/google/go-github/github" gitlab "github.com/xanzy/go-gitlab" @@ -73,6 +74,13 @@ func githubSearch(query string) ([]Repo, error) { client := github.NewClient(tc) + if os.Getenv("GITHUB_URL") != "" { + baseEndpoint, _ := url.Parse(os.Getenv("GITHUB_URL")) + client.BaseURL = baseEndpoint + uploadEndpoint, _ := url.Parse(os.Getenv("GITHUB_URL") + "upload/") + client.UploadURL = uploadEndpoint + } + opts := &github.SearchOptions{} allRepos := map[string]*github.Repository{} numProcessedResults := 0 @@ -100,12 +108,18 @@ func githubSearch(query string) ([]Repo, error) { opts.Page = resp.NextPage } + hostname := "github.com" + if os.Getenv("GITHUB_URL") != "" { + baseEndpoint, _ := url.Parse(os.Getenv("GITHUB_URL")) + hostname = baseEndpoint.Hostname() + } + repos := []Repo{} for _, r := range allRepos { repos = append(repos, Repo{ Name: r.GetName(), Owner: r.Owner.GetLogin(), - CloneURL: fmt.Sprintf("git@github.com:%s", r.GetFullName()), + CloneURL: fmt.Sprintf("git@%s:%s", hostname, r.GetFullName()), Provider: "github", }) } diff --git a/merge/merge.go b/merge/merge.go index 7ce6d7d..5a21969 100644 --- a/merge/merge.go +++ b/merge/merge.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "time" + "net/url" "github.com/google/go-github/github" "golang.org/x/oauth2" @@ -51,6 +52,13 @@ func GitHubMerge(ctx context.Context, input Input, repoLimiter *time.Ticker, mer tc := oauth2.NewClient(ctx, ts) client := github.NewClient(tc) + if os.Getenv("GITHUB_URL") != "" { + baseEndpoint, _ := url.Parse(os.Getenv("GITHUB_URL")) + client.BaseURL = baseEndpoint + uploadEndpoint, _ := url.Parse(os.Getenv("GITHUB_URL") + "upload/") + client.UploadURL = uploadEndpoint + } + // OK to merge? // (1) Check if the PR is mergeable diff --git a/push/push.go b/push/push.go index 1df4c67..c57a547 100644 --- a/push/push.go +++ b/push/push.go @@ -101,6 +101,13 @@ func GithubPush(ctx context.Context, input Input, repoLimiter *time.Ticker, push tc := oauth2.NewClient(ctx, ts) client := github.NewClient(tc) + if os.Getenv("GITHUB_URL") != "" { + baseEndpoint, _ := url.Parse(os.Getenv("GITHUB_URL")) + client.BaseURL = baseEndpoint + uploadEndpoint, _ := url.Parse(os.Getenv("GITHUB_URL") + "upload/") + client.UploadURL = uploadEndpoint + } + // Open a pull request, if one doesn't exist already head := fmt.Sprintf("%s:%s", input.RepoOwner, input.BranchName) base := "master"