Skip to content

Commit

Permalink
Add priority queue for repository cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
robertjndw committed Jan 17, 2024
1 parent a1b7e56 commit aa9704a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 13 deletions.
47 changes: 34 additions & 13 deletions HadesCloneContainer/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package main

import (
"container/heap"
"os"
"path"
"strconv"
"strings"

"github.com/go-git/go-git/v5"
Expand All @@ -19,39 +21,46 @@ func isValidPath(path string) bool {
}

func main() {
if os.Getenv("DEBUG") == "true" {
log.SetLevel(log.DebugLevel)
}

if env_dir := os.Getenv("REPOSITORY_DIR"); env_dir != "" && isValidPath(env_dir) {
dir = env_dir
}
log.Info("Starting HadesCloneContainer")
log.Info("Cloning repositories to ", dir)

repos := getReposFromEnv()
for _, repo := range repos {
repo_map := getReposFromEnv()
repos := getReposFromMap(repo_map)
for repos.Len() > 0 {
repo := heap.Pop(&repos).(*Item).Repository
log.Debugf("Cloning repository: %+v", repo)
repodir := dir
// URL is mandatory
if repo["URL"] == "" {
if repo.URL == "" {
log.Warn("Skipping repository without URL")
continue
}
clone_options := &git.CloneOptions{
URL: repo["URL"],
URL: repo.URL,
}
// Check if username and password are set and use them for authentication
if repo["USERNAME"] != "" && repo["PASSWORD"] != "" {
if repo.Username != "" && repo.Password != "" {
clone_options.Auth = &http.BasicAuth{
Username: repo["USERNAME"],
Password: repo["PASSWORD"],
Username: repo.Username,
Password: repo.Password,
}
}
// Check if a branch is specified
if repo["BRANCH"] != "" {
clone_options.ReferenceName = plumbing.ReferenceName("refs/heads/" + repo["BRANCH"])
if repo.Branch != "" {
clone_options.ReferenceName = plumbing.ReferenceName("refs/heads/" + repo.Branch)
}

if repo["PATH"] != "" {
repodir = path.Join(repodir, repo["PATH"])
if repo.Path != "" {
repodir = path.Join(repodir, repo.Path)
} else {
parts := strings.Split(repo["URL"], "/")
parts := strings.Split(repo.URL, "/")
if len(parts) > 0 {
repoName := strings.TrimSuffix(parts[len(parts)-1], ".git")
repodir = path.Join(repodir, repoName)
Expand All @@ -64,7 +73,7 @@ func main() {
log.WithError(err).Error("Failed to clone repository")
continue
}
log.Infof("Cloned repository %s to %s", repo["URL"], repodir)
log.Infof("Cloned repository %s to %s", repo.URL, repodir)
}
}

Expand Down Expand Up @@ -100,3 +109,15 @@ func getReposFromEnv() map[string]map[string]string {

return repoVars
}

func getReposFromMap(repo_map map[string]map[string]string) PriorityQueue {
repos := make(PriorityQueue, 0)
heap.Init(&repos)
for _, repo := range repo_map {
tmp := FromMap(repo)
order, _ := strconv.Atoi(repo["ORDER"])
item := &Item{Repository: tmp, order: order}
heap.Push(&repos, item)
}
return repos
}
50 changes: 50 additions & 0 deletions HadesCloneContainer/pq.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import "container/heap"

/// Based on the example from https://pkg.go.dev/container/heap#example-package-PriorityQueue

type Item struct {
Repository
order int
index int
}

type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
// We want Pop to give us the lowest order number
return pq[i].order < pq[j].order
}

func (pq PriorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].index = i
pq[j].index = j
}

func (pq *PriorityQueue) Push(x any) {
n := len(*pq)
item := x.(*Item)
item.index = n
*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() any {
old := *pq
n := len(old)
item := old[n-1]
old[n-1] = nil // avoid memory leak
item.index = -1 // for safety
*pq = old[0 : n-1]
return item
}

// update modifies the priority and value of an Item in the queue.
func (pq *PriorityQueue) update(item *Item, value Repository, order int) {
item.Repository = value
item.order = order
heap.Fix(pq, item.index)
}
19 changes: 19 additions & 0 deletions HadesCloneContainer/repo_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

type Repository struct {
URL string
Username string
Password string
Branch string
Path string
}

func FromMap(m map[string]string) Repository {
return Repository{
URL: m["URL"],
Username: m["USERNAME"],
Password: m["PASSWORD"],
Branch: m["BRANCH"],
Path: m["PATH"],
}
}

0 comments on commit aa9704a

Please sign in to comment.