X Tutup
Skip to content

Commit ec25b73

Browse files
committed
gh auth status
1 parent 9f5daec commit ec25b73

File tree

9 files changed

+492
-47
lines changed

9 files changed

+492
-47
lines changed

api/client.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ func (err HTTPError) Error() string {
196196
return fmt.Sprintf("HTTP %d (%s)", err.StatusCode, err.RequestURL)
197197
}
198198

199+
type MissingScopesError struct {
200+
error
201+
}
202+
199203
func (c Client) HasMinimumScopes(hostname string) (bool, error) {
200204
apiEndpoint := ghinstance.RESTPrefix(hostname)
201205

@@ -243,11 +247,10 @@ func (c Client) HasMinimumScopes(hostname string) (bool, error) {
243247
}
244248

245249
if len(errorMsgs) > 0 {
246-
return false, errors.New(strings.Join(errorMsgs, ";"))
250+
return false, &MissingScopesError{error: errors.New(strings.Join(errorMsgs, ";"))}
247251
}
248252

249253
return true, nil
250-
251254
}
252255

253256
// GraphQL performs a GraphQL request and parses the response

command/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
authCmd "github.com/cli/cli/pkg/cmd/auth"
2626
authLoginCmd "github.com/cli/cli/pkg/cmd/auth/login"
2727
authLogoutCmd "github.com/cli/cli/pkg/cmd/auth/logout"
28+
authStatusCmd "github.com/cli/cli/pkg/cmd/auth/status"
2829
gistCreateCmd "github.com/cli/cli/pkg/cmd/gist/create"
2930
prCmd "github.com/cli/cli/pkg/cmd/pr"
3031
repoCmd "github.com/cli/cli/pkg/cmd/repo"
@@ -138,6 +139,7 @@ func init() {
138139
RootCmd.AddCommand(authCmd.Cmd)
139140
authCmd.Cmd.AddCommand(authLoginCmd.NewCmdLogin(cmdFactory, nil))
140141
authCmd.Cmd.AddCommand(authLogoutCmd.NewCmdLogout(cmdFactory, nil))
142+
authCmd.Cmd.AddCommand(authStatusCmd.NewCmdStatus(cmdFactory, nil))
141143

142144
resolvedBaseRepo := func() (ghrepo.Interface, error) {
143145
httpClient, err := cmdFactory.HttpClient()

pkg/cmd/auth/auth.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,4 @@ var Cmd = &cobra.Command{
88
Use: "auth <command>",
99
Short: "Login, logout, and refresh your authentication",
1010
Long: `Manage gh's authentication state.`,
11-
// TODO this all doesn't exist yet
12-
//Example: heredoc.Doc(`
13-
// $ gh auth login
14-
// $ gh auth status
15-
// $ gh auth refresh --scopes gist
16-
// $ gh auth logout
17-
//`),
1811
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package login
1+
package client
22

33
import (
44
"fmt"
@@ -8,8 +8,8 @@ import (
88
"github.com/cli/cli/internal/config"
99
)
1010

11-
func validateHostCfg(hostname string, cfg config.Config) error {
12-
apiClient, err := clientFromCfg(hostname, cfg)
11+
func ValidateHostCfg(hostname string, cfg config.Config) error {
12+
apiClient, err := ClientFromCfg(hostname, cfg)
1313
if err != nil {
1414
return err
1515
}
@@ -22,7 +22,7 @@ func validateHostCfg(hostname string, cfg config.Config) error {
2222
return nil
2323
}
2424

25-
var clientFromCfg = func(hostname string, cfg config.Config) (*api.Client, error) {
25+
var ClientFromCfg = func(hostname string, cfg config.Config) (*api.Client, error) {
2626
var opts []api.ClientOption
2727

2828
token, err := cfg.Get(hostname, "oauth_token")

pkg/cmd/auth/login/login.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/cli/cli/api"
1313
"github.com/cli/cli/internal/config"
1414
"github.com/cli/cli/internal/ghinstance"
15+
"github.com/cli/cli/pkg/cmd/auth/client"
1516
"github.com/cli/cli/pkg/cmdutil"
1617
"github.com/cli/cli/pkg/iostreams"
1718
"github.com/cli/cli/pkg/prompt"
@@ -119,7 +120,7 @@ func loginRun(opts *LoginOptions) error {
119120
return err
120121
}
121122

122-
err = validateHostCfg(opts.Hostname, cfg)
123+
err = client.ValidateHostCfg(opts.Hostname, cfg)
123124
if err != nil {
124125
return err
125126
}
@@ -167,9 +168,9 @@ func loginRun(opts *LoginOptions) error {
167168
existingToken, _ := cfg.Get(hostname, "oauth_token")
168169

169170
if existingToken != "" {
170-
err := validateHostCfg(hostname, cfg)
171+
err := client.ValidateHostCfg(hostname, cfg)
171172
if err == nil {
172-
apiClient, err := clientFromCfg(hostname, cfg)
173+
apiClient, err := client.ClientFromCfg(hostname, cfg)
173174
if err != nil {
174175
return err
175176
}
@@ -235,7 +236,7 @@ func loginRun(opts *LoginOptions) error {
235236
return err
236237
}
237238

238-
err = validateHostCfg(hostname, cfg)
239+
err = client.ValidateHostCfg(hostname, cfg)
239240
if err != nil {
240241
return err
241242
}
@@ -263,7 +264,7 @@ func loginRun(opts *LoginOptions) error {
263264

264265
fmt.Fprintf(opts.IO.ErrOut, "%s Configured git protocol\n", utils.GreenCheck())
265266

266-
apiClient, err := clientFromCfg(hostname, cfg)
267+
apiClient, err := client.ClientFromCfg(hostname, cfg)
267268
if err != nil {
268269
return err
269270
}

pkg/cmd/auth/login/login_test.go

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package login
22

33
import (
44
"bytes"
5-
"io/ioutil"
65
"net/http"
76
"os"
87
"regexp"
98
"testing"
109

1110
"github.com/cli/cli/api"
1211
"github.com/cli/cli/internal/config"
12+
"github.com/cli/cli/pkg/cmd/auth/client"
1313
"github.com/cli/cli/pkg/cmdutil"
1414
"github.com/cli/cli/pkg/httpmock"
1515
"github.com/cli/cli/pkg/iostreams"
@@ -164,19 +164,6 @@ func Test_NewCmdLogin(t *testing.T) {
164164
}
165165
}
166166

167-
func scopesResponder(scopes string) func(*http.Request) (*http.Response, error) {
168-
return func(req *http.Request) (*http.Response, error) {
169-
return &http.Response{
170-
StatusCode: 200,
171-
Request: req,
172-
Header: map[string][]string{
173-
"X-Oauth-Scopes": {scopes},
174-
},
175-
Body: ioutil.NopCloser(bytes.NewBufferString("")),
176-
}, nil
177-
}
178-
}
179-
180167
func Test_loginRun_nontty(t *testing.T) {
181168
tests := []struct {
182169
name string
@@ -200,7 +187,7 @@ func Test_loginRun_nontty(t *testing.T) {
200187
Token: "abc123",
201188
},
202189
httpStubs: func(reg *httpmock.Registry) {
203-
reg.Register(httpmock.REST("GET", "api/v3/"), scopesResponder("repo,read:org"))
190+
reg.Register(httpmock.REST("GET", "api/v3/"), httpmock.ScopesResponder("repo,read:org"))
204191
},
205192
wantHosts: "albert.wesker:\n oauth_token: abc123\n",
206193
},
@@ -211,7 +198,7 @@ func Test_loginRun_nontty(t *testing.T) {
211198
Token: "abc456",
212199
},
213200
httpStubs: func(reg *httpmock.Registry) {
214-
reg.Register(httpmock.REST("GET", ""), scopesResponder("read:org"))
201+
reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("read:org"))
215202
},
216203
wantErr: regexp.MustCompile(`missing required scope 'repo'`),
217204
},
@@ -222,7 +209,7 @@ func Test_loginRun_nontty(t *testing.T) {
222209
Token: "abc456",
223210
},
224211
httpStubs: func(reg *httpmock.Registry) {
225-
reg.Register(httpmock.REST("GET", ""), scopesResponder("repo"))
212+
reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("repo"))
226213
},
227214
wantErr: regexp.MustCompile(`missing required scope 'read:org'`),
228215
},
@@ -233,7 +220,7 @@ func Test_loginRun_nontty(t *testing.T) {
233220
Token: "abc456",
234221
},
235222
httpStubs: func(reg *httpmock.Registry) {
236-
reg.Register(httpmock.REST("GET", ""), scopesResponder("repo,admin:org"))
223+
reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("repo,admin:org"))
237224
},
238225
wantHosts: "github.com:\n oauth_token: abc456\n",
239226
},
@@ -252,19 +239,19 @@ func Test_loginRun_nontty(t *testing.T) {
252239
tt.opts.IO = io
253240
t.Run(tt.name, func(t *testing.T) {
254241
reg := &httpmock.Registry{}
255-
origClientFromCfg := clientFromCfg
242+
origClientFromCfg := client.ClientFromCfg
256243
defer func() {
257-
clientFromCfg = origClientFromCfg
244+
client.ClientFromCfg = origClientFromCfg
258245
}()
259-
clientFromCfg = func(_ string, _ config.Config) (*api.Client, error) {
246+
client.ClientFromCfg = func(_ string, _ config.Config) (*api.Client, error) {
260247
httpClient := &http.Client{Transport: reg}
261248
return api.NewClientFromHTTP(httpClient), nil
262249
}
263250

264251
if tt.httpStubs != nil {
265252
tt.httpStubs(reg)
266253
} else {
267-
reg.Register(httpmock.REST("GET", ""), scopesResponder("repo,read:org"))
254+
reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("repo,read:org"))
268255
}
269256

270257
mainBuf := bytes.Buffer{}
@@ -305,7 +292,7 @@ func Test_loginRun_Survey(t *testing.T) {
305292
_ = cfg.Set("github.com", "oauth_token", "ghi789")
306293
},
307294
httpStubs: func(reg *httpmock.Registry) {
308-
reg.Register(httpmock.REST("GET", ""), scopesResponder("repo,read:org,"))
295+
reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("repo,read:org,"))
309296
reg.Register(
310297
httpmock.GraphQL(`query UserCurrent\b`),
311298
httpmock.StringResponse(`{"data":{"viewer":{"login":"jillv"}}}`))
@@ -328,7 +315,7 @@ func Test_loginRun_Survey(t *testing.T) {
328315
as.StubOne("HTTPS") // git_protocol
329316
},
330317
httpStubs: func(reg *httpmock.Registry) {
331-
reg.Register(httpmock.REST("GET", "api/v3/"), scopesResponder("repo,read:org,"))
318+
reg.Register(httpmock.REST("GET", "api/v3/"), httpmock.ScopesResponder("repo,read:org,"))
332319
reg.Register(
333320
httpmock.GraphQL(`query UserCurrent\b`),
334321
httpmock.StringResponse(`{"data":{"viewer":{"login":"jillv"}}}`))
@@ -345,7 +332,7 @@ func Test_loginRun_Survey(t *testing.T) {
345332
as.StubOne("HTTPS") // git_protocol
346333
},
347334
httpStubs: func(reg *httpmock.Registry) {
348-
reg.Register(httpmock.REST("GET", "api/v3/"), scopesResponder("repo,read:org,"))
335+
reg.Register(httpmock.REST("GET", "api/v3/"), httpmock.ScopesResponder("repo,read:org,"))
349336
reg.Register(
350337
httpmock.GraphQL(`query UserCurrent\b`),
351338
httpmock.StringResponse(`{"data":{"viewer":{"login":"jillv"}}}`))
@@ -397,18 +384,18 @@ func Test_loginRun_Survey(t *testing.T) {
397384

398385
t.Run(tt.name, func(t *testing.T) {
399386
reg := &httpmock.Registry{}
400-
origClientFromCfg := clientFromCfg
387+
origClientFromCfg := client.ClientFromCfg
401388
defer func() {
402-
clientFromCfg = origClientFromCfg
389+
client.ClientFromCfg = origClientFromCfg
403390
}()
404-
clientFromCfg = func(_ string, _ config.Config) (*api.Client, error) {
391+
client.ClientFromCfg = func(_ string, _ config.Config) (*api.Client, error) {
405392
httpClient := &http.Client{Transport: reg}
406393
return api.NewClientFromHTTP(httpClient), nil
407394
}
408395
if tt.httpStubs != nil {
409396
tt.httpStubs(reg)
410397
} else {
411-
reg.Register(httpmock.REST("GET", ""), scopesResponder("repo,read:org,"))
398+
reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("repo,read:org,"))
412399
reg.Register(
413400
httpmock.GraphQL(`query UserCurrent\b`),
414401
httpmock.StringResponse(`{"data":{"viewer":{"login":"jillv"}}}`))

0 commit comments

Comments
 (0)
X Tutup