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 }
73
74 func (u User) String() string {
75 return Stringify(u)
76 }
77
78
79
80
81
82
83 func (s *UsersService) Get(ctx context.Context, user string) (*User, *Response, error) {
84 var u string
85 if user != "" {
86 u = fmt.Sprintf("users/%v", user)
87 } else {
88 u = "user"
89 }
90 req, err := s.client.NewRequest("GET", u, nil)
91 if err != nil {
92 return nil, nil, err
93 }
94
95 uResp := new(User)
96 resp, err := s.client.Do(ctx, req, uResp)
97 if err != nil {
98 return nil, resp, err
99 }
100
101 return uResp, resp, nil
102 }
103
104
105
106
107 func (s *UsersService) GetByID(ctx context.Context, id int64) (*User, *Response, error) {
108 u := fmt.Sprintf("user/%d", id)
109 req, err := s.client.NewRequest("GET", u, nil)
110 if err != nil {
111 return nil, nil, err
112 }
113
114 user := new(User)
115 resp, err := s.client.Do(ctx, req, user)
116 if err != nil {
117 return nil, resp, err
118 }
119
120 return user, resp, nil
121 }
122
123
124
125
126 func (s *UsersService) Edit(ctx context.Context, user *User) (*User, *Response, error) {
127 u := "user"
128 req, err := s.client.NewRequest("PATCH", u, user)
129 if err != nil {
130 return nil, nil, err
131 }
132
133 uResp := new(User)
134 resp, err := s.client.Do(ctx, req, uResp)
135 if err != nil {
136 return nil, resp, err
137 }
138
139 return uResp, resp, nil
140 }
141
142
143
144 type HovercardOptions struct {
145
146
147 SubjectType string `url:"subject_type"`
148
149
150 SubjectID string `url:"subject_id"`
151 }
152
153
154 type Hovercard struct {
155 Contexts []*UserContext `json:"contexts,omitempty"`
156 }
157
158
159 type UserContext struct {
160 Message *string `json:"message,omitempty"`
161 Octicon *string `json:"octicon,omitempty"`
162 }
163
164
165
166
167
168 func (s *UsersService) GetHovercard(ctx context.Context, user string, opts *HovercardOptions) (*Hovercard, *Response, error) {
169 u := fmt.Sprintf("users/%v/hovercard", user)
170 u, err := addOptions(u, opts)
171 if err != nil {
172 return nil, nil, err
173 }
174
175 req, err := s.client.NewRequest("GET", u, nil)
176 if err != nil {
177 return nil, nil, err
178 }
179
180 hc := new(Hovercard)
181 resp, err := s.client.Do(ctx, req, hc)
182 if err != nil {
183 return nil, resp, err
184 }
185
186 return hc, resp, nil
187 }
188
189
190
191 type UserListOptions struct {
192
193 Since int64 `url:"since,omitempty"`
194
195
196
197
198 ListOptions
199 }
200
201
202
203
204
205
206 func (s *UsersService) ListAll(ctx context.Context, opts *UserListOptions) ([]*User, *Response, error) {
207 u, err := addOptions("users", opts)
208 if err != nil {
209 return nil, nil, err
210 }
211
212 req, err := s.client.NewRequest("GET", u, nil)
213 if err != nil {
214 return nil, nil, err
215 }
216
217 var users []*User
218 resp, err := s.client.Do(ctx, req, &users)
219 if err != nil {
220 return nil, resp, err
221 }
222
223 return users, resp, nil
224 }
225
226
227
228
229
230 func (s *UsersService) ListInvitations(ctx context.Context, opts *ListOptions) ([]*RepositoryInvitation, *Response, error) {
231 u, err := addOptions("user/repository_invitations", opts)
232 if err != nil {
233 return nil, nil, err
234 }
235
236 req, err := s.client.NewRequest("GET", u, nil)
237 if err != nil {
238 return nil, nil, err
239 }
240
241 invites := []*RepositoryInvitation{}
242 resp, err := s.client.Do(ctx, req, &invites)
243 if err != nil {
244 return nil, resp, err
245 }
246
247 return invites, resp, nil
248 }
249
250
251
252
253
254 func (s *UsersService) AcceptInvitation(ctx context.Context, invitationID int64) (*Response, error) {
255 u := fmt.Sprintf("user/repository_invitations/%v", invitationID)
256 req, err := s.client.NewRequest("PATCH", u, nil)
257 if err != nil {
258 return nil, err
259 }
260
261 return s.client.Do(ctx, req, nil)
262 }
263
264
265
266
267
268 func (s *UsersService) DeclineInvitation(ctx context.Context, invitationID int64) (*Response, error) {
269 u := fmt.Sprintf("user/repository_invitations/%v", invitationID)
270 req, err := s.client.NewRequest("DELETE", u, nil)
271 if err != nil {
272 return nil, err
273 }
274
275 return s.client.Do(ctx, req, nil)
276 }
277
View as plain text