git-project-manager/internal/remotes/remotes.go

83 lines
1.9 KiB
Go
Raw Normal View History

2024-01-15 19:57:15 +00:00
package remotes
2023-12-05 21:56:47 +00:00
2023-12-07 17:08:56 +00:00
import (
2023-12-10 04:19:19 +00:00
"github.com/pterm/pterm"
2023-12-07 17:08:56 +00:00
"github.com/xanzy/go-gitlab"
2024-01-15 20:39:35 +00:00
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
2023-12-07 17:08:56 +00:00
)
2023-12-29 20:02:17 +00:00
// Will determine number of total projects,
// then based on projectsPerPage (request) and
// projectsPerGoroutine, will spin off goroutines
// with offsets
const (
projectsPerPage = 20
projectsPerGoroutine = 200
)
2023-12-05 21:56:47 +00:00
2023-12-07 17:08:56 +00:00
type User struct {
ID int
Username string
Email string
Name string
AvatarURL string
}
func (c *Client) Api() *gitlab.Client {
return c.apiClient
2023-12-07 17:08:56 +00:00
}
func (c *Client) GetTotalProjects(opts *gitlab.ListProjectsOptions) int {
2023-12-29 20:02:17 +00:00
reqOpts := *opts
reqOpts.ListOptions = gitlab.ListOptions{
Page: 1,
PerPage: 1,
}
2023-12-07 17:08:56 +00:00
2023-12-29 20:02:17 +00:00
var projects int
if _, r, e := c.apiClient.Projects.ListProjects(opts, gitlab.WithContext(c.Ctx)); e == nil {
2023-12-29 20:02:17 +00:00
projects = r.TotalItems
2023-12-07 17:08:56 +00:00
}
2023-12-29 20:02:17 +00:00
return projects
2023-12-07 17:08:56 +00:00
}
// 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) (
2024-01-15 20:39:35 +00:00
[]*projects.Project, *gitlab.Response, error) {
pList := make([]*projects.Project, 0)
projects, resp, err := c.apiClient.Projects.ListProjects(
2023-12-07 17:08:56 +00:00
opts,
gitlab.WithContext(c.Ctx),
)
if err == nil {
pList = append(pList, c.handleProjects(projects)...)
}
return pList, resp, err
2023-12-05 21:56:47 +00:00
}
// A nil return indicates an API error or GitLab doesn't know what
// language the project uses.
2024-01-15 20:39:35 +00:00
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
}
2024-01-15 20:39:35 +00:00
var pLangs projects.ProjectLanguages
pLangs = make([]*projects.ProjectLanguage, len(*l))
2023-12-29 18:35:56 +00:00
var i int
for name, pcnt := range *l {
2024-01-15 20:39:35 +00:00
pLangs[i] = &projects.ProjectLanguage{
2023-12-29 18:35:56 +00:00
Name: name,
Percentage: pcnt,
}
2023-12-29 18:35:56 +00:00
i++
}
2023-12-29 18:35:56 +00:00
return &pLangs
}