Merge pull request #5186 from cli/credential-helper-fix

Fix authenticating git operations after `auth login --with-token`
This commit is contained in:
Mislav Marohnić 2022-02-10 15:59:41 +01:00 committed by GitHub
commit c9f44ffda9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 5 deletions

View file

@ -112,6 +112,9 @@ func helperRun(opts *CredentialOptions) error {
gotUser = tokenUser
} else {
gotUser, _, _ = cfg.GetWithSource(lookupHost, "user")
if gotUser == "" {
gotUser = tokenUser
}
}
if gotUser == "" || gotToken == "" {

View file

@ -15,11 +15,6 @@ func (c tinyConfig) GetWithSource(host, key string) (string, string, error) {
return c[fmt.Sprintf("%s:%s", host, key)], c["_source"], nil
}
func (c tinyConfig) Get(host, key string) (val string, err error) {
val, _, err = c.GetWithSource(host, key)
return
}
func Test_helperRun(t *testing.T) {
tests := []struct {
name string
@ -170,6 +165,30 @@ func Test_helperRun(t *testing.T) {
wantStdout: "",
wantStderr: "",
},
{
name: "no username configured",
opts: CredentialOptions{
Operation: "get",
Config: func() (config, error) {
return tinyConfig{
"_source": "/Users/monalisa/.config/gh/hosts.yml",
"example.com:oauth_token": "OTOKEN",
}, nil
},
},
input: heredoc.Doc(`
protocol=https
host=example.com
`),
wantErr: false,
wantStdout: heredoc.Doc(`
protocol=https
host=example.com
username=x-access-token
password=OTOKEN
`),
wantStderr: "",
},
{
name: "token from env",
opts: CredentialOptions{