users.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Copyright 2013 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package github
  6. import (
  7. "context"
  8. "fmt"
  9. )
  10. // UsersService handles communication with the user related
  11. // methods of the GitHub API.
  12. //
  13. // GitHub API docs: https://developer.github.com/v3/users/
  14. type UsersService service
  15. // User represents a GitHub user.
  16. type User struct {
  17. Login *string `json:"login,omitempty"`
  18. ID *int64 `json:"id,omitempty"`
  19. AvatarURL *string `json:"avatar_url,omitempty"`
  20. HTMLURL *string `json:"html_url,omitempty"`
  21. GravatarID *string `json:"gravatar_id,omitempty"`
  22. Name *string `json:"name,omitempty"`
  23. Company *string `json:"company,omitempty"`
  24. Blog *string `json:"blog,omitempty"`
  25. Location *string `json:"location,omitempty"`
  26. Email *string `json:"email,omitempty"`
  27. Hireable *bool `json:"hireable,omitempty"`
  28. Bio *string `json:"bio,omitempty"`
  29. PublicRepos *int `json:"public_repos,omitempty"`
  30. PublicGists *int `json:"public_gists,omitempty"`
  31. Followers *int `json:"followers,omitempty"`
  32. Following *int `json:"following,omitempty"`
  33. CreatedAt *Timestamp `json:"created_at,omitempty"`
  34. UpdatedAt *Timestamp `json:"updated_at,omitempty"`
  35. SuspendedAt *Timestamp `json:"suspended_at,omitempty"`
  36. Type *string `json:"type,omitempty"`
  37. SiteAdmin *bool `json:"site_admin,omitempty"`
  38. TotalPrivateRepos *int `json:"total_private_repos,omitempty"`
  39. OwnedPrivateRepos *int `json:"owned_private_repos,omitempty"`
  40. PrivateGists *int `json:"private_gists,omitempty"`
  41. DiskUsage *int `json:"disk_usage,omitempty"`
  42. Collaborators *int `json:"collaborators,omitempty"`
  43. Plan *Plan `json:"plan,omitempty"`
  44. // API URLs
  45. URL *string `json:"url,omitempty"`
  46. EventsURL *string `json:"events_url,omitempty"`
  47. FollowingURL *string `json:"following_url,omitempty"`
  48. FollowersURL *string `json:"followers_url,omitempty"`
  49. GistsURL *string `json:"gists_url,omitempty"`
  50. OrganizationsURL *string `json:"organizations_url,omitempty"`
  51. ReceivedEventsURL *string `json:"received_events_url,omitempty"`
  52. ReposURL *string `json:"repos_url,omitempty"`
  53. StarredURL *string `json:"starred_url,omitempty"`
  54. SubscriptionsURL *string `json:"subscriptions_url,omitempty"`
  55. // TextMatches is only populated from search results that request text matches
  56. // See: search.go and https://developer.github.com/v3/search/#text-match-metadata
  57. TextMatches []TextMatch `json:"text_matches,omitempty"`
  58. // Permissions identifies the permissions that a user has on a given
  59. // repository. This is only populated when calling Repositories.ListCollaborators.
  60. Permissions *map[string]bool `json:"permissions,omitempty"`
  61. }
  62. func (u User) String() string {
  63. return Stringify(u)
  64. }
  65. // Get fetches a user. Passing the empty string will fetch the authenticated
  66. // user.
  67. //
  68. // GitHub API docs: https://developer.github.com/v3/users/#get-a-single-user
  69. func (s *UsersService) Get(ctx context.Context, user string) (*User, *Response, error) {
  70. var u string
  71. if user != "" {
  72. u = fmt.Sprintf("users/%v", user)
  73. } else {
  74. u = "user"
  75. }
  76. req, err := s.client.NewRequest("GET", u, nil)
  77. if err != nil {
  78. return nil, nil, err
  79. }
  80. uResp := new(User)
  81. resp, err := s.client.Do(ctx, req, uResp)
  82. if err != nil {
  83. return nil, resp, err
  84. }
  85. return uResp, resp, nil
  86. }
  87. // GetByID fetches a user.
  88. //
  89. // Note: GetByID uses the undocumented GitHub API endpoint /user/:id.
  90. func (s *UsersService) GetByID(ctx context.Context, id int64) (*User, *Response, error) {
  91. u := fmt.Sprintf("user/%d", id)
  92. req, err := s.client.NewRequest("GET", u, nil)
  93. if err != nil {
  94. return nil, nil, err
  95. }
  96. user := new(User)
  97. resp, err := s.client.Do(ctx, req, user)
  98. if err != nil {
  99. return nil, resp, err
  100. }
  101. return user, resp, nil
  102. }
  103. // Edit the authenticated user.
  104. //
  105. // GitHub API docs: https://developer.github.com/v3/users/#update-the-authenticated-user
  106. func (s *UsersService) Edit(ctx context.Context, user *User) (*User, *Response, error) {
  107. u := "user"
  108. req, err := s.client.NewRequest("PATCH", u, user)
  109. if err != nil {
  110. return nil, nil, err
  111. }
  112. uResp := new(User)
  113. resp, err := s.client.Do(ctx, req, uResp)
  114. if err != nil {
  115. return nil, resp, err
  116. }
  117. return uResp, resp, nil
  118. }
  119. // UserListOptions specifies optional parameters to the UsersService.ListAll
  120. // method.
  121. type UserListOptions struct {
  122. // ID of the last user seen
  123. Since int64 `url:"since,omitempty"`
  124. // Note: Pagination is powered exclusively by the Since parameter,
  125. // ListOptions.Page has no effect.
  126. // ListOptions.PerPage controls an undocumented GitHub API parameter.
  127. ListOptions
  128. }
  129. // ListAll lists all GitHub users.
  130. //
  131. // To paginate through all users, populate 'Since' with the ID of the last user.
  132. //
  133. // GitHub API docs: https://developer.github.com/v3/users/#get-all-users
  134. func (s *UsersService) ListAll(ctx context.Context, opt *UserListOptions) ([]*User, *Response, error) {
  135. u, err := addOptions("users", opt)
  136. if err != nil {
  137. return nil, nil, err
  138. }
  139. req, err := s.client.NewRequest("GET", u, nil)
  140. if err != nil {
  141. return nil, nil, err
  142. }
  143. var users []*User
  144. resp, err := s.client.Do(ctx, req, &users)
  145. if err != nil {
  146. return nil, resp, err
  147. }
  148. return users, resp, nil
  149. }
  150. // ListInvitations lists all currently-open repository invitations for the
  151. // authenticated user.
  152. //
  153. // GitHub API docs: https://developer.github.com/v3/repos/invitations/#list-a-users-repository-invitations
  154. func (s *UsersService) ListInvitations(ctx context.Context, opt *ListOptions) ([]*RepositoryInvitation, *Response, error) {
  155. u, err := addOptions("user/repository_invitations", opt)
  156. if err != nil {
  157. return nil, nil, err
  158. }
  159. req, err := s.client.NewRequest("GET", u, nil)
  160. if err != nil {
  161. return nil, nil, err
  162. }
  163. // TODO: remove custom Accept header when this API fully launches.
  164. req.Header.Set("Accept", mediaTypeRepositoryInvitationsPreview)
  165. invites := []*RepositoryInvitation{}
  166. resp, err := s.client.Do(ctx, req, &invites)
  167. if err != nil {
  168. return nil, resp, err
  169. }
  170. return invites, resp, nil
  171. }
  172. // AcceptInvitation accepts the currently-open repository invitation for the
  173. // authenticated user.
  174. //
  175. // GitHub API docs: https://developer.github.com/v3/repos/invitations/#accept-a-repository-invitation
  176. func (s *UsersService) AcceptInvitation(ctx context.Context, invitationID int64) (*Response, error) {
  177. u := fmt.Sprintf("user/repository_invitations/%v", invitationID)
  178. req, err := s.client.NewRequest("PATCH", u, nil)
  179. if err != nil {
  180. return nil, err
  181. }
  182. // TODO: remove custom Accept header when this API fully launches.
  183. req.Header.Set("Accept", mediaTypeRepositoryInvitationsPreview)
  184. return s.client.Do(ctx, req, nil)
  185. }
  186. // DeclineInvitation declines the currently-open repository invitation for the
  187. // authenticated user.
  188. //
  189. // GitHub API docs: https://developer.github.com/v3/repos/invitations/#decline-a-repository-invitation
  190. func (s *UsersService) DeclineInvitation(ctx context.Context, invitationID int64) (*Response, error) {
  191. u := fmt.Sprintf("user/repository_invitations/%v", invitationID)
  192. req, err := s.client.NewRequest("DELETE", u, nil)
  193. if err != nil {
  194. return nil, err
  195. }
  196. // TODO: remove custom Accept header when this API fully launches.
  197. req.Header.Set("Accept", mediaTypeRepositoryInvitationsPreview)
  198. return s.client.Do(ctx, req, nil)
  199. }