X Tutup
Skip to content

Commit fdad37e

Browse files
authored
Merge pull request cli#4019 from cli/enterprise-env
Fix error message when using GH_ENTERPRISE_TOKEN but host is ambiguous
2 parents fa354a9 + 82c6fb7 commit fdad37e

File tree

9 files changed

+51
-19
lines changed

9 files changed

+51
-19
lines changed

internal/config/from_env.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,11 @@ func AuthTokenProvidedFromEnv() bool {
106106
os.Getenv(GH_TOKEN) != "" ||
107107
os.Getenv(GITHUB_TOKEN) != ""
108108
}
109+
110+
func IsHostEnv(src string) bool {
111+
return src == GH_HOST
112+
}
113+
114+
func IsEnterpriseEnv(src string) bool {
115+
return src == GH_ENTERPRISE_TOKEN || src == GITHUB_ENTERPRISE_TOKEN
116+
}

internal/config/from_env_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestInheritEnv(t *testing.T) {
4444
baseConfig: ``,
4545
hostname: "github.com",
4646
wants: wants{
47-
hosts: []string(nil),
47+
hosts: []string{},
4848
token: "",
4949
source: ".config.gh.config.yml",
5050
writeable: true,
@@ -104,7 +104,7 @@ func TestInheritEnv(t *testing.T) {
104104
GITHUB_ENTERPRISE_TOKEN: "ENTOKEN",
105105
hostname: "example.org",
106106
wants: wants{
107-
hosts: []string(nil),
107+
hosts: []string{},
108108
token: "ENTOKEN",
109109
source: "GITHUB_ENTERPRISE_TOKEN",
110110
writeable: false,
@@ -116,7 +116,7 @@ func TestInheritEnv(t *testing.T) {
116116
GH_ENTERPRISE_TOKEN: "ENTOKEN",
117117
hostname: "example.org",
118118
wants: wants{
119-
hosts: []string(nil),
119+
hosts: []string{},
120120
token: "ENTOKEN",
121121
source: "GH_ENTERPRISE_TOKEN",
122122
writeable: false,
@@ -221,7 +221,7 @@ func TestInheritEnv(t *testing.T) {
221221
GITHUB_ENTERPRISE_TOKEN: "GITHUBTOKEN",
222222
hostname: "example.org",
223223
wants: wants{
224-
hosts: []string(nil),
224+
hosts: []string{},
225225
token: "GHTOKEN",
226226
source: "GH_ENTERPRISE_TOKEN",
227227
writeable: false,

internal/config/from_file.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (c *fileConfig) UnsetHost(hostname string) {
104104
func (c *fileConfig) configForHost(hostname string) (*HostConfig, error) {
105105
hosts, err := c.hostEntries()
106106
if err != nil {
107-
return nil, fmt.Errorf("failed to parse hosts config: %w", err)
107+
return nil, err
108108
}
109109

110110
for _, hc := range hosts {
@@ -209,7 +209,7 @@ func (c *fileConfig) Aliases() (*AliasConfig, error) {
209209
func (c *fileConfig) hostEntries() ([]*HostConfig, error) {
210210
entry, err := c.FindEntry("hosts")
211211
if err != nil {
212-
return nil, fmt.Errorf("could not find hosts config: %w", err)
212+
return []*HostConfig{}, nil
213213
}
214214

215215
hostConfigs, err := c.parseHosts(entry.ValueNode)

internal/config/from_file_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package config
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func Test_fileConfig_Hosts(t *testing.T) {
11+
c := NewBlankConfig()
12+
hosts, err := c.Hosts()
13+
require.NoError(t, err)
14+
assert.Equal(t, []string{}, hosts)
15+
}

pkg/cmd/auth/logout/logout.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ func logoutRun(opts *LogoutOptions) error {
7474

7575
candidates, err := cfg.Hosts()
7676
if err != nil {
77+
return err
78+
}
79+
if len(candidates) == 0 {
7780
return fmt.Errorf("not logged in to any hosts")
7881
}
7982

pkg/cmd/auth/refresh/refresh.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ func refreshRun(opts *RefreshOptions) error {
8383

8484
candidates, err := cfg.Hosts()
8585
if err != nil {
86+
return err
87+
}
88+
if len(candidates) == 0 {
8689
return fmt.Errorf("not logged in to any hosts. Use 'gh auth login' to authenticate with a host")
8790
}
8891

pkg/cmd/auth/status/status.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ func statusRun(opts *StatusOptions) error {
6969
statusInfo := map[string][]string{}
7070

7171
hostnames, err := cfg.Hosts()
72-
if len(hostnames) == 0 || err != nil {
72+
if err != nil {
73+
return err
74+
}
75+
if len(hostnames) == 0 {
7376
fmt.Fprintf(stderr,
7477
"You are not logged into any GitHub hosts. Run %s to authenticate.\n", cs.Bold("gh auth login"))
7578
return cmdutil.SilentError

pkg/cmd/factory/remote_resolver.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package factory
22

33
import (
44
"errors"
5+
"fmt"
56
"net/url"
67
"sort"
78

@@ -12,8 +13,6 @@ import (
1213
"github.com/cli/cli/pkg/set"
1314
)
1415

15-
const GH_HOST = "GH_HOST"
16-
1716
type remoteResolver struct {
1817
readRemotes func() (git.RemoteSet, error)
1918
getConfig func() (config.Config, error)
@@ -75,18 +74,19 @@ func (rr *remoteResolver) Resolver() func() (context.Remotes, error) {
7574
// For enviornment default host (GH_HOST) do not fallback to cachedRemotes if none match
7675
if src != "" {
7776
filteredRemotes := cachedRemotes.FilterByHosts([]string{defaultHost})
78-
if src == GH_HOST || len(filteredRemotes) > 0 {
77+
if config.IsHostEnv(src) || len(filteredRemotes) > 0 {
7978
cachedRemotes = filteredRemotes
8079
}
8180
}
8281

8382
if len(cachedRemotes) == 0 {
84-
if src == GH_HOST {
85-
remotesError = errors.New("none of the git remotes configured for this repository correspond to the GH_HOST environment variable. Try adding a matching remote or unsetting the variable.")
86-
} else {
87-
remotesError = errors.New("none of the git remotes configured for this repository point to a known GitHub host. To tell gh about a new GitHub host, please use `gh auth login`")
83+
dummyHostname := "example.com" // any non-github.com hostname is fine here
84+
if config.IsHostEnv(src) {
85+
return nil, fmt.Errorf("none of the git remotes configured for this repository correspond to the %s environment variable. Try adding a matching remote or unsetting the variable.", src)
86+
} else if v, src, _ := cfg.GetWithSource(dummyHostname, "oauth_token"); v != "" && config.IsEnterpriseEnv(src) {
87+
return nil, errors.New("set the GH_HOST environment variable to specify which GitHub host to use")
8888
}
89-
return nil, remotesError
89+
return nil, errors.New("none of the git remotes configured for this repository point to a known GitHub host. To tell gh about a new GitHub host, please use `gh auth login`")
9090
}
9191

9292
return cachedRemotes, nil

pkg/cmd/root/help_topic.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ var HelpTopics = map[string]map[string]string{
3333
previously stored credentials.
3434
3535
GH_ENTERPRISE_TOKEN, GITHUB_ENTERPRISE_TOKEN (in order of precedence): an authentication
36-
token for API requests to GitHub Enterprise.
36+
token for API requests to GitHub Enterprise. When setting this, also set GH_HOST.
37+
38+
GH_HOST: specify the GitHub hostname for commands that would otherwise assume the
39+
"github.com" host when not in a context of an existing repository.
3740
3841
GH_REPO: specify the GitHub repository in the "[HOST/]OWNER/REPO" format for commands
3942
that otherwise operate on a local repository.
4043
41-
GH_HOST: specify the GitHub hostname for commands that would otherwise assume
42-
the "github.com" host when not in a context of an existing repository.
43-
4444
GH_EDITOR, GIT_EDITOR, VISUAL, EDITOR (in order of precedence): the editor tool to use
4545
for authoring text.
4646

0 commit comments

Comments
 (0)
X Tutup