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 {
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,

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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