mirror of
https://github.com/portainer/portainer.git
synced 2025-07-25 16:29:44 +02:00
Co-authored-by: itsconquest <william.conquest@portainer.io> Co-authored-by: oscarzhou <oscar.zhou@portainer.io> Co-authored-by: Chaim Lev-Ari <chiptus@users.noreply.github.com>
45 lines
1.4 KiB
Go
45 lines
1.4 KiB
Go
package stackutils
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/pkg/errors"
|
|
portainer "github.com/portainer/portainer/api"
|
|
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")
|
|
ErrInvalidGitCredential = errors.New("Invalid git credential")
|
|
)
|
|
|
|
// DownloadGitRepository downloads the target git repository on the disk
|
|
// The first return value represents the commit hash of the downloaded git repository
|
|
func DownloadGitRepository(config gittypes.RepoConfig, gitService portainer.GitService, getProjectPath func() string) (string, error) {
|
|
username := ""
|
|
password := ""
|
|
if config.Authentication != nil {
|
|
username = config.Authentication.Username
|
|
password = config.Authentication.Password
|
|
}
|
|
|
|
projectPath := getProjectPath()
|
|
err := gitService.CloneRepository(projectPath, config.URL, config.ReferenceName, username, password, config.TLSSkipVerify)
|
|
if err != nil {
|
|
if err == gittypes.ErrAuthenticationFailure {
|
|
newErr := ErrInvalidGitCredential
|
|
return "", newErr
|
|
}
|
|
|
|
newErr := fmt.Errorf("unable to clone git repository: %w", err)
|
|
return "", newErr
|
|
}
|
|
|
|
commitID, err := gitService.LatestCommitID(config.URL, config.ReferenceName, username, password, config.TLSSkipVerify)
|
|
if err != nil {
|
|
newErr := fmt.Errorf("unable to fetch git repository id: %w", err)
|
|
return "", newErr
|
|
}
|
|
return commitID, nil
|
|
}
|