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" "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) { func cloneRepository(repoDestination string, gitlabUrl string) (string, error) {
cloneCmd := exec.Command("git", "clone", gitlabUrl, repoDestination) cloneCmd := exec.Command("git", "clone", gitlabUrl, repoDestination)

View file

@ -3,12 +3,10 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"net/http" "net/http"
"strings"
) )
func fetchRepositories() ([]Repository, error) { func fetchRepositoriesGitlab() ([]Repository, error) {
// default options // default options
membership := "membership=true" membership := "membership=true"
@ -55,57 +53,13 @@ func fetchRepositories() ([]Repository, error) {
return repositories, nil return repositories, nil
} }
func checkoutRepositories(repositories []Repository) { func getGitlabURL(gitlabToken string, gitlabHost string, repoName string) (string) {
for _, repo := range repositories { // make gitlab url
url := fmt.Sprintf("https://gitlab-token:%s@%s/%s.git",
gitlabToken,
gitlabHost,
repoName)
// create clone gitlab url return url
repoName := string(repo.PathWithNamespace)
gitlabUrl := fmt.Sprintf("https://gitlab-token:%s@%s/%s.git",
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, gitlabUrl)
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)
}
} }

View file

@ -29,7 +29,7 @@ func main() {
verifyGitAvailable() verifyGitAvailable()
// fetch repository information from gitlab // fetch repository information from gitlab
repositories, err := fetchRepositories() repositories, err := fetchRepositoriesGitlab()
if err != nil { if err != nil {
log.Fatalf("Error fetching repositories: %v", err) log.Fatalf("Error fetching repositories: %v", err)
} }