refactor gitlab current user
This commit is contained in:
parent
382a0f6d8d
commit
0e5fed0bbd
|
@ -17,9 +17,7 @@ type gitlabClient struct {
|
||||||
|
|
||||||
logger *slog.Logger
|
logger *slog.Logger
|
||||||
|
|
||||||
// root group cache
|
rootContent map[string]fstree.GroupSource
|
||||||
rootGroupCache map[string]fstree.GroupSource
|
|
||||||
currentUserCache *User
|
|
||||||
|
|
||||||
// API response cache
|
// API response cache
|
||||||
groupCacheMux sync.RWMutex
|
groupCacheMux sync.RWMutex
|
||||||
|
@ -43,18 +41,26 @@ func NewClient(logger *slog.Logger, config config.GitlabClientConfig) (*gitlabCl
|
||||||
|
|
||||||
logger: logger,
|
logger: logger,
|
||||||
|
|
||||||
rootGroupCache: nil,
|
rootContent: nil,
|
||||||
currentUserCache: nil,
|
|
||||||
|
|
||||||
groupCache: map[int]*Group{},
|
groupCache: map[int]*Group{},
|
||||||
userCache: map[int]*User{},
|
userCache: map[int]*User{},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 {
|
||||||
|
gitlabClient.UserIDs = append(gitlabClient.UserIDs, currentUser.ID)
|
||||||
|
}
|
||||||
|
|
||||||
return gitlabClient, nil
|
return gitlabClient, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *gitlabClient) FetchRootGroupContent() (map[string]fstree.GroupSource, error) {
|
func (c *gitlabClient) FetchRootGroupContent() (map[string]fstree.GroupSource, error) {
|
||||||
// use cached values if available
|
// use cached values if available
|
||||||
if c.rootGroupCache == nil {
|
if c.rootContent == nil {
|
||||||
rootGroupCache := make(map[string]fstree.GroupSource)
|
rootGroupCache := make(map[string]fstree.GroupSource)
|
||||||
|
|
||||||
// fetch root groups
|
// fetch root groups
|
||||||
|
@ -73,23 +79,14 @@ func (c *gitlabClient) FetchRootGroupContent() (map[string]fstree.GroupSource, e
|
||||||
}
|
}
|
||||||
rootGroupCache[user.Name] = user
|
rootGroupCache[user.Name] = user
|
||||||
}
|
}
|
||||||
// fetch current user if configured
|
|
||||||
if c.IncludeCurrentUser {
|
|
||||||
currentUser, err := c.fetchCurrentUser()
|
|
||||||
if err != nil {
|
|
||||||
c.logger.Warn(err.Error())
|
|
||||||
} else {
|
|
||||||
rootGroupCache[currentUser.Name] = currentUser
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
c.rootGroupCache = rootGroupCache
|
c.rootContent = rootGroupCache
|
||||||
}
|
}
|
||||||
return c.rootGroupCache, nil
|
return c.rootContent, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *gitlabClient) FetchGroupContent(gid uint64) (map[string]fstree.GroupSource, map[string]fstree.RepositorySource, error) {
|
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)) {
|
if slices.Contains[[]int, int](c.UserIDs, int(gid)) {
|
||||||
// gid is a user
|
// gid is a user
|
||||||
user, err := c.fetchUser(int(gid))
|
user, err := c.fetchUser(int(gid))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -64,23 +64,6 @@ func (c *gitlabClient) fetchUser(uid int) (*User, error) {
|
||||||
return &newUser, nil
|
return &newUser, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *gitlabClient) fetchCurrentUser() (*User, error) {
|
|
||||||
if c.currentUserCache == nil {
|
|
||||||
gitlabUser, _, err := c.client.Users.CurrentUser()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to fetch current user: %v", err)
|
|
||||||
}
|
|
||||||
newUser := User{
|
|
||||||
ID: gitlabUser.ID,
|
|
||||||
Name: gitlabUser.Username,
|
|
||||||
|
|
||||||
childProjects: nil,
|
|
||||||
}
|
|
||||||
c.currentUserCache = &newUser
|
|
||||||
}
|
|
||||||
return c.currentUserCache, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *gitlabClient) fetchUserContent(user *User) (map[string]fstree.GroupSource, map[string]fstree.RepositorySource, error) {
|
func (c *gitlabClient) fetchUserContent(user *User) (map[string]fstree.GroupSource, map[string]fstree.RepositorySource, error) {
|
||||||
// Only a single routine can fetch the user content at the time.
|
// Only a single routine can fetch the user content at the time.
|
||||||
// We lock for the whole duration of the function to avoid fetching the same data from the API
|
// We lock for the whole duration of the function to avoid fetching the same data from the API
|
||||||
|
|
Loading…
Reference in New Issue