2017-02-20 14:32:44 -05:00
|
|
|
module API
|
|
|
|
module V3
|
|
|
|
class Triggers < Grape::API
|
|
|
|
include PaginationParams
|
|
|
|
|
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
2017-03-15 14:09:24 -04:00
|
|
|
resource :projects, requirements: { id: %r{[^/]+} } do
|
2017-03-05 12:49:30 -05:00
|
|
|
desc 'Trigger a GitLab project build' do
|
|
|
|
success ::API::V3::Entities::TriggerRequest
|
|
|
|
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
|
2017-03-20 08:49:31 -04:00
|
|
|
post ":id/(ref/:ref/)trigger/builds", requirements: { ref: /.+/ } do
|
2017-03-05 12:49:30 -05:00
|
|
|
# validate variables
|
2017-08-24 04:08:32 -04:00
|
|
|
params[:variables] = params[:variables].to_h
|
|
|
|
unless params[:variables].all? { |key, value| key.is_a?(String) && value.is_a?(String) }
|
2017-03-05 12:49:30 -05:00
|
|
|
render_api_error!('variables needs to be a map of key-valued strings', 400)
|
|
|
|
end
|
|
|
|
|
2017-08-25 15:04:57 -04:00
|
|
|
project = find_project(params[:id])
|
|
|
|
not_found! unless project
|
|
|
|
|
|
|
|
result = Ci::PipelineTriggerService.new(project, nil, params).execute
|
2017-08-24 04:08:32 -04:00
|
|
|
not_found! unless result
|
2017-06-06 08:23:19 -04:00
|
|
|
|
2017-08-24 04:08:32 -04:00
|
|
|
if result[:http_status]
|
|
|
|
render_api_error!(result[:message], result[:http_status])
|
2017-03-05 12:49:30 -05:00
|
|
|
else
|
2017-08-24 04:08:32 -04:00
|
|
|
pipeline = result[:pipeline]
|
|
|
|
|
2017-08-28 10:29:28 -04:00
|
|
|
# We switched to Ci::PipelineVariable from Ci::TriggerRequest.variables.
|
2017-08-24 04:08:32 -04:00
|
|
|
# Ci::TriggerRequest doesn't save variables anymore.
|
2017-08-28 10:29:28 -04:00
|
|
|
# Here is copying Ci::PipelineVariable to Ci::TriggerRequest.variables for presenting the variables.
|
|
|
|
# The same endpoint in v4 API pressents Pipeline instead of TriggerRequest, so it doesn't need such a process.
|
2017-09-04 12:22:57 -04:00
|
|
|
trigger_request = pipeline.trigger_requests.last
|
2017-08-31 11:17:56 -04:00
|
|
|
trigger_request.variables = params[:variables]
|
2017-08-24 04:08:32 -04:00
|
|
|
|
|
|
|
present trigger_request, with: ::API::V3::Entities::TriggerRequest
|
2017-03-05 12:49:30 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Get triggers list' do
|
|
|
|
success ::API::V3::Entities::Trigger
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
use :pagination
|
|
|
|
end
|
|
|
|
get ':id/triggers' do
|
|
|
|
authenticate!
|
|
|
|
authorize! :admin_build, user_project
|
|
|
|
|
|
|
|
triggers = user_project.triggers.includes(:trigger_requests)
|
|
|
|
|
|
|
|
present paginate(triggers), with: ::API::V3::Entities::Trigger
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Get specific trigger of a project' do
|
|
|
|
success ::API::V3::Entities::Trigger
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :token, type: String, desc: 'The unique token of trigger'
|
|
|
|
end
|
|
|
|
get ':id/triggers/:token' do
|
|
|
|
authenticate!
|
|
|
|
authorize! :admin_build, user_project
|
|
|
|
|
|
|
|
trigger = user_project.triggers.find_by(token: params[:token].to_s)
|
|
|
|
return not_found!('Trigger') unless trigger
|
|
|
|
|
|
|
|
present trigger, with: ::API::V3::Entities::Trigger
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Create a trigger' do
|
|
|
|
success ::API::V3::Entities::Trigger
|
|
|
|
end
|
|
|
|
post ':id/triggers' do
|
|
|
|
authenticate!
|
|
|
|
authorize! :admin_build, user_project
|
|
|
|
|
|
|
|
trigger = user_project.triggers.create
|
|
|
|
|
|
|
|
present trigger, with: ::API::V3::Entities::Trigger
|
|
|
|
end
|
|
|
|
|
2017-02-20 14:32:44 -05:00
|
|
|
desc 'Delete a trigger' do
|
2017-03-05 12:49:30 -05:00
|
|
|
success ::API::V3::Entities::Trigger
|
2017-02-20 14:32:44 -05:00
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :token, type: String, desc: 'The unique token of trigger'
|
|
|
|
end
|
|
|
|
delete ':id/triggers/:token' do
|
|
|
|
authenticate!
|
|
|
|
authorize! :admin_build, user_project
|
|
|
|
|
|
|
|
trigger = user_project.triggers.find_by(token: params[:token].to_s)
|
|
|
|
return not_found!('Trigger') unless trigger
|
|
|
|
|
|
|
|
trigger.destroy
|
|
|
|
|
2017-03-05 12:49:30 -05:00
|
|
|
present trigger, with: ::API::V3::Entities::Trigger
|
2017-02-20 14:32:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|