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: Support self-hosted GitHub Enterprise servers #3748

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
46 changes: 39 additions & 7 deletions internal/chezmoi/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@ import (
"context"
"net/http"
"os"
"strings"

"github.com/google/go-github/v62/github"
"golang.org/x/oauth2"
)

// NewGitHubClient returns a new github.Client configured with an access token
// and a http client, if available.
func NewGitHubClient(ctx context.Context, httpClient *http.Client) *github.Client {
for _, key := range []string{
"CHEZMOI_GITHUB_ACCESS_TOKEN",
"CHEZMOI_GITHUB_TOKEN",
"GITHUB_ACCESS_TOKEN",
"GITHUB_TOKEN",
} {
func NewGitHubClient(ctx context.Context, httpClient *http.Client, host string) *github.Client {
for _, key := range accessTokenEnvKeys(host) {
if accessToken := os.Getenv(key); accessToken != "" {
httpClient = oauth2.NewClient(
context.WithValue(ctx, oauth2.HTTPClient, httpClient),
Expand All @@ -29,3 +25,39 @@ func NewGitHubClient(ctx context.Context, httpClient *http.Client) *github.Clien
}
return github.NewClient(httpClient)
}

func accessTokenEnvKeys(host string) []string {
var keys []string
for _, chezmoiKey := range []string{"CHEZMOI", ""} {
hostKeys := []string{makeHostKey(host)}
if host == "github.com" {
hostKeys = append(hostKeys, "GITHUB")
}
for _, hostKey := range hostKeys {
for _, accessKey := range []string{"ACCESS", ""} {
key := strings.Join(nonEmpty([]string{chezmoiKey, hostKey, accessKey, "TOKEN"}), "_")
keys = append(keys, key)
}
}
}
return keys
}

func makeHostKey(host string) string {
// FIXME split host on non-ASCII characters
// FIXME convert everything to uppercase
// FIXME join components with _
return host
}

func nonEmpty[S []T, T comparable](s S) S {
// FIXME use something from slices for this
result := make([]T, 0, len(s))
var zero T
for _, e := range s {
if e != zero {
result = append(result, e)
}
}
return result
}
27 changes: 27 additions & 0 deletions internal/chezmoi/github_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package chezmoi

import (
"testing"

"github.com/alecthomas/assert/v2"
)

func TestAccessTokenEnvKeys(t *testing.T) {
for _, tc := range []struct {
host string
expected []string
}{
{
expected: []string{
"CHEZMOI_GITHUB_ACCESS_TOKEN",
"CHEZMOI_GITHUB_TOKEN",
"GITHUB_ACCESS_TOKEN",
"GITHUB_TOKEN",
},
},
} {
t.Run(tc.host, func(t *testing.T) {
assert.Equal(t, tc.expected, accessTokenEnvKeys(tc.host))
})
}
}
5 changes: 5 additions & 0 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,11 @@ func newConfig(options ...configOption) (*Config, error) {
homeDir: userHomeDir,
templateFuncs: sprig.TxtFuncMap(),

// Password manager data.
gitHub: gitHubData{
clientsByHost: make(map[string]gitHubClientResult),
},

// Command configurations.
apply: applyCmdConfig{
filter: chezmoi.NewEntryTypeFilter(chezmoi.EntryTypesAll, chezmoi.EntryTypesNone),
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/doctorcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ func (c *latestVersionCheck) Run(system chezmoi.System, homeDirAbsPath chezmoi.A

ctx := context.Background()

gitHubClient := chezmoi.NewGitHubClient(ctx, c.httpClient)
gitHubClient := chezmoi.NewGitHubClient(ctx, c.httpClient, "github.com")
rr, _, err := gitHubClient.Repositories.GetLatestRelease(ctx, "twpayne", "chezmoi")
var rateLimitErr *github.RateLimitError
var abuseRateLimitErr *github.AbuseRateLimitError
Expand Down
Loading
Loading