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
|
|
|
|
2024-05-05 20:23:07 +00:00
|
|
|
"github.com/badjware/gitlabfs/fstree"
|
2020-12-30 00:49:11 +00:00
|
|
|
"github.com/xanzy/go-gitlab"
|
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
ID int
|
|
|
|
Name string
|
|
|
|
|
2024-05-05 20:09:03 +00:00
|
|
|
mux sync.Mutex
|
|
|
|
|
|
|
|
// user content cache
|
2024-05-05 20:23:07 +00:00
|
|
|
projectCache map[string]fstree.RepositorySource
|
2020-12-30 00:49:11 +00:00
|
|
|
}
|
|
|
|
|
2024-05-05 20:09:03 +00:00
|
|
|
func (u *User) GetGroupID() uint64 {
|
|
|
|
return uint64(u.ID)
|
2020-12-30 00:49:11 +00:00
|
|
|
}
|
|
|
|
|
2020-12-31 20:00:10 +00:00
|
|
|
func (u *User) InvalidateCache() {
|
|
|
|
u.mux.Lock()
|
|
|
|
defer u.mux.Unlock()
|
|
|
|
|
2024-05-05 20:09:03 +00:00
|
|
|
u.projectCache = nil
|
2020-12-31 20:00:10 +00:00
|
|
|
}
|
|
|
|
|
2024-05-05 20:09:03 +00:00
|
|
|
func (c *gitlabClient) fetchUser(uid int) (*User, error) {
|
|
|
|
// start by searching the cache
|
|
|
|
// TODO: cache invalidation?
|
|
|
|
user, found := c.userCache[uid]
|
|
|
|
if found {
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not in cache, fetch group infos from API
|
2020-12-30 00:49:11 +00:00
|
|
|
gitlabUser, _, err := c.client.Users.GetUser(uid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to fetch user with id %v: %v", uid, err)
|
|
|
|
}
|
2024-05-05 20:09:03 +00:00
|
|
|
newUser := User{
|
|
|
|
ID: gitlabUser.ID,
|
|
|
|
Name: gitlabUser.Username,
|
|
|
|
|
|
|
|
projectCache: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
// save in cache
|
|
|
|
c.userCache[uid] = &newUser
|
|
|
|
|
|
|
|
return &newUser, nil
|
2020-12-30 00:49:11 +00:00
|
|
|
}
|
|
|
|
|
2024-05-05 20:09:03 +00:00
|
|
|
func (c *gitlabClient) fetchCurrentUser() (*User, error) {
|
|
|
|
if c.currentUserCache == nil {
|
2021-03-24 02:18:48 +00:00
|
|
|
gitlabUser, _, err := c.client.Users.CurrentUser()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to fetch current user: %v", err)
|
|
|
|
}
|
2024-05-05 20:09:03 +00:00
|
|
|
newUser := User{
|
|
|
|
ID: gitlabUser.ID,
|
|
|
|
Name: gitlabUser.Username,
|
|
|
|
|
|
|
|
projectCache: nil,
|
|
|
|
}
|
|
|
|
c.currentUserCache = &newUser
|
2020-12-30 00:49:11 +00:00
|
|
|
}
|
2024-05-05 20:09:03 +00:00
|
|
|
return c.currentUserCache, nil
|
2020-12-30 00:49:11 +00:00
|
|
|
}
|
|
|
|
|
2024-05-05 20:23:07 +00:00
|
|
|
func (c *gitlabClient) fetchUserContent(user *User) (map[string]fstree.GroupSource, map[string]fstree.RepositorySource, error) {
|
2020-12-31 20:00:10 +00:00
|
|
|
user.mux.Lock()
|
|
|
|
defer user.mux.Unlock()
|
|
|
|
|
|
|
|
// Get cached data if available
|
2024-05-05 20:09:03 +00:00
|
|
|
// TODO: cache cache invalidation?
|
|
|
|
if user.projectCache == nil {
|
2024-05-05 20:23:07 +00:00
|
|
|
projectCache := make(map[string]fstree.RepositorySource)
|
2024-05-05 20:09:03 +00:00
|
|
|
|
|
|
|
// Fetch the user repositories
|
|
|
|
listProjectOpt := &gitlab.ListProjectsOptions{
|
|
|
|
ListOptions: gitlab.ListOptions{
|
|
|
|
Page: 1,
|
|
|
|
PerPage: 100,
|
|
|
|
}}
|
|
|
|
for {
|
|
|
|
gitlabProjects, response, err := c.client.Projects.ListUserProjects(user.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
|
|
|
}
|
|
|
|
|
2024-05-05 20:09:03 +00:00
|
|
|
user.projectCache = projectCache
|
|
|
|
}
|
2024-05-05 20:23:07 +00:00
|
|
|
return make(map[string]fstree.GroupSource), user.projectCache, nil
|
2020-12-30 00:49:11 +00:00
|
|
|
}
|