From 9be0c2734ae3e3cc84196cf167a0c327c7cf8570 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Thu, 8 Feb 2018 17:51:02 +0200 Subject: [PATCH] Add external plugins support to extend system hooks Signed-off-by: Dmitriy Zaporozhets --- app/services/system_hooks_service.rb | 9 +++++++++ config/application.rb | 1 + config/initializers/9_plugins.rb | 29 ++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 config/initializers/9_plugins.rb diff --git a/app/services/system_hooks_service.rb b/app/services/system_hooks_service.rb index a6b7a6e1416..71de74e5a82 100644 --- a/app/services/system_hooks_service.rb +++ b/app/services/system_hooks_service.rb @@ -11,6 +11,15 @@ class SystemHooksService SystemHook.hooks_for(hooks_scope).find_each do |hook| hook.async_execute(data, 'system_hooks') end + + # Execute external plugins + PLUGINS.each do |plugin| + begin + plugin.new.execute(data) + rescue => e + Rails.logger.warn("GitLab -> Plugins -> #{plugin.class.name} raised an axception during execution. #{e}") + end + end end private diff --git a/config/application.rb b/config/application.rb index 918bd4d57cf..f2fc6270748 100644 --- a/config/application.rb +++ b/config/application.rb @@ -28,6 +28,7 @@ module Gitlab config.eager_load_paths.push(*%W[#{config.root}/lib #{config.root}/app/models/hooks #{config.root}/app/models/members + #{config.root}/plugins #{config.root}/app/models/project_services #{config.root}/app/workers/concerns #{config.root}/app/services/concerns diff --git a/config/initializers/9_plugins.rb b/config/initializers/9_plugins.rb new file mode 100644 index 00000000000..9f252ccd296 --- /dev/null +++ b/config/initializers/9_plugins.rb @@ -0,0 +1,29 @@ +class PluginsSystem + attr_accessor :plugins, :files + + def initialize + @files = Dir.glob(Rails.root.join('plugins', '*_plugin.rb')) + end + + def valid_plugins + files.map do |file| + file_name = File.basename(file, '.rb') + + # Just give sample data to method and expect it to not crash. + begin + klass = Object.const_get(file_name.classify) + klass.new.execute(Gitlab::DataBuilder::Push::SAMPLE_DATA) + rescue => e + Rails.logger.warn("GitLab -> Plugins -> #{file_name} raised an exception during boot check. #{e}") + next + else + Rails.logger.info "GitLab -> Plugins -> #{file_name} passed boot check" + klass + end + end + end +end + +# Load external plugins from /plugins directory +# and set into PLUGINS variable +PLUGINS = PluginsSystem.new.valid_plugins