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