From 34f3b904e90899ebafa05ad473eb622bfefbad98 Mon Sep 17 00:00:00 2001 From: Massaki Archambault Date: Wed, 14 Aug 2024 00:01:47 -0400 Subject: [PATCH] change user configuration of gitlab to user name This should be easier to configure than having to hunt down the user id --- config.example.yaml | 4 ++-- config/config.test.yaml | 4 ++-- config/loader.go | 6 +++--- config/loader_test.go | 10 +++++----- forges/gitlab/client.go | 20 +++++++++++++++++--- 5 files changed, 29 insertions(+), 15 deletions(-) diff --git a/config.example.yaml b/config.example.yaml index 1719ec9..9d4568b 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -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 diff --git a/config/config.test.yaml b/config/config.test.yaml index d74bb68..a4329c9 100644 --- a/config/config.test.yaml +++ b/config/config.test.yaml @@ -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 diff --git a/config/loader.go b/config/loader.go index 4070aa2..48043d8 100644 --- a/config/loader.go +++ b/config/loader.go @@ -38,8 +38,8 @@ type ( URL string `yaml:"url,omitempty"` Token string `yaml:"token,omitempty"` - GroupIDs []int `yaml:"group_ids,omitempty"` - UserIDs []int `yaml:"user_ids,omitempty"` + GroupIDs []int `yaml:"group_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, }, diff --git a/config/loader_test.go b/config/loader_test.go index cee2618..4a33457 100644 --- a/config/loader_test.go +++ b/config/loader_test.go @@ -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", }, diff --git a/forges/gitlab/client.go b/forges/gitlab/client.go index 70b06ab..fdc3963 100644 --- a/forges/gitlab/client.go +++ b/forges/gitlab/client.go @@ -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 {