2023-12-07 17:08:56 +00:00
|
|
|
package projects
|
|
|
|
|
|
|
|
import (
|
2023-12-08 21:52:26 +00:00
|
|
|
"strings"
|
2023-12-07 17:08:56 +00:00
|
|
|
|
|
|
|
"github.com/pterm/pterm"
|
2024-01-15 19:57:15 +00:00
|
|
|
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes"
|
2024-01-14 15:33:15 +00:00
|
|
|
"golang.org/x/exp/slices"
|
2023-12-07 17:08:56 +00:00
|
|
|
)
|
|
|
|
|
2024-01-15 19:57:15 +00:00
|
|
|
func (c *Cache) ProjectString(p *remotes.Project) string {
|
2023-12-08 21:52:26 +00:00
|
|
|
info := strings.Builder{}
|
|
|
|
|
2023-12-09 04:13:17 +00:00
|
|
|
info.WriteString(pterm.LightGreen("\n--------------\n"))
|
|
|
|
info.WriteString(pterm.Bold.Sprint(p.Name))
|
2023-12-08 21:52:26 +00:00
|
|
|
info.WriteRune('\n')
|
|
|
|
if p.Description != "" {
|
|
|
|
info.WriteString(p.Description)
|
|
|
|
info.WriteRune('\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
info.WriteString("\nPath: " + pterm.LightGreen(p.PathWithNamespace))
|
|
|
|
info.WriteString("\nProjectID: " + pterm.LightGreen(p.ID))
|
|
|
|
info.WriteString("\nURL: " + pterm.LightGreen(p.WebURL))
|
|
|
|
info.WriteString("\nLastActivity: " + pterm.LightMagenta(p.LastActivityAt.String()))
|
|
|
|
info.WriteString("\nAliases: ")
|
|
|
|
|
|
|
|
aliases := c.GetProjectAliases(p)
|
|
|
|
info.WriteString(ProjectAliasesString(aliases))
|
|
|
|
|
2023-12-09 04:13:17 +00:00
|
|
|
info.WriteString(pterm.LightGreen("\n--------------\n"))
|
2023-12-08 21:52:26 +00:00
|
|
|
return info.String()
|
|
|
|
}
|
|
|
|
|
2023-12-20 16:23:11 +00:00
|
|
|
func (c *Cache) ProjectStrings(prefix string) []string {
|
|
|
|
projects := make([]string, 0, len(c.Projects))
|
|
|
|
for _, p := range c.Projects {
|
|
|
|
if strings.HasPrefix(p.NameWithNamespace, prefix) {
|
|
|
|
projects = append(projects, p.NameWithNamespace)
|
|
|
|
}
|
2023-12-08 21:52:26 +00:00
|
|
|
}
|
2024-01-14 15:33:15 +00:00
|
|
|
return slices.Clip(projects)
|
2023-12-08 21:52:26 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 19:57:15 +00:00
|
|
|
func (c *Cache) GetProjectByPath(path string) *remotes.Project {
|
2023-12-11 20:42:50 +00:00
|
|
|
for _, p := range c.Projects {
|
|
|
|
if p.PathWithNamespace == path {
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-15 19:57:15 +00:00
|
|
|
func (c *Cache) GetProjectByRemoteAndId(remote string, id int) *remotes.Project {
|
2023-12-08 21:52:26 +00:00
|
|
|
for _, p := range c.Projects {
|
2024-01-14 15:33:15 +00:00
|
|
|
if p.ID == id && p.Remote == remote {
|
2023-12-08 21:52:26 +00:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-15 19:57:15 +00:00
|
|
|
func (c *Cache) GetProjectByID(id int) *remotes.Project {
|
2023-12-08 21:52:26 +00:00
|
|
|
for _, p := range c.Projects {
|
2024-01-14 15:33:15 +00:00
|
|
|
if p.ID == id {
|
2023-12-08 21:52:26 +00:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-14 15:33:15 +00:00
|
|
|
// Plural form of GetProjectByID
|
|
|
|
// Since multiple remotes may have the same project ID,
|
|
|
|
// this will return all matching
|
2024-01-15 19:57:15 +00:00
|
|
|
func (c *Cache) GetProjectsByID(id int) []*remotes.Project {
|
|
|
|
projects := make([]*remotes.Project, 0)
|
2024-01-14 15:33:15 +00:00
|
|
|
for _, p := range c.Projects {
|
|
|
|
if p.ID == id {
|
|
|
|
projects = append(projects, p)
|
2023-12-07 17:08:56 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-14 15:33:15 +00:00
|
|
|
return projects
|
2023-12-07 17:08:56 +00:00
|
|
|
}
|