From 5d10beb60a2af2032d536aaf8f1ace7c10fc3f78 Mon Sep 17 00:00:00 2001 From: William Martin Date: Thu, 30 Nov 2023 12:04:14 +0100 Subject: [PATCH] Use config to look up user for status --- pkg/cmd/auth/status/status.go | 7 ++----- pkg/cmd/auth/status/status_test.go | 30 +----------------------------- 2 files changed, 3 insertions(+), 34 deletions(-) diff --git a/pkg/cmd/auth/status/status.go b/pkg/cmd/auth/status/status.go index 831dfc503..2f684597c 100644 --- a/pkg/cmd/auth/status/status.go +++ b/pkg/cmd/auth/status/status.go @@ -9,7 +9,6 @@ import ( "strings" "github.com/MakeNowJust/heredoc" - "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/internal/config" "github.com/cli/cli/v2/pkg/cmd/auth/shared" "github.com/cli/cli/v2/pkg/cmdutil" @@ -137,11 +136,9 @@ func statusRun(opts *StatusOptions) error { } failed = true } else { - apiClient := api.NewClientFromHTTP(httpClient) - username, err := api.CurrentLoginName(apiClient, hostname) + username, err := authCfg.User(hostname) if err != nil { - addMsg("%s %s: api call failed: %s", cs.Red("X"), hostname, err) - failed = true + return err } addMsg("%s Logged in to %s as %s (%s)", cs.SuccessIcon(), hostname, cs.Bold(username), tokenSource) diff --git a/pkg/cmd/auth/status/status_test.go b/pkg/cmd/auth/status/status_test.go index eb14ae42e..7017e80c9 100644 --- a/pkg/cmd/auth/status/status_test.go +++ b/pkg/cmd/auth/status/status_test.go @@ -117,15 +117,10 @@ func Test_statusRun(t *testing.T) { httpStubs: func(reg *httpmock.Registry) { // mocks for HeaderHasMinimumScopes api requests to a non-github.com host reg.Register(httpmock.REST("GET", "api/v3/"), httpmock.ScopesResponder("repo,read:org")) - // mock for CurrentLoginName - // TODO: Remove in favour of asking the config for the user - reg.Register( - httpmock.GraphQL(`query UserCurrent\b`), - httpmock.StringResponse(`{"data":{"viewer":{"login":"monalisa"}}}`)) }, wantOut: heredoc.Doc(` ghe.io - ✓ Logged in to ghe.io as monalisa (GH_CONFIG_DIR/hosts.yml) + ✓ Logged in to ghe.io as monalisa-ghe (GH_CONFIG_DIR/hosts.yml) ✓ Git operations for ghe.io configured to use https protocol. ✓ Token: ****** ✓ Token scopes: repo,read:org @@ -183,14 +178,6 @@ func Test_statusRun(t *testing.T) { reg.Register( httpmock.REST("GET", "api/v3/"), httpmock.WithHeader(httpmock.ScopesResponder("repo,read:org"), "X-Oauth-Scopes", "")) - // mock for CurrentLoginName, one for each host - // TODO: remove for user config - reg.Register( - httpmock.GraphQL(`query UserCurrent\b`), - httpmock.StringResponse(`{"data":{"viewer":{"login":"monalisa"}}}`)) - reg.Register( - httpmock.GraphQL(`query UserCurrent\b`), - httpmock.StringResponse(`{"data":{"viewer":{"login":"monalisa-ghe"}}}`)) }, wantOut: heredoc.Doc(` github.com @@ -217,10 +204,6 @@ func Test_statusRun(t *testing.T) { reg.Register( httpmock.REST("GET", ""), httpmock.ScopesResponder("")) - // mock for CurrentLoginName - reg.Register( - httpmock.GraphQL(`query UserCurrent\b`), - httpmock.StringResponse(`{"data":{"viewer":{"login":"monalisa"}}}`)) }, wantOut: heredoc.Doc(` github.com @@ -240,10 +223,6 @@ func Test_statusRun(t *testing.T) { reg.Register( httpmock.REST("GET", ""), httpmock.ScopesResponder("")) - // mock for CurrentLoginName - reg.Register( - httpmock.GraphQL(`query UserCurrent\b`), - httpmock.StringResponse(`{"data":{"viewer":{"login":"monalisa"}}}`)) }, wantOut: heredoc.Doc(` github.com @@ -266,13 +245,6 @@ func Test_statusRun(t *testing.T) { reg.Register(httpmock.REST("GET", "api/v3/"), httpmock.ScopesResponder("repo,read:org")) // mocks for HeaderHasMinimumScopes on github.com reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("repo,read:org")) - // mock for CurrentLoginName, one for each host - reg.Register( - httpmock.GraphQL(`query UserCurrent\b`), - httpmock.StringResponse(`{"data":{"viewer":{"login":"monalisa"}}}`)) - reg.Register( - httpmock.GraphQL(`query UserCurrent\b`), - httpmock.StringResponse(`{"data":{"viewer":{"login":"monalisa-ghe"}}}`)) }, wantOut: heredoc.Doc(` github.com