milestones.go 8.1 KB

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