2018-09-29 22:34:47 +00:00
# frozen_string_literal: true
2017-05-11 19:12:04 +00:00
module API
class PipelineSchedules < Grape :: API
include PaginationParams
2017-05-16 14:58:08 +00:00
before { authenticate! }
2017-05-11 19:12:04 +00:00
params do
requires :id , type : String , desc : 'The ID of a project'
end
2018-11-08 12:18:17 +00:00
resource :projects , requirements : API :: NAMESPACE_OR_PROJECT_REQUIREMENTS do
2017-05-24 10:25:13 +00:00
desc 'Get all pipeline schedules' do
2017-05-11 19:12:04 +00:00
success Entities :: PipelineSchedule
end
params do
use :pagination
2017-05-27 13:22:33 +00:00
optional :scope , type : String , values : %w[ active inactive ] ,
desc : 'The scope of pipeline schedules'
2017-05-11 19:12:04 +00:00
end
2018-08-27 15:31:01 +00:00
# rubocop: disable CodeReuse/ActiveRecord
2017-05-11 19:12:04 +00:00
get ':id/pipeline_schedules' do
authorize! :read_pipeline_schedule , user_project
2017-05-27 13:22:33 +00:00
schedules = PipelineSchedulesFinder . new ( user_project ) . execute ( scope : params [ :scope ] )
. preload ( [ :owner , :last_pipeline ] )
present paginate ( schedules ) , with : Entities :: PipelineSchedule
2017-05-11 19:12:04 +00:00
end
2018-08-27 15:31:01 +00:00
# rubocop: enable CodeReuse/ActiveRecord
2017-05-11 19:12:04 +00:00
2017-05-12 16:48:34 +00:00
desc 'Get a single pipeline schedule' do
2017-05-27 12:29:01 +00:00
success Entities :: PipelineScheduleDetails
2017-05-11 19:12:04 +00:00
end
params do
2019-01-16 12:09:29 +00:00
requires :pipeline_schedule_id , type : Integer , desc : 'The pipeline schedule id'
2017-05-11 19:12:04 +00:00
end
get ':id/pipeline_schedules/:pipeline_schedule_id' do
2017-05-27 12:29:01 +00:00
present pipeline_schedule , with : Entities :: PipelineScheduleDetails
2017-05-11 19:12:04 +00:00
end
2017-05-24 10:25:13 +00:00
desc 'Create a new pipeline schedule' do
2017-05-27 12:29:01 +00:00
success Entities :: PipelineScheduleDetails
2017-05-11 19:12:04 +00:00
end
params do
2017-05-12 16:48:34 +00:00
requires :description , type : String , desc : 'The description of pipeline schedule'
2018-09-11 22:02:09 +00:00
requires :ref , type : String , desc : 'The branch/tag name will be triggered' , allow_blank : false
2017-05-12 16:48:34 +00:00
requires :cron , type : String , desc : 'The cron'
2017-05-27 13:22:33 +00:00
optional :cron_timezone , type : String , default : 'UTC' , desc : 'The timezone'
optional :active , type : Boolean , default : true , desc : 'The activation of pipeline schedule'
2017-05-11 19:12:04 +00:00
end
post ':id/pipeline_schedules' do
authorize! :create_pipeline_schedule , user_project
2017-05-12 18:10:16 +00:00
pipeline_schedule = Ci :: CreatePipelineScheduleService
. new ( user_project , current_user , declared_params ( include_missing : false ) )
. execute
2017-05-11 19:12:04 +00:00
2017-05-17 12:26:18 +00:00
if pipeline_schedule . persisted?
2017-05-27 12:29:01 +00:00
present pipeline_schedule , with : Entities :: PipelineScheduleDetails
2017-05-11 19:12:04 +00:00
else
render_validation_error! ( pipeline_schedule )
end
end
2017-05-24 10:25:13 +00:00
desc 'Edit a pipeline schedule' do
2017-05-27 12:29:01 +00:00
success Entities :: PipelineScheduleDetails
2017-05-11 19:12:04 +00:00
end
params do
2017-05-12 16:48:34 +00:00
requires :pipeline_schedule_id , type : Integer , desc : 'The pipeline schedule id'
optional :description , type : String , desc : 'The description of pipeline schedule'
optional :ref , type : String , desc : 'The branch/tag name will be triggered'
optional :cron , type : String , desc : 'The cron'
optional :cron_timezone , type : String , desc : 'The timezone'
optional :active , type : Boolean , desc : 'The activation of pipeline schedule'
2017-05-11 19:12:04 +00:00
end
put ':id/pipeline_schedules/:pipeline_schedule_id' do
2017-07-07 10:09:35 +00:00
authorize! :update_pipeline_schedule , pipeline_schedule
2017-05-11 19:12:04 +00:00
if pipeline_schedule . update ( declared_params ( include_missing : false ) )
2017-05-27 12:29:01 +00:00
present pipeline_schedule , with : Entities :: PipelineScheduleDetails
2017-05-11 19:12:04 +00:00
else
render_validation_error! ( pipeline_schedule )
end
end
2017-05-24 10:25:13 +00:00
desc 'Take ownership of a pipeline schedule' do
2017-05-27 12:29:01 +00:00
success Entities :: PipelineScheduleDetails
2017-05-11 19:12:04 +00:00
end
params do
2019-01-16 12:09:29 +00:00
requires :pipeline_schedule_id , type : Integer , desc : 'The pipeline schedule id'
2017-05-11 19:12:04 +00:00
end
post ':id/pipeline_schedules/:pipeline_schedule_id/take_ownership' do
2017-07-07 10:09:35 +00:00
authorize! :update_pipeline_schedule , pipeline_schedule
2017-05-11 19:12:04 +00:00
2017-05-12 18:37:09 +00:00
if pipeline_schedule . own! ( current_user )
2017-05-27 12:29:01 +00:00
present pipeline_schedule , with : Entities :: PipelineScheduleDetails
2017-05-11 19:12:04 +00:00
else
render_validation_error! ( pipeline_schedule )
end
end
2017-05-12 16:48:34 +00:00
desc 'Delete a pipeline schedule' do
2017-05-27 12:29:01 +00:00
success Entities :: PipelineScheduleDetails
2017-05-11 19:12:04 +00:00
end
params do
2019-01-16 12:09:29 +00:00
requires :pipeline_schedule_id , type : Integer , desc : 'The pipeline schedule id'
2017-05-11 19:12:04 +00:00
end
delete ':id/pipeline_schedules/:pipeline_schedule_id' do
2017-07-07 10:09:35 +00:00
authorize! :admin_pipeline_schedule , pipeline_schedule
2017-05-11 19:12:04 +00:00
2017-08-24 08:41:54 +00:00
destroy_conditionally! ( pipeline_schedule )
2017-05-11 19:12:04 +00:00
end
2017-08-18 08:25:35 +00:00
2020-02-04 09:08:32 +00:00
desc 'Play a scheduled pipeline immediately' do
detail 'This feature was added in GitLab 12.8'
end
params do
requires :pipeline_schedule_id , type : Integer , desc : 'The pipeline schedule id'
end
post ':id/pipeline_schedules/:pipeline_schedule_id/play' do
authorize! :play_pipeline_schedule , pipeline_schedule
2020-02-14 21:09:08 +00:00
job_id = RunPipelineScheduleWorker # rubocop:disable CodeReuse/Worker
2020-02-04 09:08:32 +00:00
. perform_async ( pipeline_schedule . id , current_user . id )
if job_id
created!
else
render_api_error! ( 'Unable to schedule pipeline run immediately' , 500 )
end
end
2017-08-21 17:21:37 +00:00
desc 'Create a new pipeline schedule variable' do
success Entities :: Variable
end
2017-08-18 08:25:35 +00:00
params do
requires :pipeline_schedule_id , type : Integer , desc : 'The pipeline schedule id'
2017-08-21 17:21:37 +00:00
requires :key , type : String , desc : 'The key of the variable'
requires :value , type : String , desc : 'The value of the variable'
2019-05-06 13:11:42 +00:00
optional :variable_type , type : String , values : Ci :: PipelineScheduleVariable . variable_types . keys , desc : 'The type of variable, must be one of env_var or file. Defaults to env_var'
2017-08-18 08:25:35 +00:00
end
2017-08-21 17:21:37 +00:00
post ':id/pipeline_schedules/:pipeline_schedule_id/variables' do
authorize! :update_pipeline_schedule , pipeline_schedule
2017-08-18 08:25:35 +00:00
2017-08-21 17:21:37 +00:00
variable_params = declared_params ( include_missing : false )
variable = pipeline_schedule . variables . create ( variable_params )
if variable . persisted?
present variable , with : Entities :: Variable
else
render_validation_error! ( variable )
2017-08-18 08:25:35 +00:00
end
2017-08-21 17:21:37 +00:00
end
2017-08-18 08:25:35 +00:00
2017-08-21 17:21:37 +00:00
desc 'Edit a pipeline schedule variable' do
success Entities :: Variable
end
params do
requires :pipeline_schedule_id , type : Integer , desc : 'The pipeline schedule id'
requires :key , type : String , desc : 'The key of the variable'
optional :value , type : String , desc : 'The value of the variable'
2019-05-06 13:11:42 +00:00
optional :variable_type , type : String , values : Ci :: PipelineScheduleVariable . variable_types . keys , desc : 'The type of variable, must be one of env_var or file'
2017-08-21 17:21:37 +00:00
end
put ':id/pipeline_schedules/:pipeline_schedule_id/variables/:key' do
authorize! :update_pipeline_schedule , pipeline_schedule
2017-08-18 08:25:35 +00:00
2017-08-24 12:51:46 +00:00
if pipeline_schedule_variable . update ( declared_params ( include_missing : false ) )
present pipeline_schedule_variable , with : Entities :: Variable
2017-08-21 17:21:37 +00:00
else
2017-08-24 12:51:46 +00:00
render_validation_error! ( pipeline_schedule_variable )
2017-08-18 08:25:35 +00:00
end
2017-08-21 17:21:37 +00:00
end
2017-08-18 08:25:35 +00:00
2017-08-21 17:21:37 +00:00
desc 'Delete a pipeline schedule variable' do
success Entities :: Variable
end
params do
requires :pipeline_schedule_id , type : Integer , desc : 'The pipeline schedule id'
requires :key , type : String , desc : 'The key of the variable'
end
delete ':id/pipeline_schedules/:pipeline_schedule_id/variables/:key' do
authorize! :admin_pipeline_schedule , pipeline_schedule
2017-08-18 08:25:35 +00:00
2017-08-21 17:21:37 +00:00
status :accepted
2017-08-24 12:51:46 +00:00
present pipeline_schedule_variable . destroy , with : Entities :: Variable
2017-08-18 08:25:35 +00:00
end
2017-05-11 19:12:04 +00:00
end
2017-05-18 09:22:41 +00:00
helpers do
2018-08-27 15:31:01 +00:00
# rubocop: disable CodeReuse/ActiveRecord
2017-05-18 09:22:41 +00:00
def pipeline_schedule
@pipeline_schedule || =
2017-08-30 11:40:19 +00:00
user_project
. pipeline_schedules
. preload ( :owner , :last_pipeline )
. find_by ( id : params . delete ( :pipeline_schedule_id ) ) . tap do | pipeline_schedule |
2017-09-04 12:53:19 +00:00
unless can? ( current_user , :read_pipeline_schedule , pipeline_schedule )
2017-08-30 11:40:19 +00:00
not_found! ( 'Pipeline Schedule' )
end
end
2017-08-24 12:51:46 +00:00
end
2018-08-27 15:31:01 +00:00
# rubocop: enable CodeReuse/ActiveRecord
2017-08-24 12:51:46 +00:00
2018-08-27 15:31:01 +00:00
# rubocop: disable CodeReuse/ActiveRecord
2017-08-24 12:51:46 +00:00
def pipeline_schedule_variable
@pipeline_schedule_variable || =
2017-08-30 11:40:19 +00:00
pipeline_schedule . variables . find_by ( key : params [ :key ] ) . tap do | pipeline_schedule_variable |
unless pipeline_schedule_variable
not_found! ( 'Pipeline Schedule Variable' )
end
end
2017-05-18 09:22:41 +00:00
end
2018-08-27 15:31:01 +00:00
# rubocop: enable CodeReuse/ActiveRecord
2017-05-18 09:22:41 +00:00
end
2017-05-11 19:12:04 +00:00
end
end