Use config to look up user for status

This commit is contained in:
William Martin 2023-11-30 12:04:14 +01:00
parent 08c7bd1df2
commit 5d10beb60a
2 changed files with 3 additions and 34 deletions

View file

@ -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)

View file

@ -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