Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for github enterprise server #132

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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") != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we change the environment variable name to something like: GITHUB_URL and expect the user to pass in the full URL? This would potentially remove confusion from users whether they would have to include https: at the front or not.

GithubHostname := os.Getenv("GITHUB_HOSTNAME")
baseUrl := fmt.Sprintf("https://%s/", GithubHostname)

githubClient, _ = github.NewEnterpriseClient(baseUrl, baseUrl, tc)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NewEnterpriseClient function checks out 👌 ... I was initially asking whether this took into account the path aspects for GHES API endpoints, but confirmed that it should be handled appropriately.


} 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