forked from profclems/glab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.go
59 lines (47 loc) · 1.14 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package api
import (
"fmt"
"github.com/xanzy/go-gitlab"
)
var CurrentUser = func(client *gitlab.Client) (*gitlab.User, error) {
if client == nil {
client = apiClient.Lab()
}
u, _, err := client.Users.CurrentUser()
if err != nil {
return nil, err
}
return u, nil
}
var UserByName = func(client *gitlab.Client, name string) (*gitlab.User, error) {
opts := &gitlab.ListUsersOptions{Username: gitlab.String(name)}
if client == nil {
client = apiClient.Lab()
}
if opts.PerPage == 0 {
opts.PerPage = DefaultListLimit
}
// Handle special case of '@me' which maps to the currently authenticated user
if name == "@me" {
return CurrentUser(client)
}
users, _, err := client.Users.ListUsers(opts)
if err != nil {
return nil, err
}
if len(users) != 1 {
return nil, fmt.Errorf("failed to find user by name : %s", name)
}
return users[0], nil
}
var UsersByNames = func(client *gitlab.Client, names []string) ([]*gitlab.User, error) {
users := make([]*gitlab.User, 0)
for _, name := range names {
user, err := UserByName(client, name)
if err != nil {
return nil, err
}
users = append(users, user)
}
return users, nil
}