/
hooks.rb
264 lines (229 loc) · 7.6 KB
/
hooks.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# frozen_string_literal: true
module Samson
module Hooks
class UserError < StandardError
end
VIEW_HOOKS = [
:stage_form,
:stage_show,
:stage_form_checkbox,
:project_form,
:project_form_checkbox,
:build_button,
:build_new,
:build_show,
:deploy_changeset_tab_nav,
:deploy_changeset_tab_body,
:deploy_group_show,
:deploy_group_form,
:deploy_group_table_header,
:deploy_group_table_cell,
:deploys_header,
:deploy_show_view,
:deploy_view,
:deploy_form, # for external plugin, so they can add extra form fields
:admin_menu,
:manage_menu,
:project_dashboard,
:project_tabs_view
].freeze
EVENT_HOOKS = [
:after_deploy,
:after_deploy_setup,
:after_docker_build,
:before_deploy,
:before_docker_build,
:before_docker_repository_usage,
:buddy_request,
:build_permitted_params,
:buildkite_release_params,
:can,
:deploy_group_includes,
:deploy_group_permitted_params,
:deploy_permitted_params,
:error,
:ignore_error,
:deploy_env,
:deploy_execution_env,
:link_parts_for_resource,
:project_docker_build_method_options,
:project_permitted_params,
:ref_status,
:release_deploy_conditions,
:resolve_docker_image_tag,
:stage_clone,
:stage_permitted_params,
:trace_method,
:trace_scope,
:asynchronous_performance_tracer,
:repo_provider_status,
:repo_commit_from_ref,
:repo_compare,
:validate_deploy
].freeze
# Hooks that are slow and we want performance info on
TRACED = [
:after_deploy,
:after_deploy_setup,
:after_docker_build,
:before_deploy,
:before_docker_build,
:before_docker_repository_usage,
:ref_status,
:stage_clone
].freeze
(TRACED & EVENT_HOOKS).sort == TRACED.sort || raise("Unknown hook in traced")
KNOWN = VIEW_HOOKS + EVENT_HOOKS
@hooks = Hash.new { |h, k| h[k] = [] }
@class_decorators = Hash.new { |h, k| h[k] = [] }
class Plugin
attr_reader :name, :folder
def initialize(path)
@path = path
@folder = File.expand_path('../../../', @path)
@name = File.basename(@folder).sub(/-[^-]*\z/, '').sub(/\Asamson_/, "")
end
def load
lib = "#{@folder}/lib"
$LOAD_PATH << lib
require @path
engine.config.eager_load_paths << lib
end
def add_migrations
migrations = File.join(@folder, "db/migrate")
Rails.application.config.paths["db/migrate"] << migrations if Dir.exist?(migrations)
end
def add_decorators
Dir["#{decorators_root}/**/*_decorator.rb"].each do |path|
Samson::Hooks.decorator(decorator_class(path), path)
end
end
def add_assets_to_precompile
engine.config.assets.precompile += %W[#{name}/application.css #{name}/application.js]
end
def engine
@engine ||= Kernel.const_get("::Samson#{@name.camelize}::SamsonPlugin")
end
private
def decorators_root
@decorators_root ||= Pathname("#{@folder}/decorators")
end
# {root}/xyz_decorator.rb -> Xyz
# {root}/xy/z_decorator.rb -> Xy::Z
def decorator_class(path)
relative_path = Pathname.new(path).relative_path_from(decorators_root).to_s
relative_path.sub('_decorator.rb', '').split('/').map(&:classify).join('::')
end
end
class << self
def plugins
@plugins ||= begin
Gem.find_files("samson_*/samson_plugin.rb").
map { |path| Plugin.new(path) }.
select { |p| active_plugin?(p.name) }.
sort_by(&:name)
end
end
def active_plugin?(plugin_name)
@plugin_list ||= ENV['PLUGINS'].to_s.split(',').map(&:strip).to_set
(@plugin_list.include?(plugin_name) || @plugin_list.include?('all')) &&
!@plugin_list.include?("-#{plugin_name}")
end
# configure
def callback(name, &block)
hooks(name) << block
end
def view(name, plugin)
hooks(name) << (plugin.include?("/") ? plugin : "#{plugin}/#{name}")
end
def decorator(class_name, file)
@class_decorators[class_name] << file
end
# temporarily add a hook for testing
def with_callback(name, *hook_blocks)
original_hooks = @hooks[name].dup
@hooks[name] = hook_blocks
yield
ensure
@hooks[name] = original_hooks
end
# temporarily removes all callbacks for specified hook except for those of the passed in plugin for testing
def only_callbacks_for_plugin(plugin_name, hook_name)
original_hooks = @hooks[hook_name]
@hooks[hook_name] = @hooks[hook_name].select do |proc|
proc.source_location.first.include?("/#{plugin_name}/")
end
yield
ensure
@hooks[hook_name] = original_hooks
end
# use
def fire(name, *args)
traced(name) { hooks(name).map { |hook| hook.call(*args) } }
end
def render_views(name, view, *args)
hooks(name).each_with_object("".html_safe) do |partial, html|
html << view.render(partial, *args)
end
end
def load_decorators(klass)
@class_decorators[klass.name].each { |path| require_dependency(path) }
end
def plugin_setup
Samson::Hooks.plugins.
each(&:load).
each(&:add_migrations).
each(&:add_assets_to_precompile).
each(&:add_decorators)
end
def symlink_plugin_fixtures
fixture_path = ActiveSupport::TestCase.fixture_path
links = plugins.flat_map do |plugin|
fixtures = Dir.glob(File.join(plugin.folder, 'test', 'fixtures', '*'))
fixtures.map! { |fixture| [fixture, File.join(fixture_path, File.basename(fixture))] }
end
# avoid errors when running in parallel
links.each { |from, to| File.symlink(from, to) rescue false } # rubocop:disable Style/RescueModifier
if ENV['TEST_ENV_NUMBER'].to_s == '' # only run in first parallel test
# rails test does not trigger after_run and rake does not work with at_exit
# https://github.com/rails/rails/pull/26515
callback = -> do
links.each { |_, to| File.delete(to) rescue false } # rubocop:disable Style/RescueModifier
end
if Minitest.respond_to?(:run_with_rails_extension) && Minitest.run_with_rails_extension
at_exit(&callback)
else
Minitest.after_run(&callback)
end
end
end
def render_javascripts(view)
render_assets view, 'javascripts', 'application.js', :javascript_include_tag
end
def render_stylesheets(view)
render_assets view, 'stylesheets', 'application.css', :stylesheet_link_tag
end
private
def traced(name, &block)
if TRACED.include?(name)
Samson::PerformanceTracer.trace_execution_scoped("Custom/Hooks/#{name}", &block)
else
yield
end
end
def render_assets(view, folder, file, method)
Samson::Hooks.plugins.each do |plugin|
full_file = plugin.engine.config.root.join("app/assets/#{folder}/#{plugin.name}/#{file}")
next unless File.exist?(full_file)
view.concat(view.send(method, "#{plugin.name}/#{file}"))
end
nil
end
def hooks(name)
raise "Using unsupported hook #{name.inspect}" unless KNOWN.include?(name)
@hooks[name]
end
end
end
end
Samson::Hooks.plugin_setup