2015-12-10 12:04:40 -05:00
|
|
|
module API
|
|
|
|
class Triggers < Grape::API
|
2016-11-09 10:47:39 -05:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
2015-12-10 12:04:40 -05:00
|
|
|
resource :projects do
|
2016-11-09 10:47:39 -05:00
|
|
|
desc 'Trigger a GitLab project build' do
|
|
|
|
success 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
|
2016-11-07 12:11:54 -05:00
|
|
|
post ":id/(ref/:ref/)trigger/builds" do
|
2016-11-24 07:22:38 -05:00
|
|
|
project = find_project(params[:id])
|
2015-12-10 12:04:40 -05:00
|
|
|
trigger = Ci::Trigger.find_by_token(params[:token].to_s)
|
|
|
|
not_found! unless project && trigger
|
|
|
|
unauthorized! unless trigger.project == project
|
|
|
|
|
|
|
|
# validate variables
|
|
|
|
variables = params[:variables]
|
|
|
|
if variables
|
|
|
|
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)
|
|
|
|
end
|
|
|
|
|
|
|
|
# convert variables from Mash to Hash
|
|
|
|
variables = variables.to_h
|
|
|
|
end
|
|
|
|
|
|
|
|
# create request and trigger builds
|
|
|
|
trigger_request = Ci::CreateTriggerRequestService.new.execute(project, trigger, params[:ref].to_s, variables)
|
|
|
|
if trigger_request
|
|
|
|
present trigger_request, with: Entities::TriggerRequest
|
|
|
|
else
|
|
|
|
errors = 'No builds created'
|
|
|
|
render_api_error!(errors, 400)
|
|
|
|
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-01-04 10:38:32 -05:00
|
|
|
get ':id/triggers' do
|
|
|
|
authenticate!
|
2016-02-01 17:58:04 -05:00
|
|
|
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
|
|
|
|
requires :token, type: String, desc: 'The unique token of trigger'
|
|
|
|
end
|
2016-01-07 12:48:33 -05:00
|
|
|
get ':id/triggers/:token' do
|
2016-01-05 06:25:16 -05:00
|
|
|
authenticate!
|
2016-02-01 17:58:04 -05:00
|
|
|
authorize! :admin_build, user_project
|
2016-01-05 06:25:16 -05:00
|
|
|
|
2016-01-13 06:16:27 -05:00
|
|
|
trigger = user_project.triggers.find_by(token: params[:token].to_s)
|
|
|
|
return not_found!('Trigger') unless trigger
|
2016-01-05 06:25:16 -05:00
|
|
|
|
2016-01-13 06:16:27 -05:00
|
|
|
present trigger, with: Entities::Trigger
|
2016-01-05 06:25:16 -05:00
|
|
|
end
|
|
|
|
|
2016-11-09 10:47:39 -05:00
|
|
|
desc 'Create a trigger' do
|
|
|
|
success Entities::Trigger
|
|
|
|
end
|
2016-01-05 05:44:10 -05:00
|
|
|
post ':id/triggers' do
|
|
|
|
authenticate!
|
2016-02-01 17:58:04 -05:00
|
|
|
authorize! :admin_build, user_project
|
2016-01-05 05:44:10 -05:00
|
|
|
|
2016-01-13 06:16:27 -05:00
|
|
|
trigger = user_project.triggers.create
|
2016-01-05 05:44:10 -05:00
|
|
|
|
|
|
|
present trigger, with: Entities::Trigger
|
|
|
|
end
|
|
|
|
|
2016-11-09 10:47:39 -05:00
|
|
|
desc 'Delete a trigger' do
|
|
|
|
success Entities::Trigger
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :token, type: String, desc: 'The unique token of trigger'
|
|
|
|
end
|
2016-01-07 12:48:33 -05:00
|
|
|
delete ':id/triggers/:token' do
|
2016-01-05 05:27:38 -05:00
|
|
|
authenticate!
|
2016-02-01 17:58:04 -05:00
|
|
|
authorize! :admin_build, user_project
|
2016-01-05 05:27:38 -05:00
|
|
|
|
2016-01-13 06:16:27 -05:00
|
|
|
trigger = user_project.triggers.find_by(token: params[:token].to_s)
|
2016-01-05 05:27:38 -05:00
|
|
|
return not_found!('Trigger') unless trigger
|
|
|
|
|
|
|
|
trigger.destroy
|
|
|
|
|
|
|
|
present trigger, with: Entities::Trigger
|
|
|
|
end
|
2015-12-10 12:04:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|