Start moving gitlab code to remote interface
This commit is contained in:
27
internal/remotes/gitlab/gitlab.go
Normal file
27
internal/remotes/gitlab/gitlab.go
Normal file
@ -0,0 +1,27 @@
|
||||
package gitlabremote
|
||||
|
||||
import (
|
||||
"github.com/xanzy/go-gitlab"
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote"
|
||||
)
|
||||
|
||||
type GitlabRemote struct {
|
||||
info *remote.RemoteInfo
|
||||
api *gitlab.Client
|
||||
}
|
||||
|
||||
func (r *GitlabRemote) GetInfo() *remote.RemoteInfo {
|
||||
return r.info
|
||||
}
|
||||
|
||||
func NewGitlabRemote(remoteInfo *remote.RemoteInfo) (*GitlabRemote, error) {
|
||||
api, err := NewGitlabApi(remoteInfo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
gl := &GitlabRemote{
|
||||
info: remoteInfo,
|
||||
api: api,
|
||||
}
|
||||
return gl, nil
|
||||
}
|
98
internal/remotes/gitlab/gitlab_api.go
Normal file
98
internal/remotes/gitlab/gitlab_api.go
Normal file
@ -0,0 +1,98 @@
|
||||
package gitlabremote
|
||||
|
||||
import (
|
||||
"github.com/pterm/pterm"
|
||||
"github.com/xanzy/go-gitlab"
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote"
|
||||
)
|
||||
|
||||
func NewGitlabApi(info *remote.RemoteInfo) (*gitlab.Client, error) {
|
||||
client, err := gitlab.NewClient(info.Token, gitlab.WithBaseURL(info.Host))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func (r *GitlabRemote) GetNumProjects(opts *remote.RemoteQueryOpts) int {
|
||||
listOpts := *DefaultListOpts
|
||||
listOpts.PerPage = 1
|
||||
listOpts.Simple = gitlab.Ptr[bool](true)
|
||||
_, resp, err := r.api.Projects.ListProjects(&listOpts)
|
||||
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,
|
||||
gitlab.WithContext(r.info.Ctx),
|
||||
)
|
||||
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 {
|
||||
l, _, e := r.api.Projects.GetProjectLanguages(project.ID, gitlab.WithContext(r.info.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
|
||||
}
|
78
internal/remotes/gitlab/gitlab_strean.go
Normal file
78
internal/remotes/gitlab/gitlab_strean.go
Normal file
@ -0,0 +1,78 @@
|
||||
package gitlabremote
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"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/remote"
|
||||
)
|
||||
|
||||
// Will determine number of total projects,
|
||||
// then based on projectsPerPage (request) and
|
||||
// projectsPerGoroutine, will spin off goroutines
|
||||
// with offsets
|
||||
const (
|
||||
projectsPerPage = 20
|
||||
projectsPerGoroutine = 200
|
||||
)
|
||||
|
||||
var DefaultListOpts = &gitlab.ListProjectsOptions{
|
||||
ListOptions: gitlab.ListOptions{
|
||||
PerPage: projectsPerPage,
|
||||
Page: 1,
|
||||
},
|
||||
Archived: gitlab.Ptr[bool](false),
|
||||
}
|
||||
|
||||
func (r *GitlabRemote) StreamProjects(pi *load.ProgressInfo, opts *remote.RemoteQueryOpts) {
|
||||
defer close(pi.ProgressChan)
|
||||
defer close(pi.ProjectsChan)
|
||||
|
||||
listOpts := *DefaultListOpts
|
||||
listOpts.Owned = gitlab.Ptr[bool](opts.OwnerOnly)
|
||||
|
||||
// Get total number of projects
|
||||
numGoroutines := pi.NumProjects / projectsPerGoroutine
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
startPage := 1
|
||||
for i := 1; i <= numGoroutines+1; i++ {
|
||||
wg.Add(1)
|
||||
endPage := startPage + (projectsPerGoroutine / projectsPerPage)
|
||||
go func(startPage int, endPage int) {
|
||||
defer wg.Done()
|
||||
opts := listOpts
|
||||
opts.Page = startPage
|
||||
for {
|
||||
projects, resp, err := r.ListProjects(&opts)
|
||||
|
||||
if err != nil {
|
||||
pi.ErrorChan <- err
|
||||
break
|
||||
}
|
||||
|
||||
pi.ProjectsChan <- projects
|
||||
pi.ProgressChan <- load.Progress{
|
||||
Page: resp.CurrentPage,
|
||||
Pages: resp.TotalPages,
|
||||
Projects: len(projects),
|
||||
TotalProjects: resp.TotalItems,
|
||||
}
|
||||
|
||||
// We're done when we have it all or our context is done
|
||||
// or we've hit our total pages
|
||||
if r.info.Ctx.Err() != nil || resp.NextPage == 0 {
|
||||
break
|
||||
} else if opts.Page == endPage {
|
||||
break
|
||||
}
|
||||
|
||||
opts.Page = resp.NextPage
|
||||
}
|
||||
}(startPage, endPage)
|
||||
startPage = endPage + 1
|
||||
}
|
||||
wg.Wait()
|
||||
pi.DoneChan <- nil
|
||||
}
|
Reference in New Issue
Block a user