Merge pull request #1893 from mmontes11/tip-access-token-with-hostname

Access token tip message using hostname
This commit is contained in:
Nate Smith 2020-09-22 11:39:19 -05:00 committed by GitHub
commit de3ef151ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 11 deletions

View file

@ -228,9 +228,7 @@ func loginRun(opts *LoginOptions) error {
}
} else {
fmt.Fprintln(opts.IO.ErrOut)
fmt.Fprintln(opts.IO.ErrOut, heredoc.Doc(`
Tip: you can generate a Personal Access Token here https://github.com/settings/tokens
The minimum required scopes are 'repo' and 'read:org'.`))
fmt.Fprintln(opts.IO.ErrOut, heredoc.Doc(getAccessTokenTip(hostname)))
var token string
err := prompt.SurveyAskOne(&survey.Password{
Message: "Paste your authentication token:",
@ -313,3 +311,13 @@ func hostnameValidator(v interface{}) error {
}
return nil
}
func getAccessTokenTip(hostname string) string {
ghHostname := hostname
if ghHostname == "" {
ghHostname = ghinstance.OverridableDefault()
}
return fmt.Sprintf(`
Tip: you can generate a Personal Access Token here https://%s/settings/tokens
The minimum required scopes are 'repo' and 'read:org'.`, ghHostname)
}

View file

@ -279,12 +279,13 @@ func Test_loginRun_nontty(t *testing.T) {
func Test_loginRun_Survey(t *testing.T) {
tests := []struct {
name string
opts *LoginOptions
httpStubs func(*httpmock.Registry)
askStubs func(*prompt.AskStubber)
wantHosts string
cfg func(config.Config)
name string
opts *LoginOptions
httpStubs func(*httpmock.Registry)
askStubs func(*prompt.AskStubber)
wantHosts string
wantErrOut *regexp.Regexp
cfg func(config.Config)
}{
{
name: "already authenticated",
@ -304,7 +305,8 @@ func Test_loginRun_Survey(t *testing.T) {
as.StubOne(0) // host type github.com
as.StubOne(false) // do not continue
},
wantHosts: "", // nothing should have been written to hosts
wantHosts: "", // nothing should have been written to hosts
wantErrOut: regexp.MustCompile("Logging into github.com"),
},
{
name: "hostname set",
@ -324,6 +326,7 @@ func Test_loginRun_Survey(t *testing.T) {
httpmock.GraphQL(`query UserCurrent\b`),
httpmock.StringResponse(`{"data":{"viewer":{"login":"jillv"}}}`))
},
wantErrOut: regexp.MustCompile("Tip: you can generate a Personal Access Token here https://rebecca.chambers/settings/tokens"),
},
{
name: "choose enterprise",
@ -344,6 +347,7 @@ func Test_loginRun_Survey(t *testing.T) {
httpmock.GraphQL(`query UserCurrent\b`),
httpmock.StringResponse(`{"data":{"viewer":{"login":"jillv"}}}`))
},
wantErrOut: regexp.MustCompile("Tip: you can generate a Personal Access Token here https://brad.vickers/settings/tokens"),
},
{
name: "choose github.com",
@ -357,6 +361,7 @@ func Test_loginRun_Survey(t *testing.T) {
as.StubOne("def456") // auth token
as.StubOne("HTTPS") // git_protocol
},
wantErrOut: regexp.MustCompile("Tip: you can generate a Personal Access Token here https://github.com/settings/tokens"),
},
{
name: "sets git_protocol",
@ -370,6 +375,7 @@ func Test_loginRun_Survey(t *testing.T) {
as.StubOne("def456") // auth token
as.StubOne("SSH") // git_protocol
},
wantErrOut: regexp.MustCompile("Tip: you can generate a Personal Access Token here https://github.com/settings/tokens"),
},
// TODO how to test browser auth?
}
@ -378,7 +384,7 @@ func Test_loginRun_Survey(t *testing.T) {
if tt.opts == nil {
tt.opts = &LoginOptions{}
}
io, _, _, _ := iostreams.Test()
io, _, _, stderr := iostreams.Test()
io.SetStdinTTY(true)
io.SetStderrTTY(true)
@ -430,6 +436,11 @@ func Test_loginRun_Survey(t *testing.T) {
}
assert.Equal(t, tt.wantHosts, hostsBuf.String())
if tt.wantErrOut == nil {
assert.Equal(t, "", stderr.String())
} else {
assert.Regexp(t, tt.wantErrOut, stderr.String())
}
reg.Verify(t)
})
}