83 lines
1.9 KiB
Go
83 lines
1.9 KiB
Go
package remotes
|
|
|
|
import (
|
|
"github.com/pterm/pterm"
|
|
"github.com/xanzy/go-gitlab"
|
|
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
|
|
)
|
|
|
|
// Will determine number of total projects,
|
|
// then based on projectsPerPage (request) and
|
|
// projectsPerGoroutine, will spin off goroutines
|
|
// with offsets
|
|
const (
|
|
projectsPerPage = 20
|
|
projectsPerGoroutine = 200
|
|
)
|
|
|
|
type User struct {
|
|
ID int
|
|
Username string
|
|
Email string
|
|
Name string
|
|
AvatarURL string
|
|
}
|
|
|
|
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,
|
|
}
|
|
i++
|
|
}
|
|
|
|
return &pLangs
|
|
}
|