Skip to content

Commit

Permalink
check root namespace (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
noamd-legit committed Jan 14, 2024
1 parent 4bbb761 commit 4144a5f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 20 deletions.
18 changes: 10 additions & 8 deletions internal/clients/gitlab/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion internal/collectors/gitlab/group_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 2 additions & 10 deletions internal/collectors/gitlab/repository_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand Down Expand Up @@ -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))
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/collectors/gitlab/user_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
}
Expand Down

0 comments on commit 4144a5f

Please sign in to comment.