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

150 lines
4.5 KiB
Ruby
Raw Normal View History

2015-12-10 12:04:40 -05:00
module API
class Triggers < Grape::API
2016-12-04 12:11:19 -05:00
include PaginationParams
2016-11-09 10:47:39 -05: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-03-05 12:49:30 -05:00
desc 'Trigger a GitLab project pipeline' do
2017-03-05 14:58:08 -05:00
success Entities::Pipeline
2016-11-09 10:47:39 -05:00
end
params do
requires :ref, type: String, desc: 'The commit sha or name of a branch or tag'
requires :token, type: String, desc: 'The unique token of trigger'
optional :variables, type: Hash, desc: 'The list of variables to be injected into build'
end
post ":id/(ref/:ref/)trigger/pipeline", requirements: { ref: /.+/ } do
Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-ce/issues/42283')
2015-12-10 12:04:40 -05:00
# validate variables
2017-07-26 05:31:09 -04:00
params[:variables] = params[:variables].to_h
unless params[:variables].all? { |key, value| key.is_a?(String) && value.is_a?(String) }
render_api_error!('variables needs to be a map of key-valued strings', 400)
2015-12-10 12:04:40 -05:00
end
2017-07-26 05:31:09 -04:00
project = find_project(params[:id])
not_found! unless project
result = Ci::PipelineTriggerService.new(project, nil, params).execute
not_found! unless result
2017-07-26 05:31:09 -04:00
if result[:http_status]
render_api_error!(result[:message], result[:http_status])
2015-12-10 12:04:40 -05:00
else
2017-07-26 05:31:09 -04:00
present result[:pipeline], with: Entities::Pipeline
2015-12-10 12:04:40 -05:00
end
end
2016-01-04 10:38:32 -05:00
2016-11-09 10:47:39 -05:00
desc 'Get triggers list' do
success Entities::Trigger
end
2016-12-04 12:11:19 -05:00
params do
use :pagination
end
2016-01-04 10:38:32 -05:00
get ':id/triggers' do
authenticate!
authorize! :admin_build, user_project
2016-01-04 10:38:32 -05:00
triggers = user_project.triggers.includes(:trigger_requests)
2016-11-09 10:47:39 -05:00
present paginate(triggers), with: Entities::Trigger
2016-01-04 10:38:32 -05:00
end
2016-01-05 05:27:38 -05:00
2016-11-09 10:47:39 -05:00
desc 'Get specific trigger of a project' do
success Entities::Trigger
end
params do
2017-03-05 12:49:30 -05:00
requires :trigger_id, type: Integer, desc: 'The trigger ID'
2016-11-09 10:47:39 -05:00
end
2017-03-05 12:49:30 -05:00
get ':id/triggers/:trigger_id' do
authenticate!
authorize! :admin_build, user_project
2017-03-05 14:58:08 -05:00
trigger = user_project.triggers.find(params.delete(:trigger_id))
2016-01-13 06:16:27 -05:00
return not_found!('Trigger') unless trigger
2016-01-13 06:16:27 -05:00
present trigger, with: Entities::Trigger
end
2016-11-09 10:47:39 -05:00
desc 'Create a trigger' do
success Entities::Trigger
end
2017-03-05 12:49:30 -05:00
params do
requires :description, type: String, desc: 'The trigger description'
end
2016-01-05 05:44:10 -05:00
post ':id/triggers' do
authenticate!
authorize! :admin_build, user_project
2016-01-05 05:44:10 -05:00
2017-03-05 12:49:30 -05:00
trigger = user_project.triggers.create(
declared_params(include_missing: false).merge(owner: current_user))
if trigger.valid?
present trigger, with: Entities::Trigger
else
render_validation_error!(trigger)
end
end
desc 'Update a trigger' do
success Entities::Trigger
end
params do
requires :trigger_id, type: Integer, desc: 'The trigger ID'
optional :description, type: String, desc: 'The trigger description'
end
2017-03-05 14:58:08 -05:00
put ':id/triggers/:trigger_id' do
2017-03-05 12:49:30 -05:00
authenticate!
authorize! :admin_build, user_project
2017-03-05 14:58:08 -05:00
trigger = user_project.triggers.find(params.delete(:trigger_id))
2017-03-05 12:49:30 -05:00
return not_found!('Trigger') unless trigger
2016-01-05 05:44:10 -05:00
2017-03-05 14:58:08 -05:00
if trigger.update(declared_params(include_missing: false))
present trigger, with: Entities::Trigger
else
render_validation_error!(trigger)
end
2016-01-05 05:44:10 -05:00
end
2017-03-05 12:49:30 -05:00
desc 'Take ownership of trigger' do
success Entities::Trigger
end
params do
requires :trigger_id, type: Integer, desc: 'The trigger ID'
end
post ':id/triggers/:trigger_id/take_ownership' do
2017-03-05 12:49:30 -05:00
authenticate!
authorize! :admin_build, user_project
2017-03-05 14:58:08 -05:00
trigger = user_project.triggers.find(params.delete(:trigger_id))
2017-03-05 12:49:30 -05:00
return not_found!('Trigger') unless trigger
if trigger.update(owner: current_user)
2017-03-05 14:58:08 -05:00
status :ok
2017-03-05 12:49:30 -05:00
present trigger, with: Entities::Trigger
else
render_validation_error!(trigger)
end
end
2016-11-09 10:47:39 -05:00
desc 'Delete a trigger' do
success Entities::Trigger
end
params do
2017-03-05 12:49:30 -05:00
requires :trigger_id, type: Integer, desc: 'The trigger ID'
2016-11-09 10:47:39 -05:00
end
2017-03-05 12:49:30 -05:00
delete ':id/triggers/:trigger_id' do
2016-01-05 05:27:38 -05:00
authenticate!
authorize! :admin_build, user_project
2016-01-05 05:27:38 -05:00
2017-03-05 14:58:08 -05:00
trigger = user_project.triggers.find(params.delete(:trigger_id))
2016-01-05 05:27:38 -05:00
return not_found!('Trigger') unless trigger
2017-03-02 07:14:13 -05:00
destroy_conditionally!(trigger)
2016-01-05 05:27:38 -05:00
end
2015-12-10 12:04:40 -05:00
end
end
end