diff --git a/README.md b/README.md index 7c456a8..7b05599 100644 --- a/README.md +++ b/README.md @@ -192,6 +192,11 @@ $ brew install git-xargs export GITHUB_OAUTH_TOKEN= ``` +1. **Setup authentication with your Github Enterprise server**. To use a Github Enterprise server, set the GITHUB_HOSTNAME environment variable: + ```bash + export GITHUB_HOSTNAME= + ``` + 1. **Provide a script or command and target some repos**. Here's a simple example of running the `touch` command in every repo in your GitHub organization. Follow the same pattern to start running your own scripts and commands against your own repos! @@ -204,6 +209,7 @@ $ brew install git-xargs touch git-xargs-is-awesome.txt ``` + # Reference ## How to supply commands or scripts to run diff --git a/auth/auth.go b/auth/auth.go index 3332f55..83b1be4 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -2,12 +2,12 @@ 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" ) @@ -52,8 +52,20 @@ func ConfigureGithubClient() GithubClient { 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(github.NewClient(tc)) + client := NewClient(githubClient) return client } diff --git a/auth/auth_test.go b/auth/auth_test.go index 51252f7..31da954 100644 --- a/auth/auth_test.go +++ b/auth/auth_test.go @@ -8,11 +8,23 @@ import ( ) // TestConfigureGithubClient performs a sanity check that you can configure a production GitHub API client +// If GITHUB_HOSTNAME, use the github.NewEnterpriseClient func TestConfigureGithubClient(t *testing.T) { t.Parallel() - client := ConfigureGithubClient() - assert.NotNil(t, client) + t.Run("returns github client", func(t *testing.T) { + client := ConfigureGithubClient() + assert.NotNil(t, client) + }) + t.Run("returns github client with GithubHostname", func(t *testing.T) { + GithubHostname := "ghe.my-domain.com" + os.Setenv("GITHUB_HOSTNAME", GithubHostname) + + client := ConfigureGithubClient() + assert.NotNil(t, client) + + }) + } // TestNoGithubOauthTokenPassed temporarily drops the existing GITHUB_OAUTH_TOKEN env var to ensure that the validation