group_milestones.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //
  2. // Copyright 2018, 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. "time"
  21. )
  22. // GroupMilestonesService handles communication with the milestone related
  23. // methods of the GitLab API.
  24. //
  25. // GitLab API docs: https://docs.gitlab.com/ce/api/group_milestones.html
  26. type GroupMilestonesService struct {
  27. client *Client
  28. }
  29. // GroupMilestone represents a GitLab milestone.
  30. //
  31. // GitLab API docs: https://docs.gitlab.com/ce/api/group_milestones.html
  32. type GroupMilestone struct {
  33. ID int `json:"id"`
  34. IID int `json:"iid"`
  35. GroupID int `json:"group_id"`
  36. Title string `json:"title"`
  37. Description string `json:"description"`
  38. StartDate *ISOTime `json:"start_date"`
  39. DueDate *ISOTime `json:"due_date"`
  40. State string `json:"state"`
  41. UpdatedAt *time.Time `json:"updated_at"`
  42. CreatedAt *time.Time `json:"created_at"`
  43. }
  44. func (m GroupMilestone) String() string {
  45. return Stringify(m)
  46. }
  47. // ListGroupMilestonesOptions represents the available
  48. // ListGroupMilestones() options.
  49. //
  50. // GitLab API docs:
  51. // https://docs.gitlab.com/ce/api/group_milestones.html#list-group-milestones
  52. type ListGroupMilestonesOptions struct {
  53. ListOptions
  54. IIDs []int `url:"iids,omitempty" json:"iids,omitempty"`
  55. State string `url:"state,omitempty" json:"state,omitempty"`
  56. Search string `url:"search,omitempty" json:"search,omitempty"`
  57. }
  58. // ListGroupMilestones returns a list of group milestones.
  59. //
  60. // GitLab API docs:
  61. // https://docs.gitlab.com/ce/api/group_milestones.html#list-group-milestones
  62. func (s *GroupMilestonesService) ListGroupMilestones(gid interface{}, opt *ListGroupMilestonesOptions, options ...OptionFunc) ([]*GroupMilestone, *Response, error) {
  63. group, err := parseID(gid)
  64. if err != nil {
  65. return nil, nil, err
  66. }
  67. u := fmt.Sprintf("groups/%s/milestones", url.QueryEscape(group))
  68. req, err := s.client.NewRequest("GET", u, opt, options)
  69. if err != nil {
  70. return nil, nil, err
  71. }
  72. var m []*GroupMilestone
  73. resp, err := s.client.Do(req, &m)
  74. if err != nil {
  75. return nil, resp, err
  76. }
  77. return m, resp, err
  78. }
  79. // GetGroupMilestone gets a single group milestone.
  80. //
  81. // GitLab API docs:
  82. // https://docs.gitlab.com/ce/api/group_milestones.html#get-single-milestone
  83. func (s *GroupMilestonesService) GetGroupMilestone(gid interface{}, milestone int, options ...OptionFunc) (*GroupMilestone, *Response, error) {
  84. group, err := parseID(gid)
  85. if err != nil {
  86. return nil, nil, err
  87. }
  88. u := fmt.Sprintf("groups/%s/milestones/%d", url.QueryEscape(group), milestone)
  89. req, err := s.client.NewRequest("GET", u, nil, options)
  90. if err != nil {
  91. return nil, nil, err
  92. }
  93. m := new(GroupMilestone)
  94. resp, err := s.client.Do(req, m)
  95. if err != nil {
  96. return nil, resp, err
  97. }
  98. return m, resp, err
  99. }
  100. // CreateGroupMilestoneOptions represents the available CreateGroupMilestone() options.
  101. //
  102. // GitLab API docs:
  103. // https://docs.gitlab.com/ce/api/group_milestones.html#create-new-milestone
  104. type CreateGroupMilestoneOptions struct {
  105. Title *string `url:"title,omitempty" json:"title,omitempty"`
  106. Description *string `url:"description,omitempty" json:"description,omitempty"`
  107. StartDate *ISOTime `url:"start_date,omitempty" json:"start_date,omitempty"`
  108. DueDate *ISOTime `url:"due_date,omitempty" json:"due_date,omitempty"`
  109. }
  110. // CreateGroupMilestone creates a new group milestone.
  111. //
  112. // GitLab API docs:
  113. // https://docs.gitlab.com/ce/api/group_milestones.html#create-new-milestone
  114. func (s *GroupMilestonesService) CreateGroupMilestone(gid interface{}, opt *CreateGroupMilestoneOptions, options ...OptionFunc) (*GroupMilestone, *Response, error) {
  115. group, err := parseID(gid)
  116. if err != nil {
  117. return nil, nil, err
  118. }
  119. u := fmt.Sprintf("groups/%s/milestones", url.QueryEscape(group))
  120. req, err := s.client.NewRequest("POST", u, opt, options)
  121. if err != nil {
  122. return nil, nil, err
  123. }
  124. m := new(GroupMilestone)
  125. resp, err := s.client.Do(req, m)
  126. if err != nil {
  127. return nil, resp, err
  128. }
  129. return m, resp, err
  130. }
  131. // UpdateGroupMilestoneOptions represents the available UpdateGroupMilestone() options.
  132. //
  133. // GitLab API docs:
  134. // https://docs.gitlab.com/ce/api/group_milestones.html#edit-milestone
  135. type UpdateGroupMilestoneOptions struct {
  136. Title *string `url:"title,omitempty" json:"title,omitempty"`
  137. Description *string `url:"description,omitempty" json:"description,omitempty"`
  138. StartDate *ISOTime `url:"start_date,omitempty" json:"start_date,omitempty"`
  139. DueDate *ISOTime `url:"due_date,omitempty" json:"due_date,omitempty"`
  140. StateEvent *string `url:"state_event,omitempty" json:"state_event,omitempty"`
  141. }
  142. // UpdateGroupMilestone updates an existing group milestone.
  143. //
  144. // GitLab API docs:
  145. // https://docs.gitlab.com/ce/api/group_milestones.html#edit-milestone
  146. func (s *GroupMilestonesService) UpdateGroupMilestone(gid interface{}, milestone int, opt *UpdateGroupMilestoneOptions, options ...OptionFunc) (*GroupMilestone, *Response, error) {
  147. group, err := parseID(gid)
  148. if err != nil {
  149. return nil, nil, err
  150. }
  151. u := fmt.Sprintf("groups/%s/milestones/%d", url.QueryEscape(group), milestone)
  152. req, err := s.client.NewRequest("PUT", u, opt, options)
  153. if err != nil {
  154. return nil, nil, err
  155. }
  156. m := new(GroupMilestone)
  157. resp, err := s.client.Do(req, m)
  158. if err != nil {
  159. return nil, resp, err
  160. }
  161. return m, resp, err
  162. }
  163. // GetGroupMilestoneIssuesOptions represents the available GetGroupMilestoneIssues() options.
  164. //
  165. // GitLab API docs:
  166. // https://docs.gitlab.com/ce/api/group_milestones.html#get-all-issues-assigned-to-a-single-milestone
  167. type GetGroupMilestoneIssuesOptions ListOptions
  168. // GetGroupMilestoneIssues gets all issues assigned to a single group milestone.
  169. //
  170. // GitLab API docs:
  171. // https://docs.gitlab.com/ce/api/group_milestones.html#get-all-issues-assigned-to-a-single-milestone
  172. func (s *GroupMilestonesService) GetGroupMilestoneIssues(gid interface{}, milestone int, opt *GetGroupMilestoneIssuesOptions, options ...OptionFunc) ([]*Issue, *Response, error) {
  173. group, err := parseID(gid)
  174. if err != nil {
  175. return nil, nil, err
  176. }
  177. u := fmt.Sprintf("groups/%s/milestones/%d/issues", url.QueryEscape(group), milestone)
  178. req, err := s.client.NewRequest("GET", u, opt, options)
  179. if err != nil {
  180. return nil, nil, err
  181. }
  182. var i []*Issue
  183. resp, err := s.client.Do(req, &i)
  184. if err != nil {
  185. return nil, resp, err
  186. }
  187. return i, resp, err
  188. }
  189. // GetGroupMilestoneMergeRequestsOptions represents the available
  190. // GetGroupMilestoneMergeRequests() options.
  191. //
  192. // GitLab API docs:
  193. // https://docs.gitlab.com/ce/api/group_milestones.html#get-all-merge-requests-assigned-to-a-single-milestone
  194. type GetGroupMilestoneMergeRequestsOptions ListOptions
  195. // GetGroupMilestoneMergeRequests gets all merge requests assigned to a
  196. // single group milestone.
  197. //
  198. // GitLab API docs:
  199. // https://docs.gitlab.com/ce/api/group_milestones.html#get-all-merge-requests-assigned-to-a-single-milestone
  200. func (s *GroupMilestonesService) GetGroupMilestoneMergeRequests(gid interface{}, milestone int, opt *GetGroupMilestoneMergeRequestsOptions, options ...OptionFunc) ([]*MergeRequest, *Response, error) {
  201. group, err := parseID(gid)
  202. if err != nil {
  203. return nil, nil, err
  204. }
  205. u := fmt.Sprintf("groups/%s/milestones/%d/merge_requests", url.QueryEscape(group), milestone)
  206. req, err := s.client.NewRequest("GET", u, opt, options)
  207. if err != nil {
  208. return nil, nil, err
  209. }
  210. var mr []*MergeRequest
  211. resp, err := s.client.Do(req, &mr)
  212. if err != nil {
  213. return nil, resp, err
  214. }
  215. return mr, resp, err
  216. }