Refactor gitlab to generic remotes
This commit is contained in:
20
internal/remotes/load/load.go
Normal file
20
internal/remotes/load/load.go
Normal file
@ -0,0 +1,20 @@
|
||||
package load
|
||||
|
||||
import (
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
|
||||
)
|
||||
|
||||
type ProgressInfo struct {
|
||||
ProgressChan chan Progress
|
||||
ProjectsChan chan []*projects.Project
|
||||
ErrorChan chan error
|
||||
DoneChan chan interface{}
|
||||
NumProjects int
|
||||
}
|
||||
|
||||
type Progress struct {
|
||||
Page int
|
||||
Pages int
|
||||
Projects int
|
||||
TotalProjects int
|
||||
}
|
72
internal/remotes/projects/projects.go
Normal file
72
internal/remotes/projects/projects.go
Normal file
@ -0,0 +1,72 @@
|
||||
package projects
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
)
|
||||
|
||||
type Project struct {
|
||||
ID int
|
||||
Description string
|
||||
SSHURLToRepo string
|
||||
HTTPURLToRepo string
|
||||
WebURL string
|
||||
Name string
|
||||
NameWithNamespace string
|
||||
Path string
|
||||
PathWithNamespace string
|
||||
AvatarURL string
|
||||
LastActivityAt time.Time
|
||||
Readme string
|
||||
Remote string
|
||||
Owner string
|
||||
Languages *ProjectLanguages
|
||||
gitRepo *git.Repository
|
||||
}
|
||||
|
||||
type ProjectLanguages []*ProjectLanguage
|
||||
|
||||
type ProjectLanguage struct {
|
||||
Name string
|
||||
Percentage float32
|
||||
}
|
||||
|
||||
func (p *Project) String() string {
|
||||
var projectString string
|
||||
if p != nil {
|
||||
projectString = fmt.Sprintf("%s (%s)", p.Path, p.PathWithNamespace)
|
||||
}
|
||||
return projectString
|
||||
}
|
||||
|
||||
func (p *Project) GetLanguage() *ProjectLanguage {
|
||||
if p.Languages == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var lang *ProjectLanguage
|
||||
var maxPcnt float32
|
||||
for _, p := range *p.Languages {
|
||||
if p.Percentage > maxPcnt {
|
||||
lang = p
|
||||
}
|
||||
maxPcnt = p.Percentage
|
||||
}
|
||||
|
||||
return lang
|
||||
}
|
||||
|
||||
func (p *Project) SanitizedPath() string {
|
||||
return strings.Trim(p.PathWithNamespace, " '\"%<>|`")
|
||||
}
|
||||
|
||||
func (p *Project) SetRepo(r *git.Repository) {
|
||||
p.gitRepo = r
|
||||
}
|
||||
|
||||
func (p *Project) GetRepo() *git.Repository {
|
||||
return p.gitRepo
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package remotes
|
||||
package projects
|
||||
|
||||
import "github.com/pterm/pterm"
|
||||
|
@ -1,4 +1,4 @@
|
||||
package remotes
|
||||
package projects
|
||||
|
||||
import (
|
||||
"errors"
|
13
internal/remotes/remote/remote.go
Normal file
13
internal/remotes/remote/remote.go
Normal file
@ -0,0 +1,13 @@
|
||||
package remote
|
||||
|
||||
import "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/load"
|
||||
|
||||
// Any remote needs to be able to return
|
||||
// the number of projects the user has access to and also
|
||||
// stream all projects along with updates to channels
|
||||
// provided by *load.ProgressInfo
|
||||
type Remote interface {
|
||||
Name() string
|
||||
GetNumProjects(*RemoteQueryOpts) int
|
||||
StreamProjects(*RemoteQueryOpts) *load.ProgressInfo
|
||||
}
|
8
internal/remotes/remote/remote_opts.go
Normal file
8
internal/remotes/remote/remote_opts.go
Normal file
@ -0,0 +1,8 @@
|
||||
package remote
|
||||
|
||||
import "context"
|
||||
|
||||
type RemoteQueryOpts struct {
|
||||
Ctx context.Context
|
||||
OwnerOnly bool
|
||||
}
|
@ -1,13 +1,9 @@
|
||||
package remotes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
"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,
|
||||
@ -19,32 +15,6 @@ const (
|
||||
projectsPerGoroutine = 200
|
||||
)
|
||||
|
||||
type Project struct {
|
||||
ID int
|
||||
Description string
|
||||
SSHURLToRepo string
|
||||
HTTPURLToRepo string
|
||||
WebURL string
|
||||
Name string
|
||||
NameWithNamespace string
|
||||
Path string
|
||||
PathWithNamespace string
|
||||
AvatarURL string
|
||||
LastActivityAt time.Time
|
||||
Readme string
|
||||
Remote string
|
||||
Owner string
|
||||
Languages *ProjectLanguages
|
||||
gitRepo *git.Repository
|
||||
}
|
||||
|
||||
type ProjectLanguages []*ProjectLanguage
|
||||
|
||||
type ProjectLanguage struct {
|
||||
Name string
|
||||
Percentage float32
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID int
|
||||
Username string
|
||||
@ -57,43 +27,6 @@ func (c *Client) Api() *gitlab.Client {
|
||||
return c.apiClient
|
||||
}
|
||||
|
||||
func (p *Project) String() string {
|
||||
var projectString string
|
||||
if p != nil {
|
||||
projectString = fmt.Sprintf("%s (%s)", p.Path, p.PathWithNamespace)
|
||||
}
|
||||
return projectString
|
||||
}
|
||||
|
||||
func (p *Project) GetLanguage() *ProjectLanguage {
|
||||
if p.Languages == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var lang *ProjectLanguage
|
||||
var maxPcnt float32
|
||||
for _, p := range *p.Languages {
|
||||
if p.Percentage > maxPcnt {
|
||||
lang = p
|
||||
}
|
||||
maxPcnt = p.Percentage
|
||||
}
|
||||
|
||||
return lang
|
||||
}
|
||||
|
||||
func (p *Project) SanitizedPath() string {
|
||||
return strings.Trim(p.PathWithNamespace, " '\"%<>|`")
|
||||
}
|
||||
|
||||
func (p *Project) SetRepo(r *git.Repository) {
|
||||
p.gitRepo = r
|
||||
}
|
||||
|
||||
func (p *Project) GetRepo() *git.Repository {
|
||||
return p.gitRepo
|
||||
}
|
||||
|
||||
func (c *Client) GetTotalProjects(opts *gitlab.ListProjectsOptions) int {
|
||||
reqOpts := *opts
|
||||
reqOpts.ListOptions = gitlab.ListOptions{
|
||||
@ -112,8 +45,8 @@ func (c *Client) GetTotalProjects(opts *gitlab.ListProjectsOptions) int {
|
||||
// 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) (
|
||||
[]*Project, *gitlab.Response, error) {
|
||||
pList := make([]*Project, 0)
|
||||
[]*projects.Project, *gitlab.Response, error) {
|
||||
pList := make([]*projects.Project, 0)
|
||||
projects, resp, err := c.apiClient.Projects.ListProjects(
|
||||
opts,
|
||||
gitlab.WithContext(c.Ctx),
|
||||
@ -126,19 +59,19 @@ func (c *Client) ListProjects(opts *gitlab.ListProjectsOptions) (
|
||||
|
||||
// A nil return indicates an API error or GitLab doesn't know what
|
||||
// language the project uses.
|
||||
func (c *Client) GetProjectLanguages(project *gitlab.Project) *ProjectLanguages {
|
||||
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 ProjectLanguages
|
||||
pLangs = make([]*ProjectLanguage, len(*l))
|
||||
var pLangs projects.ProjectLanguages
|
||||
pLangs = make([]*projects.ProjectLanguage, len(*l))
|
||||
|
||||
var i int
|
||||
for name, pcnt := range *l {
|
||||
pLangs[i] = &ProjectLanguage{
|
||||
pLangs[i] = &projects.ProjectLanguage{
|
||||
Name: name,
|
||||
Percentage: pcnt,
|
||||
}
|
||||
|
@ -1,26 +1,14 @@
|
||||
package remotes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"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/projects"
|
||||
)
|
||||
|
||||
type ProgressInfo struct {
|
||||
ProgressChan chan Progress
|
||||
ProjectsChan chan []*Project
|
||||
ErrorChan chan error
|
||||
DoneChan chan interface{}
|
||||
NumProjects int
|
||||
}
|
||||
|
||||
type Progress struct {
|
||||
Page int
|
||||
Pages int
|
||||
Projects int
|
||||
TotalProjects int
|
||||
}
|
||||
|
||||
var DefaultListOpts = &gitlab.ListProjectsOptions{
|
||||
ListOptions: gitlab.ListOptions{
|
||||
PerPage: projectsPerPage,
|
||||
@ -33,13 +21,14 @@ var DefaultListOpts = &gitlab.ListProjectsOptions{
|
||||
// channels that stream progress info and then finally the full
|
||||
// list of projects on separate channels. If ownerOnly=true, only
|
||||
// projects for which you are an owner will be loaded
|
||||
func (c *Client) StreamProjects(ownerOnly bool, projects int) *ProgressInfo {
|
||||
pi := &ProgressInfo{
|
||||
ProgressChan: make(chan Progress),
|
||||
ProjectsChan: make(chan []*Project),
|
||||
func (c *Client) StreamProjects(ownerOnly bool, numProjects int) *load.ProgressInfo {
|
||||
fmt.Println(numProjects)
|
||||
pi := &load.ProgressInfo{
|
||||
ProgressChan: make(chan load.Progress),
|
||||
ProjectsChan: make(chan []*projects.Project),
|
||||
ErrorChan: make(chan error),
|
||||
DoneChan: make(chan interface{}),
|
||||
NumProjects: projects,
|
||||
NumProjects: numProjects,
|
||||
}
|
||||
|
||||
go c.streamProjects(pi, ownerOnly)
|
||||
@ -47,7 +36,7 @@ func (c *Client) StreamProjects(ownerOnly bool, projects int) *ProgressInfo {
|
||||
return pi
|
||||
}
|
||||
|
||||
func (c *Client) streamProjects(pi *ProgressInfo, ownerOnly bool) {
|
||||
func (c *Client) streamProjects(pi *load.ProgressInfo, ownerOnly bool) {
|
||||
defer close(pi.ProgressChan)
|
||||
defer close(pi.ProjectsChan)
|
||||
|
||||
@ -75,7 +64,7 @@ func (c *Client) streamProjects(pi *ProgressInfo, ownerOnly bool) {
|
||||
}
|
||||
|
||||
pi.ProjectsChan <- projects
|
||||
pi.ProgressChan <- Progress{
|
||||
pi.ProgressChan <- load.Progress{
|
||||
Page: resp.CurrentPage,
|
||||
Pages: resp.TotalPages,
|
||||
Projects: len(projects),
|
||||
@ -99,16 +88,16 @@ func (c *Client) streamProjects(pi *ProgressInfo, ownerOnly bool) {
|
||||
pi.DoneChan <- nil
|
||||
}
|
||||
|
||||
func (c *Client) handleProjects(projects []*gitlab.Project) []*Project {
|
||||
func (c *Client) handleProjects(gitProjects []*gitlab.Project) []*projects.Project {
|
||||
// Opportunity to perform any filtering or additional lookups
|
||||
// on a per-project basis
|
||||
pList := make([]*Project, 0, len(projects))
|
||||
for _, project := range projects {
|
||||
pList := make([]*projects.Project, 0, len(gitProjects))
|
||||
for _, project := range gitProjects {
|
||||
var owner string
|
||||
if project.Owner != nil {
|
||||
owner = project.Owner.Email
|
||||
}
|
||||
p := &Project{
|
||||
p := &projects.Project{
|
||||
ID: project.ID,
|
||||
Description: project.Description,
|
||||
SSHURLToRepo: project.SSHURLToRepo,
|
||||
|
Reference in New Issue
Block a user