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

150 lines
3.1 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-08 21:52:26 +00:00
"fmt"
2023-12-09 04:13:17 +00:00
"strings"
2023-12-07 17:08:56 +00:00
"time"
2023-12-10 04:19:19 +00:00
"github.com/go-git/go-git/v5"
"github.com/pterm/pterm"
2023-12-07 17:08:56 +00:00
"github.com/xanzy/go-gitlab"
)
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 Project struct {
2023-12-08 21:52:26 +00:00
ID int
Description string
SSHURLToRepo string
HTTPURLToRepo string
WebURL string
2023-12-07 17:08:56 +00:00
Name string
NameWithNamespace string
Path string
PathWithNamespace string
AvatarURL string
LastActivityAt time.Time
2023-12-10 04:19:19 +00:00
Readme string
Remote string
Owner string
2023-12-29 18:35:56 +00:00
Languages *ProjectLanguages
2023-12-10 04:19:19 +00:00
gitRepo *git.Repository
2023-12-07 17:08:56 +00:00
}
2023-12-29 18:35:56 +00:00
type ProjectLanguages []*ProjectLanguage
type ProjectLanguage struct {
Name string
Percentage float32
}
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
}
2023-12-08 21:52:26 +00:00
func (p *Project) String() string {
var projectString string
if p != nil {
projectString = fmt.Sprintf("%s (%s)", p.Path, p.PathWithNamespace)
}
return projectString
2023-12-08 21:52:26 +00:00
}
2023-12-29 18:35:56 +00:00
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
}
2023-12-09 04:13:17 +00:00
func (p *Project) SanitizedPath() string {
return strings.Trim(p.PathWithNamespace, " '\"%<>|`")
}
2023-12-10 04:19:19 +00:00
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 {
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) (
[]*Project, *gitlab.Response, error) {
pList := make([]*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.
2023-12-29 18:35:56 +00:00
func (c *Client) GetProjectLanguages(project *gitlab.Project) *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
}
2023-12-29 18:35:56 +00:00
var pLangs ProjectLanguages
pLangs = make([]*ProjectLanguage, len(*l))
var i int
for name, pcnt := range *l {
pLangs[i] = &ProjectLanguage{
Name: name,
Percentage: pcnt,
}
2023-12-29 18:35:56 +00:00
i++
}
2023-12-29 18:35:56 +00:00
return &pLangs
}