From 94bbff6d07743782d5a7f0bc22fdc2f3144dcefa Mon Sep 17 00:00:00 2001 From: Ryan D McGuire Date: Tue, 16 Jan 2024 14:26:56 -0500 Subject: [PATCH] Gitea remote support --- cmd/util_init.go | 6 ++++++ internal/cache/cache_load.go | 9 +++++++-- internal/config/config.go | 5 +++-- internal/remotes/gitea/gitea.go | 12 +++++++++++- internal/remotes/gitea/gitea_api.go | 5 +++-- internal/remotes/gitlab/gitlab.go | 7 +++++++ internal/remotes/remote/remote.go | 6 +++++- internal/remotes/remotes.go | 9 +++++++-- 8 files changed, 49 insertions(+), 10 deletions(-) diff --git a/cmd/util_init.go b/cmd/util_init.go index b48a7d7..962f98e 100644 --- a/cmd/util_init.go +++ b/cmd/util_init.go @@ -59,6 +59,9 @@ func initProjectCache(cmd *cobra.Command, args []string) { func getGiteaRemotes(cmd *cobra.Command) []remote.Remote { gitRemotes := make([]remote.Remote, 0) for _, gitea := range conf.Giteas { + if gitea.CloneProto == "" { + gitea.CloneProto = config.DefaultCloneProto + } giteaRemote, err := gitearemote.NewGiteaRemote(&remote.RemoteInfo{ Ctx: cmd.Context(), Host: gitea.Host, @@ -92,6 +95,9 @@ func getGitLabRemotes(cmd *cobra.Command) []remote.Remote { // Load Gitlabs for _, gl := range conf.Gitlabs { + if gl.CloneProto == "" { + gl.CloneProto = config.DefaultCloneProto + } gitlabRemote, err := gitlabremote.NewGitlabRemote(&remote.RemoteInfo{ Ctx: cmd.Context(), Host: gl.Host, diff --git a/internal/cache/cache_load.go b/internal/cache/cache_load.go index 23cc0af..85c7218 100644 --- a/internal/cache/cache_load.go +++ b/internal/cache/cache_load.go @@ -17,7 +17,7 @@ func (c *Cache) LoadRemotes() { for _, r := range *c.remotes { if !remote.IsAlive(r) { c.log.Error("Skipping load of remote, not alive", c.log.Args( - "remote", r.GetInfo(), + "remote", r.String(), )) continue } @@ -31,7 +31,12 @@ func (c *Cache) LoadRemotes() { Ctx: r.GetInfo().Ctx, 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 pBar, _ := pterm.DefaultProgressbar. diff --git a/internal/config/config.go b/internal/config/config.go index 638d70f..5f91914 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -35,8 +35,9 @@ type GitlabConfig struct { type CloneProto string const ( - CloneProtoSSH CloneProto = "ssh" - CloneProtoHTTP CloneProto = "http" + CloneProtoSSH CloneProto = "ssh" + CloneProtoHTTP CloneProto = "http" + DefaultCloneProto CloneProto = CloneProtoSSH ) type editorConfig struct { diff --git a/internal/remotes/gitea/gitea.go b/internal/remotes/gitea/gitea.go index 40df2c7..783d0d8 100644 --- a/internal/remotes/gitea/gitea.go +++ b/internal/remotes/gitea/gitea.go @@ -1,6 +1,8 @@ package gitearemote import ( + "fmt" + "code.gitea.io/sdk/gitea" "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 } +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) { - 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 { return nil, err diff --git a/internal/remotes/gitea/gitea_api.go b/internal/remotes/gitea/gitea_api.go index 484a6ac..8956c02 100644 --- a/internal/remotes/gitea/gitea_api.go +++ b/internal/remotes/gitea/gitea_api.go @@ -3,6 +3,7 @@ package gitearemote import ( "errors" "fmt" + "strconv" "code.gitea.io/sdk/gitea" "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 _, resp, err := r.api.SearchRepos(gitea.SearchRepoOptions{ListOptions: gitea.ListOptions{PageSize: 1}}) if err != nil { + fmt.Println(err) return -1 } - fmt.Println(resp) - + projects, _ = strconv.Atoi(resp.Header.Get("X-Total-Count")) return projects } diff --git a/internal/remotes/gitlab/gitlab.go b/internal/remotes/gitlab/gitlab.go index f7c2b9f..5d23c7c 100644 --- a/internal/remotes/gitlab/gitlab.go +++ b/internal/remotes/gitlab/gitlab.go @@ -1,6 +1,8 @@ package gitlabremote import ( + "fmt" + "github.com/xanzy/go-gitlab" "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 } +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) { api, err := NewGitlabApi(remoteInfo) if err != nil { diff --git a/internal/remotes/remote/remote.go b/internal/remotes/remote/remote.go index c30e598..9ef818b 100644 --- a/internal/remotes/remote/remote.go +++ b/internal/remotes/remote/remote.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net" + "net/url" "time" "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config" @@ -26,6 +27,7 @@ const defNetDialTimeoutSecs = 3 // provided by *load.ProgressInfo type Remote interface { GetInfo() *RemoteInfo // Returns basic RemoteInfo struct + String() string // String info for remote GetNumProjects(*RemoteQueryOpts) int // Returns total number of accessible projects StreamProjects(*load.ProgressInfo, *RemoteQueryOpts) // Streams projects to chans provided in load.ProgressInfo } @@ -39,8 +41,10 @@ func IsAlive(remote Remote) bool { port = 22 } + remoteUrl, _ := url.Parse(remote.GetInfo().Host) + d, err := net.DialTimeout("tcp", - fmt.Sprintf("%s:%d", remote.GetInfo().Host, port), + fmt.Sprintf("%s:%d", remoteUrl.Host, port), defNetDialTimeoutSecs*time.Second) if err == nil { diff --git a/internal/remotes/remotes.go b/internal/remotes/remotes.go index 46091b0..2958a72 100644 --- a/internal/remotes/remotes.go +++ b/internal/remotes/remotes.go @@ -1,6 +1,8 @@ package remotes 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/projects" "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 // returns slice of load.ProgressInfo, does not block, streamer is // 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{ ProgressChan: make(chan load.Progress), ProjectsChan: make(chan []*projects.Project), @@ -41,8 +43,11 @@ func StreamRemote(r remote.Remote, opts *remote.RemoteQueryOpts) *load.ProgressI DoneChan: make(chan interface{}), 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) - return progressInfo + return progressInfo, nil } func NewRemotes() *Remotes {