From 39ff96303b3208752610c779b9af82fc9490d06b Mon Sep 17 00:00:00 2001 From: Dmitry Salakhov Date: Fri, 6 Aug 2021 10:37:46 +1200 Subject: [PATCH] fix(stacks): support pull and redeploy without username (#5393) --- api/git/git.go | 30 +++++++++++-------- .../stacks/stack_update_git_redeploy.go | 4 +-- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/api/git/git.go b/api/git/git.go index b8bce4dc9..32bb5054e 100644 --- a/api/git/git.go +++ b/api/git/git.go @@ -47,13 +47,7 @@ func (c gitClient) download(ctx context.Context, dst string, opt cloneOptions) e gitOptions := git.CloneOptions{ URL: opt.repositoryUrl, Depth: opt.depth, - } - - if opt.password != "" || opt.username != "" { - gitOptions.Auth = &githttp.BasicAuth{ - Username: opt.username, - Password: opt.password, - } + Auth: getAuth(opt.username, opt.password), } if opt.referenceName != "" { @@ -79,12 +73,8 @@ func (c gitClient) latestCommitID(ctx context.Context, opt fetchOptions) (string URLs: []string{opt.repositoryUrl}, }) - listOptions := &git.ListOptions{} - if opt.password != "" || opt.username != "" { - listOptions.Auth = &githttp.BasicAuth{ - Username: opt.username, - Password: opt.password, - } + listOptions := &git.ListOptions{ + Auth: getAuth(opt.username, opt.password), } refs, err := remote.List(listOptions) @@ -101,6 +91,20 @@ func (c gitClient) latestCommitID(ctx context.Context, opt fetchOptions) (string return "", errors.Errorf("could not find ref %q in the repository", opt.referenceName) } +func getAuth(username, password string) *githttp.BasicAuth { + if password != "" { + if username == "" { + username = "token" + } + + return &githttp.BasicAuth{ + Username: username, + Password: password, + } + } + return nil +} + // Service represents a service for managing Git. type Service struct { httpsCli *http.Client diff --git a/api/http/handler/stacks/stack_update_git_redeploy.go b/api/http/handler/stacks/stack_update_git_redeploy.go index fe0f209df..bacb32c07 100644 --- a/api/http/handler/stacks/stack_update_git_redeploy.go +++ b/api/http/handler/stacks/stack_update_git_redeploy.go @@ -32,8 +32,8 @@ func (payload *stackGitRedployPayload) Validate(r *http.Request) error { payload.RepositoryReferenceName = defaultGitReferenceName } - if payload.RepositoryAuthentication && (govalidator.IsNull(payload.RepositoryUsername) || govalidator.IsNull(payload.RepositoryPassword)) { - return errors.New("Invalid repository credentials. Username and password must be specified when authentication is enabled") + if payload.RepositoryAuthentication && govalidator.IsNull(payload.RepositoryPassword) { + return errors.New("invalid repository credentials. Username and password must be specified when authentication is enabled") } return nil }