groups.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. // GroupsService handles communication with the group related methods of
  22. // the GitLab API.
  23. //
  24. // GitLab API docs: https://docs.gitlab.com/ce/api/groups.html
  25. type GroupsService struct {
  26. client *Client
  27. }
  28. // Group represents a GitLab group.
  29. //
  30. // GitLab API docs: https://docs.gitlab.com/ce/api/groups.html
  31. type Group struct {
  32. ID int `json:"id"`
  33. Name string `json:"name"`
  34. Path string `json:"path"`
  35. Description string `json:"description"`
  36. Visibility *VisibilityValue `json:"visibility"`
  37. LFSEnabled bool `json:"lfs_enabled"`
  38. AvatarURL string `json:"avatar_url"`
  39. WebURL string `json:"web_url"`
  40. RequestAccessEnabled bool `json:"request_access_enabled"`
  41. FullName string `json:"full_name"`
  42. FullPath string `json:"full_path"`
  43. ParentID int `json:"parent_id"`
  44. Projects []*Project `json:"projects"`
  45. Statistics *StorageStatistics `json:"statistics"`
  46. }
  47. // ListGroupsOptions represents the available ListGroups() options.
  48. //
  49. // GitLab API docs: https://docs.gitlab.com/ce/api/groups.html#list-project-groups
  50. type ListGroupsOptions struct {
  51. ListOptions
  52. AllAvailable *bool `url:"all_available,omitempty" json:"all_available,omitempty"`
  53. OrderBy *string `url:"order_by,omitempty" json:"order_by,omitempty"`
  54. Owned *bool `url:"owned,omitempty" json:"owned,omitempty"`
  55. Search *string `url:"search,omitempty" json:"search,omitempty"`
  56. Sort *string `url:"sort,omitempty" json:"sort,omitempty"`
  57. Statistics *bool `url:"statistics,omitempty" json:"statistics,omitempty"`
  58. }
  59. // ListGroups gets a list of groups (as user: my groups, as admin: all groups).
  60. //
  61. // GitLab API docs:
  62. // https://docs.gitlab.com/ce/api/groups.html#list-project-groups
  63. func (s *GroupsService) ListGroups(opt *ListGroupsOptions, options ...OptionFunc) ([]*Group, *Response, error) {
  64. req, err := s.client.NewRequest("GET", "groups", opt, options)
  65. if err != nil {
  66. return nil, nil, err
  67. }
  68. var g []*Group
  69. resp, err := s.client.Do(req, &g)
  70. if err != nil {
  71. return nil, resp, err
  72. }
  73. return g, resp, err
  74. }
  75. // GetGroup gets all details of a group.
  76. //
  77. // GitLab API docs: https://docs.gitlab.com/ce/api/groups.html#details-of-a-group
  78. func (s *GroupsService) GetGroup(gid interface{}, options ...OptionFunc) (*Group, *Response, error) {
  79. group, err := parseID(gid)
  80. if err != nil {
  81. return nil, nil, err
  82. }
  83. u := fmt.Sprintf("groups/%s", url.QueryEscape(group))
  84. req, err := s.client.NewRequest("GET", u, nil, options)
  85. if err != nil {
  86. return nil, nil, err
  87. }
  88. g := new(Group)
  89. resp, err := s.client.Do(req, g)
  90. if err != nil {
  91. return nil, resp, err
  92. }
  93. return g, resp, err
  94. }
  95. // CreateGroupOptions represents the available CreateGroup() options.
  96. //
  97. // GitLab API docs: https://docs.gitlab.com/ce/api/groups.html#new-group
  98. type CreateGroupOptions struct {
  99. Name *string `url:"name,omitempty" json:"name,omitempty"`
  100. Path *string `url:"path,omitempty" json:"path,omitempty"`
  101. Description *string `url:"description,omitempty" json:"description,omitempty"`
  102. Visibility *VisibilityValue `url:"visibility,omitempty" json:"visibility,omitempty"`
  103. LFSEnabled *bool `url:"lfs_enabled,omitempty" json:"lfs_enabled,omitempty"`
  104. RequestAccessEnabled *bool `url:"request_access_enabled,omitempty" json:"request_access_enabled,omitempty"`
  105. ParentID *int `url:"parent_id,omitempty" json:"parent_id,omitempty"`
  106. }
  107. // CreateGroup creates a new project group. Available only for users who can
  108. // create groups.
  109. //
  110. // GitLab API docs: https://docs.gitlab.com/ce/api/groups.html#new-group
  111. func (s *GroupsService) CreateGroup(opt *CreateGroupOptions, options ...OptionFunc) (*Group, *Response, error) {
  112. req, err := s.client.NewRequest("POST", "groups", opt, options)
  113. if err != nil {
  114. return nil, nil, err
  115. }
  116. g := new(Group)
  117. resp, err := s.client.Do(req, g)
  118. if err != nil {
  119. return nil, resp, err
  120. }
  121. return g, resp, err
  122. }
  123. // TransferGroup transfers a project to the Group namespace. Available only
  124. // for admin.
  125. //
  126. // GitLab API docs:
  127. // https://docs.gitlab.com/ce/api/groups.html#transfer-project-to-group
  128. func (s *GroupsService) TransferGroup(gid interface{}, pid interface{}, options ...OptionFunc) (*Group, *Response, error) {
  129. group, err := parseID(gid)
  130. if err != nil {
  131. return nil, nil, err
  132. }
  133. project, err := parseID(pid)
  134. if err != nil {
  135. return nil, nil, err
  136. }
  137. u := fmt.Sprintf("groups/%s/projects/%s", url.QueryEscape(group),
  138. url.QueryEscape(project))
  139. req, err := s.client.NewRequest("POST", u, nil, options)
  140. if err != nil {
  141. return nil, nil, err
  142. }
  143. g := new(Group)
  144. resp, err := s.client.Do(req, g)
  145. if err != nil {
  146. return nil, resp, err
  147. }
  148. return g, resp, err
  149. }
  150. // UpdateGroupOptions represents the set of available options to update a Group;
  151. // as of today these are exactly the same available when creating a new Group.
  152. //
  153. // GitLab API docs: https://docs.gitlab.com/ce/api/groups.html#update-group
  154. type UpdateGroupOptions CreateGroupOptions
  155. // UpdateGroup updates an existing group; only available to group owners and
  156. // administrators.
  157. //
  158. // GitLab API docs: https://docs.gitlab.com/ce/api/groups.html#update-group
  159. func (s *GroupsService) UpdateGroup(gid interface{}, opt *UpdateGroupOptions, options ...OptionFunc) (*Group, *Response, error) {
  160. group, err := parseID(gid)
  161. if err != nil {
  162. return nil, nil, err
  163. }
  164. u := fmt.Sprintf("groups/%s", url.QueryEscape(group))
  165. req, err := s.client.NewRequest("PUT", u, opt, options)
  166. if err != nil {
  167. return nil, nil, err
  168. }
  169. g := new(Group)
  170. resp, err := s.client.Do(req, g)
  171. if err != nil {
  172. return nil, resp, err
  173. }
  174. return g, resp, err
  175. }
  176. // DeleteGroup removes group with all projects inside.
  177. //
  178. // GitLab API docs: https://docs.gitlab.com/ce/api/groups.html#remove-group
  179. func (s *GroupsService) DeleteGroup(gid interface{}, options ...OptionFunc) (*Response, error) {
  180. group, err := parseID(gid)
  181. if err != nil {
  182. return nil, err
  183. }
  184. u := fmt.Sprintf("groups/%s", url.QueryEscape(group))
  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. }
  191. // SearchGroup get all groups that match your string in their name or path.
  192. //
  193. // GitLab API docs: https://docs.gitlab.com/ce/api/groups.html#search-for-group
  194. func (s *GroupsService) SearchGroup(query string, options ...OptionFunc) ([]*Group, *Response, error) {
  195. var q struct {
  196. Search string `url:"search,omitempty" json:"search,omitempty"`
  197. }
  198. q.Search = query
  199. req, err := s.client.NewRequest("GET", "groups", &q, options)
  200. if err != nil {
  201. return nil, nil, err
  202. }
  203. var g []*Group
  204. resp, err := s.client.Do(req, &g)
  205. if err != nil {
  206. return nil, resp, err
  207. }
  208. return g, resp, err
  209. }
  210. // ListGroupProjectsOptions represents the available ListGroupProjects()
  211. // options.
  212. //
  213. // GitLab API docs:
  214. // https://docs.gitlab.com/ce/api/groups.html#list-a-group-39-s-projects
  215. type ListGroupProjectsOptions ListProjectsOptions
  216. // ListGroupProjects get a list of group projects
  217. //
  218. // GitLab API docs:
  219. // https://docs.gitlab.com/ce/api/groups.html#list-a-group-39-s-projects
  220. func (s *GroupsService) ListGroupProjects(gid interface{}, opt *ListGroupProjectsOptions, options ...OptionFunc) ([]*Project, *Response, error) {
  221. group, err := parseID(gid)
  222. if err != nil {
  223. return nil, nil, err
  224. }
  225. u := fmt.Sprintf("groups/%s/projects", url.QueryEscape(group))
  226. req, err := s.client.NewRequest("GET", u, opt, options)
  227. if err != nil {
  228. return nil, nil, err
  229. }
  230. var p []*Project
  231. resp, err := s.client.Do(req, &p)
  232. if err != nil {
  233. return nil, resp, err
  234. }
  235. return p, resp, err
  236. }
  237. // ListSubgroupsOptions represents the available ListSubgroupsOptions()
  238. // options.
  239. //
  240. // GitLab API docs:
  241. // https://docs.gitlab.com/ce/api/groups.html#list-a-groups-s-subgroups
  242. type ListSubgroupsOptions ListGroupsOptions
  243. // ListSubgroups gets a list of subgroups for a given project.
  244. //
  245. // GitLab API docs:
  246. // https://docs.gitlab.com/ce/api/groups.html#list-a-groups-s-subgroups
  247. func (s *GroupsService) ListSubgroups(gid interface{}, opt *ListSubgroupsOptions, options ...OptionFunc) ([]*Group, *Response, error) {
  248. group, err := parseID(gid)
  249. if err != nil {
  250. return nil, nil, err
  251. }
  252. u := fmt.Sprintf("groups/%s/subgroups", url.QueryEscape(group))
  253. req, err := s.client.NewRequest("GET", u, opt, options)
  254. if err != nil {
  255. return nil, nil, err
  256. }
  257. var g []*Group
  258. resp, err := s.client.Do(req, &g)
  259. if err != nil {
  260. return nil, resp, err
  261. }
  262. return g, resp, err
  263. }