75 lines
1.7 KiB
Go
75 lines
1.7 KiB
Go
package projects
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab"
|
|
"golang.org/x/exp/slices"
|
|
)
|
|
|
|
func (c *Cache) deleteAlias(alias *ProjectAlias) {
|
|
for i, a := range c.Aliases {
|
|
if a.Alias == alias.Alias {
|
|
c.Aliases = append(c.Aliases[:i], c.Aliases[i+1:]...)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *Cache) DeleteAlias(alias *ProjectAlias) {
|
|
c.lock.Lock()
|
|
defer c.lock.Unlock()
|
|
c.deleteAlias(alias)
|
|
}
|
|
|
|
func (c *Cache) addAlias(alias string, projectID int) error {
|
|
if c.GetAliasByName(alias) != nil {
|
|
return errors.New("Failed to add alias, already exists")
|
|
}
|
|
|
|
c.Aliases = append(c.Aliases,
|
|
&ProjectAlias{
|
|
Alias: alias,
|
|
ProjectID: projectID,
|
|
})
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *Cache) AddAlias(alias string, projectID int) error {
|
|
c.lock.Lock()
|
|
defer c.lock.Unlock()
|
|
return c.addAlias(alias, projectID)
|
|
}
|
|
|
|
func (c *Cache) GetProjectsWithAliases() []*gitlab.Project {
|
|
projectList := make([]*gitlab.Project, 0)
|
|
projectsFound := make([]int, 0)
|
|
for _, a := range c.Aliases {
|
|
if !slices.Contains(projectsFound, a.ProjectID) {
|
|
projectList = append(projectList, c.GetProjectByAlias(a))
|
|
projectsFound = append(projectsFound, a.ProjectID)
|
|
}
|
|
}
|
|
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
|
|
}
|
|
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,
|
|
)
|
|
}
|