2015-09-25 12:07:36 +05:30
|
|
|
module Ci
|
|
|
|
module API
|
|
|
|
class Triggers < Grape::API
|
|
|
|
resource :projects do
|
2017-08-17 22:00:37 +05:30
|
|
|
desc 'Trigger a GitLab CI project build' do
|
|
|
|
success Entities::TriggerRequest
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :id, type: Integer, desc: 'The ID of a CI project'
|
|
|
|
requires :ref, type: String, desc: "The name of project's branch or tag"
|
|
|
|
requires :token, type: String, desc: 'The unique token of the trigger'
|
|
|
|
optional :variables, type: Hash, desc: 'Optional build variables'
|
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
post ":id/refs/:ref/trigger" do
|
2017-08-17 22:00:37 +05:30
|
|
|
project = Project.find_by(ci_id: params[:id])
|
|
|
|
trigger = Ci::Trigger.find_by_token(params[:token])
|
2015-09-25 12:07:36 +05:30
|
|
|
not_found! unless project && trigger
|
|
|
|
unauthorized! unless trigger.project == project
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
# Validate variables
|
|
|
|
variables = params[:variables].to_h
|
|
|
|
unless 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-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# create request and trigger builds
|
2017-08-17 22:00:37 +05:30
|
|
|
trigger_request = Ci::CreateTriggerRequestService.new.execute(project, trigger, params[:ref], variables)
|
2015-09-25 12:07:36 +05:30
|
|
|
if trigger_request
|
|
|
|
present trigger_request, with: Entities::TriggerRequest
|
|
|
|
else
|
|
|
|
errors = 'No builds created'
|
|
|
|
render_api_error!(errors, 400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|