X Tutup
Skip to content

Commit 2bb5e05

Browse files
committed
Send GITHUB_TOKEN to github.com and GITHUB_ENTERPRISE_TOKEN to GHES
1 parent 3e7a258 commit 2bb5e05

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

cmd/gh/main.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,17 @@ func main() {
9292
}
9393
}
9494

95-
authCheckEnabled := cmdutil.IsAuthCheckEnabled(cmd)
96-
97-
// TODO support other names
98-
ghtoken := os.Getenv("GITHUB_TOKEN")
99-
if ghtoken != "" {
100-
authCheckEnabled = false
101-
}
102-
95+
authCheckEnabled := os.Getenv("GITHUB_TOKEN") == "" &&
96+
os.Getenv("GITHUB_ENTERPRISE_TOKEN") == "" &&
97+
cmdutil.IsAuthCheckEnabled(cmd)
10398
if authCheckEnabled {
104-
hasAuth := false
105-
10699
cfg, err := cmdFactory.Config()
107-
if err == nil {
108-
hasAuth = cmdutil.CheckAuth(cfg)
100+
if err != nil {
101+
fmt.Fprintf(stderr, "failed to read configuration: %s\n", err)
102+
os.Exit(2)
109103
}
110104

111-
if !hasAuth {
105+
if !cmdutil.CheckAuth(cfg) {
112106
fmt.Fprintln(stderr, utils.Bold("Welcome to GitHub CLI!"))
113107
fmt.Fprintln(stderr)
114108
fmt.Fprintln(stderr, "To authenticate, please run `gh auth login`.")

pkg/cmd/factory/http.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ func httpClient(io *iostreams.IOStreams, cfg config.Config, appVersion string, s
2323
opts = append(opts,
2424
api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", appVersion)),
2525
api.AddHeaderFunc("Authorization", func(req *http.Request) (string, error) {
26-
if token := os.Getenv("GITHUB_TOKEN"); token != "" {
26+
hostname := ghinstance.NormalizeHostname(req.URL.Hostname())
27+
28+
if token := os.Getenv("GITHUB_TOKEN"); hostname == ghinstance.Default() && token != "" {
29+
return fmt.Sprintf("token %s", token), nil
30+
}
31+
if token := os.Getenv("GITHUB_ENTERPRISE_TOKEN"); ghinstance.IsEnterprise(hostname) && token != "" {
2732
return fmt.Sprintf("token %s", token), nil
2833
}
2934

30-
hostname := ghinstance.NormalizeHostname(req.URL.Hostname())
3135
token, err := cfg.Get(hostname, "oauth_token")
3236
if err != nil || token == "" {
3337
// Users shouldn't see this because of the pre-execute auth check on commands

pkg/cmd/root/root.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command {
4141
Open an issue using “gh issue create -R cli/cli”
4242
`),
4343
"help:environment": heredoc.Doc(`
44-
GITHUB_TOKEN: an authentication token for API requests. Setting this avoids being
45-
prompted to authenticate and overrides any previously stored credentials.
44+
GITHUB_TOKEN: an authentication token for github.com API requests. Setting this avoids
45+
being prompted to authenticate and takes precedence over previously stored credentials.
46+
47+
GITHUB_ENTERPRISE_TOKEN: an authentication token for API requests to GitHub Enterprise.
4648
4749
GH_REPO: specify the GitHub repository in the "[HOST/]OWNER/REPO" format for commands
4850
that otherwise operate on a local repository.

0 commit comments

Comments
 (0)
X Tutup