Start moving gitlab code to remote interface

This commit is contained in:
2024-01-16 11:15:52 -05:00
parent e846821c44
commit d7181b1cf6
11 changed files with 339 additions and 186 deletions

View File

@@ -1,9 +1,9 @@
package remotes
import (
"github.com/pterm/pterm"
"github.com/xanzy/go-gitlab"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/load"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote"
)
// Will determine number of total projects,
@@ -15,68 +15,31 @@ const (
projectsPerGoroutine = 200
)
type User struct {
ID int
Username string
Email string
Name string
AvatarURL string
type Remotes []remote.Remote
func (r *Remotes) AddRemote(remote remote.Remote) {
*r = append(*r, remote)
}
func (c *Client) Api() *gitlab.Client {
return c.apiClient
}
func (c *Client) GetTotalProjects(opts *gitlab.ListProjectsOptions) int {
reqOpts := *opts
reqOpts.ListOptions = gitlab.ListOptions{
Page: 1,
PerPage: 1,
}
var projects int
if _, r, e := c.apiClient.Projects.ListProjects(opts, gitlab.WithContext(c.Ctx)); e == nil {
projects = r.TotalItems
}
return projects
}
// Returns a list of projects along with the next page and an error
// if there was an error
func (c *Client) ListProjects(opts *gitlab.ListProjectsOptions) (
[]*projects.Project, *gitlab.Response, error) {
pList := make([]*projects.Project, 0)
projects, resp, err := c.apiClient.Projects.ListProjects(
opts,
gitlab.WithContext(c.Ctx),
)
if err == nil {
pList = append(pList, c.handleProjects(projects)...)
}
return pList, resp, err
}
// A nil return indicates an API error or GitLab doesn't know what
// language the project uses.
func (c *Client) GetProjectLanguages(project *gitlab.Project) *projects.ProjectLanguages {
l, _, e := c.apiClient.Projects.GetProjectLanguages(project.ID, gitlab.WithContext(c.Ctx))
if e != nil {
pterm.Error.Printfln("Failed requesting project languages: %s", e.Error())
return nil
}
var pLangs projects.ProjectLanguages
pLangs = make([]*projects.ProjectLanguage, len(*l))
var i int
for name, pcnt := range *l {
pLangs[i] = &projects.ProjectLanguage{
Name: name,
Percentage: pcnt,
// Launches project streamsers for all remotes in goroutines
// returns slice of load.ProgressInfo
func (r *Remotes) StreamRemotes(opts *remote.RemoteQueryOpts) []*load.ProgressInfo {
progressInfos := make([]*load.ProgressInfo, len(*r))
for i, remoteInstance := range *r {
progressInfos[i] = &load.ProgressInfo{
ProgressChan: make(chan load.Progress),
ProjectsChan: make(chan []*projects.Project),
ErrorChan: make(chan error),
DoneChan: make(chan interface{}),
NumProjects: remoteInstance.GetNumProjects(opts),
}
i++
go remoteInstance.StreamProjects(progressInfos[i], opts)
}
return &pLangs
return progressInfos
}
func NewRemotes() *Remotes {
var remotes Remotes
remotes = make([]remote.Remote, 0)
return &remotes
}