fix(stacks): support pull and redeploy without username (#5393)

This commit is contained in:
Dmitry Salakhov
2021-08-06 10:37:46 +12:00
committed by GitHub
parent fad0b09447
commit 39ff96303b
2 changed files with 19 additions and 15 deletions

View File

@@ -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

View File

@@ -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
}