2018-02-26 12:21:41 -05:00
|
|
|
# frozen_string_literal: true
|
2013-10-29 10:39:46 -04:00
|
|
|
module API
|
2020-06-29 17:09:07 -04:00
|
|
|
class Services < Grape::API::Instance
|
2019-02-28 11:55:22 -05:00
|
|
|
services = Helpers::ServicesHelpers.services
|
|
|
|
service_classes = Helpers::ServicesHelpers.service_classes
|
2017-02-14 20:52:44 -05:00
|
|
|
|
|
|
|
if Rails.env.development?
|
2018-02-27 11:04:34 -05:00
|
|
|
services['mock-ci'] = [
|
2017-02-14 20:52:44 -05:00
|
|
|
{
|
|
|
|
required: true,
|
|
|
|
name: :mock_service_url,
|
|
|
|
type: String,
|
|
|
|
desc: 'URL to the mock service'
|
|
|
|
}
|
|
|
|
]
|
2018-02-27 11:04:34 -05:00
|
|
|
services['mock-deployment'] = []
|
|
|
|
services['mock-monitoring'] = []
|
2017-02-14 20:52:44 -05:00
|
|
|
|
2019-02-28 11:55:22 -05:00
|
|
|
service_classes += Helpers::ServicesHelpers.development_service_classes
|
2017-02-14 20:52:44 -05:00
|
|
|
end
|
2016-12-05 09:40:53 -05:00
|
|
|
|
2018-02-27 11:04:34 -05:00
|
|
|
SERVICES = services.freeze
|
|
|
|
SERVICE_CLASSES = service_classes.freeze
|
|
|
|
|
2018-02-26 12:21:41 -05:00
|
|
|
SERVICE_CLASSES.each do |service|
|
|
|
|
event_names = service.try(:event_names) || next
|
|
|
|
event_names.each do |event_name|
|
|
|
|
SERVICES[service.to_param.tr("_", "-")] << {
|
|
|
|
required: false,
|
|
|
|
name: event_name.to_sym,
|
|
|
|
type: String,
|
2018-03-16 15:09:35 -04:00
|
|
|
desc: service.event_description(event_name)
|
2018-02-26 12:21:41 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
TRIGGER_SERVICES = {
|
2016-12-05 09:40:53 -05:00
|
|
|
'mattermost-slash-commands' => [
|
|
|
|
{
|
|
|
|
name: :token,
|
|
|
|
type: String,
|
|
|
|
desc: 'The Mattermost token'
|
|
|
|
}
|
2016-12-29 04:48:18 -05:00
|
|
|
],
|
|
|
|
'slack-slash-commands' => [
|
|
|
|
{
|
|
|
|
name: :token,
|
|
|
|
type: String,
|
|
|
|
desc: 'The Slack token'
|
|
|
|
}
|
2016-12-05 09:40:53 -05:00
|
|
|
]
|
|
|
|
}.freeze
|
|
|
|
|
2017-03-15 14:09:24 -04:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
2019-01-16 07:09:29 -05:00
|
|
|
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2016-11-14 09:10:35 -05:00
|
|
|
before { authenticate! }
|
|
|
|
before { authorize_admin_project }
|
|
|
|
|
2016-12-05 09:40:53 -05:00
|
|
|
helpers do
|
|
|
|
def service_attributes(service)
|
|
|
|
service.fields.inject([]) do |arr, hash|
|
|
|
|
arr << hash[:name].to_sym
|
2015-08-26 19:58:49 -04:00
|
|
|
end
|
2016-12-05 09:40:53 -05:00
|
|
|
end
|
|
|
|
end
|
2014-10-14 13:07:34 -04:00
|
|
|
|
2020-01-07 10:07:34 -05:00
|
|
|
desc 'Get all active project services' do
|
|
|
|
success Entities::ProjectServiceBasic
|
|
|
|
end
|
|
|
|
get ":id/services" do
|
|
|
|
services = user_project.services.active
|
|
|
|
|
|
|
|
present services, with: Entities::ProjectServiceBasic
|
|
|
|
end
|
|
|
|
|
2018-02-26 12:21:41 -05:00
|
|
|
SERVICES.each do |service_slug, settings|
|
2016-12-05 09:40:53 -05:00
|
|
|
desc "Set #{service_slug} service for project"
|
|
|
|
params do
|
|
|
|
settings.each do |setting|
|
|
|
|
if setting[:required]
|
|
|
|
requires setting[:name], type: setting[:type], desc: setting[:desc]
|
|
|
|
else
|
|
|
|
optional setting[:name], type: setting[:type], desc: setting[:desc]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
put ":id/services/#{service_slug}" do
|
|
|
|
service = user_project.find_or_initialize_service(service_slug.underscore)
|
|
|
|
service_params = declared_params(include_missing: false).merge(active: true)
|
2014-10-14 13:07:34 -04:00
|
|
|
|
2018-07-02 06:43:06 -04:00
|
|
|
if service.update(service_params)
|
2018-01-06 01:18:13 -05:00
|
|
|
present service, with: Entities::ProjectService
|
2015-08-26 19:58:49 -04:00
|
|
|
else
|
2016-12-05 09:40:53 -05:00
|
|
|
render_api_error!('400 Bad Request', 400)
|
2015-08-26 19:58:49 -04:00
|
|
|
end
|
2014-10-14 13:07:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-05 09:40:53 -05:00
|
|
|
desc "Delete a service for project"
|
|
|
|
params do
|
2018-02-26 12:21:41 -05:00
|
|
|
requires :service_slug, type: String, values: SERVICES.keys, desc: 'The name of the service'
|
2016-12-05 09:40:53 -05:00
|
|
|
end
|
|
|
|
delete ":id/services/:service_slug" do
|
|
|
|
service = user_project.find_or_initialize_service(params[:service_slug].underscore)
|
2015-09-11 07:38:37 -04:00
|
|
|
|
2017-08-24 04:41:54 -04:00
|
|
|
destroy_conditionally!(service) do
|
|
|
|
attrs = service_attributes(service).inject({}) do |hash, key|
|
|
|
|
hash.merge!(key => nil)
|
|
|
|
end
|
2016-12-05 09:40:53 -05:00
|
|
|
|
2018-07-02 06:43:06 -04:00
|
|
|
unless service.update(attrs.merge(active: false))
|
2017-08-24 04:41:54 -04:00
|
|
|
render_api_error!('400 Bad Request', 400)
|
|
|
|
end
|
2014-10-14 13:07:34 -04:00
|
|
|
end
|
|
|
|
end
|
2015-09-03 09:38:54 -04:00
|
|
|
|
2016-12-05 09:40:53 -05:00
|
|
|
desc 'Get the service settings for project' do
|
|
|
|
success Entities::ProjectService
|
|
|
|
end
|
|
|
|
params do
|
2018-02-26 12:21:41 -05:00
|
|
|
requires :service_slug, type: String, values: SERVICES.keys, desc: 'The name of the service'
|
2016-12-05 09:40:53 -05:00
|
|
|
end
|
|
|
|
get ":id/services/:service_slug" do
|
|
|
|
service = user_project.find_or_initialize_service(params[:service_slug].underscore)
|
2019-12-06 16:07:54 -05:00
|
|
|
present service, with: Entities::ProjectService
|
2015-09-03 09:38:54 -04:00
|
|
|
end
|
2013-10-29 10:39:46 -04:00
|
|
|
end
|
2016-11-14 09:10:35 -05:00
|
|
|
|
2018-02-26 12:21:41 -05:00
|
|
|
TRIGGER_SERVICES.each do |service_slug, settings|
|
2017-01-30 06:29:55 -05:00
|
|
|
helpers do
|
2017-05-31 01:50:53 -04:00
|
|
|
def slash_command_service(project, service_slug, params)
|
2020-04-21 11:21:10 -04:00
|
|
|
project.services.active.find do |service|
|
2017-01-30 06:29:55 -05:00
|
|
|
service.try(:token) == params[:token] && service.to_param == service_slug.underscore
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-05 09:40:53 -05:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
2016-11-17 06:06:45 -05:00
|
|
|
end
|
2019-01-16 07:09:29 -05:00
|
|
|
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2016-12-05 09:40:53 -05:00
|
|
|
desc "Trigger a slash command for #{service_slug}" do
|
|
|
|
detail 'Added in GitLab 8.13'
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
settings.each do |setting|
|
|
|
|
requires setting[:name], type: setting[:type], desc: setting[:desc]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
post ":id/services/#{service_slug.underscore}/trigger" do
|
|
|
|
project = find_project(params[:id])
|
2016-11-14 09:10:35 -05:00
|
|
|
|
2016-12-05 09:40:53 -05:00
|
|
|
# This is not accurate, but done to prevent leakage of the project names
|
|
|
|
not_found!('Service') unless project
|
2016-11-18 05:38:54 -05:00
|
|
|
|
2017-05-31 01:50:53 -04:00
|
|
|
service = slash_command_service(project, service_slug, params)
|
2017-01-30 06:29:55 -05:00
|
|
|
result = service.try(:trigger, params)
|
2016-11-14 09:10:35 -05:00
|
|
|
|
2016-12-05 09:40:53 -05:00
|
|
|
if result
|
|
|
|
status result[:status] || 200
|
|
|
|
present result
|
|
|
|
else
|
|
|
|
not_found!('Service')
|
|
|
|
end
|
2016-11-14 09:10:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-10-29 10:39:46 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
API::Services.prepend_if_ee('EE::API::Services')
|