X Tutup
Skip to content

Commit 81caade

Browse files
committed
Access token tip message using hostname
1 parent 012b004 commit 81caade

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

pkg/cmd/auth/login/login.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,7 @@ func loginRun(opts *LoginOptions) error {
228228
}
229229
} else {
230230
fmt.Fprintln(opts.IO.ErrOut)
231-
fmt.Fprintln(opts.IO.ErrOut, heredoc.Doc(`
232-
Tip: you can generate a Personal Access Token here https://github.com/settings/tokens
233-
The minimum required scopes are 'repo' and 'read:org'.`))
231+
fmt.Fprintln(opts.IO.ErrOut, heredoc.Doc(getAccessTokenTip(hostname)))
234232
var token string
235233
err := prompt.SurveyAskOne(&survey.Password{
236234
Message: "Paste your authentication token:",
@@ -313,3 +311,13 @@ func hostnameValidator(v interface{}) error {
313311
}
314312
return nil
315313
}
314+
315+
func getAccessTokenTip(hostname string) string {
316+
ghHostname := hostname
317+
if ghHostname == "" {
318+
ghHostname = "github.com"
319+
}
320+
return fmt.Sprintf(`
321+
Tip: you can generate a Personal Access Token here https://%s/settings/tokens
322+
The minimum required scopes are 'repo' and 'read:org'.`, ghHostname)
323+
}

pkg/cmd/auth/login/login_test.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,13 @@ func Test_loginRun_nontty(t *testing.T) {
279279

280280
func Test_loginRun_Survey(t *testing.T) {
281281
tests := []struct {
282-
name string
283-
opts *LoginOptions
284-
httpStubs func(*httpmock.Registry)
285-
askStubs func(*prompt.AskStubber)
286-
wantHosts string
287-
cfg func(config.Config)
282+
name string
283+
opts *LoginOptions
284+
httpStubs func(*httpmock.Registry)
285+
askStubs func(*prompt.AskStubber)
286+
wantHosts string
287+
wantErrOut *regexp.Regexp
288+
cfg func(config.Config)
288289
}{
289290
{
290291
name: "already authenticated",
@@ -304,7 +305,8 @@ func Test_loginRun_Survey(t *testing.T) {
304305
as.StubOne(0) // host type github.com
305306
as.StubOne(false) // do not continue
306307
},
307-
wantHosts: "", // nothing should have been written to hosts
308+
wantHosts: "", // nothing should have been written to hosts
309+
wantErrOut: regexp.MustCompile("Logging into github.com"),
308310
},
309311
{
310312
name: "hostname set",
@@ -324,6 +326,7 @@ func Test_loginRun_Survey(t *testing.T) {
324326
httpmock.GraphQL(`query UserCurrent\b`),
325327
httpmock.StringResponse(`{"data":{"viewer":{"login":"jillv"}}}`))
326328
},
329+
wantErrOut: regexp.MustCompile("Tip: you can generate a Personal Access Token here https://rebecca.chambers/settings/tokens"),
327330
},
328331
{
329332
name: "choose enterprise",
@@ -344,6 +347,7 @@ func Test_loginRun_Survey(t *testing.T) {
344347
httpmock.GraphQL(`query UserCurrent\b`),
345348
httpmock.StringResponse(`{"data":{"viewer":{"login":"jillv"}}}`))
346349
},
350+
wantErrOut: regexp.MustCompile("Tip: you can generate a Personal Access Token here https://brad.vickers/settings/tokens"),
347351
},
348352
{
349353
name: "choose github.com",
@@ -357,6 +361,7 @@ func Test_loginRun_Survey(t *testing.T) {
357361
as.StubOne("def456") // auth token
358362
as.StubOne("HTTPS") // git_protocol
359363
},
364+
wantErrOut: regexp.MustCompile("Tip: you can generate a Personal Access Token here https://github.com/settings/tokens"),
360365
},
361366
{
362367
name: "sets git_protocol",
@@ -370,6 +375,7 @@ func Test_loginRun_Survey(t *testing.T) {
370375
as.StubOne("def456") // auth token
371376
as.StubOne("SSH") // git_protocol
372377
},
378+
wantErrOut: regexp.MustCompile("Tip: you can generate a Personal Access Token here https://github.com/settings/tokens"),
373379
},
374380
// TODO how to test browser auth?
375381
}
@@ -378,7 +384,7 @@ func Test_loginRun_Survey(t *testing.T) {
378384
if tt.opts == nil {
379385
tt.opts = &LoginOptions{}
380386
}
381-
io, _, _, _ := iostreams.Test()
387+
io, _, _, stderr := iostreams.Test()
382388

383389
io.SetStdinTTY(true)
384390
io.SetStderrTTY(true)
@@ -430,6 +436,11 @@ func Test_loginRun_Survey(t *testing.T) {
430436
}
431437

432438
assert.Equal(t, tt.wantHosts, hostsBuf.String())
439+
if tt.wantErrOut == nil {
440+
assert.Equal(t, "", stderr.String())
441+
} else {
442+
assert.True(t, tt.wantErrOut.MatchString(stderr.String()))
443+
}
433444
reg.Verify(t)
434445
})
435446
}

0 commit comments

Comments
 (0)
X Tutup