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

216 lines
5.1 KiB
Go
Raw Normal View History

2023-12-05 21:56:47 +00:00
package gitlab
2023-12-07 17:08:56 +00:00
import (
"context"
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-08 21:52:26 +00:00
const defProjectsPerPage = 30
2023-12-05 21:56:47 +00:00
type Client struct {
2023-12-07 17:08:56 +00:00
Ctx context.Context
2023-12-05 21:56:47 +00:00
gitlab *gitlab.Client
}
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
gitRepo *git.Repository
2023-12-07 17:08:56 +00:00
}
type User struct {
ID int
Username string
Email string
Name string
AvatarURL string
}
type ProgressInfo struct {
ProgressChan chan Progress
ProjectsChan chan []*Project
ErrorChan chan error
DoneChan chan interface{}
}
type Progress struct {
Page int
Pages int
Projects int
TotalProjects int
}
2023-12-08 21:52:26 +00:00
func (p *Project) String() string {
return fmt.Sprintf("%s (%s)", p.Path, p.PathWithNamespace)
}
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 (p *Project) GetGitInfo() string {
repo := p.GetRepo()
if repo == nil {
return "No Repo"
}
var str string
str += "\n" + pterm.LightRed("Project: ") + pterm.Bold.Sprint(p.Name) + "\n"
2023-12-10 04:19:19 +00:00
head, _ := repo.Head()
branch := head.Name().String()[11:]
b, _ := repo.Branch(branch)
2023-12-11 16:50:11 +00:00
if b != nil {
str += pterm.LightCyan("Branch: ") + pterm.Bold.Sprint(b.Name) + "\n"
2023-12-11 16:50:11 +00:00
} else {
str += pterm.LightCyan("NEW Branch: ") + pterm.Bold.Sprint(branch) + "\n"
2023-12-11 16:50:11 +00:00
}
2023-12-10 04:19:19 +00:00
commit, _ := repo.CommitObject(head.Hash())
str += "\n" + commit.String()
str += pterm.LightMagenta("GitLab: ") + pterm.Bold.Sprint(p.HTTPURLToRepo) + "\n"
if remotes, _ := repo.Remotes(); len(remotes) > 0 {
str += pterm.LightBlue("Remote: ") + pterm.Bold.Sprint(remotes[0].Config().URLs[0])
}
2023-12-10 04:19:19 +00:00
return pterm.DefaultBox.
WithLeftPadding(5).WithRightPadding(5).
WithBoxStyle(&pterm.Style{pterm.FgLightBlue}).
WithTitle(pterm.Bold.Sprint(pterm.LightGreen("Project Git Status"))).
Sprint(str)
}
2023-12-07 17:08:56 +00:00
// Given there may be thousands of projects, this will return
// channels that stream progress info and then finally the full
2023-12-10 04:19:19 +00:00
// 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) *ProgressInfo {
2023-12-07 17:08:56 +00:00
pi := &ProgressInfo{
ProgressChan: make(chan Progress),
ProjectsChan: make(chan []*Project),
ErrorChan: make(chan error),
DoneChan: make(chan interface{}),
}
2023-12-10 04:19:19 +00:00
go c.streamProjects(pi, ownerOnly)
2023-12-07 17:08:56 +00:00
return pi
}
2023-12-10 04:19:19 +00:00
func (c *Client) streamProjects(pi *ProgressInfo, ownerOnly bool) {
2023-12-07 17:08:56 +00:00
defer close(pi.ProgressChan)
defer close(pi.ProjectsChan)
listOpts := &gitlab.ListProjectsOptions{
ListOptions: gitlab.ListOptions{
PerPage: defProjectsPerPage,
Page: 1,
},
2023-12-10 04:19:19 +00:00
Archived: gitlab.Ptr[bool](false),
Owned: gitlab.Ptr[bool](ownerOnly),
2023-12-07 17:08:56 +00:00
}
var numProjects int
for {
projects, resp, err := c.ListProjects(listOpts)
2023-12-10 15:10:46 +00:00
if err != nil {
pi.ErrorChan <- err
break
}
2023-12-07 17:08:56 +00:00
// We're done when we have it all or our context is done
if c.Ctx.Err() != nil || resp.NextPage == 0 {
pi.DoneChan <- nil
break
}
numProjects += len(projects)
pi.ProjectsChan <- projects
pi.ProgressChan <- Progress{
Page: resp.CurrentPage,
Pages: resp.TotalPages,
Projects: numProjects,
TotalProjects: resp.TotalItems,
}
listOpts.Page = resp.NextPage
}
}
// 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.gitlab.Projects.ListProjects(
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
}
2023-12-07 17:08:56 +00:00
func (c *Client) handleProjects(projects []*gitlab.Project) []*Project {
// Opportunity to perform any filtering or additional lookups
// on a per-project basis
pList := make([]*Project, 0, len(projects))
for _, project := range projects {
p := &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,
2023-12-10 04:19:19 +00:00
AvatarURL: project.AvatarURL,
2023-12-07 17:08:56 +00:00
LastActivityAt: *project.LastActivityAt,
2023-12-10 04:19:19 +00:00
Readme: project.ReadmeURL,
2023-12-07 17:08:56 +00:00
}
pList = append(pList, p)
}
return pList
2023-12-05 21:56:47 +00:00
}
2023-12-07 17:08:56 +00:00
func NewGitlabClient(ctx context.Context, host, token string) (*Client, error) {
2023-12-05 21:56:47 +00:00
client, err := gitlab.NewClient(token, gitlab.WithBaseURL(host))
if err != nil {
return nil, err
}
gitlabClient := &Client{
2023-12-07 17:08:56 +00:00
Ctx: ctx,
2023-12-05 21:56:47 +00:00
gitlab: client,
}
return gitlabClient, nil
}