change user configuration of gitlab to user name

This should be easier to configure than having to hunt down the user id
This commit is contained in:
Massaki Archambault 2024-08-14 00:01:47 -04:00
parent 68cbf311b8
commit 34f3b904e9
5 changed files with 29 additions and 15 deletions

View File

@ -28,8 +28,8 @@ gitlab:
group_ids:
- 9970 # gitlab-org
# A list of the user ids to expose their personal projects in the filesystem.
user_ids: []
# A list of the name of the user to expose their repositories un the filesystem
user_names: []
# Set how archived projects are handled.
# 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
group_ids:
- 123
user_ids:
- 456
user_names:
- test-user
archived_project_handling: hide
include_current_user: true

View File

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

View File

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

View File

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