git-project-manager/internal/cache/cache_aliases.go

80 lines
1.9 KiB
Go
Raw Normal View History

2024-01-15 21:02:15 +00:00
package cache
2023-12-08 21:52:26 +00:00
2023-12-09 04:13:17 +00:00
import (
"errors"
2024-01-15 20:39:35 +00:00
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
2023-12-09 04:13:17 +00:00
"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)
}
2023-12-08 21:52:26 +00:00
func (c *Cache) addAlias(alias string, projectID int, remote string) error {
2023-12-08 21:52:26 +00:00
if c.GetAliasByName(alias) != nil {
return errors.New("Failed to add alias, already exists")
}
c.Aliases = append(c.Aliases,
&ProjectAlias{
Alias: alias,
ProjectID: projectID,
Remote: remote,
2023-12-08 21:52:26 +00:00
})
return nil
}
func (c *Cache) AddAlias(alias string, projectID int, remote string) error {
2023-12-08 21:52:26 +00:00
c.lock.Lock()
defer c.lock.Unlock()
return c.addAlias(alias, projectID, remote)
2023-12-08 21:52:26 +00:00
}
2023-12-09 04:13:17 +00:00
2024-01-15 20:39:35 +00:00
func (c *Cache) GetProjectsWithAliases() []*projects.Project {
projectList := make([]*projects.Project, 0)
2023-12-09 04:13:17 +00:00
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
}
// 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)
}
2023-12-09 04:13:17 +00:00
}
}
2023-12-10 16:15:52 +00:00
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,
))
}
2023-12-10 16:15:52 +00:00
}