Remove all legacy config

This commit is contained in:
2024-01-17 16:56:41 -05:00
parent 1c373ff00f
commit 4b389fca1c
13 changed files with 101 additions and 142 deletions

View File

@ -4,7 +4,6 @@ import (
"fmt"
"code.gitea.io/sdk/gitea"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/info"
)
@ -13,36 +12,12 @@ type GiteaRemote struct {
api *gitea.Client
}
func (r *GiteaRemote) GetInfos(conf config.Config) []info.RemoteInfo {
// Prepare infos
infos := make([]info.RemoteInfo, len(conf.Giteas))
for i, g := range conf.Giteas {
// Set Defaults
proto := info.CloneProtoSSH
if g.CloneProto == info.CloneProtoHTTP {
proto = info.CloneProtoHTTP
}
if g.Name == "" {
g.Name = g.Host
}
infos[i] = info.RemoteInfo{
Host: g.Host,
Name: g.Name,
Type: "gitea",
Token: g.Token,
CloneProto: proto,
}
}
return infos
}
func (r *GiteaRemote) GetInfo() *info.RemoteInfo {
return r.info
}
func (r *GiteaRemote) GetType() string {
return r.info.Type
return r.info.Type.String()
}
func (r *GiteaRemote) String() string {
@ -52,7 +27,7 @@ func (r *GiteaRemote) String() string {
func NewGiteaRemote(remoteInfo *info.RemoteInfo) (*GiteaRemote, error) {
client, err := gitea.NewClient(remoteInfo.Host,
gitea.SetContext(remoteInfo.Ctx),
gitea.SetContext(remoteInfo.Context()),
gitea.SetToken(remoteInfo.Token),
)

View File

@ -4,7 +4,6 @@ import (
"fmt"
"github.com/xanzy/go-gitlab"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/info"
)
@ -13,46 +12,12 @@ type GitlabRemote struct {
api *gitlab.Client
}
func (r *GitlabRemote) GetInfos(conf config.Config) []info.RemoteInfo {
// Support legacy fields
if conf.GitlabHost != "" && conf.GitlabToken != "" {
conf.Gitlabs = append(conf.Gitlabs, config.GitlabConfig{
Host: conf.GitlabHost,
Name: conf.GitlabHost,
Token: conf.GitlabToken,
CloneProto: info.CloneProtoSSH,
})
}
// Prepare infos
infos := make([]info.RemoteInfo, len(conf.Gitlabs))
for i, g := range conf.Gitlabs {
// Set Defaults
proto := info.CloneProtoSSH
if g.CloneProto == info.CloneProtoHTTP {
proto = info.CloneProtoHTTP
}
if g.Name == "" {
g.Name = g.Host
}
infos[i] = info.RemoteInfo{
Host: g.Host,
Name: g.Name,
Type: "gitlab",
Token: g.Token,
CloneProto: proto,
}
}
return infos
}
func (r *GitlabRemote) GetInfo() *info.RemoteInfo {
return r.info
}
func (r *GitlabRemote) GetType() string {
return r.info.Type
return r.info.Type.String()
}
func (r *GitlabRemote) String() string {

View File

@ -120,6 +120,6 @@ func (r *GitlabRemote) GetProjectLanguages(project *gitlab.Project) *projects.Pr
func (r *GitlabRemote) GetDefaultRequestOptions() []gitlab.RequestOptionFunc {
requestOpts := make([]gitlab.RequestOptionFunc, 1)
requestOpts[0] = gitlab.WithContext(r.GetInfo().Ctx)
requestOpts[0] = gitlab.WithContext(r.GetInfo().Context())
return requestOpts
}

View File

@ -54,7 +54,7 @@ func (r *GitlabRemote) StreamProjects(pi *load.ProgressInfo, opts *remote.Remote
// We're done when we have it all or our context is done
// or we've hit our total pages
if r.info.Ctx.Err() != nil || resp.NextPage == 0 {
if r.info.Context().Err() != nil || resp.NextPage == 0 {
break
} else if opts.Page == endPage {
break

View File

@ -12,13 +12,58 @@ const (
DefaultCloneProto CloneProto = CloneProtoSSH
)
type RemoteType string
type RemoteTypes []RemoteType
// Register remote types here and also add a case
// to func GetRemoteTypeFromString
var (
RemoteTypeGitlab RemoteType = "gitlab"
RemoteTypeGitea RemoteType = "gitea"
RemoteTypesAll RemoteTypes = []RemoteType{
RemoteTypeGitea,
RemoteTypeGitlab,
}
)
func GetRemoteTypeFromString(remoteType string) RemoteType {
var rt RemoteType
switch remoteType {
case RemoteTypeGitea.String():
rt = RemoteTypeGitea
case RemoteTypeGitlab.String():
rt = RemoteTypeGitlab
}
return rt
}
func (rt *RemoteTypes) Strings() []string {
rtStrings := make([]string, len(*rt))
for i, t := range *rt {
rtStrings[i] = string(t)
}
return rtStrings
}
func (rt *RemoteType) String() string {
return string(*rt)
}
// Globally shared info for all remote types
// Stub package to prevent import cycle
type RemoteInfo struct {
Ctx context.Context // Base context for all API calls
Host string // Host as URL with protocol (e.g. https://gitlab.com)
Name string // Human-friendly name for remote
Token string // API token for remote
Type string // Remote type (e.g. gitlab, gitea)
CloneProto CloneProto // CloneProto (ssh or http) determines what url to use for git clone
Host string // Host as URL with protocol (e.g. https://gitlab.com)
Name string // Human-friendly name for remote
Token string // API token for remote
Type RemoteType // Remote type (e.g. gitlab, gitea)
CloneProto CloneProto // CloneProto (ssh or http) determines what url to use for git clone
ctx context.Context
}
func (ri *RemoteInfo) SetContext(ctx context.Context) {
ri.ctx = ctx
}
func (ri *RemoteInfo) Context() context.Context {
return ri.ctx
}

View File

@ -6,7 +6,6 @@ import (
"net/url"
"time"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/info"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/load"
)
@ -18,9 +17,6 @@ const defNetDialTimeoutSecs = 3
// stream all projects along with updates to channels
// provided by *load.ProgressInfo
type Remote interface {
// Helper to process configs.
// Realistically, conf.Gitlabs and conf.Giteas should be conf.Remotes
GetInfos(config.Config) []info.RemoteInfo
String() string // String info for remote
GetInfo() *info.RemoteInfo // Returns basic RemoteInfo struct
GetType() string // Returns the remote type (e.g. GitLab, Gitea)