Merge pull request #6347 from cli/respect-GH_HOST

Respect GH_HOST in `auth login`, `repo delete`
This commit is contained in:
Mislav Marohnić 2022-09-26 15:48:11 +02:00 committed by GitHub
commit ec49359467
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 14 deletions

View file

@ -50,7 +50,7 @@ func FromFullName(nwo string) (Interface, error) {
// FromFullNameWithHost is like FromFullName that defaults to a specific host for values that don't
// explicitly include a hostname.
func FromFullNameWithHost(nwo, fallbackHost string) (Interface, error) {
repo, err := repository.ParseWithHost(nwo, defaultHost())
repo, err := repository.ParseWithHost(nwo, fallbackHost)
if err != nil {
return nil, err
}

View file

@ -12,6 +12,7 @@ import (
"github.com/cli/cli/v2/pkg/cmd/auth/shared"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
ghAuth "github.com/cli/go-gh/pkg/auth"
"github.com/spf13/cobra"
)
@ -99,7 +100,7 @@ func NewCmdLogin(f *cmdutil.Factory, runF func(*LoginOptions) error) *cobra.Comm
}
if opts.Hostname == "" && (!opts.Interactive || opts.Web) {
opts.Hostname = ghinstance.Default()
opts.Hostname, _ = ghAuth.DefaultHost()
}
opts.MainExecutable = f.Executable()

View file

@ -31,22 +31,34 @@ func stubHomeDir(t *testing.T, dir string) {
func Test_NewCmdLogin(t *testing.T) {
tests := []struct {
name string
cli string
stdin string
stdinTTY bool
wants LoginOptions
wantsErr bool
name string
cli string
stdin string
stdinTTY bool
defaultHost string
wants LoginOptions
wantsErr bool
}{
{
name: "nontty, with-token",
stdin: "abc123\n",
cli: "--with-token",
name: "nontty, with-token",
stdin: "abc123\n",
cli: "--with-token",
defaultHost: "github.com",
wants: LoginOptions{
Hostname: "github.com",
Token: "abc123",
},
},
{
name: "nontty, Enterprise host",
stdin: "abc123\n",
cli: "--with-token",
defaultHost: "git.example.com",
wants: LoginOptions{
Hostname: "git.example.com",
Token: "abc123",
},
},
{
name: "tty, with-token",
stdinTTY: true,
@ -163,6 +175,8 @@ func Test_NewCmdLogin(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("GH_HOST", tt.defaultHost)
ios, stdin, _, _ := iostreams.Test()
f := &cmdutil.Factory{
IOStreams: ios,

View file

@ -6,12 +6,11 @@ import (
"strings"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/ghinstance"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/internal/prompter"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
ghAuth "github.com/cli/go-gh/pkg/auth"
"github.com/spf13/cobra"
)
@ -78,7 +77,8 @@ func deleteRun(opts *DeleteOptions) error {
} else {
repoSelector := opts.RepoArg
if !strings.Contains(repoSelector, "/") {
currentUser, err := api.CurrentLoginName(apiClient, ghinstance.Default())
defaultHost, _ := ghAuth.DefaultHost()
currentUser, err := api.CurrentLoginName(apiClient, defaultHost)
if err != nil {
return err
}