Commit 3bf44826 authored by Sean McGivern's avatar Sean McGivern

Merge branch '43620-lib-api-services-rb-retains-a-lot-of-memory-and-objects' into 'master'

Fix a performance/memory issue in lib/api/services.rb

Closes #43620

See merge request gitlab-org/gitlab-ce!17369
parents 6c404813 e139d7af
# frozen_string_literal: true
module API module API
class Services < Grape::API class Services < Grape::API
chat_notification_settings = [ CHAT_NOTIFICATION_SETTINGS = [
{ {
required: true, required: true,
name: :webhook, name: :webhook,
...@@ -19,9 +20,9 @@ module API ...@@ -19,9 +20,9 @@ module API
type: String, type: String,
desc: 'The default chat channel' desc: 'The default chat channel'
} }
] ].freeze
chat_notification_flags = [ CHAT_NOTIFICATION_FLAGS = [
{ {
required: false, required: false,
name: :notify_only_broken_pipelines, name: :notify_only_broken_pipelines,
...@@ -34,9 +35,9 @@ module API ...@@ -34,9 +35,9 @@ module API
type: Boolean, type: Boolean,
desc: 'Send notifications only for the default branch' desc: 'Send notifications only for the default branch'
} }
] ].freeze
chat_notification_channels = [ CHAT_NOTIFICATION_CHANNELS = [
{ {
required: false, required: false,
name: :push_channel, name: :push_channel,
...@@ -85,9 +86,9 @@ module API ...@@ -85,9 +86,9 @@ module API
type: String, type: String,
desc: 'The name of the channel to receive wiki_page_events notifications' desc: 'The name of the channel to receive wiki_page_events notifications'
} }
] ].freeze
chat_notification_events = [ CHAT_NOTIFICATION_EVENTS = [
{ {
required: false, required: false,
name: :push_events, name: :push_events,
...@@ -136,9 +137,9 @@ module API ...@@ -136,9 +137,9 @@ module API
type: Boolean, type: Boolean,
desc: 'Enable notifications for wiki_page_events' desc: 'Enable notifications for wiki_page_events'
} }
] ].freeze
services = { SERVICES = {
'asana' => [ 'asana' => [
{ {
required: true, required: true,
...@@ -627,10 +628,10 @@ module API ...@@ -627,10 +628,10 @@ module API
} }
], ],
'slack' => [ 'slack' => [
chat_notification_settings, CHAT_NOTIFICATION_SETTINGS,
chat_notification_flags, CHAT_NOTIFICATION_FLAGS,
chat_notification_channels, CHAT_NOTIFICATION_CHANNELS,
chat_notification_events CHAT_NOTIFICATION_EVENTS
].flatten, ].flatten,
'microsoft-teams' => [ 'microsoft-teams' => [
{ {
...@@ -641,10 +642,10 @@ module API ...@@ -641,10 +642,10 @@ module API
} }
], ],
'mattermost' => [ 'mattermost' => [
chat_notification_settings, CHAT_NOTIFICATION_SETTINGS,
chat_notification_flags, CHAT_NOTIFICATION_FLAGS,
chat_notification_channels, CHAT_NOTIFICATION_CHANNELS,
chat_notification_events CHAT_NOTIFICATION_EVENTS
].flatten, ].flatten,
'teamcity' => [ 'teamcity' => [
{ {
...@@ -672,9 +673,9 @@ module API ...@@ -672,9 +673,9 @@ module API
desc: 'The password of the user' desc: 'The password of the user'
} }
] ]
} }.freeze
service_classes = [ SERVICE_CLASSES = [
AsanaService, AsanaService,
AssemblaService, AssemblaService,
BambooService, BambooService,
...@@ -703,10 +704,10 @@ module API ...@@ -703,10 +704,10 @@ module API
MattermostService, MattermostService,
MicrosoftTeamsService, MicrosoftTeamsService,
TeamcityService TeamcityService
] ].freeze
if Rails.env.development? if Rails.env.development?
services['mock-ci'] = [ SERVICES['mock-ci'] = [
{ {
required: true, required: true,
name: :mock_service_url, name: :mock_service_url,
...@@ -714,17 +715,29 @@ module API ...@@ -714,17 +715,29 @@ module API
desc: 'URL to the mock service' desc: 'URL to the mock service'
} }
] ]
services['mock-deployment'] = [] SERVICES['mock-deployment'] = []
services['mock-monitoring'] = [] SERVICES['mock-monitoring'] = []
service_classes += [ SERVICE_CLASSES += [
MockCiService, MockCiService,
MockDeploymentService, MockDeploymentService,
MockMonitoringService MockMonitoringService
] ]
end end
trigger_services = { 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,
desc: ServicesHelper.service_event_description(event_name)
}
end
end
TRIGGER_SERVICES = {
'mattermost-slash-commands' => [ 'mattermost-slash-commands' => [
{ {
name: :token, name: :token,
...@@ -756,22 +769,9 @@ module API ...@@ -756,22 +769,9 @@ module API
end end
end end
services.each do |service_slug, settings| SERVICES.each do |service_slug, settings|
desc "Set #{service_slug} service for project" desc "Set #{service_slug} service for project"
params do params do
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,
desc: ServicesHelper.service_event_description(event_name)
}
end
end
services.freeze
settings.each do |setting| settings.each do |setting|
if setting[:required] if setting[:required]
requires setting[:name], type: setting[:type], desc: setting[:desc] requires setting[:name], type: setting[:type], desc: setting[:desc]
...@@ -794,7 +794,7 @@ module API ...@@ -794,7 +794,7 @@ module API
desc "Delete a service for project" desc "Delete a service for project"
params do params do
requires :service_slug, type: String, values: services.keys, desc: 'The name of the service' requires :service_slug, type: String, values: SERVICES.keys, desc: 'The name of the service'
end end
delete ":id/services/:service_slug" do delete ":id/services/:service_slug" do
service = user_project.find_or_initialize_service(params[:service_slug].underscore) service = user_project.find_or_initialize_service(params[:service_slug].underscore)
...@@ -814,7 +814,7 @@ module API ...@@ -814,7 +814,7 @@ module API
success Entities::ProjectService success Entities::ProjectService
end end
params do params do
requires :service_slug, type: String, values: services.keys, desc: 'The name of the service' requires :service_slug, type: String, values: SERVICES.keys, desc: 'The name of the service'
end end
get ":id/services/:service_slug" do get ":id/services/:service_slug" do
service = user_project.find_or_initialize_service(params[:service_slug].underscore) service = user_project.find_or_initialize_service(params[:service_slug].underscore)
...@@ -822,7 +822,7 @@ module API ...@@ -822,7 +822,7 @@ module API
end end
end end
trigger_services.each do |service_slug, settings| TRIGGER_SERVICES.each do |service_slug, settings|
helpers do helpers do
def slash_command_service(project, service_slug, params) def slash_command_service(project, service_slug, params)
project.services.active.where(template: false).find do |service| project.services.active.where(template: false).find do |service|
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment