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

Add default host as known host when environment auth token exists #36

Merged
merged 1 commit into from May 9, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/config/config.go
Expand Up @@ -72,6 +72,9 @@ func (c config) Hosts() []string {
if host := os.Getenv(ghHost); host != "" {
hosts.Add(host)
}
if token, _ := c.AuthToken(defaultHost); token != "" {
hosts.Add(defaultHost)
}
entries := c.hosts.keys()
hosts.AddValues(entries)
return hosts.ToSlice()
Expand Down
59 changes: 59 additions & 0 deletions internal/config/config_test.go
Expand Up @@ -398,6 +398,65 @@ func TestConfigHost(t *testing.T) {
}
}

func TestConfigHosts(t *testing.T) {
tests := []struct {
name string
cfg Config
ghHost string
ghToken string
wantHosts []string
}{
{
name: "no known hosts",
cfg: testLoadedNoHostConfig(),
wantHosts: []string{},
},
{
name: "includes GH_HOST",
cfg: testLoadedNoHostConfig(),
ghHost: "test.com",
wantHosts: []string{"test.com"},
},
{
name: "includes authenticated hosts",
cfg: testLoadedConfig(),
wantHosts: []string{"github.com", "enterprise.com"},
},
{
name: "includes default host if environment auth token",
cfg: testLoadedNoHostConfig(),
ghToken: "TOKEN",
wantHosts: []string{"github.com"},
},
{
name: "deduplicates hosts",
cfg: testLoadedConfig(),
ghHost: "test.com",
ghToken: "TOKEN",
wantHosts: []string{"test.com", "github.com", "enterprise.com"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.ghHost != "" {
k := "GH_HOST"
old := os.Getenv(k)
os.Setenv(k, tt.ghHost)
defer os.Setenv(k, old)
}
if tt.ghToken != "" {
k := "GH_TOKEN"
old := os.Getenv(k)
os.Setenv(k, tt.ghToken)
defer os.Setenv(k, old)
}
hosts := tt.cfg.Hosts()
assert.Equal(t, tt.wantHosts, hosts)
})
}
}

func TestConfigAuthToken(t *testing.T) {
orig_GITHUB_TOKEN := os.Getenv("GITHUB_TOKEN")
orig_GITHUB_ENTERPRISE_TOKEN := os.Getenv("GITHUB_ENTERPRISE_TOKEN")
Expand Down
1 change: 1 addition & 0 deletions internal/set/string_set.go
Expand Up @@ -10,6 +10,7 @@ type stringSet struct {
func NewStringSet() *stringSet {
s := &stringSet{}
s.m = make(map[string]struct{})
s.v = []string{}
return s
}

Expand Down