Skip to content

Commit 2f1e9ec

Browse files
committed
review feedback
1 parent f7ee39d commit 2f1e9ec

File tree

2 files changed

+16
-32
lines changed

2 files changed

+16
-32
lines changed

pkg/cmd/auth/refresh/refresh.go

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,34 @@ type RefreshOptions struct {
1919

2020
Hostname string
2121
Scopes []string
22+
AuthFlow func(config.Config, string, []string) error
2223
}
2324

2425
func NewCmdRefresh(f *cmdutil.Factory, runF func(*RefreshOptions) error) *cobra.Command {
2526
opts := &RefreshOptions{
2627
IO: f.IOStreams,
2728
Config: f.Config,
29+
AuthFlow: func(cfg config.Config, hostname string, scopes []string) error {
30+
_, err := config.AuthFlowWithConfig(cfg, hostname, "", scopes)
31+
return err
32+
},
2833
}
2934

3035
cmd := &cobra.Command{
3136
Use: "refresh",
3237
Args: cobra.ExactArgs(0),
33-
Short: "Request new scopes for a token",
34-
Long: heredoc.Doc(`Expand the permission scopes for a given host's token.
35-
36-
This command allows you to add additional scopes to an existing authentication token via a web
37-
browser. This enables gh to access more of the GitHub API, which may be required as gh adds
38-
features or as you use the gh api command.
39-
40-
Unfortunately at this time there is no way to add scopes without a web browser's involvement
41-
due to how GitHub authentication works.
42-
43-
The --hostname flag allows you to operate on a GitHub host other than github.com.
38+
Short: "Refresh stored authentication credentials",
39+
Long: heredoc.Doc(`Expand or fix the permission scopes for stored credentials
4440
45-
The --scopes flag accepts a comma separated list of scopes you want to add to a token. If
46-
absent, this command ensures that a host's token has the default set of scopes required by gh.
47-
48-
Note that if GITHUB_TOKEN is in the current environment, this command will not work.
41+
The --scopes flag accepts a comma separated list of scopes you want your gh credentials to have. If
42+
absent, this command ensures that gh has access to a minimum set of scopes.
4943
`),
5044
Example: heredoc.Doc(`
5145
$ gh auth refresh --scopes write:org,read:public_key
5246
# => open a browser to add write:org and read:public_key scopes for use with gh api
5347
5448
$ gh auth refresh
55-
# => ensure that the required minimum scopes are enabled for a token and open a browser to add if not
49+
# => open a browser to ensure your authentication credentials have the correct minimum scopes
5650
`),
5751
RunE: func(cmd *cobra.Command, args []string) error {
5852
if runF != nil {
@@ -64,7 +58,7 @@ func NewCmdRefresh(f *cmdutil.Factory, runF func(*RefreshOptions) error) *cobra.
6458
}
6559

6660
cmd.Flags().StringVarP(&opts.Hostname, "hostname", "h", "", "The GitHub host to use for authentication")
67-
cmd.Flags().StringSliceVarP(&opts.Scopes, "scopes", "s", []string{}, "Additional scopes to add to a token")
61+
cmd.Flags().StringSliceVarP(&opts.Scopes, "scopes", "s", nil, "Additional authentication scopes for gh to have")
6862

6963
return cmd
7064
}
@@ -118,10 +112,5 @@ func refreshRun(opts *RefreshOptions) error {
118112
}
119113
}
120114

121-
return doAuthFlow(cfg, hostname, opts.Scopes)
122-
}
123-
124-
var doAuthFlow = func(cfg config.Config, hostname string, scopes []string) error {
125-
_, err := config.AuthFlowWithConfig(cfg, hostname, "", scopes)
126-
return err
115+
return opts.AuthFlow(cfg, hostname, opts.Scopes)
127116
}

pkg/cmd/auth/refresh/refresh_test.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@ func Test_NewCmdRefresh(t *testing.T) {
2525
name: "no arguments",
2626
wants: RefreshOptions{
2727
Hostname: "",
28-
Scopes: []string{},
2928
},
3029
},
3130
{
3231
name: "hostname",
3332
cli: "-h aline.cedrac",
3433
wants: RefreshOptions{
3534
Hostname: "aline.cedrac",
36-
Scopes: []string{},
3735
},
3836
},
3937
{
@@ -136,11 +134,10 @@ func Test_refreshRun(t *testing.T) {
136134
},
137135
opts: &RefreshOptions{
138136
Hostname: "obed.morton",
139-
Scopes: []string{},
140137
},
141138
wantAuthArgs: authArgs{
142139
hostname: "obed.morton",
143-
scopes: []string{},
140+
scopes: nil,
144141
},
145142
},
146143
{
@@ -150,11 +147,10 @@ func Test_refreshRun(t *testing.T) {
150147
},
151148
opts: &RefreshOptions{
152149
Hostname: "",
153-
Scopes: []string{},
154150
},
155151
wantAuthArgs: authArgs{
156152
hostname: "github.com",
157-
scopes: []string{},
153+
scopes: nil,
158154
},
159155
},
160156
{
@@ -165,14 +161,13 @@ func Test_refreshRun(t *testing.T) {
165161
},
166162
opts: &RefreshOptions{
167163
Hostname: "",
168-
Scopes: []string{},
169164
},
170165
askStubs: func(as *prompt.AskStubber) {
171166
as.StubOne("github.com")
172167
},
173168
wantAuthArgs: authArgs{
174169
hostname: "github.com",
175-
scopes: []string{},
170+
scopes: nil,
176171
},
177172
},
178173
{
@@ -192,7 +187,7 @@ func Test_refreshRun(t *testing.T) {
192187
for _, tt := range tests {
193188
t.Run(tt.name, func(t *testing.T) {
194189
aa := authArgs{}
195-
doAuthFlow = func(_ config.Config, hostname string, scopes []string) error {
190+
tt.opts.AuthFlow = func(_ config.Config, hostname string, scopes []string) error {
196191
aa.hostname = hostname
197192
aa.scopes = scopes
198193
return nil

0 commit comments

Comments
 (0)