gitforgefs/gitlab/group.go

122 lines
2.8 KiB
Go
Raw Normal View History

2020-12-30 00:49:11 +00:00
package gitlab
import (
"fmt"
2020-12-31 20:00:10 +00:00
"sync"
2020-12-30 00:49:11 +00:00
"github.com/badjware/gitlabfs/fs"
2020-12-30 00:49:11 +00:00
"github.com/xanzy/go-gitlab"
)
type Group struct {
ID int
Name string
mux sync.Mutex
// group content cache
groupCache map[string]fs.GroupSource
projectCache map[string]fs.RepositorySource
2020-12-30 00:49:11 +00:00
}
func (g *Group) GetGroupID() uint64 {
return uint64(g.ID)
2020-12-30 00:49:11 +00:00
}
2020-12-31 20:00:10 +00:00
func (g *Group) InvalidateCache() {
g.mux.Lock()
defer g.mux.Unlock()
g.groupCache = nil
g.projectCache = nil
2020-12-31 20:00:10 +00:00
}
func (c *gitlabClient) fetchGroup(gid int) (*Group, error) {
// start by searching the cache
// TODO: cache invalidation?
group, found := c.groupCache[gid]
if found {
return group, nil
}
// If not in cache, fetch group infos from API
2020-12-30 00:49:11 +00:00
gitlabGroup, _, err := c.client.Groups.GetGroup(gid)
if err != nil {
return nil, fmt.Errorf("failed to fetch group with id %v: %v", gid, err)
}
newGroup := Group{
ID: gitlabGroup.ID,
Name: gitlabGroup.Path,
groupCache: nil,
projectCache: nil,
}
// save in cache
c.groupCache[gid] = &newGroup
return &newGroup, nil
2020-12-30 00:49:11 +00:00
}
func (c *gitlabClient) fetchGroupContent(group *Group) (map[string]fs.GroupSource, map[string]fs.RepositorySource, error) {
2020-12-31 20:00:10 +00:00
group.mux.Lock()
defer group.mux.Unlock()
// Get cached data if available
// TODO: cache cache invalidation?
if group.groupCache == nil || group.projectCache == nil {
groupCache := make(map[string]fs.GroupSource)
projectCache := make(map[string]fs.RepositorySource)
// List subgroups in path
ListGroupsOpt := &gitlab.ListSubgroupsOptions{
ListOptions: gitlab.ListOptions{
Page: 1,
PerPage: 100,
},
AllAvailable: gitlab.Bool(true),
2020-12-30 00:49:11 +00:00
}
for {
gitlabGroups, response, err := c.client.Groups.ListSubgroups(group.ID, ListGroupsOpt)
if err != nil {
return nil, nil, fmt.Errorf("failed to fetch groups in gitlab: %v", err)
}
for _, gitlabGroup := range gitlabGroups {
group, _ := c.fetchGroup(gitlabGroup.ID)
groupCache[group.Name] = group
}
if response.CurrentPage >= response.TotalPages {
break
}
// Get the next page
ListGroupsOpt.Page = response.NextPage
2020-12-30 00:49:11 +00:00
}
// List projects in path
listProjectOpt := &gitlab.ListGroupProjectsOptions{
ListOptions: gitlab.ListOptions{
Page: 1,
PerPage: 100,
}}
for {
gitlabProjects, response, err := c.client.Groups.ListGroupProjects(group.ID, listProjectOpt)
if err != nil {
return nil, nil, fmt.Errorf("failed to fetch projects in gitlab: %v", err)
}
for _, gitlabProject := range gitlabProjects {
project := c.newProjectFromGitlabProject(gitlabProject)
projectCache[project.Name] = &project
}
if response.CurrentPage >= response.TotalPages {
break
}
// Get the next page
listProjectOpt.Page = response.NextPage
2020-12-30 00:49:11 +00:00
}
group.groupCache = groupCache
group.projectCache = projectCache
}
return group.groupCache, group.projectCache, nil
2020-12-30 00:49:11 +00:00
}