Rename gitlab to remotes
This commit is contained in:
149
internal/remotes/remotes.go
Normal file
149
internal/remotes/remotes.go
Normal file
@ -0,0 +1,149 @@
|
||||
package remotes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/pterm/pterm"
|
||||
"github.com/xanzy/go-gitlab"
|
||||
)
|
||||
|
||||
// Will determine number of total projects,
|
||||
// then based on projectsPerPage (request) and
|
||||
// projectsPerGoroutine, will spin off goroutines
|
||||
// with offsets
|
||||
const (
|
||||
projectsPerPage = 20
|
||||
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
|
||||
Email string
|
||||
Name string
|
||||
AvatarURL string
|
||||
}
|
||||
|
||||
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{
|
||||
Page: 1,
|
||||
PerPage: 1,
|
||||
}
|
||||
|
||||
var projects int
|
||||
if _, r, e := c.apiClient.Projects.ListProjects(opts, gitlab.WithContext(c.Ctx)); e == nil {
|
||||
projects = r.TotalItems
|
||||
}
|
||||
|
||||
return projects
|
||||
}
|
||||
|
||||
// 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(
|
||||
opts,
|
||||
gitlab.WithContext(c.Ctx),
|
||||
)
|
||||
if err == nil {
|
||||
pList = append(pList, c.handleProjects(projects)...)
|
||||
}
|
||||
return pList, resp, err
|
||||
}
|
||||
|
||||
// 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 {
|
||||
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 i int
|
||||
for name, pcnt := range *l {
|
||||
pLangs[i] = &ProjectLanguage{
|
||||
Name: name,
|
||||
Percentage: pcnt,
|
||||
}
|
||||
i++
|
||||
}
|
||||
|
||||
return &pLangs
|
||||
}
|
83
internal/remotes/remotes_client.go
Normal file
83
internal/remotes/remotes_client.go
Normal file
@ -0,0 +1,83 @@
|
||||
package remotes
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/xanzy/go-gitlab"
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
Ctx context.Context
|
||||
Config *config.GitlabConfig
|
||||
apiClient *gitlab.Client
|
||||
}
|
||||
|
||||
type Clients []*Client
|
||||
|
||||
type ClientOpts struct {
|
||||
Ctx context.Context
|
||||
Name string
|
||||
Host string
|
||||
Token string
|
||||
}
|
||||
|
||||
func (c *Clients) AddClients(gitlabClient ...*Client) error {
|
||||
var err error
|
||||
for _, client := range gitlabClient {
|
||||
if c.GetClientByHost(client.Config.Host) != nil {
|
||||
err = errors.Join(err, fmt.Errorf("Client with host %s already exists", client.Config.Host))
|
||||
} else {
|
||||
*c = append(*c, client)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Clients) GetClientByHost(host string) *Client {
|
||||
for _, client := range *c {
|
||||
if client.Config.Host == host {
|
||||
return client
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewCLients() *Clients {
|
||||
var clients Clients
|
||||
clients = make([]*Client, 0)
|
||||
return &clients
|
||||
}
|
||||
|
||||
func NewGitlabClients(clientOpts []*ClientOpts) (*Clients, error) {
|
||||
var err error
|
||||
clients := NewCLients()
|
||||
for _, opts := range clientOpts {
|
||||
gitlabClient, e := NewGitlabClient(opts)
|
||||
if e != nil {
|
||||
err = errors.Join(err, e)
|
||||
continue
|
||||
}
|
||||
err = errors.Join(err, clients.AddClients(gitlabClient))
|
||||
}
|
||||
return clients, err
|
||||
}
|
||||
|
||||
func NewGitlabClient(opts *ClientOpts) (*Client, error) {
|
||||
client, err := gitlab.NewClient(opts.Token, gitlab.WithBaseURL(opts.Host))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
gitlabClient := &Client{
|
||||
Ctx: opts.Ctx,
|
||||
Config: &config.GitlabConfig{
|
||||
Name: opts.Name,
|
||||
Host: opts.Host,
|
||||
Token: opts.Token,
|
||||
},
|
||||
apiClient: client,
|
||||
}
|
||||
return gitlabClient, nil
|
||||
}
|
37
internal/remotes/remotes_git.go
Normal file
37
internal/remotes/remotes_git.go
Normal file
@ -0,0 +1,37 @@
|
||||
package remotes
|
||||
|
||||
import "github.com/pterm/pterm"
|
||||
|
||||
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"
|
||||
|
||||
head, _ := repo.Head()
|
||||
branch := head.Name().String()[11:]
|
||||
b, _ := repo.Branch(branch)
|
||||
if b != nil {
|
||||
str += pterm.LightCyan("Branch: ") + pterm.Bold.Sprint(b.Name) + "\n"
|
||||
} else {
|
||||
str += pterm.LightCyan("NEW Branch: ") + pterm.Bold.Sprint(branch) + "\n"
|
||||
}
|
||||
|
||||
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])
|
||||
}
|
||||
|
||||
return pterm.DefaultBox.
|
||||
WithLeftPadding(5).WithRightPadding(5).
|
||||
WithBoxStyle(&pterm.Style{pterm.FgLightBlue}).
|
||||
WithTitle(pterm.Bold.Sprint(pterm.LightGreen("Project Git Status"))).
|
||||
Sprint(str)
|
||||
}
|
131
internal/remotes/remotes_load.go
Normal file
131
internal/remotes/remotes_load.go
Normal file
@ -0,0 +1,131 @@
|
||||
package remotes
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/xanzy/go-gitlab"
|
||||
)
|
||||
|
||||
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,
|
||||
Page: 1,
|
||||
},
|
||||
Archived: gitlab.Ptr[bool](false),
|
||||
}
|
||||
|
||||
// Given there may be thousands of projects, this will return
|
||||
// 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),
|
||||
ErrorChan: make(chan error),
|
||||
DoneChan: make(chan interface{}),
|
||||
NumProjects: projects,
|
||||
}
|
||||
|
||||
go c.streamProjects(pi, ownerOnly)
|
||||
|
||||
return pi
|
||||
}
|
||||
|
||||
func (c *Client) streamProjects(pi *ProgressInfo, ownerOnly bool) {
|
||||
defer close(pi.ProgressChan)
|
||||
defer close(pi.ProjectsChan)
|
||||
|
||||
listOpts := *DefaultListOpts
|
||||
listOpts.Owned = gitlab.Ptr[bool](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 := c.ListProjects(&opts)
|
||||
|
||||
if err != nil {
|
||||
pi.ErrorChan <- err
|
||||
break
|
||||
}
|
||||
|
||||
pi.ProjectsChan <- projects
|
||||
pi.ProgressChan <- 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 c.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
|
||||
}
|
||||
|
||||
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 {
|
||||
var owner string
|
||||
if project.Owner != nil {
|
||||
owner = project.Owner.Email
|
||||
}
|
||||
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,
|
||||
Remote: c.Config.Host,
|
||||
Owner: owner,
|
||||
AvatarURL: project.AvatarURL,
|
||||
LastActivityAt: *project.LastActivityAt,
|
||||
Readme: project.ReadmeURL,
|
||||
Languages: c.GetProjectLanguages(project),
|
||||
}
|
||||
pList = append(pList, p)
|
||||
}
|
||||
return pList
|
||||
}
|
78
internal/remotes/remotes_net.go
Normal file
78
internal/remotes/remotes_net.go
Normal file
@ -0,0 +1,78 @@
|
||||
package remotes
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
giturl "github.com/whilp/git-urls"
|
||||
)
|
||||
|
||||
const defNetDialTimeoutSecs = 5
|
||||
|
||||
type GitlabProto int
|
||||
|
||||
const (
|
||||
GitlabProtoSSH GitlabProto = iota
|
||||
GitlabProtoHTTP
|
||||
)
|
||||
|
||||
var (
|
||||
ErrUnknownHost error = errors.New("No addresses found for host")
|
||||
)
|
||||
|
||||
func (p *Project) CheckHost(proto GitlabProto) error {
|
||||
switch proto {
|
||||
case GitlabProtoHTTP:
|
||||
return p.checkHTTPRemote()
|
||||
case GitlabProtoSSH:
|
||||
return p.checkSSHRemote()
|
||||
}
|
||||
return errors.New("Unknown git protocol")
|
||||
}
|
||||
|
||||
func (p *Project) checkHTTPRemote() error {
|
||||
u, err := giturl.Parse(p.HTTPURLToRepo)
|
||||
if err == nil {
|
||||
err = p.checkHost(u)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *Project) checkSSHRemote() error {
|
||||
u, err := giturl.Parse(p.SSHURLToRepo)
|
||||
if err == nil {
|
||||
err = p.checkHost(u)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *Project) checkHost(u *url.URL) error {
|
||||
var port int
|
||||
if u.Port() == "" {
|
||||
switch u.Scheme {
|
||||
case "http":
|
||||
port = 80
|
||||
case "ssh":
|
||||
port = 22
|
||||
case "https":
|
||||
port = 443
|
||||
}
|
||||
} else {
|
||||
port, _ = strconv.Atoi(u.Port())
|
||||
}
|
||||
|
||||
d, err := net.DialTimeout("tcp",
|
||||
fmt.Sprintf("%s:%d", u.Hostname(), port),
|
||||
defNetDialTimeoutSecs*time.Second)
|
||||
|
||||
if err == nil {
|
||||
_, err = d.Write([]byte("ok"))
|
||||
d.Close()
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
Reference in New Issue
Block a user