Compare commits

...

2 Commits

Author SHA1 Message Date
Massaki Archambault d7fda7c901
Merge c5df43b3e7 into 026089d786 2024-08-14 00:08:44 -04:00
Massaki Archambault c5df43b3e7 change user configuration of gitlab to user name
This should be easier to configure than having to hunt down the user id
2024-08-14 00:01:49 -04:00
5 changed files with 29 additions and 15 deletions

View File

@ -28,8 +28,8 @@ gitlab:
group_ids: group_ids:
- 9970 # gitlab-org - 9970 # gitlab-org
# A list of the user ids to expose their personal projects in the filesystem. # A list of the name of the user to expose their repositories un the filesystem
user_ids: [] user_names: []
# Set how archived projects are handled. # Set how archived projects are handled.
# If set to "show", it will add them to the filesystem and treat them like any other project # If set to "show", it will add them to the filesystem and treat them like any other project

View File

@ -9,8 +9,8 @@ gitlab:
pull_method: ssh pull_method: ssh
group_ids: group_ids:
- 123 - 123
user_ids: user_names:
- 456 - test-user
archived_project_handling: hide archived_project_handling: hide
include_current_user: true include_current_user: true

View File

@ -39,7 +39,7 @@ type (
Token string `yaml:"token,omitempty"` Token string `yaml:"token,omitempty"`
GroupIDs []int `yaml:"group_ids,omitempty"` GroupIDs []int `yaml:"group_ids,omitempty"`
UserIDs []int `yaml:"user_ids,omitempty"` UserNames []string `yaml:"user_ids,omitempty"`
ArchivedProjectHandling string `yaml:"archived_project_handling,omitempty"` ArchivedProjectHandling string `yaml:"archived_project_handling,omitempty"`
IncludeCurrentUser bool `yaml:"include_current_user,omitempty"` IncludeCurrentUser bool `yaml:"include_current_user,omitempty"`
@ -96,7 +96,7 @@ func LoadConfig(configPath string) (*Config, error) {
Token: "", Token: "",
PullMethod: "http", PullMethod: "http",
GroupIDs: []int{9970}, GroupIDs: []int{9970},
UserIDs: []int{}, UserNames: []string{},
ArchivedProjectHandling: "hide", ArchivedProjectHandling: "hide",
IncludeCurrentUser: true, IncludeCurrentUser: true,
}, },

View File

@ -25,7 +25,7 @@ func TestLoadConfig(t *testing.T) {
Token: "12345", Token: "12345",
PullMethod: "ssh", PullMethod: "ssh",
GroupIDs: []int{123}, GroupIDs: []int{123},
UserIDs: []int{456}, UserNames: []int{456},
ArchivedProjectHandling: "hide", ArchivedProjectHandling: "hide",
IncludeCurrentUser: true, IncludeCurrentUser: true,
}, },
@ -148,7 +148,7 @@ func TestMakeGitlabConfig(t *testing.T) {
PullMethod: "http", PullMethod: "http",
Token: "", Token: "",
GroupIDs: []int{9970}, GroupIDs: []int{9970},
UserIDs: []int{}, UserNames: []int{},
ArchivedProjectHandling: "hide", ArchivedProjectHandling: "hide",
IncludeCurrentUser: true, IncludeCurrentUser: true,
}, },
@ -158,7 +158,7 @@ func TestMakeGitlabConfig(t *testing.T) {
PullMethod: "http", PullMethod: "http",
Token: "", Token: "",
GroupIDs: []int{9970}, GroupIDs: []int{9970},
UserIDs: []int{}, UserNames: []int{},
ArchivedProjectHandling: "hide", ArchivedProjectHandling: "hide",
IncludeCurrentUser: true, IncludeCurrentUser: true,
}, },
@ -173,7 +173,7 @@ func TestMakeGitlabConfig(t *testing.T) {
PullMethod: "invalid", PullMethod: "invalid",
Token: "", Token: "",
GroupIDs: []int{9970}, GroupIDs: []int{9970},
UserIDs: []int{}, UserNames: []int{},
ArchivedProjectHandling: "hide", ArchivedProjectHandling: "hide",
IncludeCurrentUser: true, IncludeCurrentUser: true,
}, },
@ -187,7 +187,7 @@ func TestMakeGitlabConfig(t *testing.T) {
PullMethod: "http", PullMethod: "http",
Token: "", Token: "",
GroupIDs: []int{9970}, GroupIDs: []int{9970},
UserIDs: []int{}, UserNames: []int{},
IncludeCurrentUser: true, IncludeCurrentUser: true,
ArchivedProjectHandling: "invalid", ArchivedProjectHandling: "invalid",
}, },

View File

@ -19,6 +19,8 @@ type gitlabClient struct {
rootContent map[string]fstree.GroupSource rootContent map[string]fstree.GroupSource
userIDs []int
// API response cache // API response cache
groupCacheMux sync.RWMutex groupCacheMux sync.RWMutex
groupCache map[int]*Group groupCache map[int]*Group
@ -43,6 +45,8 @@ func NewClient(logger *slog.Logger, config config.GitlabClientConfig) (*gitlabCl
rootContent: nil, rootContent: nil,
userIDs: []int{},
groupCache: map[int]*Group{}, groupCache: map[int]*Group{},
userCache: map[int]*User{}, userCache: map[int]*User{},
} }
@ -52,7 +56,17 @@ func NewClient(logger *slog.Logger, config config.GitlabClientConfig) (*gitlabCl
if err != nil { if err != nil {
logger.Warn("failed to fetch the current user:", "error", err.Error()) logger.Warn("failed to fetch the current user:", "error", err.Error())
} else { } else {
gitlabClient.UserIDs = append(gitlabClient.UserIDs, currentUser.ID) 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)
}
} }
return gitlabClient, nil return gitlabClient, nil
@ -72,7 +86,7 @@ func (c *gitlabClient) FetchRootGroupContent() (map[string]fstree.GroupSource, e
rootGroupCache[group.Name] = group rootGroupCache[group.Name] = group
} }
// fetch users // fetch users
for _, uid := range c.UserIDs { for _, uid := range c.userIDs {
user, err := c.fetchUser(uid) user, err := c.fetchUser(uid)
if err != nil { if err != nil {
return nil, err return nil, err
@ -86,7 +100,7 @@ func (c *gitlabClient) FetchRootGroupContent() (map[string]fstree.GroupSource, e
} }
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)) { 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 {