gitforgefs/platforms/gitlab/client.go

118 lines
2.7 KiB
Go
Raw Normal View History

2020-12-27 03:30:19 +00:00
package gitlab
import (
"fmt"
2024-06-06 05:51:34 +00:00
"log/slog"
"slices"
2020-12-27 03:30:19 +00:00
"github.com/badjware/gitlabfs/fstree"
2020-12-27 03:30:19 +00:00
"github.com/xanzy/go-gitlab"
)
2020-12-30 23:00:37 +00:00
const (
PullMethodHTTP = "http"
PullMethodSSH = "ssh"
)
type GitlabClientConfig struct {
URL string `yaml:"url,omitempty"`
Token string `yaml:"token,omitempty"`
GroupIDs []int `yaml:"group_ids,omitempty"`
UserIDs []int `yaml:"user_ids,omitempty"`
IncludeCurrentUser bool `yaml:"include_current_user,omitempty"`
PullMethod string `yaml:"pull_method,omitempty"`
2020-12-27 03:30:19 +00:00
}
2020-12-29 02:37:18 +00:00
type gitlabClient struct {
GitlabClientConfig
2020-12-29 02:37:18 +00:00
client *gitlab.Client
2024-06-06 05:51:34 +00:00
logger *slog.Logger
// root group cache
rootGroupCache map[string]fstree.GroupSource
currentUserCache *User
// API response cache
groupCache map[int]*Group
userCache map[int]*User
2020-12-29 02:37:18 +00:00
}
2024-06-06 05:51:34 +00:00
func NewClient(logger *slog.Logger, gitlabUrl string, gitlabToken string, p GitlabClientConfig) (*gitlabClient, error) {
2020-12-27 03:30:19 +00:00
client, err := gitlab.NewClient(
gitlabToken,
gitlab.WithBaseURL(gitlabUrl),
)
if err != nil {
2020-12-29 02:37:18 +00:00
return nil, fmt.Errorf("failed to create gitlab client: %v", err)
2020-12-27 03:30:19 +00:00
}
2020-12-29 02:37:18 +00:00
gitlabClient := &gitlabClient{
GitlabClientConfig: p,
client: client,
2024-06-06 05:51:34 +00:00
logger: logger,
rootGroupCache: nil,
currentUserCache: nil,
groupCache: map[int]*Group{},
userCache: map[int]*User{},
2020-12-27 03:30:19 +00:00
}
return gitlabClient, nil
}
func (c *gitlabClient) FetchRootGroupContent() (map[string]fstree.GroupSource, error) {
// use cached values if available
if c.rootGroupCache == nil {
rootGroupCache := make(map[string]fstree.GroupSource)
// fetch root groups
for _, gid := range c.GroupIDs {
group, err := c.fetchGroup(gid)
if err != nil {
return nil, err
}
rootGroupCache[group.Name] = group
}
// fetch users
for _, uid := range c.UserIDs {
user, err := c.fetchUser(uid)
if err != nil {
return nil, err
}
rootGroupCache[user.Name] = user
}
// fetch current user if configured
if c.IncludeCurrentUser {
currentUser, err := c.fetchCurrentUser()
if err != nil {
2024-06-06 05:51:34 +00:00
c.logger.Warn(err.Error())
} else {
rootGroupCache[currentUser.Name] = currentUser
}
}
c.rootGroupCache = rootGroupCache
}
return c.rootGroupCache, nil
}
func (c *gitlabClient) FetchGroupContent(gid uint64) (map[string]fstree.GroupSource, map[string]fstree.RepositorySource, error) {
if slices.Contains[[]int, int](c.UserIDs, int(gid)) || (c.currentUserCache != nil && c.currentUserCache.ID == int(gid)) {
// gid is a user
user, err := c.fetchUser(int(gid))
if err != nil {
return nil, nil, err
}
return c.fetchUserContent(user)
} else {
// gid is a group
group, err := c.fetchGroup(int(gid))
if err != nil {
return nil, nil, err
}
return c.fetchGroupContent(group)
}
}