Skip to content

Commit

Permalink
chore: snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jun 24, 2024
1 parent b4b5565 commit 246ad0e
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 70 deletions.
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

0 comments on commit 246ad0e

Please sign in to comment.