Merge branch 'fix_git-credential_command' into 'main'

fix: git-credential command by using oauth2 user

Closes #7414

See merge request https://gitlab.com/gitlab-org/cli/-/merge_requests/1371

Merged-by: Shekhar Patnaik <spatnaik@gitlab.com>
Approved-by: Shekhar Patnaik <spatnaik@gitlab.com>
Approved-by: Ezekiel Kigbo <3397881-ekigbo@users.noreply.gitlab.com>
Reviewed-by: Ezekiel Kigbo <3397881-ekigbo@users.noreply.gitlab.com>
Co-authored-by: Ole Reifschneider <ole.reifschneider@getmercury.io>
This commit is contained in:
Shekhar Patnaik 2023-11-20 11:33:16 +00:00
commit b8d7447a55
2 changed files with 9 additions and 40 deletions

View File

@ -15,7 +15,7 @@ import (
const tokenUser = "oauth2"
type configExt interface {
GetWithSource(string, string, bool) (string, string, error)
Get(string, string) (string, error)
}
type CredentialOptions struct {
@ -101,25 +101,15 @@ func helperRun(opts *CredentialOptions) error {
return err
}
var gotUser string
gotToken, source, _ := cfg.GetWithSource(expectedParams["host"], "token", true)
if strings.HasSuffix(source, "_TOKEN") {
gotUser = tokenUser
} else {
gotUser, _, _ = cfg.GetWithSource(expectedParams["host"], "user", true)
}
gotToken, _ := cfg.Get(expectedParams["host"], "token")
if gotUser == "" || gotToken == "" {
return cmdutils.SilentError
}
if expectedParams["username"] != "" && gotUser != tokenUser && !strings.EqualFold(expectedParams["username"], gotUser) {
if gotToken == "" {
return cmdutils.SilentError
}
fmt.Fprintf(opts.IO.StdOut, "protocol=%s\n", expectedParams["protocol"])
fmt.Fprintf(opts.IO.StdOut, "host=%s\n", expectedParams["host"])
fmt.Fprintf(opts.IO.StdOut, "username=%s\n", gotUser)
fmt.Fprintf(opts.IO.StdOut, "username=%s\n", tokenUser)
fmt.Fprintf(opts.IO.StdOut, "password=%s\n", gotToken)
return nil

View File

@ -11,8 +11,8 @@ import (
type tinyConfig map[string]string
func (c tinyConfig) GetWithSource(host, key string, searchENVVars bool) (string, string, error) {
return c[fmt.Sprintf("%s:%s", host, key)], c["_source"], nil
func (c tinyConfig) Get(host, key string) (string, error) {
return c[fmt.Sprintf("%s:%s", host, key)], nil
}
func Test_helperRun(t *testing.T) {
@ -44,7 +44,7 @@ func Test_helperRun(t *testing.T) {
wantStdout: heredoc.Doc(`
protocol=https
host=example.com
username=monalisa
username=oauth2
password=OTOKEN
`),
wantStderr: "",
@ -70,7 +70,7 @@ func Test_helperRun(t *testing.T) {
wantStdout: heredoc.Doc(`
protocol=https
host=example.com
username=monalisa
username=oauth2
password=OTOKEN
`),
wantStderr: "",
@ -94,7 +94,7 @@ func Test_helperRun(t *testing.T) {
wantStdout: heredoc.Doc(`
protocol=https
host=example.com
username=monalisa
username=oauth2
password=OTOKEN
`),
wantStderr: "",
@ -118,27 +118,6 @@ func Test_helperRun(t *testing.T) {
wantStdout: "",
wantStderr: "",
},
{
name: "user mismatch",
opts: CredentialOptions{
Operation: "get",
Config: func() (configExt, error) {
return tinyConfig{
"_source": "/Users/monalisa/.config/glab/config.yml",
"example.com:user": "monalisa",
"example.com:token": "OTOKEN",
}, nil
},
},
input: heredoc.Doc(`
protocol=https
host=example.com
username=clemsbot
`),
wantErr: true,
wantStdout: "",
wantStderr: "",
},
{
name: "token from env",
opts: CredentialOptions{