Add multi-remote support for GitLab (#1)
Co-authored-by: Ryan D McGuire <ryand_mcguire@sweetwater.com> Reviewed-on: 50W/git-project-manager#1
This commit is contained in:
@ -3,15 +3,24 @@ package config
|
||||
import "time"
|
||||
|
||||
type Config struct {
|
||||
Editor editorConfig `yaml:"editor" json:"editor"`
|
||||
GitlabHost string `yaml:"gitlabHost" json:"gitlabHost"`
|
||||
GitlabToken string `yaml:"gitlabToken,omitempty" json:"gitlabToken,omitempty"`
|
||||
LogLevel string `yaml:"logLevel" json:"logLevel" enum:"info,warn,debug,error"`
|
||||
ProjectPath string `yaml:"projectPath" json:"projectPath"`
|
||||
Cache cacheConfig `yaml:"cache" json:"cache"`
|
||||
// Named keys above maintained for backwards compatibility
|
||||
// Ideally only Gitlabs is used
|
||||
GitlabHost string `yaml:"gitlabHost,omitempty" json:"gitlabHost,omitempty"`
|
||||
GitlabToken string `yaml:"gitlabToken,omitempty" json:"gitlabToken,omitempty"`
|
||||
Gitlabs []GitlabConfig `yaml:"gitlabs" json:"gitlabs"`
|
||||
LogLevel string `yaml:"logLevel" json:"logLevel" enum:"info,warn,debug,error"`
|
||||
ProjectPath string `yaml:"projectPath" json:"projectPath"`
|
||||
Cache cacheConfig `yaml:"cache" json:"cache"`
|
||||
Dump struct {
|
||||
Full bool
|
||||
}
|
||||
Full bool `yaml:"full" json:"full"`
|
||||
} `yaml:"dump" json:"dump"`
|
||||
Editor editorConfig `yaml:"editor" json:"editor"`
|
||||
}
|
||||
|
||||
type GitlabConfig struct {
|
||||
Host string `yaml:"host" json:"host"`
|
||||
Name string `yaml:"name" json:"name"`
|
||||
Token string `yaml:"token" json:"token"`
|
||||
}
|
||||
|
||||
type editorConfig struct {
|
||||
@ -38,10 +47,13 @@ type cacheConfig struct {
|
||||
}
|
||||
|
||||
var DefaultConfig = Config{
|
||||
GitlabHost: "https://gitlab.com",
|
||||
GitlabToken: "yourtokenhere",
|
||||
LogLevel: "warn",
|
||||
ProjectPath: "~/work/projects",
|
||||
Gitlabs: []GitlabConfig{{
|
||||
Host: "https://gitlab.com",
|
||||
Token: "yourtokenhere",
|
||||
Name: "GitLab",
|
||||
}},
|
||||
Cache: cacheConfig{
|
||||
Ttl: 168 * time.Hour,
|
||||
Load: loadConfig{
|
||||
|
@ -1,10 +1,8 @@
|
||||
package gitlab
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
@ -21,11 +19,6 @@ const (
|
||||
projectsPerGoroutine = 200
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
Ctx context.Context
|
||||
gitlab *gitlab.Client
|
||||
}
|
||||
|
||||
type Project struct {
|
||||
ID int
|
||||
Description string
|
||||
@ -39,6 +32,8 @@ type Project struct {
|
||||
AvatarURL string
|
||||
LastActivityAt time.Time
|
||||
Readme string
|
||||
Remote string
|
||||
Owner string
|
||||
Languages *ProjectLanguages
|
||||
gitRepo *git.Repository
|
||||
}
|
||||
@ -58,22 +53,16 @@ type User struct {
|
||||
AvatarURL string
|
||||
}
|
||||
|
||||
type ProgressInfo struct {
|
||||
ProgressChan chan Progress
|
||||
ProjectsChan chan []*Project
|
||||
ErrorChan chan error
|
||||
DoneChan chan interface{}
|
||||
}
|
||||
|
||||
type Progress struct {
|
||||
Page int
|
||||
Pages int
|
||||
Projects int
|
||||
TotalProjects int
|
||||
func (c *Client) Api() *gitlab.Client {
|
||||
return c.apiClient
|
||||
}
|
||||
|
||||
func (p *Project) String() string {
|
||||
return fmt.Sprintf("%s (%s)", p.Path, p.PathWithNamespace)
|
||||
var projectString string
|
||||
if p != nil {
|
||||
projectString = fmt.Sprintf("%s (%s)", p.Path, p.PathWithNamespace)
|
||||
}
|
||||
return projectString
|
||||
}
|
||||
|
||||
func (p *Project) GetLanguage() *ProjectLanguage {
|
||||
@ -105,117 +94,7 @@ 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
|
||||
str += "\n" + pterm.LightRed("Project: ") + pterm.Bold.Sprint(p.Name) + "\n"
|
||||
|
||||
head, _ := repo.Head()
|
||||
branch := head.Name().String()[11:]
|
||||
b, _ := repo.Branch(branch)
|
||||
if b != nil {
|
||||
str += pterm.LightCyan("Branch: ") + pterm.Bold.Sprint(b.Name) + "\n"
|
||||
} else {
|
||||
str += pterm.LightCyan("NEW Branch: ") + pterm.Bold.Sprint(branch) + "\n"
|
||||
}
|
||||
|
||||
commit, _ := repo.CommitObject(head.Hash())
|
||||
str += "\n" + commit.String()
|
||||
|
||||
str += pterm.LightMagenta("GitLab: ") + pterm.Bold.Sprint(p.HTTPURLToRepo) + "\n"
|
||||
|
||||
if remotes, _ := repo.Remotes(); len(remotes) > 0 {
|
||||
str += pterm.LightBlue("Remote: ") + pterm.Bold.Sprint(remotes[0].Config().URLs[0])
|
||||
}
|
||||
|
||||
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. 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),
|
||||
ErrorChan: make(chan error),
|
||||
DoneChan: make(chan interface{}),
|
||||
}
|
||||
|
||||
go c.streamProjects(pi, ownerOnly)
|
||||
|
||||
return pi
|
||||
}
|
||||
|
||||
func (c *Client) streamProjects(pi *ProgressInfo, ownerOnly bool) {
|
||||
defer close(pi.ProgressChan)
|
||||
defer close(pi.ProjectsChan)
|
||||
|
||||
listOpts := &gitlab.ListProjectsOptions{
|
||||
ListOptions: gitlab.ListOptions{
|
||||
PerPage: projectsPerPage,
|
||||
Page: 1,
|
||||
},
|
||||
Archived: gitlab.Ptr[bool](false),
|
||||
Owned: gitlab.Ptr[bool](ownerOnly),
|
||||
}
|
||||
|
||||
// Get total number of projects
|
||||
projectsTotal := c.getTotalProjects(listOpts)
|
||||
numGoroutines := projectsTotal / projectsPerGoroutine
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
startPage := 1
|
||||
for i := 1; i <= numGoroutines+1; i++ {
|
||||
wg.Add(1)
|
||||
endPage := startPage + (projectsPerGoroutine / projectsPerPage)
|
||||
go func(startPage int, endPage int) {
|
||||
defer wg.Done()
|
||||
opts := *listOpts
|
||||
opts.Page = startPage
|
||||
for {
|
||||
projects, resp, err := c.ListProjects(&opts)
|
||||
|
||||
if err != nil {
|
||||
pi.ErrorChan <- err
|
||||
break
|
||||
}
|
||||
|
||||
pi.ProjectsChan <- projects
|
||||
pi.ProgressChan <- Progress{
|
||||
Page: resp.CurrentPage,
|
||||
Pages: resp.TotalPages,
|
||||
Projects: len(projects),
|
||||
TotalProjects: resp.TotalItems,
|
||||
}
|
||||
|
||||
// We're done when we have it all or our context is done
|
||||
// or we've hit our total pages
|
||||
if c.Ctx.Err() != nil || resp.NextPage == 0 {
|
||||
break
|
||||
} else if opts.Page == endPage {
|
||||
break
|
||||
}
|
||||
|
||||
opts.Page = resp.NextPage
|
||||
}
|
||||
}(startPage, endPage)
|
||||
startPage = endPage + 1
|
||||
}
|
||||
wg.Wait()
|
||||
pi.DoneChan <- nil
|
||||
}
|
||||
|
||||
func (c *Client) getTotalProjects(opts *gitlab.ListProjectsOptions) int {
|
||||
func (c *Client) GetTotalProjects(opts *gitlab.ListProjectsOptions) int {
|
||||
reqOpts := *opts
|
||||
reqOpts.ListOptions = gitlab.ListOptions{
|
||||
Page: 1,
|
||||
@ -223,7 +102,7 @@ func (c *Client) getTotalProjects(opts *gitlab.ListProjectsOptions) int {
|
||||
}
|
||||
|
||||
var projects int
|
||||
if _, r, e := c.gitlab.Projects.ListProjects(opts, gitlab.WithContext(c.Ctx)); e == nil {
|
||||
if _, r, e := c.apiClient.Projects.ListProjects(opts, gitlab.WithContext(c.Ctx)); e == nil {
|
||||
projects = r.TotalItems
|
||||
}
|
||||
|
||||
@ -235,7 +114,7 @@ func (c *Client) getTotalProjects(opts *gitlab.ListProjectsOptions) int {
|
||||
func (c *Client) ListProjects(opts *gitlab.ListProjectsOptions) (
|
||||
[]*Project, *gitlab.Response, error) {
|
||||
pList := make([]*Project, 0)
|
||||
projects, resp, err := c.gitlab.Projects.ListProjects(
|
||||
projects, resp, err := c.apiClient.Projects.ListProjects(
|
||||
opts,
|
||||
gitlab.WithContext(c.Ctx),
|
||||
)
|
||||
@ -245,35 +124,10 @@ func (c *Client) ListProjects(opts *gitlab.ListProjectsOptions) (
|
||||
return pList, resp, err
|
||||
}
|
||||
|
||||
func (c *Client) handleProjects(projects []*gitlab.Project) []*Project {
|
||||
// Opportunity to perform any filtering or additional lookups
|
||||
// on a per-project basis
|
||||
pList := make([]*Project, 0, len(projects))
|
||||
for _, project := range projects {
|
||||
p := &Project{
|
||||
ID: project.ID,
|
||||
Description: project.Description,
|
||||
SSHURLToRepo: project.SSHURLToRepo,
|
||||
HTTPURLToRepo: project.HTTPURLToRepo,
|
||||
WebURL: project.WebURL,
|
||||
Name: project.Name,
|
||||
NameWithNamespace: project.NameWithNamespace,
|
||||
Path: project.Path,
|
||||
PathWithNamespace: project.PathWithNamespace,
|
||||
AvatarURL: project.AvatarURL,
|
||||
LastActivityAt: *project.LastActivityAt,
|
||||
Readme: project.ReadmeURL,
|
||||
Languages: c.GetProjectLanguages(project),
|
||||
}
|
||||
pList = append(pList, p)
|
||||
}
|
||||
return pList
|
||||
}
|
||||
|
||||
// A nil return indicates an API error or GitLab doesn't know what
|
||||
// language the project uses.
|
||||
func (c *Client) GetProjectLanguages(project *gitlab.Project) *ProjectLanguages {
|
||||
l, _, e := c.gitlab.Projects.GetProjectLanguages(project.ID, gitlab.WithContext(c.Ctx))
|
||||
l, _, e := c.apiClient.Projects.GetProjectLanguages(project.ID, gitlab.WithContext(c.Ctx))
|
||||
if e != nil {
|
||||
pterm.Error.Printfln("Failed requesting project languages: %s", e.Error())
|
||||
return nil
|
||||
@ -293,15 +147,3 @@ func (c *Client) GetProjectLanguages(project *gitlab.Project) *ProjectLanguages
|
||||
|
||||
return &pLangs
|
||||
}
|
||||
|
||||
func NewGitlabClient(ctx context.Context, host, token string) (*Client, error) {
|
||||
client, err := gitlab.NewClient(token, gitlab.WithBaseURL(host))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
gitlabClient := &Client{
|
||||
Ctx: ctx,
|
||||
gitlab: client,
|
||||
}
|
||||
return gitlabClient, nil
|
||||
}
|
||||
|
83
internal/gitlab/gitlab_client.go
Normal file
83
internal/gitlab/gitlab_client.go
Normal file
@ -0,0 +1,83 @@
|
||||
package gitlab
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/xanzy/go-gitlab"
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
Ctx context.Context
|
||||
Config *config.GitlabConfig
|
||||
apiClient *gitlab.Client
|
||||
}
|
||||
|
||||
type Clients []*Client
|
||||
|
||||
type ClientOpts struct {
|
||||
Ctx context.Context
|
||||
Name string
|
||||
Host string
|
||||
Token string
|
||||
}
|
||||
|
||||
func (c *Clients) AddClients(gitlabClient ...*Client) error {
|
||||
var err error
|
||||
for _, client := range gitlabClient {
|
||||
if c.GetClientByHost(client.Config.Host) != nil {
|
||||
err = errors.Join(err, fmt.Errorf("Client with host %s already exists", client.Config.Host))
|
||||
} else {
|
||||
*c = append(*c, client)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Clients) GetClientByHost(host string) *Client {
|
||||
for _, client := range *c {
|
||||
if client.Config.Host == host {
|
||||
return client
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewCLients() *Clients {
|
||||
var clients Clients
|
||||
clients = make([]*Client, 0)
|
||||
return &clients
|
||||
}
|
||||
|
||||
func NewGitlabClients(clientOpts []*ClientOpts) (*Clients, error) {
|
||||
var err error
|
||||
clients := NewCLients()
|
||||
for _, opts := range clientOpts {
|
||||
gitlabClient, e := NewGitlabClient(opts)
|
||||
if e != nil {
|
||||
err = errors.Join(err, e)
|
||||
continue
|
||||
}
|
||||
err = errors.Join(err, clients.AddClients(gitlabClient))
|
||||
}
|
||||
return clients, err
|
||||
}
|
||||
|
||||
func NewGitlabClient(opts *ClientOpts) (*Client, error) {
|
||||
client, err := gitlab.NewClient(opts.Token, gitlab.WithBaseURL(opts.Host))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
gitlabClient := &Client{
|
||||
Ctx: opts.Ctx,
|
||||
Config: &config.GitlabConfig{
|
||||
Name: opts.Name,
|
||||
Host: opts.Host,
|
||||
Token: opts.Token,
|
||||
},
|
||||
apiClient: client,
|
||||
}
|
||||
return gitlabClient, nil
|
||||
}
|
37
internal/gitlab/gitlab_git.go
Normal file
37
internal/gitlab/gitlab_git.go
Normal file
@ -0,0 +1,37 @@
|
||||
package gitlab
|
||||
|
||||
import "github.com/pterm/pterm"
|
||||
|
||||
func (p *Project) GetGitInfo() string {
|
||||
repo := p.GetRepo()
|
||||
if repo == nil {
|
||||
return "No Repo"
|
||||
}
|
||||
|
||||
var str string
|
||||
str += "\n" + pterm.LightRed("Project: ") + pterm.Bold.Sprint(p.Name) + "\n"
|
||||
|
||||
head, _ := repo.Head()
|
||||
branch := head.Name().String()[11:]
|
||||
b, _ := repo.Branch(branch)
|
||||
if b != nil {
|
||||
str += pterm.LightCyan("Branch: ") + pterm.Bold.Sprint(b.Name) + "\n"
|
||||
} else {
|
||||
str += pterm.LightCyan("NEW Branch: ") + pterm.Bold.Sprint(branch) + "\n"
|
||||
}
|
||||
|
||||
commit, _ := repo.CommitObject(head.Hash())
|
||||
str += "\n" + commit.String()
|
||||
|
||||
str += pterm.LightMagenta("GitLab: ") + pterm.Bold.Sprint(p.HTTPURLToRepo) + "\n"
|
||||
|
||||
if remotes, _ := repo.Remotes(); len(remotes) > 0 {
|
||||
str += pterm.LightBlue("Remote: ") + pterm.Bold.Sprint(remotes[0].Config().URLs[0])
|
||||
}
|
||||
|
||||
return pterm.DefaultBox.
|
||||
WithLeftPadding(5).WithRightPadding(5).
|
||||
WithBoxStyle(&pterm.Style{pterm.FgLightBlue}).
|
||||
WithTitle(pterm.Bold.Sprint(pterm.LightGreen("Project Git Status"))).
|
||||
Sprint(str)
|
||||
}
|
131
internal/gitlab/gitlab_load.go
Normal file
131
internal/gitlab/gitlab_load.go
Normal file
@ -0,0 +1,131 @@
|
||||
package gitlab
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/xanzy/go-gitlab"
|
||||
)
|
||||
|
||||
type ProgressInfo struct {
|
||||
ProgressChan chan Progress
|
||||
ProjectsChan chan []*Project
|
||||
ErrorChan chan error
|
||||
DoneChan chan interface{}
|
||||
NumProjects int
|
||||
}
|
||||
|
||||
type Progress struct {
|
||||
Page int
|
||||
Pages int
|
||||
Projects int
|
||||
TotalProjects int
|
||||
}
|
||||
|
||||
var DefaultListOpts = &gitlab.ListProjectsOptions{
|
||||
ListOptions: gitlab.ListOptions{
|
||||
PerPage: projectsPerPage,
|
||||
Page: 1,
|
||||
},
|
||||
Archived: gitlab.Ptr[bool](false),
|
||||
}
|
||||
|
||||
// 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. If ownerOnly=true, only
|
||||
// projects for which you are an owner will be loaded
|
||||
func (c *Client) StreamProjects(ownerOnly bool, projects int) *ProgressInfo {
|
||||
pi := &ProgressInfo{
|
||||
ProgressChan: make(chan Progress),
|
||||
ProjectsChan: make(chan []*Project),
|
||||
ErrorChan: make(chan error),
|
||||
DoneChan: make(chan interface{}),
|
||||
NumProjects: projects,
|
||||
}
|
||||
|
||||
go c.streamProjects(pi, ownerOnly)
|
||||
|
||||
return pi
|
||||
}
|
||||
|
||||
func (c *Client) streamProjects(pi *ProgressInfo, ownerOnly bool) {
|
||||
defer close(pi.ProgressChan)
|
||||
defer close(pi.ProjectsChan)
|
||||
|
||||
listOpts := *DefaultListOpts
|
||||
listOpts.Owned = gitlab.Ptr[bool](ownerOnly)
|
||||
|
||||
// Get total number of projects
|
||||
numGoroutines := pi.NumProjects / projectsPerGoroutine
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
startPage := 1
|
||||
for i := 1; i <= numGoroutines+1; i++ {
|
||||
wg.Add(1)
|
||||
endPage := startPage + (projectsPerGoroutine / projectsPerPage)
|
||||
go func(startPage int, endPage int) {
|
||||
defer wg.Done()
|
||||
opts := listOpts
|
||||
opts.Page = startPage
|
||||
for {
|
||||
projects, resp, err := c.ListProjects(&opts)
|
||||
|
||||
if err != nil {
|
||||
pi.ErrorChan <- err
|
||||
break
|
||||
}
|
||||
|
||||
pi.ProjectsChan <- projects
|
||||
pi.ProgressChan <- Progress{
|
||||
Page: resp.CurrentPage,
|
||||
Pages: resp.TotalPages,
|
||||
Projects: len(projects),
|
||||
TotalProjects: resp.TotalItems,
|
||||
}
|
||||
|
||||
// We're done when we have it all or our context is done
|
||||
// or we've hit our total pages
|
||||
if c.Ctx.Err() != nil || resp.NextPage == 0 {
|
||||
break
|
||||
} else if opts.Page == endPage {
|
||||
break
|
||||
}
|
||||
|
||||
opts.Page = resp.NextPage
|
||||
}
|
||||
}(startPage, endPage)
|
||||
startPage = endPage + 1
|
||||
}
|
||||
wg.Wait()
|
||||
pi.DoneChan <- nil
|
||||
}
|
||||
|
||||
func (c *Client) handleProjects(projects []*gitlab.Project) []*Project {
|
||||
// Opportunity to perform any filtering or additional lookups
|
||||
// on a per-project basis
|
||||
pList := make([]*Project, 0, len(projects))
|
||||
for _, project := range projects {
|
||||
var owner string
|
||||
if project.Owner != nil {
|
||||
owner = project.Owner.Email
|
||||
}
|
||||
p := &Project{
|
||||
ID: project.ID,
|
||||
Description: project.Description,
|
||||
SSHURLToRepo: project.SSHURLToRepo,
|
||||
HTTPURLToRepo: project.HTTPURLToRepo,
|
||||
WebURL: project.WebURL,
|
||||
Name: project.Name,
|
||||
NameWithNamespace: project.NameWithNamespace,
|
||||
Path: project.Path,
|
||||
PathWithNamespace: project.PathWithNamespace,
|
||||
Remote: c.Config.Host,
|
||||
Owner: owner,
|
||||
AvatarURL: project.AvatarURL,
|
||||
LastActivityAt: *project.LastActivityAt,
|
||||
Readme: project.ReadmeURL,
|
||||
Languages: c.GetProjectLanguages(project),
|
||||
}
|
||||
pList = append(pList, p)
|
||||
}
|
||||
return pList
|
||||
}
|
@ -3,7 +3,6 @@ package projects
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@ -19,12 +18,13 @@ type Cache struct {
|
||||
Updated time.Time
|
||||
config *config.Config
|
||||
readFromFile bool
|
||||
lock *sync.Mutex
|
||||
lock *sync.Mutex // Lock the entire cache
|
||||
contentLock *sync.Mutex // Lock projects / aliases
|
||||
ttl time.Duration
|
||||
file string
|
||||
log *pterm.Logger
|
||||
gitlab *gitlab.Client
|
||||
path string
|
||||
gitlabs *gitlab.Clients
|
||||
}
|
||||
|
||||
type CacheOpts struct {
|
||||
@ -32,7 +32,7 @@ type CacheOpts struct {
|
||||
ProjectsPath string
|
||||
TTL time.Duration
|
||||
Logger *pterm.Logger
|
||||
Gitlab *gitlab.Client
|
||||
Gitlabs *gitlab.Clients
|
||||
Config *config.Config
|
||||
}
|
||||
|
||||
@ -94,6 +94,10 @@ func (c *Cache) write() {
|
||||
func (c *Cache) Write() {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
c.log.Debug("Saving cache to disk", c.log.Args(
|
||||
"projects", len(c.Projects),
|
||||
"aliases", len(c.Aliases),
|
||||
))
|
||||
c.write()
|
||||
}
|
||||
|
||||
@ -136,7 +140,11 @@ func (c *Cache) Clear(clearAliases bool) {
|
||||
func (c *Cache) refresh() {
|
||||
c.log.Info("Loading project cache, this may take a while\n")
|
||||
defer c.setUpdated()
|
||||
c.LoadProjects()
|
||||
// Fix any dangling aliases
|
||||
// For backwards-compatibility only
|
||||
c.setAliasRemotes()
|
||||
// Retrieve and add/update projects
|
||||
c.LoadGitlabs()
|
||||
}
|
||||
|
||||
// Iterates through all GitLab projects the user has access to, updating
|
||||
@ -148,39 +156,16 @@ func (c *Cache) Refresh() {
|
||||
}
|
||||
|
||||
func (c *Cache) String() string {
|
||||
return fmt.Sprintf("Cache Updated %s: Projects %d, Aliases %d",
|
||||
cacheString := fmt.Sprintf("Cache Updated %s: Projects %d, Aliases %d\nRemotes %d:",
|
||||
c.Updated.String(),
|
||||
len(c.Projects),
|
||||
len(c.Aliases))
|
||||
}
|
||||
|
||||
// This command will only dump projects that have
|
||||
// been cloned locally. Setting all to true will list all projects
|
||||
func (c *Cache) DumpString(all bool) string {
|
||||
str := strings.Builder{}
|
||||
var term string
|
||||
if all {
|
||||
term = pterm.Bold.Sprint("Projects")
|
||||
} else {
|
||||
term = pterm.Bold.Sprint("Local Projects")
|
||||
len(c.Aliases),
|
||||
len(*c.gitlabs),
|
||||
)
|
||||
for _, r := range *c.gitlabs {
|
||||
cacheString += " " + r.Config.Host
|
||||
}
|
||||
str.WriteString(c.String() + "\n\n" + term + ":\n")
|
||||
for _, project := range c.Projects {
|
||||
if !all && !c.IsProjectCloned(project) {
|
||||
continue
|
||||
}
|
||||
str.WriteString(" - " + pterm.FgLightBlue.Sprint(project.Name) + " (")
|
||||
str.WriteString(project.PathWithNamespace + ")\n")
|
||||
aliases := c.GetProjectAliases(project)
|
||||
if len(aliases) > 0 {
|
||||
str.WriteString(pterm.FgLightGreen.Sprint(" aliases:"))
|
||||
for _, a := range aliases {
|
||||
str.WriteString(" [" + pterm.FgCyan.Sprint(a.Alias) + "]")
|
||||
}
|
||||
str.WriteRune('\n')
|
||||
}
|
||||
}
|
||||
return str.String()
|
||||
return cacheString
|
||||
}
|
||||
|
||||
func (c *Cache) setUpdated() {
|
||||
@ -207,16 +192,25 @@ func NewProjectCache(opts *CacheOpts) (*Cache, error) {
|
||||
err = createProjectCache(opts.Path)
|
||||
}
|
||||
|
||||
// Combine old-and-new gitlabs
|
||||
var gitlabs *gitlab.Clients
|
||||
if opts.Gitlabs != nil {
|
||||
gitlabs = opts.Gitlabs
|
||||
} else {
|
||||
gitlabs = gitlab.NewCLients()
|
||||
}
|
||||
|
||||
cache := &Cache{
|
||||
Projects: make([]*gitlab.Project, 0),
|
||||
Aliases: make([]*ProjectAlias, 0),
|
||||
config: opts.Config,
|
||||
file: opts.Path,
|
||||
ttl: opts.TTL,
|
||||
lock: &sync.Mutex{},
|
||||
log: opts.Logger,
|
||||
gitlab: opts.Gitlab,
|
||||
path: opts.ProjectsPath,
|
||||
Projects: make([]*gitlab.Project, 0),
|
||||
Aliases: make([]*ProjectAlias, 0),
|
||||
config: opts.Config,
|
||||
file: opts.Path,
|
||||
ttl: opts.TTL,
|
||||
lock: &sync.Mutex{},
|
||||
contentLock: &sync.Mutex{},
|
||||
log: opts.Logger,
|
||||
gitlabs: gitlabs,
|
||||
path: opts.ProjectsPath,
|
||||
}
|
||||
|
||||
return cache, err
|
||||
|
@ -2,8 +2,6 @@ package projects
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab"
|
||||
"golang.org/x/exp/slices"
|
||||
@ -23,7 +21,7 @@ func (c *Cache) DeleteAlias(alias *ProjectAlias) {
|
||||
c.deleteAlias(alias)
|
||||
}
|
||||
|
||||
func (c *Cache) addAlias(alias string, projectID int) error {
|
||||
func (c *Cache) addAlias(alias string, projectID int, remote string) error {
|
||||
if c.GetAliasByName(alias) != nil {
|
||||
return errors.New("Failed to add alias, already exists")
|
||||
}
|
||||
@ -32,15 +30,16 @@ func (c *Cache) addAlias(alias string, projectID int) error {
|
||||
&ProjectAlias{
|
||||
Alias: alias,
|
||||
ProjectID: projectID,
|
||||
Remote: remote,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cache) AddAlias(alias string, projectID int) error {
|
||||
func (c *Cache) AddAlias(alias string, projectID int, remote string) error {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
return c.addAlias(alias, projectID)
|
||||
return c.addAlias(alias, projectID, remote)
|
||||
}
|
||||
|
||||
func (c *Cache) GetProjectsWithAliases() []*gitlab.Project {
|
||||
@ -55,20 +54,26 @@ func (c *Cache) GetProjectsWithAliases() []*gitlab.Project {
|
||||
return projectList
|
||||
}
|
||||
|
||||
func (c *Cache) GetProjectAliasStrings(project *gitlab.Project) []string {
|
||||
aliases := c.GetProjectAliases(project)
|
||||
strings := make([]string, len(aliases))
|
||||
for i, a := range c.GetProjectAliases(project) {
|
||||
strings[i] = a.Alias
|
||||
// This method only exists if a cache was built
|
||||
// before multi-remote support. Upon the first load
|
||||
// with multi-remotes, this will be run first to update
|
||||
// any missing alias remotes
|
||||
func (c *Cache) setAliasRemotes() {
|
||||
for _, alias := range c.Aliases {
|
||||
if alias.Remote == "" {
|
||||
c.setAliasRemote(alias)
|
||||
}
|
||||
}
|
||||
return strings
|
||||
}
|
||||
|
||||
func (c *Cache) GetProjectStringWithAliases(project *gitlab.Project) string {
|
||||
aliases := c.GetProjectAliasStrings(project)
|
||||
return fmt.Sprintf("%s (%s) -> %s",
|
||||
project.Name,
|
||||
strings.Join(aliases, ", "),
|
||||
project.PathWithNamespace,
|
||||
)
|
||||
func (c *Cache) setAliasRemote(alias *ProjectAlias) {
|
||||
project := c.GetProjectByID(alias.ProjectID)
|
||||
if project != nil {
|
||||
alias.Remote = project.Remote
|
||||
c.log.Debug("Fixed missing alias remote", c.log.Args(
|
||||
"alias", alias.Alias,
|
||||
"projectID", alias.ProjectID,
|
||||
"remote", alias.Remote,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
65
internal/projects/cache_load.go
Normal file
65
internal/projects/cache_load.go
Normal file
@ -0,0 +1,65 @@
|
||||
package projects
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/pterm/pterm"
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab"
|
||||
)
|
||||
|
||||
func (c *Cache) LoadGitlabs() {
|
||||
wg := &sync.WaitGroup{}
|
||||
writer := pterm.DefaultMultiPrinter
|
||||
|
||||
for _, gl := range *c.gitlabs {
|
||||
c.log.Info("Loading projects for remote", c.log.Args(
|
||||
"host", gl.Config.Host,
|
||||
"name", gl.Config.Name,
|
||||
))
|
||||
|
||||
opts := *gitlab.DefaultListOpts
|
||||
opts.Owned = &c.config.Cache.Load.OwnerOnly
|
||||
projects := gl.GetTotalProjects(&opts)
|
||||
|
||||
// Prepare progressbar
|
||||
pBar, _ := pterm.DefaultProgressbar.
|
||||
WithShowPercentage(true).
|
||||
WithTotal(projects).
|
||||
WithWriter(writer.NewWriter()).
|
||||
WithMaxWidth(100).
|
||||
Start(gl.Config.Host)
|
||||
|
||||
wg.Add(1)
|
||||
go c.LoadGitlab(gl, wg, pBar, projects)
|
||||
}
|
||||
|
||||
fmt.Println("")
|
||||
writer.Start()
|
||||
|
||||
wg.Wait()
|
||||
|
||||
writer.Stop()
|
||||
fmt.Println("")
|
||||
}
|
||||
|
||||
func (c *Cache) LoadGitlab(client *gitlab.Client, wg *sync.WaitGroup, pBar *pterm.ProgressbarPrinter, projects int) {
|
||||
defer wg.Done()
|
||||
progressInfo := client.StreamProjects(c.config.Cache.Load.OwnerOnly, projects)
|
||||
|
||||
for {
|
||||
select {
|
||||
case p := <-progressInfo.ProgressChan:
|
||||
pBar.Add(p.Projects)
|
||||
case p := <-progressInfo.ProjectsChan:
|
||||
c.AddProjects(p...)
|
||||
case e := <-progressInfo.ErrorChan:
|
||||
c.log.Error("Fetch GitLab projects error", c.log.Args("error", e, "gitlab", client.Config.Name))
|
||||
case <-client.Ctx.Done():
|
||||
c.log.Warn("LoadProjects cancelled", c.log.Args("reason", client.Ctx.Err()))
|
||||
return
|
||||
case <-progressInfo.DoneChan:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
76
internal/projects/cache_projects.go
Normal file
76
internal/projects/cache_projects.go
Normal file
@ -0,0 +1,76 @@
|
||||
package projects
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/pterm/pterm"
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab"
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
||||
func (c *Cache) AddProjects(projects ...*gitlab.Project) {
|
||||
c.contentLock.Lock()
|
||||
defer c.contentLock.Unlock()
|
||||
for _, p := range projects {
|
||||
var existing *gitlab.Project
|
||||
sameID := c.GetProjectsByID(p.ID)
|
||||
|
||||
// If there is only one by ID, we don't
|
||||
// have to worry about matching remotes.
|
||||
// If there are more than one, either match
|
||||
// remotes or update the remote if it was never
|
||||
// set due to being build from old code
|
||||
// New caches should never have empty remotes.
|
||||
if len(sameID) == 1 {
|
||||
existing = sameID[0]
|
||||
} else if len(sameID) > 1 {
|
||||
for _, pr := range sameID {
|
||||
if pr.Remote == p.Remote || pr.Remote == "" {
|
||||
existing = pr
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add a new one, or update if changed
|
||||
if existing == nil {
|
||||
c.Projects = append(c.Projects, p)
|
||||
} else if *existing != *p {
|
||||
*existing = *p
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This command will only dump projects that have
|
||||
// been cloned locally. Setting all to true will list all projects
|
||||
func (c *Cache) DumpString(all bool, search string, gitlabs ...string) string {
|
||||
str := strings.Builder{}
|
||||
var term string
|
||||
if all {
|
||||
term = pterm.Bold.Sprint("Projects")
|
||||
} else {
|
||||
term = pterm.Bold.Sprint("Local Projects")
|
||||
}
|
||||
str.WriteString(c.String() + "\n\n" + term + ":\n")
|
||||
projects := c.FuzzyFindProjects(search)
|
||||
for _, project := range projects {
|
||||
if !all && !c.IsProjectCloned(project) {
|
||||
continue
|
||||
} else if len(gitlabs) > 0 && !slices.Contains(gitlabs, project.Remote) {
|
||||
continue
|
||||
}
|
||||
str.WriteString(" - " + pterm.FgLightBlue.Sprint(project.Name) + " (")
|
||||
str.WriteString(project.PathWithNamespace + ")\n")
|
||||
aliases := c.GetProjectAliases(project)
|
||||
if len(aliases) > 0 {
|
||||
str.WriteString(pterm.FgLightGreen.Sprint(" aliases:"))
|
||||
for _, a := range aliases {
|
||||
str.WriteString(" [" + pterm.FgCyan.Sprint(a.Alias) + "]")
|
||||
}
|
||||
str.WriteRune('\n')
|
||||
}
|
||||
str.WriteString(pterm.FgLightMagenta.Sprint(" remote: "))
|
||||
str.WriteString(project.Remote + "\n")
|
||||
}
|
||||
return str.String()
|
||||
}
|
@ -4,6 +4,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/lithammer/fuzzysearch/fuzzy"
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab"
|
||||
)
|
||||
|
||||
// Performs a fuzzy find on the input string, returning the closest
|
||||
@ -35,3 +36,22 @@ func (c *Cache) FuzzyFindAlias(name string) []*ProjectAlias {
|
||||
}
|
||||
return aliases
|
||||
}
|
||||
|
||||
// Returns all matching projects by fuzzy find term
|
||||
// Matches NameWithNamespace and Aliases
|
||||
func (c *Cache) FuzzyFindProjects(search string) []*gitlab.Project {
|
||||
projects := make([]*gitlab.Project, 0, len(c.Projects))
|
||||
for _, p := range c.Projects {
|
||||
if fuzzy.MatchFold(search, p.NameWithNamespace) {
|
||||
projects = append(projects, p)
|
||||
continue
|
||||
}
|
||||
for _, a := range c.GetProjectAliases(p) {
|
||||
if fuzzy.MatchFold(search, a.Alias) {
|
||||
projects = append(projects, p)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
return projects
|
||||
}
|
||||
|
@ -1,46 +1,13 @@
|
||||
package projects
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/pterm/pterm"
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab"
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
||||
type ProjectAlias struct {
|
||||
Alias string
|
||||
ProjectID int
|
||||
}
|
||||
|
||||
func ProjectAliasesString(aliases []*ProjectAlias) string {
|
||||
var str string
|
||||
for _, a := range aliases {
|
||||
str += "[" + pterm.LightCyan(a.Alias) + "] "
|
||||
}
|
||||
return strings.Trim(str, " ")
|
||||
}
|
||||
|
||||
func (c *Cache) AliasesByProjectString() string {
|
||||
var str bytes.Buffer
|
||||
|
||||
w := new(tabwriter.Writer)
|
||||
w.Init(&str, 10, 0, 0, ' ', tabwriter.AlignRight)
|
||||
|
||||
for _, p := range c.GetProjectsWithAliases() {
|
||||
var pa string
|
||||
pa += pterm.LightBlue("- ")
|
||||
pa += fmt.Sprint(pterm.Bold.Sprint(p.String()) + " \t ")
|
||||
pa += fmt.Sprint(ProjectAliasesString(c.GetProjectAliases(p)))
|
||||
fmt.Fprintln(w, pa)
|
||||
}
|
||||
|
||||
w.Flush()
|
||||
return str.String()
|
||||
}
|
||||
|
||||
func (c *Cache) ProjectString(p *gitlab.Project) string {
|
||||
info := strings.Builder{}
|
||||
|
||||
@ -72,26 +39,7 @@ func (c *Cache) ProjectStrings(prefix string) []string {
|
||||
projects = append(projects, p.NameWithNamespace)
|
||||
}
|
||||
}
|
||||
return projects
|
||||
}
|
||||
|
||||
func (c *Cache) AliasStrings(prefix string) []string {
|
||||
aliases := make([]string, 0, len(c.Aliases))
|
||||
for _, a := range c.Aliases {
|
||||
if strings.HasPrefix(a.Alias, prefix) {
|
||||
aliases = append(aliases, a.Alias)
|
||||
}
|
||||
}
|
||||
return aliases
|
||||
}
|
||||
|
||||
func (c *Cache) GetAliasByName(name string) *ProjectAlias {
|
||||
for _, a := range c.Aliases {
|
||||
if name == a.Alias {
|
||||
return a
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return slices.Clip(projects)
|
||||
}
|
||||
|
||||
func (c *Cache) GetProjectByPath(path string) *gitlab.Project {
|
||||
@ -103,6 +51,15 @@ func (c *Cache) GetProjectByPath(path string) *gitlab.Project {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cache) GetProjectByRemoteAndId(remote string, id int) *gitlab.Project {
|
||||
for _, p := range c.Projects {
|
||||
if p.ID == id && p.Remote == remote {
|
||||
return p
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cache) GetProjectByID(id int) *gitlab.Project {
|
||||
for _, p := range c.Projects {
|
||||
if p.ID == id {
|
||||
@ -112,63 +69,15 @@ func (c *Cache) GetProjectByID(id int) *gitlab.Project {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cache) GetProjectByAlias(alias *ProjectAlias) *gitlab.Project {
|
||||
if alias == nil {
|
||||
return nil
|
||||
}
|
||||
// Plural form of GetProjectByID
|
||||
// Since multiple remotes may have the same project ID,
|
||||
// this will return all matching
|
||||
func (c *Cache) GetProjectsByID(id int) []*gitlab.Project {
|
||||
projects := make([]*gitlab.Project, 0)
|
||||
for _, p := range c.Projects {
|
||||
if p.ID == alias.ProjectID {
|
||||
return p
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cache) GetProjectAliases(project *gitlab.Project) []*ProjectAlias {
|
||||
aliases := make([]*ProjectAlias, 0)
|
||||
for _, alias := range c.Aliases {
|
||||
if alias.ProjectID == project.ID {
|
||||
aliases = append(aliases, alias)
|
||||
}
|
||||
}
|
||||
return aliases
|
||||
}
|
||||
|
||||
func (c *Cache) LoadProjects() {
|
||||
progressInfo := c.gitlab.StreamProjects(c.config.Cache.Load.OwnerOnly)
|
||||
c.Projects = make([]*gitlab.Project, 0)
|
||||
|
||||
pBar := pterm.DefaultProgressbar.
|
||||
WithShowPercentage(true).
|
||||
WithTotal(-1).
|
||||
WithTitle("Listing GitLab Projects").
|
||||
WithMaxWidth(100)
|
||||
|
||||
defer pBar.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case p := <-progressInfo.ProgressChan:
|
||||
if pBar.Total == -1 {
|
||||
pBar = pBar.WithTotal(p.TotalProjects)
|
||||
pBar, _ = pBar.Start()
|
||||
}
|
||||
// This sucks, has to be a better way, and why is the logger incompatible
|
||||
// with the progressbar?
|
||||
pterm.Debug.Println(fmt.Sprintf("Update received: %#v", p))
|
||||
pBar.Add(p.Projects)
|
||||
case p := <-progressInfo.ProjectsChan:
|
||||
c.Projects = append(c.Projects, p...)
|
||||
case e := <-progressInfo.ErrorChan:
|
||||
c.log.Error("Fetch GitLab projects error", c.log.Args("error", e))
|
||||
case <-c.gitlab.Ctx.Done():
|
||||
c.log.Warn("LoadProjects cancelled", c.log.Args("reason", c.gitlab.Ctx.Err()))
|
||||
return
|
||||
case <-progressInfo.DoneChan:
|
||||
// pBar.Add(pBar.Total - curProjects)
|
||||
fmt.Println("")
|
||||
c.log.Info("Project load complete")
|
||||
return
|
||||
if p.ID == id {
|
||||
projects = append(projects, p)
|
||||
}
|
||||
}
|
||||
return projects
|
||||
}
|
||||
|
106
internal/projects/projects_alias.go
Normal file
106
internal/projects/projects_alias.go
Normal file
@ -0,0 +1,106 @@
|
||||
package projects
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/pterm/pterm"
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab"
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
||||
type ProjectAlias struct {
|
||||
Alias string
|
||||
ProjectID int
|
||||
Remote string
|
||||
}
|
||||
|
||||
func (c *Cache) GetProjectAliasStrings(project *gitlab.Project) []string {
|
||||
aliases := c.GetProjectAliases(project)
|
||||
strings := make([]string, len(aliases))
|
||||
for i, a := range c.GetProjectAliases(project) {
|
||||
strings[i] = a.Alias
|
||||
}
|
||||
return strings
|
||||
}
|
||||
|
||||
func (c *Cache) GetProjectStringWithAliases(project *gitlab.Project) string {
|
||||
aliases := c.GetProjectAliasStrings(project)
|
||||
return fmt.Sprintf("%s (%s) -> %s",
|
||||
project.Name,
|
||||
strings.Join(aliases, ", "),
|
||||
project.PathWithNamespace,
|
||||
)
|
||||
}
|
||||
|
||||
func ProjectAliasesString(aliases []*ProjectAlias) string {
|
||||
var str string
|
||||
for _, a := range aliases {
|
||||
str += "[" + pterm.LightCyan(a.Alias) + "] "
|
||||
}
|
||||
return strings.Trim(str, " ")
|
||||
}
|
||||
|
||||
func (c *Cache) AliasesByProjectString() string {
|
||||
var str bytes.Buffer
|
||||
|
||||
w := new(tabwriter.Writer)
|
||||
w.Init(&str, 10, 0, 0, ' ', tabwriter.AlignRight)
|
||||
|
||||
for _, p := range c.GetProjectsWithAliases() {
|
||||
var pa string
|
||||
pa += pterm.LightBlue("- ")
|
||||
pa += fmt.Sprint(pterm.Bold.Sprint(p.String()) + " \t ")
|
||||
pa += fmt.Sprint(ProjectAliasesString(c.GetProjectAliases(p)))
|
||||
fmt.Fprintln(w, pa)
|
||||
}
|
||||
|
||||
w.Flush()
|
||||
return str.String()
|
||||
}
|
||||
|
||||
func (c *Cache) AliasStrings(prefix string) []string {
|
||||
aliases := make([]string, 0, len(c.Aliases))
|
||||
for _, a := range c.Aliases {
|
||||
if strings.HasPrefix(a.Alias, prefix) {
|
||||
aliases = append(aliases, a.Alias)
|
||||
}
|
||||
}
|
||||
return aliases
|
||||
}
|
||||
|
||||
func (c *Cache) GetAliasByName(name string, gitlabs ...string) *ProjectAlias {
|
||||
for _, a := range c.Aliases {
|
||||
if len(gitlabs) > 0 && !slices.Contains(gitlabs, a.Remote) {
|
||||
continue
|
||||
}
|
||||
if name == a.Alias {
|
||||
return a
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cache) GetProjectByAlias(alias *ProjectAlias) *gitlab.Project {
|
||||
if alias == nil {
|
||||
return nil
|
||||
}
|
||||
for _, p := range c.Projects {
|
||||
if p.ID == alias.ProjectID && p.Remote == alias.Remote {
|
||||
return p
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cache) GetProjectAliases(project *gitlab.Project) []*ProjectAlias {
|
||||
aliases := make([]*ProjectAlias, 0)
|
||||
for _, alias := range c.Aliases {
|
||||
if alias.ProjectID == project.ID {
|
||||
aliases = append(aliases, alias)
|
||||
}
|
||||
}
|
||||
return aliases
|
||||
}
|
Reference in New Issue
Block a user