Refactor gitlab to generic remotes

This commit is contained in:
2024-01-15 15:39:35 -05:00
parent da209c53e3
commit d6833a9ea0
21 changed files with 208 additions and 172 deletions

View File

@@ -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,
}