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

174 lines
5.3 KiB
Ruby
Raw Normal View History

2018-03-27 19:54:05 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
module API
2021-01-03 14:25:43 +05:30
class Services < ::API::Base
2021-01-29 00:20:46 +05:30
feature_category :integrations
2021-09-30 23:02:18 +05:30
integrations = Helpers::IntegrationsHelpers.integrations
integration_classes = Helpers::IntegrationsHelpers.integration_classes
2017-08-17 22:00:37 +05:30
if Rails.env.development?
2021-09-30 23:02:18 +05:30
integrations['mock-ci'] = [
2017-08-17 22:00:37 +05:30
{
required: true,
name: :mock_service_url,
type: String,
desc: 'URL to the mock service'
}
]
2021-09-30 23:02:18 +05:30
integrations['mock-deployment'] = []
integrations['mock-monitoring'] = []
2017-08-17 22:00:37 +05:30
2021-09-30 23:02:18 +05:30
integration_classes += Helpers::IntegrationsHelpers.development_integration_classes
2017-08-17 22:00:37 +05:30
end
2021-09-30 23:02:18 +05:30
INTEGRATIONS = integrations.freeze
2018-03-27 19:54:05 +05:30
2021-09-30 23:02:18 +05:30
integration_classes.each do |integration|
event_names = integration.try(:event_names) || next
2018-03-27 19:54:05 +05:30
event_names.each do |event_name|
2021-09-30 23:02:18 +05:30
INTEGRATIONS[integration.to_param.tr("_", "-")] << {
2018-03-27 19:54:05 +05:30
required: false,
name: event_name.to_sym,
type: String,
2021-09-30 23:02:18 +05:30
desc: IntegrationsHelper.integration_event_description(integration, event_name)
2018-03-27 19:54:05 +05:30
}
end
end
2021-09-30 23:02:18 +05:30
TRIGGER_INTEGRATIONS = {
2017-08-17 22:00:37 +05:30
'mattermost-slash-commands' => [
{
name: :token,
type: String,
desc: 'The Mattermost token'
}
],
'slack-slash-commands' => [
{
name: :token,
type: String,
desc: 'The Slack token'
}
]
}.freeze
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
before { authenticate! }
before { authorize_admin_project }
helpers do
2021-09-30 23:02:18 +05:30
def integration_attributes(integration)
integration.fields.inject([]) do |arr, hash|
2017-08-17 22:00:37 +05:30
arr << hash[:name].to_sym
2015-09-25 12:07:36 +05:30
end
2014-09-02 18:07:02 +05:30
end
end
2021-09-30 23:02:18 +05:30
desc 'Get all active project integrations' do
success Entities::ProjectIntegrationBasic
2020-03-13 15:44:24 +05:30
end
get ":id/services" do
2021-09-30 23:02:18 +05:30
integrations = user_project.integrations.active
2020-03-13 15:44:24 +05:30
2021-09-30 23:02:18 +05:30
present integrations, with: Entities::ProjectIntegrationBasic
2020-03-13 15:44:24 +05:30
end
2021-09-30 23:02:18 +05:30
INTEGRATIONS.each do |slug, settings|
desc "Set #{slug} integration for project"
2017-08-17 22:00:37 +05:30
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
2021-09-30 23:02:18 +05:30
put ":id/services/#{slug}" do
integration = user_project.find_or_initialize_integration(slug.underscore)
params = declared_params(include_missing: false).merge(active: true)
2017-08-17 22:00:37 +05:30
2021-09-30 23:02:18 +05:30
if integration.update(params)
present integration, with: Entities::ProjectIntegration
2015-09-25 12:07:36 +05:30
else
2017-08-17 22:00:37 +05:30
render_api_error!('400 Bad Request', 400)
end
end
end
2021-09-30 23:02:18 +05:30
desc "Delete an integration from a project"
2017-08-17 22:00:37 +05:30
params do
2021-09-30 23:02:18 +05:30
requires :slug, type: String, values: INTEGRATIONS.keys, desc: 'The name of the service'
2017-08-17 22:00:37 +05:30
end
2021-09-30 23:02:18 +05:30
delete ":id/services/:slug" do
integration = user_project.find_or_initialize_integration(params[:slug].underscore)
2017-08-17 22:00:37 +05:30
2021-09-30 23:02:18 +05:30
destroy_conditionally!(integration) do
attrs = integration_attributes(integration).index_with { nil }.merge(active: false)
2017-08-17 22:00:37 +05:30
2021-09-30 23:02:18 +05:30
render_api_error!('400 Bad Request', 400) unless integration.update(attrs)
2017-08-17 22:00:37 +05:30
end
end
2021-09-30 23:02:18 +05:30
desc 'Get the integration settings for a project' do
success Entities::ProjectIntegration
2017-08-17 22:00:37 +05:30
end
params do
2021-09-30 23:02:18 +05:30
requires :slug, type: String, values: INTEGRATIONS.keys, desc: 'The name of the service'
2017-08-17 22:00:37 +05:30
end
2021-09-30 23:02:18 +05:30
get ":id/services/:slug" do
integration = user_project.find_or_initialize_integration(params[:slug].underscore)
2021-06-08 01:23:25 +05:30
not_found!('Service') unless integration&.persisted?
2021-09-30 23:02:18 +05:30
present integration, with: Entities::ProjectIntegration
2017-08-17 22:00:37 +05:30
end
end
2021-09-30 23:02:18 +05:30
TRIGGER_INTEGRATIONS.each do |integration_slug, settings|
2017-08-17 22:00:37 +05:30
helpers do
2021-09-30 23:02:18 +05:30
def slash_command_integration(project, integration_slug, params)
project.integrations.active.find do |integration|
integration.try(:token) == params[:token] && integration.to_param == integration_slug.underscore
2015-09-25 12:07:36 +05:30
end
2015-04-26 12:48:37 +05:30
end
end
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
2021-09-30 23:02:18 +05:30
desc "Trigger a slash command for #{integration_slug}" do
2017-08-17 22:00:37 +05:30
detail 'Added in GitLab 8.13'
end
params do
settings.each do |setting|
requires setting[:name], type: setting[:type], desc: setting[:desc]
end
end
2021-09-30 23:02:18 +05:30
post ":id/services/#{integration_slug.underscore}/trigger" do
2017-08-17 22:00:37 +05:30
project = find_project(params[:id])
# This is not accurate, but done to prevent leakage of the project names
not_found!('Service') unless project
2021-09-30 23:02:18 +05:30
integration = slash_command_integration(project, integration_slug, params)
result = integration.try(:trigger, params)
2017-08-17 22:00:37 +05:30
if result
status result[:status] || 200
present result
else
not_found!('Service')
end
end
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
end
end
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
API::Services.prepend_mod_with('API::Services')