2024-01-16 16:15:52 +00:00
|
|
|
package gitlabremote
|
|
|
|
|
|
|
|
import (
|
2024-01-17 02:32:46 +00:00
|
|
|
"time"
|
|
|
|
|
2024-01-16 16:15:52 +00:00
|
|
|
"github.com/pterm/pterm"
|
|
|
|
"github.com/xanzy/go-gitlab"
|
2024-01-17 13:13:58 +00:00
|
|
|
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/info"
|
2024-01-16 16:15:52 +00:00
|
|
|
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
|
|
|
|
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote"
|
|
|
|
)
|
|
|
|
|
2024-01-17 02:32:46 +00:00
|
|
|
var DefaultListOpts = &gitlab.ListProjectsOptions{
|
|
|
|
ListOptions: gitlab.ListOptions{
|
|
|
|
PerPage: projectsPerPage,
|
|
|
|
Page: 1,
|
|
|
|
},
|
|
|
|
Archived: gitlab.Ptr[bool](false),
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
defApiWaitMin time.Duration = 2 * time.Second
|
|
|
|
defApiWaitMax time.Duration = 5 * time.Second
|
|
|
|
)
|
|
|
|
|
2024-01-17 13:13:58 +00:00
|
|
|
func NewGitlabApi(remoteInfo *info.RemoteInfo) (*gitlab.Client, error) {
|
2024-01-17 02:32:46 +00:00
|
|
|
client, err := gitlab.NewClient(
|
2024-01-17 13:13:58 +00:00
|
|
|
remoteInfo.Token,
|
|
|
|
gitlab.WithBaseURL(remoteInfo.Host),
|
2024-01-17 02:32:46 +00:00
|
|
|
gitlab.WithCustomRetryWaitMinMax(defApiWaitMin, defApiWaitMax),
|
|
|
|
)
|
2024-01-16 16:15:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *GitlabRemote) GetNumProjects(opts *remote.RemoteQueryOpts) int {
|
|
|
|
listOpts := *DefaultListOpts
|
|
|
|
listOpts.PerPage = 1
|
2024-01-16 17:48:42 +00:00
|
|
|
listOpts.Page = 1
|
2024-01-16 16:15:52 +00:00
|
|
|
listOpts.Simple = gitlab.Ptr[bool](true)
|
2024-01-17 02:32:46 +00:00
|
|
|
_, resp, err := r.api.Projects.ListProjects(&listOpts, r.GetDefaultRequestOptions()...)
|
2024-01-16 16:15:52 +00:00
|
|
|
if err != nil {
|
|
|
|
pterm.Error.Printfln("Failed getting number of GitLab projects: %s", err)
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return resp.TotalItems
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a list of projects along with the next page and an error
|
|
|
|
// if there was an error
|
|
|
|
func (r *GitlabRemote) ListProjects(opts *gitlab.ListProjectsOptions) (
|
|
|
|
[]*projects.Project, *gitlab.Response, error) {
|
|
|
|
pList := make([]*projects.Project, 0)
|
|
|
|
projects, resp, err := r.api.Projects.ListProjects(
|
|
|
|
opts,
|
2024-01-17 02:32:46 +00:00
|
|
|
r.GetDefaultRequestOptions()...,
|
2024-01-16 16:15:52 +00:00
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
pList = append(pList, r.handleProjects(projects)...)
|
|
|
|
}
|
|
|
|
return pList, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *GitlabRemote) handleProjects(gitProjects []*gitlab.Project) []*projects.Project {
|
|
|
|
// Opportunity to perform any filtering or additional lookups
|
|
|
|
// on a per-project basis
|
|
|
|
pList := make([]*projects.Project, 0, len(gitProjects))
|
|
|
|
for _, project := range gitProjects {
|
|
|
|
var owner string
|
|
|
|
if project.Owner != nil {
|
|
|
|
owner = project.Owner.Email
|
|
|
|
}
|
|
|
|
p := &projects.Project{
|
|
|
|
ID: project.ID,
|
|
|
|
Description: project.Description,
|
|
|
|
SSHURLToRepo: project.SSHURLToRepo,
|
|
|
|
HTTPURLToRepo: project.HTTPURLToRepo,
|
|
|
|
WebURL: project.WebURL,
|
|
|
|
Name: project.Name,
|
|
|
|
NameWithNamespace: project.NameWithNamespace,
|
|
|
|
Path: project.Path,
|
|
|
|
PathWithNamespace: project.PathWithNamespace,
|
|
|
|
Remote: r.info.Host,
|
|
|
|
Owner: owner,
|
|
|
|
AvatarURL: project.AvatarURL,
|
|
|
|
LastActivityAt: *project.LastActivityAt,
|
|
|
|
Readme: project.ReadmeURL,
|
|
|
|
Languages: r.GetProjectLanguages(project),
|
|
|
|
}
|
|
|
|
pList = append(pList, p)
|
|
|
|
}
|
|
|
|
return pList
|
|
|
|
}
|
|
|
|
|
|
|
|
// A nil return indicates an API error or GitLab doesn't know what
|
|
|
|
// language the project uses.
|
|
|
|
func (r *GitlabRemote) GetProjectLanguages(project *gitlab.Project) *projects.ProjectLanguages {
|
2024-01-17 02:32:46 +00:00
|
|
|
l, _, e := r.api.Projects.GetProjectLanguages(project.ID, r.GetDefaultRequestOptions()...)
|
2024-01-16 16:15:52 +00:00
|
|
|
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
|
|
|
|
}
|
2024-01-17 02:32:46 +00:00
|
|
|
|
|
|
|
func (r *GitlabRemote) GetDefaultRequestOptions() []gitlab.RequestOptionFunc {
|
|
|
|
requestOpts := make([]gitlab.RequestOptionFunc, 1)
|
|
|
|
requestOpts[0] = gitlab.WithContext(r.GetInfo().Ctx)
|
|
|
|
return requestOpts
|
|
|
|
}
|