2018-09-29 22:34:47 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-10 17:04:40 +00:00
|
|
|
module API
|
2020-04-28 03:09:53 +00:00
|
|
|
class Triggers < Grape::API
|
2016-12-04 17:11:19 +00:00
|
|
|
include PaginationParams
|
|
|
|
|
2020-03-04 21:07:54 +00:00
|
|
|
HTTP_GITLAB_EVENT_HEADER = "HTTP_#{WebHookService::GITLAB_EVENT_HEADER}".underscore.upcase
|
|
|
|
|
2016-11-09 15:47:39 +00:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
2019-01-16 12:09:29 +00:00
|
|
|
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2017-03-05 17:49:30 +00:00
|
|
|
desc 'Trigger a GitLab project pipeline' do
|
2017-03-05 19:58:08 +00:00
|
|
|
success Entities::Pipeline
|
2016-11-09 15:47:39 +00:00
|
|
|
end
|
|
|
|
params do
|
2018-09-11 22:02:09 +00:00
|
|
|
requires :ref, type: String, desc: 'The commit sha or name of a branch or tag', allow_blank: false
|
2019-03-04 13:28:48 +00:00
|
|
|
requires :token, type: String, desc: 'The unique token of trigger or job token'
|
2016-11-09 15:47:39 +00:00
|
|
|
optional :variables, type: Hash, desc: 'The list of variables to be injected into build'
|
|
|
|
end
|
2017-03-20 12:49:31 +00:00
|
|
|
post ":id/(ref/:ref/)trigger/pipeline", requirements: { ref: /.+/ } do
|
2019-09-18 14:02:45 +00:00
|
|
|
Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-foss/issues/42283')
|
2018-01-15 15:21:04 +00:00
|
|
|
|
2020-03-04 21:07:54 +00:00
|
|
|
forbidden! if gitlab_pipeline_hook_request?
|
|
|
|
|
2015-12-10 17:04:40 +00:00
|
|
|
# validate variables
|
2017-07-26 09:31:09 +00:00
|
|
|
params[:variables] = params[:variables].to_h
|
|
|
|
unless params[:variables].all? { |key, value| key.is_a?(String) && value.is_a?(String) }
|
2017-02-24 10:24:40 +00:00
|
|
|
render_api_error!('variables needs to be a map of key-valued strings', 400)
|
2015-12-10 17:04:40 +00:00
|
|
|
end
|
|
|
|
|
2017-07-26 09:31:09 +00:00
|
|
|
project = find_project(params[:id])
|
|
|
|
not_found! unless project
|
|
|
|
|
|
|
|
result = Ci::PipelineTriggerService.new(project, nil, params).execute
|
|
|
|
not_found! unless result
|
2017-06-06 12:23:19 +00:00
|
|
|
|
2017-07-26 09:31:09 +00:00
|
|
|
if result[:http_status]
|
|
|
|
render_api_error!(result[:message], result[:http_status])
|
2015-12-10 17:04:40 +00:00
|
|
|
else
|
2017-07-26 09:31:09 +00:00
|
|
|
present result[:pipeline], with: Entities::Pipeline
|
2015-12-10 17:04:40 +00:00
|
|
|
end
|
|
|
|
end
|
2016-01-04 15:38:32 +00:00
|
|
|
|
2016-11-09 15:47:39 +00:00
|
|
|
desc 'Get triggers list' do
|
|
|
|
success Entities::Trigger
|
|
|
|
end
|
2016-12-04 17:11:19 +00:00
|
|
|
params do
|
|
|
|
use :pagination
|
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-01-04 15:38:32 +00:00
|
|
|
get ':id/triggers' do
|
|
|
|
authenticate!
|
2016-02-01 22:58:04 +00:00
|
|
|
authorize! :admin_build, user_project
|
2016-01-04 15:38:32 +00:00
|
|
|
|
|
|
|
triggers = user_project.triggers.includes(:trigger_requests)
|
|
|
|
|
2018-12-19 13:15:58 +00:00
|
|
|
present paginate(triggers), with: Entities::Trigger, current_user: current_user
|
2016-01-04 15:38:32 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-01-05 10:27:38 +00:00
|
|
|
|
2016-11-09 15:47:39 +00:00
|
|
|
desc 'Get specific trigger of a project' do
|
|
|
|
success Entities::Trigger
|
|
|
|
end
|
|
|
|
params do
|
2019-01-16 12:09:29 +00:00
|
|
|
requires :trigger_id, type: Integer, desc: 'The trigger ID'
|
2016-11-09 15:47:39 +00:00
|
|
|
end
|
2017-03-05 17:49:30 +00:00
|
|
|
get ':id/triggers/:trigger_id' do
|
2016-01-05 11:25:16 +00:00
|
|
|
authenticate!
|
2016-02-01 22:58:04 +00:00
|
|
|
authorize! :admin_build, user_project
|
2016-01-05 11:25:16 +00:00
|
|
|
|
2017-03-05 19:58:08 +00:00
|
|
|
trigger = user_project.triggers.find(params.delete(:trigger_id))
|
2018-04-18 09:19:40 +00:00
|
|
|
break not_found!('Trigger') unless trigger
|
2016-01-05 11:25:16 +00:00
|
|
|
|
2018-12-19 13:15:58 +00:00
|
|
|
present trigger, with: Entities::Trigger, current_user: current_user
|
2016-01-05 11:25:16 +00:00
|
|
|
end
|
|
|
|
|
2016-11-09 15:47:39 +00:00
|
|
|
desc 'Create a trigger' do
|
|
|
|
success Entities::Trigger
|
|
|
|
end
|
2017-03-05 17:49:30 +00:00
|
|
|
params do
|
2019-01-16 12:09:29 +00:00
|
|
|
requires :description, type: String, desc: 'The trigger description'
|
2017-03-05 17:49:30 +00:00
|
|
|
end
|
2016-01-05 10:44:10 +00:00
|
|
|
post ':id/triggers' do
|
|
|
|
authenticate!
|
2016-02-01 22:58:04 +00:00
|
|
|
authorize! :admin_build, user_project
|
2016-01-05 10:44:10 +00:00
|
|
|
|
2017-03-05 17:49:30 +00:00
|
|
|
trigger = user_project.triggers.create(
|
|
|
|
declared_params(include_missing: false).merge(owner: current_user))
|
|
|
|
|
|
|
|
if trigger.valid?
|
2018-12-19 14:51:02 +00:00
|
|
|
present trigger, with: Entities::Trigger, current_user: current_user
|
2017-03-05 17:49:30 +00:00
|
|
|
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 19:58:08 +00:00
|
|
|
put ':id/triggers/:trigger_id' do
|
2017-03-05 17:49:30 +00:00
|
|
|
authenticate!
|
|
|
|
authorize! :admin_build, user_project
|
|
|
|
|
2017-03-05 19:58:08 +00:00
|
|
|
trigger = user_project.triggers.find(params.delete(:trigger_id))
|
2018-04-18 09:19:40 +00:00
|
|
|
break not_found!('Trigger') unless trigger
|
2016-01-05 10:44:10 +00:00
|
|
|
|
2020-03-26 18:08:03 +00:00
|
|
|
authorize! :admin_trigger, trigger
|
|
|
|
|
2017-03-05 19:58:08 +00:00
|
|
|
if trigger.update(declared_params(include_missing: false))
|
2018-12-19 14:51:02 +00:00
|
|
|
present trigger, with: Entities::Trigger, current_user: current_user
|
2017-03-05 19:58:08 +00:00
|
|
|
else
|
|
|
|
render_validation_error!(trigger)
|
|
|
|
end
|
2016-01-05 10:44:10 +00:00
|
|
|
end
|
|
|
|
|
2016-11-09 15:47:39 +00:00
|
|
|
desc 'Delete a trigger' do
|
|
|
|
success Entities::Trigger
|
|
|
|
end
|
|
|
|
params do
|
2019-01-16 12:09:29 +00:00
|
|
|
requires :trigger_id, type: Integer, desc: 'The trigger ID'
|
2016-11-09 15:47:39 +00:00
|
|
|
end
|
2017-03-05 17:49:30 +00:00
|
|
|
delete ':id/triggers/:trigger_id' do
|
2016-01-05 10:27:38 +00:00
|
|
|
authenticate!
|
2016-02-01 22:58:04 +00:00
|
|
|
authorize! :admin_build, user_project
|
2016-01-05 10:27:38 +00:00
|
|
|
|
2017-03-05 19:58:08 +00:00
|
|
|
trigger = user_project.triggers.find(params.delete(:trigger_id))
|
2018-04-18 09:19:40 +00:00
|
|
|
break not_found!('Trigger') unless trigger
|
2016-01-05 10:27:38 +00:00
|
|
|
|
2017-03-02 12:14:13 +00:00
|
|
|
destroy_conditionally!(trigger)
|
2016-01-05 10:27:38 +00:00
|
|
|
end
|
2015-12-10 17:04:40 +00:00
|
|
|
end
|
2020-03-04 21:07:54 +00:00
|
|
|
|
|
|
|
helpers do
|
|
|
|
def gitlab_pipeline_hook_request?
|
|
|
|
request.get_header(HTTP_GITLAB_EVENT_HEADER) == WebHookService.hook_to_event(:pipeline_hooks)
|
|
|
|
end
|
|
|
|
end
|
2015-12-10 17:04:40 +00:00
|
|
|
end
|
|
|
|
end
|