Skip to content

Commit 34f3b90

Browse files
committed
change user configuration of gitlab to user name
This should be easier to configure than having to hunt down the user id
1 parent 68cbf31 commit 34f3b90

File tree

5 files changed

+29
-15
lines changed

5 files changed

+29
-15
lines changed

config.example.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ gitlab:
2828
group_ids:
2929
- 9970 # gitlab-org
3030

31-
# A list of the user ids to expose their personal projects in the filesystem.
32-
user_ids: []
31+
# A list of the name of the user to expose their repositories un the filesystem
32+
user_names: []
3333

3434
# Set how archived projects are handled.
3535
# If set to "show", it will add them to the filesystem and treat them like any other project

config/config.test.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ gitlab:
99
pull_method: ssh
1010
group_ids:
1111
- 123
12-
user_ids:
13-
- 456
12+
user_names:
13+
- test-user
1414
archived_project_handling: hide
1515
include_current_user: true
1616

config/loader.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ type (
3838
URL string `yaml:"url,omitempty"`
3939
Token string `yaml:"token,omitempty"`
4040

41-
GroupIDs []int `yaml:"group_ids,omitempty"`
42-
UserIDs []int `yaml:"user_ids,omitempty"`
41+
GroupIDs []int `yaml:"group_ids,omitempty"`
42+
UserNames []string `yaml:"user_names,omitempty"`
4343

4444
ArchivedProjectHandling string `yaml:"archived_project_handling,omitempty"`
4545
IncludeCurrentUser bool `yaml:"include_current_user,omitempty"`
@@ -96,7 +96,7 @@ func LoadConfig(configPath string) (*Config, error) {
9696
Token: "",
9797
PullMethod: "http",
9898
GroupIDs: []int{9970},
99-
UserIDs: []int{},
99+
UserNames: []string{},
100100
ArchivedProjectHandling: "hide",
101101
IncludeCurrentUser: true,
102102
},

config/loader_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestLoadConfig(t *testing.T) {
2525
Token: "12345",
2626
PullMethod: "ssh",
2727
GroupIDs: []int{123},
28-
UserIDs: []int{456},
28+
UserNames: []int{456},
2929
ArchivedProjectHandling: "hide",
3030
IncludeCurrentUser: true,
3131
},
@@ -148,7 +148,7 @@ func TestMakeGitlabConfig(t *testing.T) {
148148
PullMethod: "http",
149149
Token: "",
150150
GroupIDs: []int{9970},
151-
UserIDs: []int{},
151+
UserNames: []int{},
152152
ArchivedProjectHandling: "hide",
153153
IncludeCurrentUser: true,
154154
},
@@ -158,7 +158,7 @@ func TestMakeGitlabConfig(t *testing.T) {
158158
PullMethod: "http",
159159
Token: "",
160160
GroupIDs: []int{9970},
161-
UserIDs: []int{},
161+
UserNames: []int{},
162162
ArchivedProjectHandling: "hide",
163163
IncludeCurrentUser: true,
164164
},
@@ -173,7 +173,7 @@ func TestMakeGitlabConfig(t *testing.T) {
173173
PullMethod: "invalid",
174174
Token: "",
175175
GroupIDs: []int{9970},
176-
UserIDs: []int{},
176+
UserNames: []int{},
177177
ArchivedProjectHandling: "hide",
178178
IncludeCurrentUser: true,
179179
},
@@ -187,7 +187,7 @@ func TestMakeGitlabConfig(t *testing.T) {
187187
PullMethod: "http",
188188
Token: "",
189189
GroupIDs: []int{9970},
190-
UserIDs: []int{},
190+
UserNames: []int{},
191191
IncludeCurrentUser: true,
192192
ArchivedProjectHandling: "invalid",
193193
},

forges/gitlab/client.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ type gitlabClient struct {
1919

2020
rootContent map[string]fstree.GroupSource
2121

22+
userIDs []int
23+
2224
// API response cache
2325
groupCacheMux sync.RWMutex
2426
groupCache map[int]*Group
@@ -43,6 +45,8 @@ func NewClient(logger *slog.Logger, config config.GitlabClientConfig) (*gitlabCl
4345

4446
rootContent: nil,
4547

48+
userIDs: []int{},
49+
4650
groupCache: map[int]*Group{},
4751
userCache: map[int]*User{},
4852
}
@@ -52,7 +56,17 @@ func NewClient(logger *slog.Logger, config config.GitlabClientConfig) (*gitlabCl
5256
if err != nil {
5357
logger.Warn("failed to fetch the current user:", "error", err.Error())
5458
} else {
55-
gitlabClient.UserIDs = append(gitlabClient.UserIDs, currentUser.ID)
59+
gitlabClient.userIDs = append(gitlabClient.userIDs, currentUser.ID)
60+
}
61+
62+
// Fetch the configured users and add them to the list
63+
for _, userName := range config.UserNames {
64+
user, _, err := client.Users.ListUsers(&gitlab.ListUsersOptions{Username: &userName})
65+
if err != nil || len(user) != 1 {
66+
logger.Warn("failed to fetch the user", "userName", userName, "error", err.Error())
67+
} else {
68+
gitlabClient.userIDs = append(gitlabClient.userIDs, user[0].ID)
69+
}
5670
}
5771

5872
return gitlabClient, nil
@@ -72,7 +86,7 @@ func (c *gitlabClient) FetchRootGroupContent() (map[string]fstree.GroupSource, e
7286
rootGroupCache[group.Name] = group
7387
}
7488
// fetch users
75-
for _, uid := range c.UserIDs {
89+
for _, uid := range c.userIDs {
7690
user, err := c.fetchUser(uid)
7791
if err != nil {
7892
return nil, err
@@ -86,7 +100,7 @@ func (c *gitlabClient) FetchRootGroupContent() (map[string]fstree.GroupSource, e
86100
}
87101

88102
func (c *gitlabClient) FetchGroupContent(gid uint64) (map[string]fstree.GroupSource, map[string]fstree.RepositorySource, error) {
89-
if slices.Contains[[]int, int](c.UserIDs, int(gid)) {
103+
if slices.Contains[[]int, int](c.userIDs, int(gid)) {
90104
// gid is a user
91105
user, err := c.fetchUser(int(gid))
92106
if err != nil {

0 commit comments

Comments
 (0)