2024-01-14 15:33:15 +00:00
|
|
|
package projects
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/pterm/pterm"
|
2024-01-15 19:57:15 +00:00
|
|
|
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes"
|
2024-01-14 15:33:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Cache) LoadGitlabs() {
|
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
writer := pterm.DefaultMultiPrinter
|
|
|
|
|
|
|
|
for _, gl := range *c.gitlabs {
|
|
|
|
c.log.Info("Loading projects for remote", c.log.Args(
|
|
|
|
"host", gl.Config.Host,
|
|
|
|
"name", gl.Config.Name,
|
|
|
|
))
|
|
|
|
|
2024-01-15 19:57:15 +00:00
|
|
|
opts := *remotes.DefaultListOpts
|
2024-01-14 15:33:15 +00:00
|
|
|
opts.Owned = &c.config.Cache.Load.OwnerOnly
|
|
|
|
projects := gl.GetTotalProjects(&opts)
|
|
|
|
|
|
|
|
// Prepare progressbar
|
|
|
|
pBar, _ := pterm.DefaultProgressbar.
|
|
|
|
WithShowPercentage(true).
|
|
|
|
WithTotal(projects).
|
|
|
|
WithWriter(writer.NewWriter()).
|
|
|
|
WithMaxWidth(100).
|
|
|
|
Start(gl.Config.Host)
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go c.LoadGitlab(gl, wg, pBar, projects)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("")
|
|
|
|
writer.Start()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
writer.Stop()
|
|
|
|
fmt.Println("")
|
|
|
|
}
|
|
|
|
|
2024-01-15 19:57:15 +00:00
|
|
|
func (c *Cache) LoadGitlab(client *remotes.Client, wg *sync.WaitGroup, pBar *pterm.ProgressbarPrinter, projects int) {
|
2024-01-14 15:33:15 +00:00
|
|
|
defer wg.Done()
|
|
|
|
progressInfo := client.StreamProjects(c.config.Cache.Load.OwnerOnly, projects)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case p := <-progressInfo.ProgressChan:
|
|
|
|
pBar.Add(p.Projects)
|
|
|
|
case p := <-progressInfo.ProjectsChan:
|
|
|
|
c.AddProjects(p...)
|
|
|
|
case e := <-progressInfo.ErrorChan:
|
|
|
|
c.log.Error("Fetch GitLab projects error", c.log.Args("error", e, "gitlab", client.Config.Name))
|
|
|
|
case <-client.Ctx.Done():
|
|
|
|
c.log.Warn("LoadProjects cancelled", c.log.Args("reason", client.Ctx.Err()))
|
|
|
|
return
|
|
|
|
case <-progressInfo.DoneChan:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|