2018-09-29 18:34:47 -04:00
# frozen_string_literal: true
2017-05-11 15:12:04 -04:00
module API
class PipelineSchedules < Grape :: API
include PaginationParams
2017-05-16 10:58:08 -04:00
before { authenticate! }
2017-05-11 15:12:04 -04:00
params do
requires :id , type : String , desc : 'The ID of a project'
end
2018-11-08 07:18:17 -05:00
resource :projects , requirements : API :: NAMESPACE_OR_PROJECT_REQUIREMENTS do
2017-05-24 06:25:13 -04:00
desc 'Get all pipeline schedules' do
2017-05-11 15:12:04 -04:00
success Entities :: PipelineSchedule
end
params do
use :pagination
2017-05-27 09:22:33 -04:00
optional :scope , type : String , values : %w[ active inactive ] ,
desc : 'The scope of pipeline schedules'
2017-05-11 15:12:04 -04:00
end
2018-08-27 11:31:01 -04:00
# rubocop: disable CodeReuse/ActiveRecord
2017-05-11 15:12:04 -04:00
get ':id/pipeline_schedules' do
authorize! :read_pipeline_schedule , user_project
2017-05-27 09:22:33 -04:00
schedules = PipelineSchedulesFinder . new ( user_project ) . execute ( scope : params [ :scope ] )
. preload ( [ :owner , :last_pipeline ] )
present paginate ( schedules ) , with : Entities :: PipelineSchedule
2017-05-11 15:12:04 -04:00
end
2018-08-27 11:31:01 -04:00
# rubocop: enable CodeReuse/ActiveRecord
2017-05-11 15:12:04 -04:00
2017-05-12 12:48:34 -04:00
desc 'Get a single pipeline schedule' do
2017-05-27 08:29:01 -04:00
success Entities :: PipelineScheduleDetails
2017-05-11 15:12:04 -04:00
end
params do
2019-01-16 07:09:29 -05:00
requires :pipeline_schedule_id , type : Integer , desc : 'The pipeline schedule id'
2017-05-11 15:12:04 -04:00
end
get ':id/pipeline_schedules/:pipeline_schedule_id' do
2017-05-27 08:29:01 -04:00
present pipeline_schedule , with : Entities :: PipelineScheduleDetails
2017-05-11 15:12:04 -04:00
end
2017-05-24 06:25:13 -04:00
desc 'Create a new pipeline schedule' do
2017-05-27 08:29:01 -04:00
success Entities :: PipelineScheduleDetails
2017-05-11 15:12:04 -04:00
end
params do
2017-05-12 12:48:34 -04:00
requires :description , type : String , desc : 'The description of pipeline schedule'
2018-09-11 18:02:09 -04:00
requires :ref , type : String , desc : 'The branch/tag name will be triggered' , allow_blank : false
2017-05-12 12:48:34 -04:00
requires :cron , type : String , desc : 'The cron'
2017-05-27 09:22:33 -04: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 15:12:04 -04:00
end
post ':id/pipeline_schedules' do
authorize! :create_pipeline_schedule , user_project
2017-05-12 14:10:16 -04:00
pipeline_schedule = Ci :: CreatePipelineScheduleService
. new ( user_project , current_user , declared_params ( include_missing : false ) )
. execute
2017-05-11 15:12:04 -04:00
2017-05-17 08:26:18 -04:00
if pipeline_schedule . persisted?
2017-05-27 08:29:01 -04:00
present pipeline_schedule , with : Entities :: PipelineScheduleDetails
2017-05-11 15:12:04 -04:00
else
render_validation_error! ( pipeline_schedule )
end
end
2017-05-24 06:25:13 -04:00
desc 'Edit a pipeline schedule' do
2017-05-27 08:29:01 -04:00
success Entities :: PipelineScheduleDetails
2017-05-11 15:12:04 -04:00
end
params do
2017-05-12 12:48:34 -04: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 15:12:04 -04:00
end
put ':id/pipeline_schedules/:pipeline_schedule_id' do
2017-07-07 06:09:35 -04:00
authorize! :update_pipeline_schedule , pipeline_schedule
2017-05-11 15:12:04 -04:00
if pipeline_schedule . update ( declared_params ( include_missing : false ) )
2017-05-27 08:29:01 -04:00
present pipeline_schedule , with : Entities :: PipelineScheduleDetails
2017-05-11 15:12:04 -04:00
else
render_validation_error! ( pipeline_schedule )
end
end
2017-05-24 06:25:13 -04:00
desc 'Take ownership of a pipeline schedule' do
2017-05-27 08:29:01 -04:00
success Entities :: PipelineScheduleDetails
2017-05-11 15:12:04 -04:00
end
params do
2019-01-16 07:09:29 -05:00
requires :pipeline_schedule_id , type : Integer , desc : 'The pipeline schedule id'
2017-05-11 15:12:04 -04:00
end
post ':id/pipeline_schedules/:pipeline_schedule_id/take_ownership' do
2017-07-07 06:09:35 -04:00
authorize! :update_pipeline_schedule , pipeline_schedule
2017-05-11 15:12:04 -04:00
2017-05-12 14:37:09 -04:00
if pipeline_schedule . own! ( current_user )
2017-05-27 08:29:01 -04:00
present pipeline_schedule , with : Entities :: PipelineScheduleDetails
2017-05-11 15:12:04 -04:00
else
render_validation_error! ( pipeline_schedule )
end
end
2017-05-12 12:48:34 -04:00
desc 'Delete a pipeline schedule' do
2017-05-27 08:29:01 -04:00
success Entities :: PipelineScheduleDetails
2017-05-11 15:12:04 -04:00
end
params do
2019-01-16 07:09:29 -05:00
requires :pipeline_schedule_id , type : Integer , desc : 'The pipeline schedule id'
2017-05-11 15:12:04 -04:00
end
delete ':id/pipeline_schedules/:pipeline_schedule_id' do
2017-07-07 06:09:35 -04:00
authorize! :admin_pipeline_schedule , pipeline_schedule
2017-05-11 15:12:04 -04:00
2017-08-24 04:41:54 -04:00
destroy_conditionally! ( pipeline_schedule )
2017-05-11 15:12:04 -04:00
end
2017-08-18 04:25:35 -04:00
2017-08-21 13:21:37 -04:00
desc 'Create a new pipeline schedule variable' do
success Entities :: Variable
end
2017-08-18 04:25:35 -04:00
params do
requires :pipeline_schedule_id , type : Integer , desc : 'The pipeline schedule id'
2017-08-21 13:21:37 -04:00
requires :key , type : String , desc : 'The key of the variable'
requires :value , type : String , desc : 'The value of the variable'
2019-05-06 09:11:42 -04: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 04:25:35 -04:00
end
2017-08-21 13:21:37 -04:00
post ':id/pipeline_schedules/:pipeline_schedule_id/variables' do
authorize! :update_pipeline_schedule , pipeline_schedule
2017-08-18 04:25:35 -04:00
2017-08-21 13:21:37 -04: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 04:25:35 -04:00
end
2017-08-21 13:21:37 -04:00
end
2017-08-18 04:25:35 -04:00
2017-08-21 13:21:37 -04: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 09:11:42 -04: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 13:21:37 -04:00
end
put ':id/pipeline_schedules/:pipeline_schedule_id/variables/:key' do
authorize! :update_pipeline_schedule , pipeline_schedule
2017-08-18 04:25:35 -04:00
2017-08-24 08:51:46 -04:00
if pipeline_schedule_variable . update ( declared_params ( include_missing : false ) )
present pipeline_schedule_variable , with : Entities :: Variable
2017-08-21 13:21:37 -04:00
else
2017-08-24 08:51:46 -04:00
render_validation_error! ( pipeline_schedule_variable )
2017-08-18 04:25:35 -04:00
end
2017-08-21 13:21:37 -04:00
end
2017-08-18 04:25:35 -04:00
2017-08-21 13:21:37 -04: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 04:25:35 -04:00
2017-08-21 13:21:37 -04:00
status :accepted
2017-08-24 08:51:46 -04:00
present pipeline_schedule_variable . destroy , with : Entities :: Variable
2017-08-18 04:25:35 -04:00
end
2017-05-11 15:12:04 -04:00
end
2017-05-18 05:22:41 -04:00
helpers do
2018-08-27 11:31:01 -04:00
# rubocop: disable CodeReuse/ActiveRecord
2017-05-18 05:22:41 -04:00
def pipeline_schedule
@pipeline_schedule || =
2017-08-30 07:40:19 -04:00
user_project
. pipeline_schedules
. preload ( :owner , :last_pipeline )
. find_by ( id : params . delete ( :pipeline_schedule_id ) ) . tap do | pipeline_schedule |
2017-09-04 08:53:19 -04:00
unless can? ( current_user , :read_pipeline_schedule , pipeline_schedule )
2017-08-30 07:40:19 -04:00
not_found! ( 'Pipeline Schedule' )
end
end
2017-08-24 08:51:46 -04:00
end
2018-08-27 11:31:01 -04:00
# rubocop: enable CodeReuse/ActiveRecord
2017-08-24 08:51:46 -04:00
2018-08-27 11:31:01 -04:00
# rubocop: disable CodeReuse/ActiveRecord
2017-08-24 08:51:46 -04:00
def pipeline_schedule_variable
@pipeline_schedule_variable || =
2017-08-30 07:40:19 -04: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 05:22:41 -04:00
end
2018-08-27 11:31:01 -04:00
# rubocop: enable CodeReuse/ActiveRecord
2017-05-18 05:22:41 -04:00
end
2017-05-11 15:12:04 -04:00
end
end