cli: Repo commands assume non-enterprise auth and fail trying to auth against github.com

Describe the bug

I’ve configured enterprise GitHub auth, but when I run a command like gh repo clone "ndempkowski/python-stuff" which refers to an enterprise repository, it seems like the cli assumes that I want to resolve against github.com. This leads to an error instead of my repo being cloned.

Steps to reproduce the behavior

  1. Authorize as an enterprise user with token auth (I think this also affects web-based login)
  2. Try to clone an enterprise repo using the OWNER/REPO format
  3. Get an error about authentication.

Example:

~/Repos [1] $ gh --version
gh version 1.0.0 (2020-09-16)
https://github.com/cli/cli/releases/tag/v1.0.0
~/Repos $ gh repo clone "ndempkowski/python-stuff"
non-200 OK status code: 401 Unauthorized body: "{\"message\":\"This endpoint requires you to be authenticated.\",\"documentation_url\":\"https://docs.github.com/v3/#authentication\"}"

Yet my auth status is shown as correctly configured:

~/Repos [1] $ gh auth status
git.hubteam.com
  ✓ Logged in to git.xxxxxxx.com as ndempkowski (~/.config/gh/hosts.yml)
  ✓ Git operations for git.xxxxxxx.com configured to use ssh protocol.

Expected vs actual behavior

I expect to be able to clone a repo using the OWNER/REPO format instead of seeing an error.

Logs

See above

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 7
  • Comments: 15 (7 by maintainers)

Most upvoted comments

I still wonder - when I have a single GHE entry, and no github.com entry in my hosts.yml - why should gh gist (and other commands) still default to contacting gihub.com?

It seems having GH_HOST auto-detect/default when there is only one host configured should resolve this issue in most cases?

Ideally, it would be great if I could set up different gh default hosts per directory prefix, like I can set up different git default user and email using includeIf:

In ~/.gitconfig, add:

[includeIf "gitdir:~/toplevelFolder1/"]
    path = ~/topLevelFolder1/.gitconfig_include

https://stackoverflow.com/a/48088291/771768

After doing the above for about a month, I still get the impression the public/enterprise combined workflow could be cleaner. So I opened https://github.com/cli/cli/issues/6688 as a feature proposal.

I was hoping there might be a way to set a default in hosts.yml, but just confirming that export GH_HOST=example.com does the trick and with https://direnv.net/ it’s easy enough to set that per-directory.

@jherland You are right. We are leaning to the solution you propose: to default to your non-github.com host if only one host was configured.

However, if you configure one Enterprise host, and then additionally authenticate to github.com as well, we will start defaulting to github.com again. This was our original reasoning to always default to github.com—to follow the principle of least surprise—but our users seem to predominately want to default to whichever one host they authenticated to.

@natdempk For now, commands require you to explicitly set a hostname if it’s not github.com:

gh repo clone example.com/owner/repo

If that proves to be tedious, you can set the GH_HOST environment variable with the name of your host:

# ~/.bash_profile
export GH_HOST=example.com

Be warned though, if you then want to clone owner/repo from github.com, you’ll have to type gh repo clone github.com/owner/repo.

Is the current design and workaround acceptable for you, or did you expect the command to work differently? Sorry for the confusion.