Add external plugins support to extend system hooks

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
This commit is contained in:
Dmitriy Zaporozhets 2018-02-08 17:51:02 +02:00
parent 1ffa07e6f3
commit 9be0c2734a
3 changed files with 39 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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