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

187 lines
6.9 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
2017-08-31 11:44:49 +00:00
resource :projects, requirements: API::PROJECT_ENDPOINT_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
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
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
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
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
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
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, 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
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'
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'
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
helpers do
def pipeline_schedule
@pipeline_schedule ||=
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)
not_found!('Pipeline Schedule')
end
end
2017-08-24 12:51:46 +00:00
end
def pipeline_schedule_variable
@pipeline_schedule_variable ||=
pipeline_schedule.variables.find_by(key: params[:key]).tap do |pipeline_schedule_variable|
unless pipeline_schedule_variable
not_found!('Pipeline Schedule Variable')
end
end
end
end
2017-05-11 19:12:04 +00:00
end
end