From 4144a5f5ed7619dc91828a45e094e062fb5ab13a Mon Sep 17 00:00:00 2001 From: noamd-legit <74864790+noamd-legit@users.noreply.github.com> Date: Sun, 14 Jan 2024 16:58:05 +0200 Subject: [PATCH] check root namespace (#290) --- internal/clients/gitlab/client.go | 18 ++++++++++-------- internal/collectors/gitlab/group_collector.go | 2 +- .../collectors/gitlab/repository_collector.go | 12 ++---------- internal/collectors/gitlab/user_collector.go | 2 +- 4 files changed, 14 insertions(+), 20 deletions(-) diff --git a/internal/clients/gitlab/client.go b/internal/clients/gitlab/client.go index 4f54d5d2..b7c7085a 100644 --- a/internal/clients/gitlab/client.go +++ b/internal/clients/gitlab/client.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "log" + "strings" "github.com/Legit-Labs/legitify/internal/clients/gitlab/pagination" "github.com/Legit-Labs/legitify/internal/clients/gitlab/transport" @@ -192,25 +193,26 @@ func (c *Client) GroupHooks(gid int) ([]*gitlab.GroupHook, error) { return result.Collected, nil } -func (c *Client) GroupPlan(group *gitlab.Group) (string, error) { - nss, resp, err := c.Client().Namespaces.SearchNamespace(group.Path) +func (c *Client) GroupPlan(namespace string) (string, error) { + nss, resp, err := c.Client().Namespaces.SearchNamespace(namespace) if err != nil { - return "", fmt.Errorf("failed to search namespace %s: %v (response: %+v)", group.Path, err, resp) + return "", fmt.Errorf("failed to search namespace %s: %v (response: %+v)", namespace, err, resp) } for _, n := range nss { - if n.FullPath == group.FullPath { + if n.FullPath == namespace { return n.Plan, nil } } - return "", fmt.Errorf("didn't find namespace for %s", group.FullPath) + return "", fmt.Errorf("didn't find namespace for %s", namespace) } -func (c *Client) IsGroupPremium(group *gitlab.Group) bool { - plan, err := c.GroupPlan(group) +func (c *Client) IsGroupPremium(group string) bool { + groupRootNamespace := strings.Split(group, "/")[0] + plan, err := c.GroupPlan(groupRootNamespace) if err != nil { - log.Printf("failed to get namespace for group %s %v", group.FullPath, err) + log.Printf("failed to get namespace for group %s %v", group, err) return false } diff --git a/internal/collectors/gitlab/group_collector.go b/internal/collectors/gitlab/group_collector.go index 9919a302..ec86c258 100644 --- a/internal/collectors/gitlab/group_collector.go +++ b/internal/collectors/gitlab/group_collector.go @@ -58,7 +58,7 @@ func (c *groupCollector) Collect() collectors.SubCollectorChannels { return } - isPremium := c.Client.IsGroupPremium(g) + isPremium := c.Client.IsGroupPremium(g.FullPath) hooks, err := c.Client.GroupHooks(fullGroup.ID) diff --git a/internal/collectors/gitlab/repository_collector.go b/internal/collectors/gitlab/repository_collector.go index 742d6183..a8af5e7f 100644 --- a/internal/collectors/gitlab/repository_collector.go +++ b/internal/collectors/gitlab/repository_collector.go @@ -84,15 +84,7 @@ func (rc *repositoryCollector) collectSpecific(repositories []types.RepositoryWi log.Println(err.Error()) return } - - isPremium := false - group, err := rc.Client.Group(r.Owner) - if err != nil { - log.Println(err.Error()) - } else { - isPremium = rc.Client.IsGroupPremium(group) - } - + isPremium := rc.Client.IsGroupPremium(r.Owner) rc.extendedCollection(project, isPremium) }) @@ -207,7 +199,7 @@ func (rc *repositoryCollector) collectAll() collectors.SubCollectorChannels { for _, completedProject := range res.Collected { completedProject := completedProject gw.Do(func() { - rc.extendedCollection(completedProject, rc.Client.IsGroupPremium(g)) + rc.extendedCollection(completedProject, rc.Client.IsGroupPremium(g.FullPath)) }) } } diff --git a/internal/collectors/gitlab/user_collector.go b/internal/collectors/gitlab/user_collector.go index fcf0b277..23820924 100644 --- a/internal/collectors/gitlab/user_collector.go +++ b/internal/collectors/gitlab/user_collector.go @@ -97,7 +97,7 @@ func (c *userCollector) collectGroupUsers(group *gitlab2.Group) { } c.CollectDataWithContext(&entity, entity.CanonicalLink(), newCollectionContext(nil, []permissions.Role{permissions.GroupRoleOwner}, - c.Client.IsGroupPremium(group))) + c.Client.IsGroupPremium(group.FullPath))) c.CollectionChangeByOne() }) }