本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
9.1KB

  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. NodeID *string `json:"node_id,omitempty"`
  20. AvatarURL *string `json:"avatar_url,omitempty"`
  21. HTMLURL *string `json:"html_url,omitempty"`
  22. GravatarID *string `json:"gravatar_id,omitempty"`
  23. Name *string `json:"name,omitempty"`
  24. Company *string `json:"company,omitempty"`
  25. Blog *string `json:"blog,omitempty"`
  26. Location *string `json:"location,omitempty"`
  27. Email *string `json:"email,omitempty"`
  28. Hireable *bool `json:"hireable,omitempty"`
  29. Bio *string `json:"bio,omitempty"`
  30. PublicRepos *int `json:"public_repos,omitempty"`
  31. PublicGists *int `json:"public_gists,omitempty"`
  32. Followers *int `json:"followers,omitempty"`
  33. Following *int `json:"following,omitempty"`
  34. CreatedAt *Timestamp `json:"created_at,omitempty"`
  35. UpdatedAt *Timestamp `json:"updated_at,omitempty"`
  36. SuspendedAt *Timestamp `json:"suspended_at,omitempty"`
  37. Type *string `json:"type,omitempty"`
  38. SiteAdmin *bool `json:"site_admin,omitempty"`
  39. TotalPrivateRepos *int `json:"total_private_repos,omitempty"`
  40. OwnedPrivateRepos *int `json:"owned_private_repos,omitempty"`
  41. PrivateGists *int `json:"private_gists,omitempty"`
  42. DiskUsage *int `json:"disk_usage,omitempty"`
  43. Collaborators *int `json:"collaborators,omitempty"`
  44. TwoFactorAuthentication *bool `json:"two_factor_authentication,omitempty"`
  45. Plan *Plan `json:"plan,omitempty"`
  46. // API URLs
  47. URL *string `json:"url,omitempty"`
  48. EventsURL *string `json:"events_url,omitempty"`
  49. FollowingURL *string `json:"following_url,omitempty"`
  50. FollowersURL *string `json:"followers_url,omitempty"`
  51. GistsURL *string `json:"gists_url,omitempty"`
  52. OrganizationsURL *string `json:"organizations_url,omitempty"`
  53. ReceivedEventsURL *string `json:"received_events_url,omitempty"`
  54. ReposURL *string `json:"repos_url,omitempty"`
  55. StarredURL *string `json:"starred_url,omitempty"`
  56. SubscriptionsURL *string `json:"subscriptions_url,omitempty"`
  57. // TextMatches is only populated from search results that request text matches
  58. // See: search.go and https://developer.github.com/v3/search/#text-match-metadata
  59. TextMatches []TextMatch `json:"text_matches,omitempty"`
  60. // Permissions identifies the permissions that a user has on a given
  61. // repository. This is only populated when calling Repositories.ListCollaborators.
  62. Permissions *map[string]bool `json:"permissions,omitempty"`
  63. }
  64. func (u User) String() string {
  65. return Stringify(u)
  66. }
  67. // Get fetches a user. Passing the empty string will fetch the authenticated
  68. // user.
  69. //
  70. // GitHub API docs: https://developer.github.com/v3/users/#get-a-single-user
  71. // and: https://developer.github.com/v3/users/#get-the-authenticated-user
  72. func (s *UsersService) Get(ctx context.Context, user string) (*User, *Response, error) {
  73. var u string
  74. if user != "" {
  75. u = fmt.Sprintf("users/%v", user)
  76. } else {
  77. u = "user"
  78. }
  79. req, err := s.client.NewRequest("GET", u, nil)
  80. if err != nil {
  81. return nil, nil, err
  82. }
  83. uResp := new(User)
  84. resp, err := s.client.Do(ctx, req, uResp)
  85. if err != nil {
  86. return nil, resp, err
  87. }
  88. return uResp, resp, nil
  89. }
  90. // GetByID fetches a user.
  91. //
  92. // Note: GetByID uses the undocumented GitHub API endpoint /user/:id.
  93. func (s *UsersService) GetByID(ctx context.Context, id int64) (*User, *Response, error) {
  94. u := fmt.Sprintf("user/%d", id)
  95. req, err := s.client.NewRequest("GET", u, nil)
  96. if err != nil {
  97. return nil, nil, err
  98. }
  99. user := new(User)
  100. resp, err := s.client.Do(ctx, req, user)
  101. if err != nil {
  102. return nil, resp, err
  103. }
  104. return user, resp, nil
  105. }
  106. // Edit the authenticated user.
  107. //
  108. // GitHub API docs: https://developer.github.com/v3/users/#update-the-authenticated-user
  109. func (s *UsersService) Edit(ctx context.Context, user *User) (*User, *Response, error) {
  110. u := "user"
  111. req, err := s.client.NewRequest("PATCH", u, user)
  112. if err != nil {
  113. return nil, nil, err
  114. }
  115. uResp := new(User)
  116. resp, err := s.client.Do(ctx, req, uResp)
  117. if err != nil {
  118. return nil, resp, err
  119. }
  120. return uResp, resp, nil
  121. }
  122. // HovercardOptions specifies optional parameters to the UsersService.GetHovercard
  123. // method.
  124. type HovercardOptions struct {
  125. // SubjectType specifies the additional information to be received about the hovercard.
  126. // Possible values are: organization, repository, issue, pull_request. (Required when using subject_id.)
  127. SubjectType string `url:"subject_type"`
  128. // SubjectID specifies the ID for the SubjectType. (Required when using subject_type.)
  129. SubjectID string `url:"subject_id"`
  130. }
  131. // Hovercard represents hovercard information about a user.
  132. type Hovercard struct {
  133. Contexts []*UserContext `json:"contexts,omitempty"`
  134. }
  135. // UserContext represents the contextual information about user.
  136. type UserContext struct {
  137. Message *string `json:"message,omitempty"`
  138. Octicon *string `json:"octicon,omitempty"`
  139. }
  140. // GetHovercard fetches contextual information about user. It requires authentication
  141. // via Basic Auth or via OAuth with the repo scope.
  142. //
  143. // GitHub API docs: https://developer.github.com/v3/users/#get-contextual-information-about-a-user
  144. func (s *UsersService) GetHovercard(ctx context.Context, user string, opt *HovercardOptions) (*Hovercard, *Response, error) {
  145. u := fmt.Sprintf("users/%v/hovercard", user)
  146. u, err := addOptions(u, opt)
  147. if err != nil {
  148. return nil, nil, err
  149. }
  150. req, err := s.client.NewRequest("GET", u, nil)
  151. if err != nil {
  152. return nil, nil, err
  153. }
  154. // TODO: remove custom Accept header when this API fully launches.
  155. req.Header.Set("Accept", mediaTypeHovercardPreview)
  156. hc := new(Hovercard)
  157. resp, err := s.client.Do(ctx, req, hc)
  158. if err != nil {
  159. return nil, resp, err
  160. }
  161. return hc, resp, nil
  162. }
  163. // UserListOptions specifies optional parameters to the UsersService.ListAll
  164. // method.
  165. type UserListOptions struct {
  166. // ID of the last user seen
  167. Since int64 `url:"since,omitempty"`
  168. // Note: Pagination is powered exclusively by the Since parameter,
  169. // ListOptions.Page has no effect.
  170. // ListOptions.PerPage controls an undocumented GitHub API parameter.
  171. ListOptions
  172. }
  173. // ListAll lists all GitHub users.
  174. //
  175. // To paginate through all users, populate 'Since' with the ID of the last user.
  176. //
  177. // GitHub API docs: https://developer.github.com/v3/users/#get-all-users
  178. func (s *UsersService) ListAll(ctx context.Context, opt *UserListOptions) ([]*User, *Response, error) {
  179. u, err := addOptions("users", opt)
  180. if err != nil {
  181. return nil, nil, err
  182. }
  183. req, err := s.client.NewRequest("GET", u, nil)
  184. if err != nil {
  185. return nil, nil, err
  186. }
  187. var users []*User
  188. resp, err := s.client.Do(ctx, req, &users)
  189. if err != nil {
  190. return nil, resp, err
  191. }
  192. return users, resp, nil
  193. }
  194. // ListInvitations lists all currently-open repository invitations for the
  195. // authenticated user.
  196. //
  197. // GitHub API docs: https://developer.github.com/v3/repos/invitations/#list-a-users-repository-invitations
  198. func (s *UsersService) ListInvitations(ctx context.Context, opt *ListOptions) ([]*RepositoryInvitation, *Response, error) {
  199. u, err := addOptions("user/repository_invitations", opt)
  200. if err != nil {
  201. return nil, nil, err
  202. }
  203. req, err := s.client.NewRequest("GET", u, nil)
  204. if err != nil {
  205. return nil, nil, err
  206. }
  207. invites := []*RepositoryInvitation{}
  208. resp, err := s.client.Do(ctx, req, &invites)
  209. if err != nil {
  210. return nil, resp, err
  211. }
  212. return invites, resp, nil
  213. }
  214. // AcceptInvitation accepts the currently-open repository invitation for the
  215. // authenticated user.
  216. //
  217. // GitHub API docs: https://developer.github.com/v3/repos/invitations/#accept-a-repository-invitation
  218. func (s *UsersService) AcceptInvitation(ctx context.Context, invitationID int64) (*Response, error) {
  219. u := fmt.Sprintf("user/repository_invitations/%v", invitationID)
  220. req, err := s.client.NewRequest("PATCH", u, nil)
  221. if err != nil {
  222. return nil, err
  223. }
  224. return s.client.Do(ctx, req, nil)
  225. }
  226. // DeclineInvitation declines the currently-open repository invitation for the
  227. // authenticated user.
  228. //
  229. // GitHub API docs: https://developer.github.com/v3/repos/invitations/#decline-a-repository-invitation
  230. func (s *UsersService) DeclineInvitation(ctx context.Context, invitationID int64) (*Response, error) {
  231. u := fmt.Sprintf("user/repository_invitations/%v", invitationID)
  232. req, err := s.client.NewRequest("DELETE", u, nil)
  233. if err != nil {
  234. return nil, err
  235. }
  236. return s.client.Do(ctx, req, nil)
  237. }
上海开阖软件有限公司 沪ICP备12045867号-1