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

132 lines
4.7 KiB
Ruby
Raw Normal View History

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
resource :projects, requirements: { id: %r{[^/]+} } 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
optional :scope, type: String, values: %w[active inactive],
desc: 'The scope of pipeline schedules'
2017-05-11 19:12:04 +00: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 19:12:04 +00:00
end
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
2017-05-12 16:48:34 +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
authorize! :read_pipeline_schedule, user_project
not_found!('PipelineSchedule') unless pipeline_schedule
2017-05-11 19:12:04 +00:00
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'
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 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-05-30 15:06:37 +00:00
authorize! :update_pipeline_schedule, user_project
2017-05-11 19:12:04 +00:00
not_found!('PipelineSchedule') unless 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
2017-05-12 16:48:34 +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-05-30 15:06:37 +00:00
authorize! :update_pipeline_schedule, user_project
2017-05-11 19:12:04 +00:00
not_found!('PipelineSchedule') unless 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
2017-05-12 16:48:34 +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
authorize! :admin_pipeline_schedule, user_project
not_found!('PipelineSchedule') unless pipeline_schedule
2017-05-11 19:12:04 +00:00
status :accepted
2017-05-27 12:29:01 +00:00
present pipeline_schedule.destroy, with: Entities::PipelineScheduleDetails
2017-05-11 19:12:04 +00: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 19:12:04 +00:00
end
end