diff --git a/README.md b/README.md index 341ad43..95ecf10 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,9 @@ Alternately, you can download via `go get github.com/clever/microplane/cmd`. In 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 79dd0ce..a783b6a 100644 --- a/initialize/initialize.go +++ b/initialize/initialize.go @@ -7,7 +7,8 @@ import ( "log" "os" "sort" - "strings" + "strings" + "net/url" "github.com/google/go-github/github" gitlab "github.com/xanzy/go-gitlab" @@ -128,6 +129,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 @@ -155,12 +163,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"