debian-mirror-gitlab/lib/api/triggers.rb

145 lines
4.6 KiB
Ruby
Raw Normal View History

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