orgs_members.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. // Membership represents the status of a user's membership in an organization or team.
  11. type Membership struct {
  12. URL *string `json:"url,omitempty"`
  13. // State is the user's status within the organization or team.
  14. // Possible values are: "active", "pending"
  15. State *string `json:"state,omitempty"`
  16. // Role identifies the user's role within the organization or team.
  17. // Possible values for organization membership:
  18. // member - non-owner organization member
  19. // admin - organization owner
  20. //
  21. // Possible values for team membership are:
  22. // member - a normal member of the team
  23. // maintainer - a team maintainer. Able to add/remove other team
  24. // members, promote other team members to team
  25. // maintainer, and edit the team’s name and description
  26. Role *string `json:"role,omitempty"`
  27. // For organization membership, the API URL of the organization.
  28. OrganizationURL *string `json:"organization_url,omitempty"`
  29. // For organization membership, the organization the membership is for.
  30. Organization *Organization `json:"organization,omitempty"`
  31. // For organization membership, the user the membership is for.
  32. User *User `json:"user,omitempty"`
  33. }
  34. func (m Membership) String() string {
  35. return Stringify(m)
  36. }
  37. // ListMembersOptions specifies optional parameters to the
  38. // OrganizationsService.ListMembers method.
  39. type ListMembersOptions struct {
  40. // If true (or if the authenticated user is not an owner of the
  41. // organization), list only publicly visible members.
  42. PublicOnly bool `url:"-"`
  43. // Filter members returned in the list. Possible values are:
  44. // 2fa_disabled, all. Default is "all".
  45. Filter string `url:"filter,omitempty"`
  46. // Role filters members returned by their role in the organization.
  47. // Possible values are:
  48. // all - all members of the organization, regardless of role
  49. // admin - organization owners
  50. // member - non-organization members
  51. //
  52. // Default is "all".
  53. Role string `url:"role,omitempty"`
  54. ListOptions
  55. }
  56. // ListMembers lists the members for an organization. If the authenticated
  57. // user is an owner of the organization, this will return both concealed and
  58. // public members, otherwise it will only return public members.
  59. //
  60. // GitHub API docs: https://developer.github.com/v3/orgs/members/#members-list
  61. func (s *OrganizationsService) ListMembers(ctx context.Context, org string, opt *ListMembersOptions) ([]*User, *Response, error) {
  62. var u string
  63. if opt != nil && opt.PublicOnly {
  64. u = fmt.Sprintf("orgs/%v/public_members", org)
  65. } else {
  66. u = fmt.Sprintf("orgs/%v/members", org)
  67. }
  68. u, err := addOptions(u, opt)
  69. if err != nil {
  70. return nil, nil, err
  71. }
  72. req, err := s.client.NewRequest("GET", u, nil)
  73. if err != nil {
  74. return nil, nil, err
  75. }
  76. var members []*User
  77. resp, err := s.client.Do(ctx, req, &members)
  78. if err != nil {
  79. return nil, resp, err
  80. }
  81. return members, resp, nil
  82. }
  83. // IsMember checks if a user is a member of an organization.
  84. //
  85. // GitHub API docs: https://developer.github.com/v3/orgs/members/#check-membership
  86. func (s *OrganizationsService) IsMember(ctx context.Context, org, user string) (bool, *Response, error) {
  87. u := fmt.Sprintf("orgs/%v/members/%v", org, user)
  88. req, err := s.client.NewRequest("GET", u, nil)
  89. if err != nil {
  90. return false, nil, err
  91. }
  92. resp, err := s.client.Do(ctx, req, nil)
  93. member, err := parseBoolResponse(err)
  94. return member, resp, err
  95. }
  96. // IsPublicMember checks if a user is a public member of an organization.
  97. //
  98. // GitHub API docs: https://developer.github.com/v3/orgs/members/#check-public-membership
  99. func (s *OrganizationsService) IsPublicMember(ctx context.Context, org, user string) (bool, *Response, error) {
  100. u := fmt.Sprintf("orgs/%v/public_members/%v", org, user)
  101. req, err := s.client.NewRequest("GET", u, nil)
  102. if err != nil {
  103. return false, nil, err
  104. }
  105. resp, err := s.client.Do(ctx, req, nil)
  106. member, err := parseBoolResponse(err)
  107. return member, resp, err
  108. }
  109. // RemoveMember removes a user from all teams of an organization.
  110. //
  111. // GitHub API docs: https://developer.github.com/v3/orgs/members/#remove-a-member
  112. func (s *OrganizationsService) RemoveMember(ctx context.Context, org, user string) (*Response, error) {
  113. u := fmt.Sprintf("orgs/%v/members/%v", org, user)
  114. req, err := s.client.NewRequest("DELETE", u, nil)
  115. if err != nil {
  116. return nil, err
  117. }
  118. return s.client.Do(ctx, req, nil)
  119. }
  120. // PublicizeMembership publicizes a user's membership in an organization. (A
  121. // user cannot publicize the membership for another user.)
  122. //
  123. // GitHub API docs: https://developer.github.com/v3/orgs/members/#publicize-a-users-membership
  124. func (s *OrganizationsService) PublicizeMembership(ctx context.Context, org, user string) (*Response, error) {
  125. u := fmt.Sprintf("orgs/%v/public_members/%v", org, user)
  126. req, err := s.client.NewRequest("PUT", u, nil)
  127. if err != nil {
  128. return nil, err
  129. }
  130. return s.client.Do(ctx, req, nil)
  131. }
  132. // ConcealMembership conceals a user's membership in an organization.
  133. //
  134. // GitHub API docs: https://developer.github.com/v3/orgs/members/#conceal-a-users-membership
  135. func (s *OrganizationsService) ConcealMembership(ctx context.Context, org, user string) (*Response, error) {
  136. u := fmt.Sprintf("orgs/%v/public_members/%v", org, user)
  137. req, err := s.client.NewRequest("DELETE", u, nil)
  138. if err != nil {
  139. return nil, err
  140. }
  141. return s.client.Do(ctx, req, nil)
  142. }
  143. // ListOrgMembershipsOptions specifies optional parameters to the
  144. // OrganizationsService.ListOrgMemberships method.
  145. type ListOrgMembershipsOptions struct {
  146. // Filter memberships to include only those with the specified state.
  147. // Possible values are: "active", "pending".
  148. State string `url:"state,omitempty"`
  149. ListOptions
  150. }
  151. // ListOrgMemberships lists the organization memberships for the authenticated user.
  152. //
  153. // GitHub API docs: https://developer.github.com/v3/orgs/members/#list-your-organization-memberships
  154. func (s *OrganizationsService) ListOrgMemberships(ctx context.Context, opt *ListOrgMembershipsOptions) ([]*Membership, *Response, error) {
  155. u := "user/memberships/orgs"
  156. u, err := addOptions(u, opt)
  157. if err != nil {
  158. return nil, nil, err
  159. }
  160. req, err := s.client.NewRequest("GET", u, nil)
  161. if err != nil {
  162. return nil, nil, err
  163. }
  164. var memberships []*Membership
  165. resp, err := s.client.Do(ctx, req, &memberships)
  166. if err != nil {
  167. return nil, resp, err
  168. }
  169. return memberships, resp, nil
  170. }
  171. // GetOrgMembership gets the membership for a user in a specified organization.
  172. // Passing an empty string for user will get the membership for the
  173. // authenticated user.
  174. //
  175. // GitHub API docs:
  176. // https://developer.github.com/v3/orgs/members/#get-organization-membership
  177. // https://developer.github.com/v3/orgs/members/#get-your-organization-membership
  178. func (s *OrganizationsService) GetOrgMembership(ctx context.Context, user, org string) (*Membership, *Response, error) {
  179. var u string
  180. if user != "" {
  181. u = fmt.Sprintf("orgs/%v/memberships/%v", org, user)
  182. } else {
  183. u = fmt.Sprintf("user/memberships/orgs/%v", org)
  184. }
  185. req, err := s.client.NewRequest("GET", u, nil)
  186. if err != nil {
  187. return nil, nil, err
  188. }
  189. membership := new(Membership)
  190. resp, err := s.client.Do(ctx, req, membership)
  191. if err != nil {
  192. return nil, resp, err
  193. }
  194. return membership, resp, nil
  195. }
  196. // EditOrgMembership edits the membership for user in specified organization.
  197. // Passing an empty string for user will edit the membership for the
  198. // authenticated user.
  199. //
  200. // GitHub API docs: https://developer.github.com/v3/orgs/members/#add-or-update-organization-membership
  201. // GitHub API docs: https://developer.github.com/v3/orgs/members/#edit-your-organization-membership
  202. func (s *OrganizationsService) EditOrgMembership(ctx context.Context, user, org string, membership *Membership) (*Membership, *Response, error) {
  203. var u, method string
  204. if user != "" {
  205. u = fmt.Sprintf("orgs/%v/memberships/%v", org, user)
  206. method = "PUT"
  207. } else {
  208. u = fmt.Sprintf("user/memberships/orgs/%v", org)
  209. method = "PATCH"
  210. }
  211. req, err := s.client.NewRequest(method, u, membership)
  212. if err != nil {
  213. return nil, nil, err
  214. }
  215. m := new(Membership)
  216. resp, err := s.client.Do(ctx, req, m)
  217. if err != nil {
  218. return nil, resp, err
  219. }
  220. return m, resp, nil
  221. }
  222. // RemoveOrgMembership removes user from the specified organization. If the
  223. // user has been invited to the organization, this will cancel their invitation.
  224. //
  225. // GitHub API docs: https://developer.github.com/v3/orgs/members/#remove-organization-membership
  226. func (s *OrganizationsService) RemoveOrgMembership(ctx context.Context, user, org string) (*Response, error) {
  227. u := fmt.Sprintf("orgs/%v/memberships/%v", org, user)
  228. req, err := s.client.NewRequest("DELETE", u, nil)
  229. if err != nil {
  230. return nil, err
  231. }
  232. return s.client.Do(ctx, req, nil)
  233. }
  234. // ListPendingOrgInvitations returns a list of pending invitations.
  235. //
  236. // GitHub API docs: https://developer.github.com/v3/orgs/members/#list-pending-organization-invitations
  237. func (s *OrganizationsService) ListPendingOrgInvitations(ctx context.Context, org string, opt *ListOptions) ([]*Invitation, *Response, error) {
  238. u := fmt.Sprintf("orgs/%v/invitations", org)
  239. u, err := addOptions(u, opt)
  240. if err != nil {
  241. return nil, nil, err
  242. }
  243. req, err := s.client.NewRequest("GET", u, nil)
  244. if err != nil {
  245. return nil, nil, err
  246. }
  247. var pendingInvitations []*Invitation
  248. resp, err := s.client.Do(ctx, req, &pendingInvitations)
  249. if err != nil {
  250. return nil, resp, err
  251. }
  252. return pendingInvitations, resp, nil
  253. }
  254. // CreateOrgInvitationOptions specifies the parameters to the OrganizationService.Invite
  255. // method.
  256. type CreateOrgInvitationOptions struct {
  257. // GitHub user ID for the person you are inviting. Not required if you provide Email.
  258. InviteeID *int64 `json:"invitee_id,omitempty"`
  259. // Email address of the person you are inviting, which can be an existing GitHub user.
  260. // Not required if you provide InviteeID
  261. Email *string `json:"email,omitempty"`
  262. // Specify role for new member. Can be one of:
  263. // * admin - Organization owners with full administrative rights to the
  264. // organization and complete access to all repositories and teams.
  265. // * direct_member - Non-owner organization members with ability to see
  266. // other members and join teams by invitation.
  267. // * billing_manager - Non-owner organization members with ability to
  268. // manage the billing settings of your organization.
  269. // Default is "direct_member".
  270. Role *string `json:"role"`
  271. TeamID []int64 `json:"team_ids"`
  272. }
  273. // CreateOrgInvitation invites people to an organization by using their GitHub user ID or their email address.
  274. // In order to create invitations in an organization,
  275. // the authenticated user must be an organization owner.
  276. //
  277. // https://developer.github.com/v3/orgs/members/#create-organization-invitation
  278. func (s *OrganizationsService) CreateOrgInvitation(ctx context.Context, org string, opt *CreateOrgInvitationOptions) (*Invitation, *Response, error) {
  279. u := fmt.Sprintf("orgs/%v/invitations", org)
  280. req, err := s.client.NewRequest("POST", u, opt)
  281. if err != nil {
  282. return nil, nil, err
  283. }
  284. // TODO: remove custom Accept header when this API fully launches.
  285. req.Header.Set("Accept", mediaTypeOrganizationInvitationPreview)
  286. var invitation *Invitation
  287. resp, err := s.client.Do(ctx, req, &invitation)
  288. if err != nil {
  289. return nil, resp, err
  290. }
  291. return invitation, resp, nil
  292. }
  293. // ListOrgInvitationTeams lists all teams associated with an invitation. In order to see invitations in an organization,
  294. // the authenticated user must be an organization owner.
  295. //
  296. // GitHub API docs: https://developer.github.com/v3/orgs/members/#list-organization-invitation-teams
  297. func (s *OrganizationsService) ListOrgInvitationTeams(ctx context.Context, org, invitationID string, opt *ListOptions) ([]*Team, *Response, error) {
  298. u := fmt.Sprintf("orgs/%v/invitations/%v/teams", org, invitationID)
  299. u, err := addOptions(u, opt)
  300. if err != nil {
  301. return nil, nil, err
  302. }
  303. req, err := s.client.NewRequest("GET", u, nil)
  304. if err != nil {
  305. return nil, nil, err
  306. }
  307. // TODO: remove custom Accept header when this API fully launches.
  308. req.Header.Set("Accept", mediaTypeOrganizationInvitationPreview)
  309. var orgInvitationTeams []*Team
  310. resp, err := s.client.Do(ctx, req, &orgInvitationTeams)
  311. if err != nil {
  312. return nil, resp, err
  313. }
  314. return orgInvitationTeams, resp, nil
  315. }