Skip to content

Commit

Permalink
feat: add support for github enterprise server (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennyg authored Nov 30, 2023
1 parent 1b1ccde commit 070d1a6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ $ brew install git-xargs
export GITHUB_OAUTH_TOKEN=<your-secret-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=<your-ghe-hostname.your-domain.com>
```

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!
Expand All @@ -204,6 +209,7 @@ $ brew install git-xargs
touch git-xargs-is-awesome.txt
```
# Reference
## How to supply commands or scripts to run
Expand Down
16 changes: 14 additions & 2 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
16 changes: 14 additions & 2 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 070d1a6

Please sign in to comment.