fix(custom-templates): update template from git [EE-5534] (#9053)

pull/9115/head
Chaim Lev-Ari 2023-06-22 21:08:47 +07:00 committed by GitHub
parent 2363d23de0
commit 58651810bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 4 deletions

View File

@ -2,6 +2,7 @@ package customtemplates
import ( import (
"errors" "errors"
"fmt"
"net/http" "net/http"
"strconv" "strconv"
@ -10,10 +11,10 @@ import (
"github.com/portainer/libhttp/response" "github.com/portainer/libhttp/response"
portainer "github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/filesystem" "github.com/portainer/portainer/api/filesystem"
"github.com/portainer/portainer/api/git"
gittypes "github.com/portainer/portainer/api/git/types" gittypes "github.com/portainer/portainer/api/git/types"
httperrors "github.com/portainer/portainer/api/http/errors" httperrors "github.com/portainer/portainer/api/http/errors"
"github.com/portainer/portainer/api/http/security" "github.com/portainer/portainer/api/http/security"
"github.com/portainer/portainer/api/stacks/stackutils"
"github.com/asaskevich/govalidator" "github.com/asaskevich/govalidator"
) )
@ -173,18 +174,34 @@ func (handler *Handler) customTemplateUpdate(w http.ResponseWriter, r *http.Requ
TLSSkipVerify: payload.TLSSkipVerify, TLSSkipVerify: payload.TLSSkipVerify,
} }
repositoryUsername := ""
repositoryPassword := ""
if payload.RepositoryAuthentication { if payload.RepositoryAuthentication {
repositoryUsername = payload.RepositoryUsername
repositoryPassword = payload.RepositoryPassword
gitConfig.Authentication = &gittypes.GitAuthentication{ gitConfig.Authentication = &gittypes.GitAuthentication{
Username: payload.RepositoryUsername, Username: payload.RepositoryUsername,
Password: payload.RepositoryPassword, Password: payload.RepositoryPassword,
} }
} }
commitHash, err := stackutils.DownloadGitRepository(*gitConfig, handler.GitService, func() string { cleanBackup, err := git.CloneWithBackup(handler.GitService, handler.FileService, git.CloneOptions{
return customTemplate.ProjectPath ProjectPath: customTemplate.ProjectPath,
URL: gitConfig.URL,
ReferenceName: gitConfig.ReferenceName,
Username: repositoryUsername,
Password: repositoryPassword,
TLSSkipVerify: gitConfig.TLSSkipVerify,
}) })
if err != nil { if err != nil {
return httperror.InternalServerError(err.Error(), err) return httperror.InternalServerError("Unable to clone git repository directory", err)
}
defer cleanBackup()
commitHash, err := handler.GitService.LatestCommitID(gitConfig.URL, gitConfig.ReferenceName, repositoryUsername, repositoryPassword, gitConfig.TLSSkipVerify)
if err != nil {
return httperror.InternalServerError("Unable get latest commit id", fmt.Errorf("failed to fetch latest commit id of the template %v: %w", customTemplate.ID, err))
} }
gitConfig.ConfigHash = commitHash gitConfig.ConfigHash = commitHash