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
17package gitlab
18
19import (
20 "fmt"
21 "time"
22)
23
24// PipelineSchedulesService handles communication with the pipeline
25// schedules related methods of the GitLab API.
26//
27// GitLab API docs: https://docs.gitlab.com/ce/api/pipeline_schedules.html
28type PipelineSchedulesService struct {
29 client *Client
30}
31
32// PipelineSchedule represents a pipeline schedule.
33//
34// GitLab API docs:
35// https://docs.gitlab.com/ce/api/pipeline_schedules.html
36type PipelineSchedule struct {
37 ID int `json:"id"`
38 Description string `json:"description"`
39 Ref string `json:"ref"`
40 Cron string `json:"cron"`
41 CronTimezone string `json:"cron_timezone"`
42 NextRunAt *time.Time `json:"next_run_at"`
43 Active bool `json:"active"`
44 CreatedAt *time.Time `json:"created_at"`
45 UpdatedAt *time.Time `json:"updated_at"`
46 Owner *User `json:"owner"`
47 LastPipeline struct {
48 ID int `json:"id"`
49 SHA string `json:"sha"`
50 Ref string `json:"ref"`
51 Status string `json:"status"`
52 } `json:"last_pipeline"`
53 Variables []*PipelineVariable `json:"variables"`
54}
55
56// ListPipelineSchedulesOptions represents the available ListPipelineTriggers() options.
57//
58// GitLab API docs:
59// https://docs.gitlab.com/ce/api/pipeline_triggers.html#list-project-triggers
60type ListPipelineSchedulesOptions ListOptions
61
62// ListPipelineSchedules gets a list of project triggers.
63//
64// GitLab API docs:
65// https://docs.gitlab.com/ce/api/pipeline_schedules.html
66func (s *PipelineSchedulesService) ListPipelineSchedules(pid interface{}, opt *ListPipelineSchedulesOptions, options ...OptionFunc) ([]*PipelineSchedule, *Response, error) {
67 project, err := parseID(pid)
68 if err != nil {
69 return nil, nil, err
70 }
71 u := fmt.Sprintf("projects/%s/pipeline_schedules", pathEscape(project))
72
73 req, err := s.client.NewRequest("GET", u, opt, options)
74 if err != nil {
75 return nil, nil, err
76 }
77
78 var ps []*PipelineSchedule
79 resp, err := s.client.Do(req, &ps)
80 if err != nil {
81 return nil, resp, err
82 }
83
84 return ps, resp, err
85}
86
87// GetPipelineSchedule gets a pipeline schedule.
88//
89// GitLab API docs:
90// https://docs.gitlab.com/ce/api/pipeline_schedules.html
91func (s *PipelineSchedulesService) GetPipelineSchedule(pid interface{}, schedule int, options ...OptionFunc) (*PipelineSchedule, *Response, error) {
92 project, err := parseID(pid)
93 if err != nil {
94 return nil, nil, err
95 }
96 u := fmt.Sprintf("projects/%s/pipeline_schedules/%d", pathEscape(project), schedule)
97
98 req, err := s.client.NewRequest("GET", u, nil, options)
99 if err != nil {
100 return nil, nil, err
101 }
102
103 p := new(PipelineSchedule)
104 resp, err := s.client.Do(req, p)
105 if err != nil {
106 return nil, resp, err
107 }
108
109 return p, resp, err
110}
111
112// CreatePipelineScheduleOptions represents the available
113// CreatePipelineSchedule() options.
114//
115// GitLab API docs:
116// https://docs.gitlab.com/ce/api/pipeline_schedules.html#create-a-new-pipeline-schedule
117type CreatePipelineScheduleOptions struct {
118 Description *string `url:"description" json:"description"`
119 Ref *string `url:"ref" json:"ref"`
120 Cron *string `url:"cron" json:"cron"`
121 CronTimezone *string `url:"cron_timezone,omitempty" json:"cron_timezone,omitempty"`
122 Active *bool `url:"active,omitempty" json:"active,omitempty"`
123}
124
125// CreatePipelineSchedule creates a pipeline schedule.
126//
127// GitLab API docs:
128// https://docs.gitlab.com/ce/api/pipeline_schedules.html#create-a-new-pipeline-schedule
129func (s *PipelineSchedulesService) CreatePipelineSchedule(pid interface{}, opt *CreatePipelineScheduleOptions, options ...OptionFunc) (*PipelineSchedule, *Response, error) {
130 project, err := parseID(pid)
131 if err != nil {
132 return nil, nil, err
133 }
134 u := fmt.Sprintf("projects/%s/pipeline_schedules", 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 p := new(PipelineSchedule)
142 resp, err := s.client.Do(req, p)
143 if err != nil {
144 return nil, resp, err
145 }
146
147 return p, resp, err
148}
149
150// EditPipelineScheduleOptions represents the available
151// EditPipelineSchedule() options.
152//
153// GitLab API docs:
154// https://docs.gitlab.com/ce/api/pipeline_schedules.html#create-a-new-pipeline-schedule
155type EditPipelineScheduleOptions struct {
156 Description *string `url:"description,omitempty" json:"description,omitempty"`
157 Ref *string `url:"ref,omitempty" json:"ref,omitempty"`
158 Cron *string `url:"cron,omitempty" json:"cron,omitempty"`
159 CronTimezone *string `url:"cron_timezone,omitempty" json:"cron_timezone,omitempty"`
160 Active *bool `url:"active,omitempty" json:"active,omitempty"`
161}
162
163// EditPipelineSchedule edits a pipeline schedule.
164//
165// GitLab API docs:
166// https://docs.gitlab.com/ce/api/pipeline_schedules.html#edit-a-pipeline-schedule
167func (s *PipelineSchedulesService) EditPipelineSchedule(pid interface{}, schedule int, opt *EditPipelineScheduleOptions, options ...OptionFunc) (*PipelineSchedule, *Response, error) {
168 project, err := parseID(pid)
169 if err != nil {
170 return nil, nil, err
171 }
172 u := fmt.Sprintf("projects/%s/pipeline_schedules/%d", pathEscape(project), schedule)
173
174 req, err := s.client.NewRequest("PUT", u, opt, options)
175 if err != nil {
176 return nil, nil, err
177 }
178
179 p := new(PipelineSchedule)
180 resp, err := s.client.Do(req, p)
181 if err != nil {
182 return nil, resp, err
183 }
184
185 return p, resp, err
186}
187
188// TakeOwnershipOfPipelineSchedule sets the owner of the specified
189// pipeline schedule to the user issuing the request.
190//
191// GitLab API docs:
192// https://docs.gitlab.com/ce/api/pipeline_schedules.html#take-ownership-of-a-pipeline-schedule
193func (s *PipelineSchedulesService) TakeOwnershipOfPipelineSchedule(pid interface{}, schedule int, options ...OptionFunc) (*PipelineSchedule, *Response, error) {
194 project, err := parseID(pid)
195 if err != nil {
196 return nil, nil, err
197 }
198 u := fmt.Sprintf("projects/%s/pipeline_schedules/%d/take_ownership", pathEscape(project), schedule)
199
200 req, err := s.client.NewRequest("POST", u, nil, options)
201 if err != nil {
202 return nil, nil, err
203 }
204
205 p := new(PipelineSchedule)
206 resp, err := s.client.Do(req, p)
207 if err != nil {
208 return nil, resp, err
209 }
210
211 return p, resp, err
212}
213
214// DeletePipelineSchedule deletes a pipeline schedule.
215//
216// GitLab API docs:
217// https://docs.gitlab.com/ce/api/pipeline_schedules.html#delete-a-pipeline-schedule
218func (s *PipelineSchedulesService) DeletePipelineSchedule(pid interface{}, schedule int, options ...OptionFunc) (*Response, error) {
219 project, err := parseID(pid)
220 if err != nil {
221 return nil, err
222 }
223 u := fmt.Sprintf("projects/%s/pipeline_schedules/%d", pathEscape(project), schedule)
224
225 req, err := s.client.NewRequest("DELETE", u, nil, options)
226 if err != nil {
227 return nil, err
228 }
229
230 return s.client.Do(req, nil)
231}
232
233// CreatePipelineScheduleVariableOptions represents the available
234// CreatePipelineScheduleVariable() options.
235//
236// GitLab API docs:
237// https://docs.gitlab.com/ce/api/pipeline_schedules.html#create-a-new-pipeline-schedule
238type CreatePipelineScheduleVariableOptions struct {
239 Key *string `url:"key" json:"key"`
240 Value *string `url:"value" json:"value"`
241 VariableType *string `url:"variable_type,omitempty" json:"variable_type,omitempty"`
242}
243
244// CreatePipelineScheduleVariable creates a pipeline schedule variable.
245//
246// GitLab API docs:
247// https://docs.gitlab.com/ce/api/pipeline_schedules.html#create-a-new-pipeline-schedule
248func (s *PipelineSchedulesService) CreatePipelineScheduleVariable(pid interface{}, schedule int, opt *CreatePipelineScheduleVariableOptions, options ...OptionFunc) (*PipelineVariable, *Response, error) {
249 project, err := parseID(pid)
250 if err != nil {
251 return nil, nil, err
252 }
253 u := fmt.Sprintf("projects/%s/pipeline_schedules/%d/variables", pathEscape(project), schedule)
254
255 req, err := s.client.NewRequest("POST", u, opt, options)
256 if err != nil {
257 return nil, nil, err
258 }
259
260 p := new(PipelineVariable)
261 resp, err := s.client.Do(req, p)
262 if err != nil {
263 return nil, resp, err
264 }
265
266 return p, resp, err
267}
268
269// EditPipelineScheduleVariableOptions represents the available
270// EditPipelineScheduleVariable() options.
271//
272// GitLab API docs:
273// https://docs.gitlab.com/ce/api/pipeline_schedules.html#edit-a-pipeline-schedule-variable
274type EditPipelineScheduleVariableOptions struct {
275 Value *string `url:"value" json:"value"`
276 VariableType *string `url:"variable_type,omitempty" json:"variable_type,omitempty"`
277}
278
279// EditPipelineScheduleVariable creates a pipeline schedule variable.
280//
281// GitLab API docs:
282// https://docs.gitlab.com/ce/api/pipeline_schedules.html#edit-a-pipeline-schedule-variable
283func (s *PipelineSchedulesService) EditPipelineScheduleVariable(pid interface{}, schedule int, key string, opt *EditPipelineScheduleVariableOptions, options ...OptionFunc) (*PipelineVariable, *Response, error) {
284 project, err := parseID(pid)
285 if err != nil {
286 return nil, nil, err
287 }
288 u := fmt.Sprintf("projects/%s/pipeline_schedules/%d/variables/%s", pathEscape(project), schedule, key)
289
290 req, err := s.client.NewRequest("PUT", u, opt, options)
291 if err != nil {
292 return nil, nil, err
293 }
294
295 p := new(PipelineVariable)
296 resp, err := s.client.Do(req, p)
297 if err != nil {
298 return nil, resp, err
299 }
300
301 return p, resp, err
302}
303
304// DeletePipelineScheduleVariable creates a pipeline schedule variable.
305//
306// GitLab API docs:
307// https://docs.gitlab.com/ce/api/pipeline_schedules.html#delete-a-pipeline-schedule-variable
308func (s *PipelineSchedulesService) DeletePipelineScheduleVariable(pid interface{}, schedule int, key string, options ...OptionFunc) (*PipelineVariable, *Response, error) {
309 project, err := parseID(pid)
310 if err != nil {
311 return nil, nil, err
312 }
313 u := fmt.Sprintf("projects/%s/pipeline_schedules/%d/variables/%s", pathEscape(project), schedule, key)
314
315 req, err := s.client.NewRequest("DELETE", u, nil, options)
316 if err != nil {
317 return nil, nil, err
318 }
319
320 p := new(PipelineVariable)
321 resp, err := s.client.Do(req, p)
322 if err != nil {
323 return nil, resp, err
324 }
325
326 return p, resp, err
327}