group_members.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // Copyright 2017, Sander van Harmelen
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. package gitlab
  17. import (
  18. "fmt"
  19. "net/url"
  20. )
  21. // GroupMembersService handles communication with the group members
  22. // related methods of the GitLab API.
  23. //
  24. // GitLab API docs: https://docs.gitlab.com/ce/api/members.html
  25. type GroupMembersService struct {
  26. client *Client
  27. }
  28. // GroupMember represents a GitLab group member.
  29. //
  30. // GitLab API docs: https://docs.gitlab.com/ce/api/members.html
  31. type GroupMember struct {
  32. ID int `json:"id"`
  33. Username string `json:"username"`
  34. Name string `json:"name"`
  35. State string `json:"state"`
  36. AvatarURL string `json:"avatar_url"`
  37. WebURL string `json:"web_url"`
  38. ExpiresAt *ISOTime `json:"expires_at"`
  39. AccessLevel AccessLevelValue `json:"access_level"`
  40. }
  41. // ListGroupMembersOptions represents the available ListGroupMembers() and
  42. // ListAllGroupMembers() options.
  43. //
  44. // GitLab API docs:
  45. // https://docs.gitlab.com/ce/api/members.html#list-all-members-of-a-group-or-project
  46. type ListGroupMembersOptions struct {
  47. ListOptions
  48. Query *string `url:"query,omitempty" json:"query,omitempty"`
  49. }
  50. // ListGroupMembers get a list of group members viewable by the authenticated
  51. // user. Returns a list including inherited members through ancestor groups.
  52. //
  53. // GitLab API docs:
  54. // https://docs.gitlab.com/ce/api/members.html#list-all-members-of-a-group-or-project
  55. func (s *GroupsService) ListGroupMembers(gid interface{}, opt *ListGroupMembersOptions, options ...OptionFunc) ([]*GroupMember, *Response, error) {
  56. group, err := parseID(gid)
  57. if err != nil {
  58. return nil, nil, err
  59. }
  60. u := fmt.Sprintf("groups/%s/members", url.QueryEscape(group))
  61. req, err := s.client.NewRequest("GET", u, opt, options)
  62. if err != nil {
  63. return nil, nil, err
  64. }
  65. var gm []*GroupMember
  66. resp, err := s.client.Do(req, &gm)
  67. if err != nil {
  68. return nil, resp, err
  69. }
  70. return gm, resp, err
  71. }
  72. // ListAllGroupMembers get a list of group members viewable by the authenticated
  73. // user. Returns a list including inherited members through ancestor groups.
  74. //
  75. // GitLab API docs:
  76. // https://docs.gitlab.com/ce/api/members.html#list-all-members-of-a-group-or-project-including-inherited-members
  77. func (s *GroupsService) ListAllGroupMembers(gid interface{}, opt *ListGroupMembersOptions, options ...OptionFunc) ([]*GroupMember, *Response, error) {
  78. group, err := parseID(gid)
  79. if err != nil {
  80. return nil, nil, err
  81. }
  82. u := fmt.Sprintf("groups/%s/members/all", url.QueryEscape(group))
  83. req, err := s.client.NewRequest("GET", u, opt, options)
  84. if err != nil {
  85. return nil, nil, err
  86. }
  87. var gm []*GroupMember
  88. resp, err := s.client.Do(req, &gm)
  89. if err != nil {
  90. return nil, resp, err
  91. }
  92. return gm, resp, err
  93. }
  94. // AddGroupMemberOptions represents the available AddGroupMember() options.
  95. //
  96. // GitLab API docs:
  97. // https://docs.gitlab.com/ce/api/members.html#add-a-member-to-a-group-or-project
  98. type AddGroupMemberOptions struct {
  99. UserID *int `url:"user_id,omitempty" json:"user_id,omitempty"`
  100. AccessLevel *AccessLevelValue `url:"access_level,omitempty" json:"access_level,omitempty"`
  101. ExpiresAt *string `url:"expires_at,omitempty" json:"expires_at"`
  102. }
  103. // GetGroupMember gets a member of a group.
  104. //
  105. // GitLab API docs:
  106. // https://docs.gitlab.com/ce/api/members.html#get-a-member-of-a-group-or-project
  107. func (s *GroupMembersService) GetGroupMember(gid interface{}, user int, options ...OptionFunc) (*GroupMember, *Response, error) {
  108. group, err := parseID(gid)
  109. if err != nil {
  110. return nil, nil, err
  111. }
  112. u := fmt.Sprintf("groups/%s/members/%d", url.QueryEscape(group), user)
  113. req, err := s.client.NewRequest("GET", u, nil, options)
  114. if err != nil {
  115. return nil, nil, err
  116. }
  117. gm := new(GroupMember)
  118. resp, err := s.client.Do(req, gm)
  119. if err != nil {
  120. return nil, resp, err
  121. }
  122. return gm, resp, err
  123. }
  124. // AddGroupMember adds a user to the list of group members.
  125. //
  126. // GitLab API docs:
  127. // https://docs.gitlab.com/ce/api/members.html#add-a-member-to-a-group-or-project
  128. func (s *GroupMembersService) AddGroupMember(gid interface{}, opt *AddGroupMemberOptions, options ...OptionFunc) (*GroupMember, *Response, error) {
  129. group, err := parseID(gid)
  130. if err != nil {
  131. return nil, nil, err
  132. }
  133. u := fmt.Sprintf("groups/%s/members", url.QueryEscape(group))
  134. req, err := s.client.NewRequest("POST", u, opt, options)
  135. if err != nil {
  136. return nil, nil, err
  137. }
  138. gm := new(GroupMember)
  139. resp, err := s.client.Do(req, gm)
  140. if err != nil {
  141. return nil, resp, err
  142. }
  143. return gm, resp, err
  144. }
  145. // EditGroupMemberOptions represents the available EditGroupMember()
  146. // options.
  147. //
  148. // GitLab API docs:
  149. // https://docs.gitlab.com/ce/api/members.html#edit-a-member-of-a-group-or-project
  150. type EditGroupMemberOptions struct {
  151. AccessLevel *AccessLevelValue `url:"access_level,omitempty" json:"access_level,omitempty"`
  152. ExpiresAt *string `url:"expires_at,omitempty" json:"expires_at"`
  153. }
  154. // EditGroupMember updates a member of a group.
  155. //
  156. // GitLab API docs:
  157. // https://docs.gitlab.com/ce/api/members.html#edit-a-member-of-a-group-or-project
  158. func (s *GroupMembersService) EditGroupMember(gid interface{}, user int, opt *EditGroupMemberOptions, options ...OptionFunc) (*GroupMember, *Response, error) {
  159. group, err := parseID(gid)
  160. if err != nil {
  161. return nil, nil, err
  162. }
  163. u := fmt.Sprintf("groups/%s/members/%d", url.QueryEscape(group), user)
  164. req, err := s.client.NewRequest("PUT", u, opt, options)
  165. if err != nil {
  166. return nil, nil, err
  167. }
  168. gm := new(GroupMember)
  169. resp, err := s.client.Do(req, gm)
  170. if err != nil {
  171. return nil, resp, err
  172. }
  173. return gm, resp, err
  174. }
  175. // RemoveGroupMember removes user from user team.
  176. //
  177. // GitLab API docs:
  178. // https://docs.gitlab.com/ce/api/members.html#remove-a-member-from-a-group-or-project
  179. func (s *GroupMembersService) RemoveGroupMember(gid interface{}, user int, options ...OptionFunc) (*Response, error) {
  180. group, err := parseID(gid)
  181. if err != nil {
  182. return nil, err
  183. }
  184. u := fmt.Sprintf("groups/%s/members/%d", url.QueryEscape(group), user)
  185. req, err := s.client.NewRequest("DELETE", u, nil, options)
  186. if err != nil {
  187. return nil, err
  188. }
  189. return s.client.Do(req, nil)
  190. }