Read about our upcoming Code of Conduct on this issue

This instance will be upgraded to Heptapod 0.28.1 on 2022-01-26 at 16:00 UTC+1 (a few minutes of down time)

Commit c8ee32e2 authored by Alexis Reigel's avatar Alexis Reigel
Browse files

use safer .hooks_for instead of .public_send

with .public_send we can't make sure that the scope on the model
actually exists.
parent 0ce6fa7791e8
......@@ -17,6 +17,12 @@ module TriggerableHooks
class_methods do
attr_reader :triggerable_hooks
def hooks_for(trigger)
return none unless self::TRIGGERS.keys.include?(trigger)
public_send(trigger) # rubocop:disable GitlabSecurity/PublicSend
end
private
def triggerable_hooks(hooks)
......
......@@ -970,7 +970,7 @@ def owner
def execute_hooks(data, hooks_scope = :push_hooks)
run_after_commit_or_now do
hooks.public_send(hooks_scope).each do |hook| # rubocop:disable GitlabSecurity/PublicSend
hooks.hooks_for(hooks_scope).each do |hook|
hook.async_execute(data, hooks_scope.to_s)
end
end
......
......@@ -8,7 +8,7 @@ def execute_hooks_for(model, event)
end
def execute_hooks(data, hooks_scope = :all)
SystemHook.public_send(hooks_scope).find_each do |hook| # rubocop:disable GitlabSecurity/PublicSend
SystemHook.hooks_for(hooks_scope).find_each do |hook|
hook.async_execute(data, 'system_hooks')
end
end
......
......@@ -14,4 +14,22 @@ class TestableHook < WebHook
expect(TestableHook).not_to respond_to :tag_push_hooks
end
end
describe '.hooks_for' do
context 'the model has the required trigger scope' do
it 'returns the record' do
hook = TestableHook.create!(url: 'http://example.com', push_events: true)
expect(TestableHook.hooks_for(:push_hooks)).to eq [hook]
end
end
context 'the model does not have the required trigger scope' do
it 'returns an empty relation' do
TestableHook.create!(url: 'http://example.com', push_events: true)
expect(TestableHook.hooks_for(:tag_push_hooks)).to eq []
end
end
end
end
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