forked from plouc/go-gitlab-client
/
users.go
131 lines (102 loc) · 4.63 KB
/
users.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package gitlab
import (
"encoding/json"
"io"
)
const (
UsersApiPath = "/users"
UserApiPath = "/users/:id"
CurrentUserApiPath = "/user"
)
type UserIdentity struct {
Provider string `json:"provider,omitempty" yaml:"provider,omitempty"`
ExternUid string `json:"extern_uid,omitempty" yaml:"extern_uid,omitempty"`
}
type User struct {
Id int `json:"id,omitempty" yaml:"id,omitempty"`
Username string `json:"username,omitempty" yaml:"username,omitempty"`
Email string `json:"email,omitempty" yaml:"email,omitempty"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
State string `json:"state,omitempty" yaml:"state,omitempty"`
AvatarUrl string `json:"avatar_url,omitempty" yaml:"avatar_url,omitempty"`
WebUrl string `json:"web_url" yaml:"web_url"`
CreatedAt string `json:"created_at,omitempty" yaml:"created_at,omitempty"`
IsAdmin bool `json:"is_admin,omitempty" yaml:"is_admin,omitempty"`
Bio string `json:"bio,omitempty" yaml:"bio,omitempty"`
Location string `json:"location,omitempty" yaml:"location,omitempty"`
Skype string `json:"skype,omitempty" yaml:"skype,omitempty"`
LinkedIn string `json:"linkedin,omitempty" yaml:"linkedin,omitempty"`
Twitter string `json:"twitter,omitempty" yaml:"twitter,omitempty"`
WebsiteUrl string `json:"website_url" yaml:"website_url"`
Organization string `json:"organization" yaml:"organization"`
LastSignInAt string `json:"last_sign_in_at,omitempty" yaml:"last_sign_in_at,omitempty"`
ConfirmedAt string `json:"confirmed_at,omitempty" yaml:"confirmed_at,omitempty"`
ThemeId int `json:"theme_id,omitempty" yaml:"theme_id,omitempty"`
LastActivityOn string `json:"last_activity_on,omitempty" yaml:"last_activity_on,omitempty"`
ColorSchemeId int `json:"color_scheme_id,omitempty" yaml:"color_scheme_id,omitempty"`
ProjectsLimit int `json:"projects_limit,omitempty" yaml:"projects_limit,omitempty"`
CurrentSignInAt string `json:"current_sign_in_at,omitempty" yaml:"current_sign_in_at,omitempty"`
Identities []*UserIdentity `json:"identities,omitempty" yaml:"identities,omitempty"`
CanCreateGroup bool `json:"can_create_group,omitempty" yaml:"can_create_group,omitempty"`
CanCreateProject bool `json:"can_create_project,omitempty" yaml:"can_create_project,omitempty"`
TwoFactorEnabled bool `json:"two_factor_enabled,omitempty" yaml:"two_factor_enabled,omitempty"`
External bool `json:"external,omitempty" yaml:"external,omitempty"`
}
type UserCollection struct {
Items []*User
}
type UsersOptions struct {
PaginationOptions
// Search users by email or username
Search string `url:"search,omitempty"`
// Search users by username
Username string `url:"username,omitempty"`
// Limit to active users
Active bool `url:"active,omitempty"`
// Limit to blocked users
Blocked bool `url:"blocked,omitempty"`
}
func (u *User) RenderJson(w io.Writer) error {
return renderJson(w, u)
}
func (u *User) RenderYaml(w io.Writer) error {
return renderYaml(w, u)
}
func (c *UserCollection) RenderJson(w io.Writer) error {
return renderJson(w, c.Items)
}
func (c *UserCollection) RenderYaml(w io.Writer) error {
return renderYaml(w, c.Items)
}
func (g *Gitlab) Users(o *UsersOptions) (*UserCollection, *ResponseMeta, error) {
u := g.ResourceUrlQ(UsersApiPath, nil, o)
collection := new(UserCollection)
contents, meta, err := g.buildAndExecRequest("GET", u.String(), nil)
if err == nil {
err = json.Unmarshal(contents, &collection.Items)
}
return collection, meta, err
}
func (g *Gitlab) User(id string) (*User, *ResponseMeta, error) {
u := g.ResourceUrl(UserApiPath, map[string]string{":id": id})
user := new(User)
contents, meta, err := g.buildAndExecRequest("GET", u.String(), nil)
if err == nil {
err = json.Unmarshal(contents, &user)
}
return user, meta, err
}
func (g *Gitlab) CurrentUser() (*User, *ResponseMeta, error) {
u := g.ResourceUrl(CurrentUserApiPath, nil)
user := new(User)
contents, meta, err := g.buildAndExecRequest("GET", u.String(), nil)
if err == nil {
err = json.Unmarshal(contents, &user)
}
return user, meta, err
}
func (g *Gitlab) RemoveUser(id string) (*ResponseMeta, error) {
u := g.ResourceUrl(UserApiPath, map[string]string{":id": id})
_, meta, err := g.buildAndExecRequest("DELETE", u.String(), nil)
return meta, err
}