This commit is contained in:
2023-12-09 23:19:19 -05:00
parent 4e76c9efe1
commit 64e07d3f40
18 changed files with 423 additions and 41 deletions

View File

@ -14,8 +14,11 @@ type Config struct {
}
type cacheConfig struct {
Ttl time.Duration `yaml:"ttl" json:"ttl"`
File string `yaml:"file" json:"file"`
Ttl time.Duration `yaml:"ttl" json:"ttl"`
File string `yaml:"file" json:"file"`
Load struct {
OwnerOnly bool `yaml:"ownerOnly" json:"ownerOnly"`
} `yaml:"load" json:"load"`
Clear struct {
ClearAliases bool `yaml:"clearAliases" json:"clearAliases"`
} `yaml:"clear" json:"clear"`

View File

@ -6,6 +6,8 @@ import (
"strings"
"time"
"github.com/go-git/go-git/v5"
"github.com/pterm/pterm"
"github.com/xanzy/go-gitlab"
)
@ -28,6 +30,8 @@ type Project struct {
PathWithNamespace string
AvatarURL string
LastActivityAt time.Time
Readme string
gitRepo *git.Repository
}
type User struct {
@ -60,10 +64,42 @@ 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 (p *Project) GetGitInfo() string {
repo := p.GetRepo()
if repo == nil {
return "No Repo"
}
var str string
head, _ := repo.Head()
branch := head.Name().String()[11:]
b, _ := repo.Branch(branch)
str = "\n" + pterm.LightCyan("Branch: ") + pterm.Bold.Sprint(b.Name) + "\n"
commit, _ := repo.CommitObject(head.Hash())
str += commit.String()
return pterm.DefaultBox.
WithLeftPadding(5).WithRightPadding(5).
WithBoxStyle(&pterm.Style{pterm.FgLightBlue}).
WithTitle(pterm.Bold.Sprint(pterm.LightGreen("Project Git Status"))).
Sprint(str)
}
// 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
func (c *Client) StreamProjects() *ProgressInfo {
// 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 {
pi := &ProgressInfo{
ProgressChan: make(chan Progress),
ProjectsChan: make(chan []*Project),
@ -71,12 +107,12 @@ func (c *Client) StreamProjects() *ProgressInfo {
DoneChan: make(chan interface{}),
}
go c.streamProjects(pi)
go c.streamProjects(pi, ownerOnly)
return pi
}
func (c *Client) streamProjects(pi *ProgressInfo) {
func (c *Client) streamProjects(pi *ProgressInfo, ownerOnly bool) {
defer close(pi.ProgressChan)
defer close(pi.ProjectsChan)
@ -85,8 +121,8 @@ func (c *Client) streamProjects(pi *ProgressInfo) {
PerPage: defProjectsPerPage,
Page: 1,
},
Archived: new(bool),
Owned: gitlab.Ptr[bool](true),
Archived: gitlab.Ptr[bool](false),
Owned: gitlab.Ptr[bool](ownerOnly),
}
var numProjects int
@ -145,7 +181,9 @@ func (c *Client) handleProjects(projects []*gitlab.Project) []*Project {
NameWithNamespace: project.NameWithNamespace,
Path: project.Path,
PathWithNamespace: project.PathWithNamespace,
AvatarURL: project.AvatarURL,
LastActivityAt: *project.LastActivityAt,
Readme: project.ReadmeURL,
}
pList = append(pList, p)
}

View File

@ -0,0 +1,78 @@
package gitlab
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
}

View File

@ -8,6 +8,7 @@ import (
"time"
"github.com/pterm/pterm"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab"
"gopkg.in/yaml.v3"
)
@ -16,6 +17,7 @@ type Cache struct {
Projects []*gitlab.Project
Aliases []*ProjectAlias
Updated time.Time
config *config.Config
readFromFile bool
lock *sync.Mutex
ttl time.Duration
@ -31,6 +33,7 @@ type CacheOpts struct {
TTL time.Duration
Logger *pterm.Logger
Gitlab *gitlab.Client
Config *config.Config
}
// Load cache, if already loaded and up to date, nothing is done.
@ -124,10 +127,21 @@ func (c *Cache) String() string {
len(c.Aliases))
}
func (c *Cache) DumpString() string {
// This command will only dump projects that have
// been cloned locally. Setting all to true will list all projects
func (c *Cache) DumpString(all bool) string {
str := strings.Builder{}
str.WriteString(c.String() + "\n\nProjects:\n")
var term string
if all {
term = pterm.Bold.Sprint("Projects")
} else {
term = pterm.Bold.Sprint("Local Projects")
}
str.WriteString(c.String() + "\n\n" + term + ":\n")
for _, project := range c.Projects {
if !all && !c.IsProjectCloned(project) {
continue
}
str.WriteString(" - " + pterm.FgLightBlue.Sprint(project.Name) + " (")
str.WriteString(project.PathWithNamespace + ")\n")
aliases := c.GetProjectAliases(project)
@ -169,6 +183,7 @@ func NewProjectCache(opts *CacheOpts) (*Cache, error) {
cache := &Cache{
Projects: make([]*gitlab.Project, 0),
Aliases: make([]*ProjectAlias, 0),
config: opts.Config,
file: opts.Path,
ttl: opts.TTL,
lock: &sync.Mutex{},

View File

@ -122,7 +122,7 @@ func (c *Cache) GetProjectAliases(project *gitlab.Project) []*ProjectAlias {
}
func (c *Cache) LoadProjects() {
progressInfo := c.gitlab.StreamProjects()
progressInfo := c.gitlab.StreamProjects(c.config.Cache.Load.OwnerOnly)
c.Projects = make([]*gitlab.Project, 0)
pBar := pterm.DefaultProgressbar.

View File

@ -2,12 +2,13 @@ package projects
import (
"os"
"path/filepath"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab"
)
func (c *Cache) GoTo(project *gitlab.Project) {
pPath := c.path + "/" + project.SanitizedPath()
pPath := c.GetProjectPath(project)
c.log.Debug("Going to project", c.log.Args(
"project", project.String(),
@ -18,6 +19,16 @@ func (c *Cache) GoTo(project *gitlab.Project) {
c.log.Info("Preparing project path")
c.PrepProjectPath(pPath)
}
os.Chdir(filepath.Dir(pPath))
}
func (c *Cache) IsProjectCloned(p *gitlab.Project) bool {
_, err := os.Stat(c.GetProjectPath(p) + "/.git")
if err == nil {
return true
}
return false
}
func (c *Cache) PrepProjectPath(path string) {
@ -28,3 +39,7 @@ func (c *Cache) PrepProjectPath(path string) {
))
}
}
func (c *Cache) GetProjectPath(p *gitlab.Project) string {
return c.path + "/" + p.SanitizedPath()
}

View File

@ -0,0 +1,60 @@
package projects
import (
"context"
"time"
git "github.com/go-git/go-git/v5"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab"
)
const gitCloneTimeoutSecs = 10
// Will either read in the current repo, preparing a report
// on its current state, or will clone the project if it has not
// already been cloned in its path
func (c *Cache) OpenProject(ctx context.Context, project *gitlab.Project) *git.Repository {
path := c.GetProjectPath(project)
cloneCtx, cncl := context.WithDeadline(ctx, time.Now().Add(gitCloneTimeoutSecs*time.Second))
defer cncl()
var repo *git.Repository
var err error
if repo, err = git.PlainOpen(path); err != nil {
if err == git.ErrRepositoryNotExists {
c.log.Debug("Project not yet cloned")
}
}
if repo == nil {
// Check to make sure we can connect before we time out
// shouldn't be necessary, but go-git does not properly
// honor its context
if err := project.CheckHost(gitlab.GitlabProtoSSH); err != nil {
c.log.Fatal("Git remote unreachable, giving up", c.log.Args("error", err))
}
c.log.Info("Cloning project from remote", c.log.Args("remote", project.SSHURLToRepo))
repo, err = git.PlainCloneContext(cloneCtx, path, false, &git.CloneOptions{
URL: project.SSHURLToRepo,
})
if err == git.ErrRepositoryAlreadyExists {
c.log.Debug("Skipping clone, already exists")
} else if err != nil {
c.log.Fatal("Failed to open git project", c.log.Args(
"error", err,
))
}
}
head, err := repo.Head()
if err != nil {
c.log.Fatal("Something went wrong, unable to fetch HEAD from repo", c.log.Args(
"error", err,
))
}
c.log.Debug("Repository ready", c.log.Args("branch", head.Name().String()))
return repo
}