2022-10-05 09:33:59 +00:00
|
|
|
package stackutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
2023-05-30 18:33:22 +00:00
|
|
|
"github.com/portainer/portainer/api/git"
|
2022-10-05 09:33:59 +00:00
|
|
|
gittypes "github.com/portainer/portainer/api/git/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrStackAlreadyExists = errors.New("A stack already exists with this name")
|
|
|
|
ErrWebhookIDAlreadyExists = errors.New("A webhook ID already exists")
|
|
|
|
)
|
|
|
|
|
|
|
|
// DownloadGitRepository downloads the target git repository on the disk
|
|
|
|
// The first return value represents the commit hash of the downloaded git repository
|
2023-04-04 00:44:42 +00:00
|
|
|
func DownloadGitRepository(config gittypes.RepoConfig, gitService portainer.GitService, getProjectPath func() string) (string, error) {
|
2022-10-05 09:33:59 +00:00
|
|
|
username := ""
|
|
|
|
password := ""
|
|
|
|
if config.Authentication != nil {
|
|
|
|
username = config.Authentication.Username
|
|
|
|
password = config.Authentication.Password
|
|
|
|
}
|
|
|
|
|
2023-04-04 00:44:42 +00:00
|
|
|
projectPath := getProjectPath()
|
2023-04-03 06:19:17 +00:00
|
|
|
err := gitService.CloneRepository(projectPath, config.URL, config.ReferenceName, username, password, config.TLSSkipVerify)
|
2022-10-05 09:33:59 +00:00
|
|
|
if err != nil {
|
2023-05-05 00:19:47 +00:00
|
|
|
if errors.Is(err, gittypes.ErrAuthenticationFailure) {
|
2023-05-30 18:33:22 +00:00
|
|
|
newErr := git.ErrInvalidGitCredential
|
2022-10-05 09:33:59 +00:00
|
|
|
return "", newErr
|
|
|
|
}
|
|
|
|
|
|
|
|
newErr := fmt.Errorf("unable to clone git repository: %w", err)
|
|
|
|
return "", newErr
|
|
|
|
}
|
|
|
|
|
2023-04-03 06:19:17 +00:00
|
|
|
commitID, err := gitService.LatestCommitID(config.URL, config.ReferenceName, username, password, config.TLSSkipVerify)
|
2022-10-05 09:33:59 +00:00
|
|
|
if err != nil {
|
|
|
|
newErr := fmt.Errorf("unable to fetch git repository id: %w", err)
|
|
|
|
return "", newErr
|
|
|
|
}
|
|
|
|
return commitID, nil
|
|
|
|
}
|