Commit ac92d70d authored by Alexis Reigel's avatar Alexis Reigel

extract concern for hook triggers

parent 88832ba9
module TriggerableHooks
AVAILABLE_TRIGGERS = {
repository_update_hooks: :repository_update_events,
push_hooks: :push_events,
tag_push_hooks: :tag_push_events,
issue_hooks: :issues_events,
confidential_issue_hooks: :confidential_issues_events,
note_hooks: :note_events,
merge_request_hooks: :merge_requests_events,
job_hooks: :job_events,
pipeline_hooks: :pipeline_events,
wiki_page_hooks: :wiki_page_events
}.freeze
extend ActiveSupport::Concern
class_methods do
attr_reader :triggerable_hooks
private
def triggerable_hooks(hooks)
triggers = AVAILABLE_TRIGGERS.slice(*hooks)
const_set('TRIGGERS', triggers)
self::TRIGGERS.each do |trigger, event|
scope trigger, -> { where(event => true) }
end
end
end
end
class ProjectHook < WebHook
TRIGGERS = {
push_hooks: :push_events,
tag_push_hooks: :tag_push_events,
issue_hooks: :issues_events,
confidential_issue_hooks: :confidential_issues_events,
note_hooks: :note_events,
merge_request_hooks: :merge_requests_events,
job_hooks: :job_events,
pipeline_hooks: :pipeline_events,
wiki_page_hooks: :wiki_page_events
}.freeze
include TriggerableHooks
TRIGGERS.each do |trigger, event|
scope trigger, -> { where(event => true) }
end
triggerable_hooks only: [
:push_hooks,
:tag_push_hooks,
:issue_hooks,
:confidential_issue_hooks,
:note_hooks,
:merge_request_hooks,
:job_hooks,
:pipeline_hooks,
:wiki_page_hooks
]
belongs_to :project
validates :project, presence: true
......
class SystemHook < WebHook
TRIGGERS = {
repository_update_hooks: :repository_update_events,
push_hooks: :push_events,
tag_push_hooks: :tag_push_events,
merge_request_hooks: :merge_requests_events
}.freeze
include TriggerableHooks
TRIGGERS.each do |trigger, event|
scope trigger, -> { where(event => true) }
end
triggerable_hooks only: [
:repository_update_hooks,
:push_hooks,
:tag_push_hooks,
:merge_request_hooks
]
default_value_for :push_events, false
default_value_for :repository_update_events, true
......
require 'rails_helper'
RSpec.describe TriggerableHooks do
before do
class TestableHook < WebHook
include TriggerableHooks
triggerable_hooks only: [:push_hooks]
end
end
describe 'scopes' do
it 'defines a scope for each of the requested triggers' do
expect(TestableHook).to respond_to :push_hooks
expect(TestableHook).not_to respond_to :tag_push_hooks
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