diff --git a/pkg/cmd/auth/gitcredential/helper.go b/pkg/cmd/auth/gitcredential/helper.go index 117584b34..bda962e50 100644 --- a/pkg/cmd/auth/gitcredential/helper.go +++ b/pkg/cmd/auth/gitcredential/helper.go @@ -112,6 +112,9 @@ func helperRun(opts *CredentialOptions) error { gotUser = tokenUser } else { gotUser, _, _ = cfg.GetWithSource(lookupHost, "user") + if gotUser == "" { + gotUser = tokenUser + } } if gotUser == "" || gotToken == "" { diff --git a/pkg/cmd/auth/gitcredential/helper_test.go b/pkg/cmd/auth/gitcredential/helper_test.go index 92cdcb692..45488682d 100644 --- a/pkg/cmd/auth/gitcredential/helper_test.go +++ b/pkg/cmd/auth/gitcredential/helper_test.go @@ -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{