2018-03-27 19:54:05 +05:30
|
|
|
# frozen_string_literal: true
|
2014-09-02 18:07:02 +05:30
|
|
|
module API
|
|
|
|
class Services < Grape::API
|
2019-05-18 00:54:41 +05:30
|
|
|
services = Helpers::ServicesHelpers.services
|
|
|
|
service_classes = Helpers::ServicesHelpers.service_classes
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
if Rails.env.development?
|
|
|
|
services['mock-ci'] = [
|
|
|
|
{
|
|
|
|
required: true,
|
|
|
|
name: :mock_service_url,
|
|
|
|
type: String,
|
|
|
|
desc: 'URL to the mock service'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
services['mock-deployment'] = []
|
|
|
|
services['mock-monitoring'] = []
|
|
|
|
|
2019-05-18 00:54:41 +05:30
|
|
|
service_classes += Helpers::ServicesHelpers.development_service_classes
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
SERVICES = services.freeze
|
|
|
|
SERVICE_CLASSES = service_classes.freeze
|
|
|
|
|
|
|
|
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-05-09 12:01:36 +05:30
|
|
|
desc: service.event_description(event_name)
|
2018-03-27 19:54:05 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
TRIGGER_SERVICES = {
|
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
|
|
|
|
def service_attributes(service)
|
|
|
|
service.fields.inject([]) do |arr, hash|
|
|
|
|
arr << hash[:name].to_sym
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
SERVICES.each do |service_slug, settings|
|
2017-08-17 22:00:37 +05:30
|
|
|
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)
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
if service.update(service_params)
|
2018-03-17 18:26:18 +05:30
|
|
|
present service, with: Entities::ProjectService
|
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
|
|
|
|
|
|
|
|
desc "Delete a service for project"
|
|
|
|
params do
|
2018-03-27 19:54:05 +05:30
|
|
|
requires :service_slug, type: String, values: SERVICES.keys, desc: 'The name of the service'
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
delete ":id/services/:service_slug" do
|
|
|
|
service = user_project.find_or_initialize_service(params[:service_slug].underscore)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
destroy_conditionally!(service) do
|
|
|
|
attrs = service_attributes(service).inject({}) do |hash, key|
|
|
|
|
hash.merge!(key => nil)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
unless service.update(attrs.merge(active: false))
|
2018-03-17 18:26:18 +05:30
|
|
|
render_api_error!('400 Bad Request', 400)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Get the service settings for project' do
|
|
|
|
success Entities::ProjectService
|
|
|
|
end
|
|
|
|
params do
|
2018-03-27 19:54:05 +05:30
|
|
|
requires :service_slug, type: String, values: SERVICES.keys, desc: 'The name of the service'
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
get ":id/services/:service_slug" do
|
|
|
|
service = user_project.find_or_initialize_service(params[:service_slug].underscore)
|
|
|
|
present service, with: Entities::ProjectService, include_passwords: current_user.admin?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
TRIGGER_SERVICES.each do |service_slug, settings|
|
2017-08-17 22:00:37 +05:30
|
|
|
helpers do
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-09-10 17:25:29 +05:30
|
|
|
def slash_command_service(project, service_slug, params)
|
2017-08-17 22:00:37 +05:30
|
|
|
project.services.active.where(template: false).find do |service|
|
|
|
|
service.try(:token) == params[:token] && service.to_param == service_slug.underscore
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2015-04-26 12:48:37 +05:30
|
|
|
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
|
2017-08-17 22:00:37 +05:30
|
|
|
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])
|
|
|
|
|
|
|
|
# This is not accurate, but done to prevent leakage of the project names
|
|
|
|
not_found!('Service') unless project
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
service = slash_command_service(project, service_slug, params)
|
2017-08-17 22:00:37 +05:30
|
|
|
result = service.try(:trigger, params)
|
|
|
|
|
|
|
|
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
|