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}
242
243// CreatePipelineScheduleVariable creates a pipeline schedule variable.
244//
245// GitLab API docs:
246// https://docs.gitlab.com/ce/api/pipeline_schedules.html#create-a-new-pipeline-schedule
247func (s *PipelineSchedulesService) CreatePipelineScheduleVariable(pid interface{}, schedule int, opt *CreatePipelineScheduleVariableOptions, options ...OptionFunc) (*PipelineVariable, *Response, error) {
248 project, err := parseID(pid)
249 if err != nil {
250 return nil, nil, err
251 }
252 u := fmt.Sprintf("projects/%s/pipeline_schedules/%d/variables", pathEscape(project), schedule)
253
254 req, err := s.client.NewRequest("POST", u, opt, options)
255 if err != nil {
256 return nil, nil, err
257 }
258
259 p := new(PipelineVariable)
260 resp, err := s.client.Do(req, p)
261 if err != nil {
262 return nil, resp, err
263 }
264
265 return p, resp, err
266}
267
268// EditPipelineScheduleVariableOptions represents the available
269// EditPipelineScheduleVariable() options.
270//
271// GitLab API docs:
272// https://docs.gitlab.com/ce/api/pipeline_schedules.html#edit-a-pipeline-schedule-variable
273type EditPipelineScheduleVariableOptions struct {
274 Value *string `url:"value" json:"value"`
275}
276
277// EditPipelineScheduleVariable creates a pipeline schedule variable.
278//
279// GitLab API docs:
280// https://docs.gitlab.com/ce/api/pipeline_schedules.html#edit-a-pipeline-schedule-variable
281func (s *PipelineSchedulesService) EditPipelineScheduleVariable(pid interface{}, schedule int, key string, opt *EditPipelineScheduleVariableOptions, options ...OptionFunc) (*PipelineVariable, *Response, error) {
282 project, err := parseID(pid)
283 if err != nil {
284 return nil, nil, err
285 }
286 u := fmt.Sprintf("projects/%s/pipeline_schedules/%d/variables/%s", pathEscape(project), schedule, key)
287
288 req, err := s.client.NewRequest("PUT", u, opt, options)
289 if err != nil {
290 return nil, nil, err
291 }
292
293 p := new(PipelineVariable)
294 resp, err := s.client.Do(req, p)
295 if err != nil {
296 return nil, resp, err
297 }
298
299 return p, resp, err
300}
301
302// DeletePipelineScheduleVariable creates a pipeline schedule variable.
303//
304// GitLab API docs:
305// https://docs.gitlab.com/ce/api/pipeline_schedules.html#delete-a-pipeline-schedule-variable
306func (s *PipelineSchedulesService) DeletePipelineScheduleVariable(pid interface{}, schedule int, key string, options ...OptionFunc) (*PipelineVariable, *Response, error) {
307 project, err := parseID(pid)
308 if err != nil {
309 return nil, nil, err
310 }
311 u := fmt.Sprintf("projects/%s/pipeline_schedules/%d/variables/%s", pathEscape(project), schedule, key)
312
313 req, err := s.client.NewRequest("DELETE", u, nil, options)
314 if err != nil {
315 return nil, nil, err
316 }
317
318 p := new(PipelineVariable)
319 resp, err := s.client.Do(req, p)
320 if err != nil {
321 return nil, resp, err
322 }
323
324 return p, resp, err
325}