2020-12-27 03:30:19 +00:00
|
|
|
package gitlab
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-06-06 05:51:34 +00:00
|
|
|
"log/slog"
|
2024-05-05 20:09:03 +00:00
|
|
|
"slices"
|
2024-07-18 03:35:18 +00:00
|
|
|
"sync"
|
2020-12-27 03:30:19 +00:00
|
|
|
|
2024-08-14 02:55:18 +00:00
|
|
|
"github.com/badjware/gitforgefs/config"
|
|
|
|
"github.com/badjware/gitforgefs/fstree"
|
2020-12-27 03:30:19 +00:00
|
|
|
"github.com/xanzy/go-gitlab"
|
|
|
|
)
|
|
|
|
|
2020-12-29 02:37:18 +00:00
|
|
|
type gitlabClient struct {
|
2024-08-04 19:44:50 +00:00
|
|
|
config.GitlabClientConfig
|
2020-12-29 02:37:18 +00:00
|
|
|
client *gitlab.Client
|
2024-05-05 20:09:03 +00:00
|
|
|
|
2024-06-06 05:51:34 +00:00
|
|
|
logger *slog.Logger
|
|
|
|
|
2024-08-05 01:39:15 +00:00
|
|
|
rootContent map[string]fstree.GroupSource
|
2024-05-05 20:09:03 +00:00
|
|
|
|
2024-08-14 04:01:47 +00:00
|
|
|
userIDs []int
|
|
|
|
|
2024-05-05 20:09:03 +00:00
|
|
|
// API response cache
|
2024-07-18 03:35:18 +00:00
|
|
|
groupCacheMux sync.RWMutex
|
|
|
|
groupCache map[int]*Group
|
|
|
|
userCacheMux sync.RWMutex
|
|
|
|
userCache map[int]*User
|
2020-12-29 02:37:18 +00:00
|
|
|
}
|
|
|
|
|
2024-08-04 22:59:57 +00:00
|
|
|
func NewClient(logger *slog.Logger, config config.GitlabClientConfig) (*gitlabClient, error) {
|
2020-12-27 03:30:19 +00:00
|
|
|
client, err := gitlab.NewClient(
|
2024-08-04 22:59:57 +00:00
|
|
|
config.Token,
|
|
|
|
gitlab.WithBaseURL(config.URL),
|
2020-12-27 03:30:19 +00:00
|
|
|
)
|
|
|
|
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{
|
2024-08-04 22:59:57 +00:00
|
|
|
GitlabClientConfig: config,
|
2024-05-05 20:09:03 +00:00
|
|
|
client: client,
|
|
|
|
|
2024-06-06 05:51:34 +00:00
|
|
|
logger: logger,
|
|
|
|
|
2024-08-05 01:39:15 +00:00
|
|
|
rootContent: nil,
|
2024-05-05 20:09:03 +00:00
|
|
|
|
2024-08-14 04:01:47 +00:00
|
|
|
userIDs: []int{},
|
|
|
|
|
2024-05-05 20:09:03 +00:00
|
|
|
groupCache: map[int]*Group{},
|
|
|
|
userCache: map[int]*User{},
|
2020-12-27 03:30:19 +00:00
|
|
|
}
|
2024-08-05 01:39:15 +00:00
|
|
|
|
|
|
|
// Fetch current user and add it to the list
|
|
|
|
currentUser, _, err := client.Users.CurrentUser()
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn("failed to fetch the current user:", "error", err.Error())
|
|
|
|
} else {
|
2024-08-14 04:01:47 +00:00
|
|
|
gitlabClient.userIDs = append(gitlabClient.userIDs, currentUser.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the configured users and add them to the list
|
|
|
|
for _, userName := range config.UserNames {
|
|
|
|
user, _, err := client.Users.ListUsers(&gitlab.ListUsersOptions{Username: &userName})
|
|
|
|
if err != nil || len(user) != 1 {
|
|
|
|
logger.Warn("failed to fetch the user", "userName", userName, "error", err.Error())
|
|
|
|
} else {
|
|
|
|
gitlabClient.userIDs = append(gitlabClient.userIDs, user[0].ID)
|
|
|
|
}
|
2024-08-05 01:39:15 +00:00
|
|
|
}
|
|
|
|
|
2020-12-27 03:30:19 +00:00
|
|
|
return gitlabClient, nil
|
|
|
|
}
|
2024-05-05 20:09:03 +00:00
|
|
|
|
2024-05-05 20:23:07 +00:00
|
|
|
func (c *gitlabClient) FetchRootGroupContent() (map[string]fstree.GroupSource, error) {
|
2024-05-05 20:09:03 +00:00
|
|
|
// use cached values if available
|
2024-08-05 01:39:15 +00:00
|
|
|
if c.rootContent == nil {
|
2024-05-05 20:23:07 +00:00
|
|
|
rootGroupCache := make(map[string]fstree.GroupSource)
|
2024-05-05 20:09:03 +00:00
|
|
|
|
|
|
|
// 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
|
2024-08-14 04:01:47 +00:00
|
|
|
for _, uid := range c.userIDs {
|
2024-05-05 20:09:03 +00:00
|
|
|
user, err := c.fetchUser(uid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rootGroupCache[user.Name] = user
|
|
|
|
}
|
|
|
|
|
2024-08-05 01:39:15 +00:00
|
|
|
c.rootContent = rootGroupCache
|
2024-05-05 20:09:03 +00:00
|
|
|
}
|
2024-08-05 01:39:15 +00:00
|
|
|
return c.rootContent, nil
|
2024-05-05 20:09:03 +00:00
|
|
|
}
|
|
|
|
|
2024-05-05 20:23:07 +00:00
|
|
|
func (c *gitlabClient) FetchGroupContent(gid uint64) (map[string]fstree.GroupSource, map[string]fstree.RepositorySource, error) {
|
2024-08-14 04:01:47 +00:00
|
|
|
if slices.Contains[[]int, int](c.userIDs, int(gid)) {
|
2024-05-05 20:09:03 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|