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

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