Gitea remote support

This commit is contained in:
Ryan McGuire 2024-01-16 14:26:56 -05:00
parent bd354842c0
commit 94bbff6d07
8 changed files with 49 additions and 10 deletions

View File

@ -59,6 +59,9 @@ func initProjectCache(cmd *cobra.Command, args []string) {
func getGiteaRemotes(cmd *cobra.Command) []remote.Remote { func getGiteaRemotes(cmd *cobra.Command) []remote.Remote {
gitRemotes := make([]remote.Remote, 0) gitRemotes := make([]remote.Remote, 0)
for _, gitea := range conf.Giteas { for _, gitea := range conf.Giteas {
if gitea.CloneProto == "" {
gitea.CloneProto = config.DefaultCloneProto
}
giteaRemote, err := gitearemote.NewGiteaRemote(&remote.RemoteInfo{ giteaRemote, err := gitearemote.NewGiteaRemote(&remote.RemoteInfo{
Ctx: cmd.Context(), Ctx: cmd.Context(),
Host: gitea.Host, Host: gitea.Host,
@ -92,6 +95,9 @@ func getGitLabRemotes(cmd *cobra.Command) []remote.Remote {
// Load Gitlabs // Load Gitlabs
for _, gl := range conf.Gitlabs { for _, gl := range conf.Gitlabs {
if gl.CloneProto == "" {
gl.CloneProto = config.DefaultCloneProto
}
gitlabRemote, err := gitlabremote.NewGitlabRemote(&remote.RemoteInfo{ gitlabRemote, err := gitlabremote.NewGitlabRemote(&remote.RemoteInfo{
Ctx: cmd.Context(), Ctx: cmd.Context(),
Host: gl.Host, Host: gl.Host,

View File

@ -17,7 +17,7 @@ func (c *Cache) LoadRemotes() {
for _, r := range *c.remotes { for _, r := range *c.remotes {
if !remote.IsAlive(r) { if !remote.IsAlive(r) {
c.log.Error("Skipping load of remote, not alive", c.log.Args( c.log.Error("Skipping load of remote, not alive", c.log.Args(
"remote", r.GetInfo(), "remote", r.String(),
)) ))
continue continue
} }
@ -31,7 +31,12 @@ func (c *Cache) LoadRemotes() {
Ctx: r.GetInfo().Ctx, Ctx: r.GetInfo().Ctx,
OwnerOnly: c.config.Cache.Load.OwnerOnly, OwnerOnly: c.config.Cache.Load.OwnerOnly,
} }
pi := remotes.StreamRemote(r, opts)
pi, err := remotes.StreamRemote(r, opts)
if err != nil {
c.log.Error("Skipping remote", c.log.Args("error", err))
continue
}
// Prepare progressbar // Prepare progressbar
pBar, _ := pterm.DefaultProgressbar. pBar, _ := pterm.DefaultProgressbar.

View File

@ -37,6 +37,7 @@ type CloneProto string
const ( const (
CloneProtoSSH CloneProto = "ssh" CloneProtoSSH CloneProto = "ssh"
CloneProtoHTTP CloneProto = "http" CloneProtoHTTP CloneProto = "http"
DefaultCloneProto CloneProto = CloneProtoSSH
) )
type editorConfig struct { type editorConfig struct {

View File

@ -1,6 +1,8 @@
package gitearemote package gitearemote
import ( import (
"fmt"
"code.gitea.io/sdk/gitea" "code.gitea.io/sdk/gitea"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote" "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote"
) )
@ -14,8 +16,16 @@ func (r *GiteaRemote) GetInfo() *remote.RemoteInfo {
return r.info return r.info
} }
func (r *GiteaRemote) String() string {
return fmt.Sprintf("Gitea %s (%s), clone proto %s",
r.GetInfo().Name, r.GetInfo().Host, r.GetInfo().CloneProto)
}
func NewGiteaRemote(remoteInfo *remote.RemoteInfo) (*GiteaRemote, error) { func NewGiteaRemote(remoteInfo *remote.RemoteInfo) (*GiteaRemote, error) {
client, err := gitea.NewClient(remoteInfo.Host, gitea.SetContext(remoteInfo.Ctx)) client, err := gitea.NewClient(remoteInfo.Host,
gitea.SetContext(remoteInfo.Ctx),
gitea.SetToken(remoteInfo.Token),
)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -3,6 +3,7 @@ package gitearemote
import ( import (
"errors" "errors"
"fmt" "fmt"
"strconv"
"code.gitea.io/sdk/gitea" "code.gitea.io/sdk/gitea"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/load" "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/load"
@ -15,11 +16,11 @@ func (r *GiteaRemote) GetNumProjects(opts *remote.RemoteQueryOpts) int {
var projects int var projects int
_, resp, err := r.api.SearchRepos(gitea.SearchRepoOptions{ListOptions: gitea.ListOptions{PageSize: 1}}) _, resp, err := r.api.SearchRepos(gitea.SearchRepoOptions{ListOptions: gitea.ListOptions{PageSize: 1}})
if err != nil { if err != nil {
fmt.Println(err)
return -1 return -1
} }
fmt.Println(resp) projects, _ = strconv.Atoi(resp.Header.Get("X-Total-Count"))
return projects return projects
} }

View File

@ -1,6 +1,8 @@
package gitlabremote package gitlabremote
import ( import (
"fmt"
"github.com/xanzy/go-gitlab" "github.com/xanzy/go-gitlab"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote" "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote"
) )
@ -14,6 +16,11 @@ func (r *GitlabRemote) GetInfo() *remote.RemoteInfo {
return r.info return r.info
} }
func (r *GitlabRemote) String() string {
return fmt.Sprintf("GitLab %s (%s), clone proto %s",
r.GetInfo().Name, r.GetInfo().Host, r.GetInfo().CloneProto)
}
func NewGitlabRemote(remoteInfo *remote.RemoteInfo) (*GitlabRemote, error) { func NewGitlabRemote(remoteInfo *remote.RemoteInfo) (*GitlabRemote, error) {
api, err := NewGitlabApi(remoteInfo) api, err := NewGitlabApi(remoteInfo)
if err != nil { if err != nil {

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"net" "net"
"net/url"
"time" "time"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config" "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
@ -26,6 +27,7 @@ const defNetDialTimeoutSecs = 3
// provided by *load.ProgressInfo // provided by *load.ProgressInfo
type Remote interface { type Remote interface {
GetInfo() *RemoteInfo // Returns basic RemoteInfo struct GetInfo() *RemoteInfo // Returns basic RemoteInfo struct
String() string // String info for remote
GetNumProjects(*RemoteQueryOpts) int // Returns total number of accessible projects GetNumProjects(*RemoteQueryOpts) int // Returns total number of accessible projects
StreamProjects(*load.ProgressInfo, *RemoteQueryOpts) // Streams projects to chans provided in load.ProgressInfo StreamProjects(*load.ProgressInfo, *RemoteQueryOpts) // Streams projects to chans provided in load.ProgressInfo
} }
@ -39,8 +41,10 @@ func IsAlive(remote Remote) bool {
port = 22 port = 22
} }
remoteUrl, _ := url.Parse(remote.GetInfo().Host)
d, err := net.DialTimeout("tcp", d, err := net.DialTimeout("tcp",
fmt.Sprintf("%s:%d", remote.GetInfo().Host, port), fmt.Sprintf("%s:%d", remoteUrl.Host, port),
defNetDialTimeoutSecs*time.Second) defNetDialTimeoutSecs*time.Second)
if err == nil { if err == nil {

View File

@ -1,6 +1,8 @@
package remotes package remotes
import ( import (
"errors"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/load" "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/load"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects" "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote" "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote"
@ -33,7 +35,7 @@ func (r *Remotes) GetRemoteByHost(host string) remote.Remote {
// Launches project streamsers for all remotes in goroutines // Launches project streamsers for all remotes in goroutines
// returns slice of load.ProgressInfo, does not block, streamer is // returns slice of load.ProgressInfo, does not block, streamer is
// launched in goroutine // launched in goroutine
func StreamRemote(r remote.Remote, opts *remote.RemoteQueryOpts) *load.ProgressInfo { func StreamRemote(r remote.Remote, opts *remote.RemoteQueryOpts) (*load.ProgressInfo, error) {
progressInfo := &load.ProgressInfo{ progressInfo := &load.ProgressInfo{
ProgressChan: make(chan load.Progress), ProgressChan: make(chan load.Progress),
ProjectsChan: make(chan []*projects.Project), ProjectsChan: make(chan []*projects.Project),
@ -41,8 +43,11 @@ func StreamRemote(r remote.Remote, opts *remote.RemoteQueryOpts) *load.ProgressI
DoneChan: make(chan interface{}), DoneChan: make(chan interface{}),
NumProjects: r.GetNumProjects(opts), NumProjects: r.GetNumProjects(opts),
} }
if progressInfo.NumProjects < 1 {
return nil, errors.New("Failed to fetch project count from remote, won't load")
}
go r.StreamProjects(progressInfo, opts) go r.StreamProjects(progressInfo, opts)
return progressInfo return progressInfo, nil
} }
func NewRemotes() *Remotes { func NewRemotes() *Remotes {