From 1eecfd45301635e5a2f2f3375cd0445fc98a5668 Mon Sep 17 00:00:00 2001 From: Velmurugan Date: Sat, 24 Aug 2024 16:24:51 +0530 Subject: [PATCH 1/2] Added `--active` flag to the `gh auth status` command --- pkg/cmd/auth/status/status.go | 6 ++++++ pkg/cmd/auth/status/status_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/pkg/cmd/auth/status/status.go b/pkg/cmd/auth/status/status.go index 2139bda1e25..73ee084e989 100644 --- a/pkg/cmd/auth/status/status.go +++ b/pkg/cmd/auth/status/status.go @@ -129,6 +129,7 @@ type StatusOptions struct { Hostname string ShowToken bool + Active bool } func NewCmdStatus(f *cmdutil.Factory, runF func(*StatusOptions) error) *cobra.Command { @@ -163,6 +164,7 @@ func NewCmdStatus(f *cmdutil.Factory, runF func(*StatusOptions) error) *cobra.Co cmd.Flags().StringVarP(&opts.Hostname, "hostname", "h", "", "Check only a specific hostname's auth status") cmd.Flags().BoolVarP(&opts.ShowToken, "show-token", "t", false, "Display the auth token") + cmd.Flags().BoolVarP(&opts.Active, "active", "a", false, "Display the active account only") return cmd } @@ -224,6 +226,10 @@ func statusRun(opts *StatusOptions) error { err = cmdutil.SilentError } + if opts.Active { + continue + } + users := authCfg.UsersForHost(hostname) for _, username := range users { if username == activeUser { diff --git a/pkg/cmd/auth/status/status_test.go b/pkg/cmd/auth/status/status_test.go index 03c26ca0489..0f41ac8abc3 100644 --- a/pkg/cmd/auth/status/status_test.go +++ b/pkg/cmd/auth/status/status_test.go @@ -44,6 +44,13 @@ func Test_NewCmdStatus(t *testing.T) { ShowToken: true, }, }, + { + name: "active", + cli: "--active", + wants: StatusOptions{ + Active: true, + }, + }, } for _, tt := range tests { @@ -433,6 +440,27 @@ func Test_statusRun(t *testing.T) { - To forget about this account, run: gh auth logout -h ghe.io -u monalisa-ghe `), }, + { + name: "active", + opts: StatusOptions{ + Active: true, + }, + cfgStubs: func(t *testing.T, c gh.Config) { + login(t, c, "github.com", "monalisa", "gho_abc123", "https") + login(t, c, "github.com", "monalisa-2", "gho_abc123", "https") + }, + httpStubs: func(reg *httpmock.Registry) { + reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("repo,read:org")) + }, + wantOut: heredoc.Doc(` + github.com + ✓ Logged in to github.com account monalisa-2 (GH_CONFIG_DIR/hosts.yml) + - Active account: true + - Git operations protocol: https + - Token: gho_****** + - Token scopes: 'repo', 'read:org' + `), + }, } for _, tt := range tests { From 03c34e71f96891cb5b944bde959be1b873643528 Mon Sep 17 00:00:00 2001 From: Andy Feller Date: Fri, 30 Aug 2024 10:57:51 -0400 Subject: [PATCH 2/2] Expand active test cases --- pkg/cmd/auth/status/status_test.go | 73 ++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/auth/status/status_test.go b/pkg/cmd/auth/status/status_test.go index 0f41ac8abc3..3f16baf4699 100644 --- a/pkg/cmd/auth/status/status_test.go +++ b/pkg/cmd/auth/status/status_test.go @@ -389,14 +389,14 @@ func Test_statusRun(t *testing.T) { { name: "multiple hosts with multiple accounts with environment tokens and with errors", opts: StatusOptions{}, - env: map[string]string{"GH_ENTERPRISE_TOKEN": "gho_abc123"}, + env: map[string]string{"GH_ENTERPRISE_TOKEN": "gho_abc123"}, // monalisa-ghe-2 cfgStubs: func(t *testing.T, c gh.Config) { login(t, c, "github.com", "monalisa", "gho_def456", "https") login(t, c, "github.com", "monalisa-2", "gho_ghi789", "https") login(t, c, "ghe.io", "monalisa-ghe", "gho_xyz123", "ssh") }, httpStubs: func(reg *httpmock.Registry) { - // Get scopes for monalia-2 + // Get scopes for monalisa-2 reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("repo,read:org")) // Get scopes for monalisa reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("repo")) @@ -441,7 +441,7 @@ func Test_statusRun(t *testing.T) { `), }, { - name: "active", + name: "multiple accounts on a host, only active users", opts: StatusOptions{ Active: true, }, @@ -461,6 +461,73 @@ func Test_statusRun(t *testing.T) { - Token scopes: 'repo', 'read:org' `), }, + { + name: "multiple hosts with multiple accounts, only active users", + opts: StatusOptions{ + Active: true, + }, + cfgStubs: func(t *testing.T, c gh.Config) { + login(t, c, "github.com", "monalisa", "gho_abc123", "https") + login(t, c, "github.com", "monalisa-2", "gho_abc123", "https") + login(t, c, "ghe.io", "monalisa-ghe", "gho_abc123", "ssh") + login(t, c, "ghe.io", "monalisa-ghe-2", "gho_abc123", "ssh") + }, + httpStubs: func(reg *httpmock.Registry) { + // Get scopes for monalisa-2 + reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("repo,read:org")) + // Get scopes for monalisa-ghe-2 + reg.Register(httpmock.REST("GET", "api/v3/"), httpmock.ScopesResponder("repo,read:org")) + }, + wantOut: heredoc.Doc(` + github.com + ✓ Logged in to github.com account monalisa-2 (GH_CONFIG_DIR/hosts.yml) + - Active account: true + - Git operations protocol: https + - Token: gho_****** + - Token scopes: 'repo', 'read:org' + + ghe.io + ✓ Logged in to ghe.io account monalisa-ghe-2 (GH_CONFIG_DIR/hosts.yml) + - Active account: true + - Git operations protocol: ssh + - Token: gho_****** + - Token scopes: 'repo', 'read:org' + `), + }, + { + name: "multiple hosts with multiple accounts, only active users with errors", + opts: StatusOptions{ + Active: true, + }, + cfgStubs: func(t *testing.T, c gh.Config) { + login(t, c, "github.com", "monalisa", "gho_abc123", "https") + login(t, c, "github.com", "monalisa-2", "gho_abc123", "https") + login(t, c, "ghe.io", "monalisa-ghe", "gho_abc123", "ssh") + login(t, c, "ghe.io", "monalisa-ghe-2", "gho_abc123", "ssh") + }, + httpStubs: func(reg *httpmock.Registry) { + // Get scopes for monalisa-2 + reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("repo,read:org")) + // Error getting scopes for monalisa-ghe-2 + reg.Register(httpmock.REST("GET", "api/v3/"), httpmock.StatusStringResponse(404, "{}")) + }, + wantErr: cmdutil.SilentError, + wantErrOut: heredoc.Doc(` + github.com + ✓ Logged in to github.com account monalisa-2 (GH_CONFIG_DIR/hosts.yml) + - Active account: true + - Git operations protocol: https + - Token: gho_****** + - Token scopes: 'repo', 'read:org' + + ghe.io + X Failed to log in to ghe.io account monalisa-ghe-2 (GH_CONFIG_DIR/hosts.yml) + - Active account: true + - The token in GH_CONFIG_DIR/hosts.yml is invalid. + - To re-authenticate, run: gh auth login -h ghe.io + - To forget about this account, run: gh auth logout -h ghe.io -u monalisa-ghe-2 + `), + }, } for _, tt := range tests {