1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "fmt"
11 )
12
13
14
15
16
17 type UsersService service
18
19
20 type User struct {
21 Login *string `json:"login,omitempty"`
22 ID *int64 `json:"id,omitempty"`
23 NodeID *string `json:"node_id,omitempty"`
24 AvatarURL *string `json:"avatar_url,omitempty"`
25 HTMLURL *string `json:"html_url,omitempty"`
26 GravatarID *string `json:"gravatar_id,omitempty"`
27 Name *string `json:"name,omitempty"`
28 Company *string `json:"company,omitempty"`
29 Blog *string `json:"blog,omitempty"`
30 Location *string `json:"location,omitempty"`
31 Email *string `json:"email,omitempty"`
32 Hireable *bool `json:"hireable,omitempty"`
33 Bio *string `json:"bio,omitempty"`
34 TwitterUsername *string `json:"twitter_username,omitempty"`
35 PublicRepos *int `json:"public_repos,omitempty"`
36 PublicGists *int `json:"public_gists,omitempty"`
37 Followers *int `json:"followers,omitempty"`
38 Following *int `json:"following,omitempty"`
39 CreatedAt *Timestamp `json:"created_at,omitempty"`
40 UpdatedAt *Timestamp `json:"updated_at,omitempty"`
41 SuspendedAt *Timestamp `json:"suspended_at,omitempty"`
42 Type *string `json:"type,omitempty"`
43 SiteAdmin *bool `json:"site_admin,omitempty"`
44 TotalPrivateRepos *int `json:"total_private_repos,omitempty"`
45 OwnedPrivateRepos *int `json:"owned_private_repos,omitempty"`
46 PrivateGists *int `json:"private_gists,omitempty"`
47 DiskUsage *int `json:"disk_usage,omitempty"`
48 Collaborators *int `json:"collaborators,omitempty"`
49 TwoFactorAuthentication *bool `json:"two_factor_authentication,omitempty"`
50 Plan *Plan `json:"plan,omitempty"`
51 LdapDn *string `json:"ldap_dn,omitempty"`
52
53
54 URL *string `json:"url,omitempty"`
55 EventsURL *string `json:"events_url,omitempty"`
56 FollowingURL *string `json:"following_url,omitempty"`
57 FollowersURL *string `json:"followers_url,omitempty"`
58 GistsURL *string `json:"gists_url,omitempty"`
59 OrganizationsURL *string `json:"organizations_url,omitempty"`
60 ReceivedEventsURL *string `json:"received_events_url,omitempty"`
61 ReposURL *string `json:"repos_url,omitempty"`
62 StarredURL *string `json:"starred_url,omitempty"`
63 SubscriptionsURL *string `json:"subscriptions_url,omitempty"`
64
65
66
67 TextMatches []*TextMatch `json:"text_matches,omitempty"`
68
69
70
71 Permissions map[string]bool `json:"permissions,omitempty"`
72 RoleName *string `json:"role_name,omitempty"`
73 }
74
75 func (u User) String() string {
76 return Stringify(u)
77 }
78
79
80
81
82
83
84 func (s *UsersService) Get(ctx context.Context, user string) (*User, *Response, error) {
85 var u string
86 if user != "" {
87 u = fmt.Sprintf("users/%v", user)
88 } else {
89 u = "user"
90 }
91 req, err := s.client.NewRequest("GET", u, nil)
92 if err != nil {
93 return nil, nil, err
94 }
95
96 uResp := new(User)
97 resp, err := s.client.Do(ctx, req, uResp)
98 if err != nil {
99 return nil, resp, err
100 }
101
102 return uResp, resp, nil
103 }
104
105
106
107
108 func (s *UsersService) GetByID(ctx context.Context, id int64) (*User, *Response, error) {
109 u := fmt.Sprintf("user/%d", id)
110 req, err := s.client.NewRequest("GET", u, nil)
111 if err != nil {
112 return nil, nil, err
113 }
114
115 user := new(User)
116 resp, err := s.client.Do(ctx, req, user)
117 if err != nil {
118 return nil, resp, err
119 }
120
121 return user, resp, nil
122 }
123
124
125
126
127 func (s *UsersService) Edit(ctx context.Context, user *User) (*User, *Response, error) {
128 u := "user"
129 req, err := s.client.NewRequest("PATCH", u, user)
130 if err != nil {
131 return nil, nil, err
132 }
133
134 uResp := new(User)
135 resp, err := s.client.Do(ctx, req, uResp)
136 if err != nil {
137 return nil, resp, err
138 }
139
140 return uResp, resp, nil
141 }
142
143
144
145 type HovercardOptions struct {
146
147
148 SubjectType string `url:"subject_type"`
149
150
151 SubjectID string `url:"subject_id"`
152 }
153
154
155 type Hovercard struct {
156 Contexts []*UserContext `json:"contexts,omitempty"`
157 }
158
159
160 type UserContext struct {
161 Message *string `json:"message,omitempty"`
162 Octicon *string `json:"octicon,omitempty"`
163 }
164
165
166
167
168
169 func (s *UsersService) GetHovercard(ctx context.Context, user string, opts *HovercardOptions) (*Hovercard, *Response, error) {
170 u := fmt.Sprintf("users/%v/hovercard", user)
171 u, err := addOptions(u, opts)
172 if err != nil {
173 return nil, nil, err
174 }
175
176 req, err := s.client.NewRequest("GET", u, nil)
177 if err != nil {
178 return nil, nil, err
179 }
180
181 hc := new(Hovercard)
182 resp, err := s.client.Do(ctx, req, hc)
183 if err != nil {
184 return nil, resp, err
185 }
186
187 return hc, resp, nil
188 }
189
190
191
192 type UserListOptions struct {
193
194 Since int64 `url:"since,omitempty"`
195
196
197
198
199 ListOptions
200 }
201
202
203
204
205
206
207 func (s *UsersService) ListAll(ctx context.Context, opts *UserListOptions) ([]*User, *Response, error) {
208 u, err := addOptions("users", opts)
209 if err != nil {
210 return nil, nil, err
211 }
212
213 req, err := s.client.NewRequest("GET", u, nil)
214 if err != nil {
215 return nil, nil, err
216 }
217
218 var users []*User
219 resp, err := s.client.Do(ctx, req, &users)
220 if err != nil {
221 return nil, resp, err
222 }
223
224 return users, resp, nil
225 }
226
227
228
229
230
231 func (s *UsersService) ListInvitations(ctx context.Context, opts *ListOptions) ([]*RepositoryInvitation, *Response, error) {
232 u, err := addOptions("user/repository_invitations", opts)
233 if err != nil {
234 return nil, nil, err
235 }
236
237 req, err := s.client.NewRequest("GET", u, nil)
238 if err != nil {
239 return nil, nil, err
240 }
241
242 invites := []*RepositoryInvitation{}
243 resp, err := s.client.Do(ctx, req, &invites)
244 if err != nil {
245 return nil, resp, err
246 }
247
248 return invites, resp, nil
249 }
250
251
252
253
254
255 func (s *UsersService) AcceptInvitation(ctx context.Context, invitationID int64) (*Response, error) {
256 u := fmt.Sprintf("user/repository_invitations/%v", invitationID)
257 req, err := s.client.NewRequest("PATCH", u, nil)
258 if err != nil {
259 return nil, err
260 }
261
262 return s.client.Do(ctx, req, nil)
263 }
264
265
266
267
268
269 func (s *UsersService) DeclineInvitation(ctx context.Context, invitationID int64) (*Response, error) {
270 u := fmt.Sprintf("user/repository_invitations/%v", invitationID)
271 req, err := s.client.NewRequest("DELETE", u, nil)
272 if err != nil {
273 return nil, err
274 }
275
276 return s.client.Do(ctx, req, nil)
277 }
278
View as plain text