pipeline_schedules.go 10 KB

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