Merge branch 'trunk' of https://github.com/cli/cli into feature/add-files-to-gist
This commit is contained in:
commit
a60a6d854b
4 changed files with 63 additions and 4 deletions
|
|
@ -9,6 +9,7 @@ import (
|
|||
"github.com/MakeNowJust/heredoc"
|
||||
"github.com/cli/cli/api"
|
||||
"github.com/cli/cli/internal/authflow"
|
||||
"github.com/cli/cli/internal/ghinstance"
|
||||
"github.com/cli/cli/pkg/iostreams"
|
||||
"github.com/cli/cli/pkg/prompt"
|
||||
)
|
||||
|
|
@ -125,7 +126,7 @@ func Login(opts *LoginOptions) error {
|
|||
fmt.Fprint(opts.IO.ErrOut, heredoc.Docf(`
|
||||
Tip: you can generate a Personal Access Token here https://%s/settings/tokens
|
||||
The minimum required scopes are %s.
|
||||
`, hostname, scopesSentence(minimumScopes)))
|
||||
`, hostname, scopesSentence(minimumScopes, ghinstance.IsEnterprise(hostname))))
|
||||
|
||||
err := prompt.SurveyAskOne(&survey.Password{
|
||||
Message: "Paste your authentication token:",
|
||||
|
|
@ -193,11 +194,14 @@ func Login(opts *LoginOptions) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func scopesSentence(scopes []string) string {
|
||||
func scopesSentence(scopes []string, isEnterprise bool) string {
|
||||
quoted := make([]string, len(scopes))
|
||||
for i, s := range scopes {
|
||||
quoted[i] = fmt.Sprintf("'%s'", s)
|
||||
if s == "workflow" && isEnterprise {
|
||||
// remove when GHE 2.x reaches EOL
|
||||
quoted[i] += " (GHE 3.0+)"
|
||||
}
|
||||
}
|
||||
// TODO: insert an "and" before the last element
|
||||
return strings.Join(quoted, ", ")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -101,3 +101,55 @@ func TestLogin_ssh(t *testing.T) {
|
|||
assert.Equal(t, "ATOKEN", cfg["example.com:oauth_token"])
|
||||
assert.Equal(t, "ssh", cfg["example.com:git_protocol"])
|
||||
}
|
||||
|
||||
func Test_scopesSentence(t *testing.T) {
|
||||
type args struct {
|
||||
scopes []string
|
||||
isEnterprise bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "basic scopes",
|
||||
args: args{
|
||||
scopes: []string{"repo", "read:org"},
|
||||
isEnterprise: false,
|
||||
},
|
||||
want: "'repo', 'read:org'",
|
||||
},
|
||||
{
|
||||
name: "empty",
|
||||
args: args{
|
||||
scopes: []string(nil),
|
||||
isEnterprise: false,
|
||||
},
|
||||
want: "",
|
||||
},
|
||||
{
|
||||
name: "workflow scope for dotcom",
|
||||
args: args{
|
||||
scopes: []string{"repo", "workflow"},
|
||||
isEnterprise: false,
|
||||
},
|
||||
want: "'repo', 'workflow'",
|
||||
},
|
||||
{
|
||||
name: "workflow scope for GHE",
|
||||
args: args{
|
||||
scopes: []string{"repo", "workflow"},
|
||||
isEnterprise: true,
|
||||
},
|
||||
want: "'repo', 'workflow' (GHE 3.0+)",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := scopesSentence(tt.args.scopes, tt.args.isEnterprise); got != tt.want {
|
||||
t.Errorf("scopesSentence() = %q, want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ func HasMinimumScopes(httpClient httpClient, hostname, authToken string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
req.Header.Set("Autorization", "token "+authToken)
|
||||
req.Header.Set("Authorization", "token "+authToken)
|
||||
|
||||
res, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -52,7 +52,9 @@ func Test_HasMinimumScopes(t *testing.T) {
|
|||
fakehttp := &httpmock.Registry{}
|
||||
defer fakehttp.Verify(t)
|
||||
|
||||
var gotAuthorization string
|
||||
fakehttp.Register(httpmock.REST("GET", ""), func(req *http.Request) (*http.Response, error) {
|
||||
gotAuthorization = req.Header.Get("authorization")
|
||||
return &http.Response{
|
||||
Request: req,
|
||||
StatusCode: 200,
|
||||
|
|
@ -70,6 +72,7 @@ func Test_HasMinimumScopes(t *testing.T) {
|
|||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
assert.Equal(t, gotAuthorization, "token ATOKEN")
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue