gitlab-org--gitlab-foss/lib/api/pipeline_schedules.rb

134 lines
4.9 KiB
Ruby
Raw Normal View History

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
2017-08-31 07:44:49 -04:00
resource :projects, requirements: API::PROJECT_ENDPOINT_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
optional :scope, type: String, values: %w[active inactive],
desc: 'The scope of pipeline schedules'
2017-05-11 15:12:04 -04:00
end
get ':id/pipeline_schedules' do
authorize! :read_pipeline_schedule, user_project
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
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
2017-05-12 12:48:34 -04: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
authorize! :read_pipeline_schedule, user_project
not_found!('PipelineSchedule') unless pipeline_schedule
2017-05-11 15:12:04 -04:00
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'
requires :ref, type: String, desc: 'The branch/tag name will be triggered'
requires :cron, type: String, desc: 'The cron'
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
authorize! :read_pipeline_schedule, user_project
2017-05-11 15:12:04 -04:00
not_found!('PipelineSchedule') unless pipeline_schedule
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
2017-05-12 12:48:34 -04: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
authorize! :read_pipeline_schedule, user_project
2017-05-11 15:12:04 -04:00
not_found!('PipelineSchedule') unless pipeline_schedule
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
2017-05-12 12:48:34 -04: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
authorize! :read_pipeline_schedule, user_project
2017-05-11 15:12:04 -04:00
not_found!('PipelineSchedule') unless pipeline_schedule
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
end
helpers do
def pipeline_schedule
@pipeline_schedule ||=
user_project.pipeline_schedules
.preload(:owner, :last_pipeline)
.find_by(id: params.delete(:pipeline_schedule_id))
end
end
2017-05-11 15:12:04 -04:00
end
end