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.

174 lines
5.7 KiB

  1. // Copyright 2018 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. // TeamListTeamMembersOptions specifies the optional parameters to the
  11. // TeamsService.ListTeamMembers method.
  12. type TeamListTeamMembersOptions struct {
  13. // Role filters members returned by their role in the team. Possible
  14. // values are "all", "member", "maintainer". Default is "all".
  15. Role string `url:"role,omitempty"`
  16. ListOptions
  17. }
  18. // ListTeamMembers lists all of the users who are members of the specified
  19. // team.
  20. //
  21. // GitHub API docs: https://developer.github.com/v3/teams/members/#list-team-members
  22. func (s *TeamsService) ListTeamMembers(ctx context.Context, team int64, opt *TeamListTeamMembersOptions) ([]*User, *Response, error) {
  23. u := fmt.Sprintf("teams/%v/members", team)
  24. u, err := addOptions(u, opt)
  25. if err != nil {
  26. return nil, nil, err
  27. }
  28. req, err := s.client.NewRequest("GET", u, nil)
  29. if err != nil {
  30. return nil, nil, err
  31. }
  32. req.Header.Set("Accept", mediaTypeNestedTeamsPreview)
  33. var members []*User
  34. resp, err := s.client.Do(ctx, req, &members)
  35. if err != nil {
  36. return nil, resp, err
  37. }
  38. return members, resp, nil
  39. }
  40. // IsTeamMember checks if a user is a member of the specified team.
  41. //
  42. // GitHub API docs: https://developer.github.com/v3/teams/members/#get-team-member
  43. //
  44. // Deprecated: This API has been marked as deprecated in the Github API docs,
  45. // TeamsService.GetTeamMembership method should be used instead.
  46. func (s *TeamsService) IsTeamMember(ctx context.Context, team int64, user string) (bool, *Response, error) {
  47. u := fmt.Sprintf("teams/%v/members/%v", team, user)
  48. req, err := s.client.NewRequest("GET", u, nil)
  49. if err != nil {
  50. return false, nil, err
  51. }
  52. resp, err := s.client.Do(ctx, req, nil)
  53. member, err := parseBoolResponse(err)
  54. return member, resp, err
  55. }
  56. // GetTeamMembership returns the membership status for a user in a team.
  57. //
  58. // GitHub API docs: https://developer.github.com/v3/teams/members/#get-team-membership
  59. func (s *TeamsService) GetTeamMembership(ctx context.Context, team int64, user string) (*Membership, *Response, error) {
  60. u := fmt.Sprintf("teams/%v/memberships/%v", team, user)
  61. req, err := s.client.NewRequest("GET", u, nil)
  62. if err != nil {
  63. return nil, nil, err
  64. }
  65. req.Header.Set("Accept", mediaTypeNestedTeamsPreview)
  66. t := new(Membership)
  67. resp, err := s.client.Do(ctx, req, t)
  68. if err != nil {
  69. return nil, resp, err
  70. }
  71. return t, resp, nil
  72. }
  73. // TeamAddTeamMembershipOptions specifies the optional
  74. // parameters to the TeamsService.AddTeamMembership method.
  75. type TeamAddTeamMembershipOptions struct {
  76. // Role specifies the role the user should have in the team. Possible
  77. // values are:
  78. // member - a normal member of the team
  79. // maintainer - a team maintainer. Able to add/remove other team
  80. // members, promote other team members to team
  81. // maintainer, and edit the team’s name and description
  82. //
  83. // Default value is "member".
  84. Role string `json:"role,omitempty"`
  85. }
  86. // AddTeamMembership adds or invites a user to a team.
  87. //
  88. // In order to add a membership between a user and a team, the authenticated
  89. // user must have 'admin' permissions to the team or be an owner of the
  90. // organization that the team is associated with.
  91. //
  92. // If the user is already a part of the team's organization (meaning they're on
  93. // at least one other team in the organization), this endpoint will add the
  94. // user to the team.
  95. //
  96. // If the user is completely unaffiliated with the team's organization (meaning
  97. // they're on none of the organization's teams), this endpoint will send an
  98. // invitation to the user via email. This newly-created membership will be in
  99. // the "pending" state until the user accepts the invitation, at which point
  100. // the membership will transition to the "active" state and the user will be
  101. // added as a member of the team.
  102. //
  103. // GitHub API docs: https://developer.github.com/v3/teams/members/#add-or-update-team-membership
  104. func (s *TeamsService) AddTeamMembership(ctx context.Context, team int64, user string, opt *TeamAddTeamMembershipOptions) (*Membership, *Response, error) {
  105. u := fmt.Sprintf("teams/%v/memberships/%v", team, user)
  106. req, err := s.client.NewRequest("PUT", u, opt)
  107. if err != nil {
  108. return nil, nil, err
  109. }
  110. t := new(Membership)
  111. resp, err := s.client.Do(ctx, req, t)
  112. if err != nil {
  113. return nil, resp, err
  114. }
  115. return t, resp, nil
  116. }
  117. // RemoveTeamMembership removes a user from a team.
  118. //
  119. // GitHub API docs: https://developer.github.com/v3/teams/members/#remove-team-membership
  120. func (s *TeamsService) RemoveTeamMembership(ctx context.Context, team int64, user string) (*Response, error) {
  121. u := fmt.Sprintf("teams/%v/memberships/%v", team, user)
  122. req, err := s.client.NewRequest("DELETE", u, nil)
  123. if err != nil {
  124. return nil, err
  125. }
  126. return s.client.Do(ctx, req, nil)
  127. }
  128. // ListPendingTeamInvitations get pending invitaion list in team.
  129. // Warning: The API may change without advance notice during the preview period.
  130. // Preview features are not supported for production use.
  131. //
  132. // GitHub API docs: https://developer.github.com/v3/teams/members/#list-pending-team-invitations
  133. func (s *TeamsService) ListPendingTeamInvitations(ctx context.Context, team int64, opt *ListOptions) ([]*Invitation, *Response, error) {
  134. u := fmt.Sprintf("teams/%v/invitations", team)
  135. u, err := addOptions(u, 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 pendingInvitations []*Invitation
  144. resp, err := s.client.Do(ctx, req, &pendingInvitations)
  145. if err != nil {
  146. return nil, resp, err
  147. }
  148. return pendingInvitations, resp, nil
  149. }