feat: prepare for other git services

This commit is contained in:
Simon Cornet 2025-02-25 13:08:04 +01:00
commit 5707b8bb06
3 changed files with 61 additions and 55 deletions

View file

@ -6,6 +6,58 @@ import (
"strings"
)
func checkoutRepositories(repositories []Repository) {
for _, repo := range repositories {
// create clone
repoName := string(repo.PathWithNamespace)
url := getGitlabURL(gitlabToken, gitlabHost, repoName)
// create repository destination
repoDestination := repoDestinationPre + repoName
// create and update bar description
descriptionPrefixPre := "Cloning repository "
descriptionPrefix := descriptionPrefixPre + repoName + " ..."
bar.Describe(descriptionPrefix)
// clone the repo
cloneOutput, err := cloneRepository(repoDestination, url)
if err != nil {
// if repo already exists, try to pull the latest changes
if strings.Contains(string(cloneOutput),
"already exists and is not an empty directory") {
descriptionPrefixPre := "Pulling repository "
descriptionPrefix := descriptionPrefixPre + repoName + " ..."
bar.Describe(descriptionPrefix)
_, err := pullRepositories(repoDestination)
if err != nil {
continue
}
pulledCount = pulledCount + 1
continue
}
// in case cloning failed and the directory does not exist
// print the clone error and continue
log.Printf("\n❌ error cloning %s: %v\n%s\n", repoName, err, cloneOutput)
errorCount = errorCount + 1
bar.Add(1)
continue
}
// finish the clone
clonedCount = clonedCount + 1
bar.Add(1)
}
}
func cloneRepository(repoDestination string, gitlabUrl string) (string, error) {
cloneCmd := exec.Command("git", "clone", gitlabUrl, repoDestination)