Class: Tokite::Hook

Inherits:
Object
  • Object
show all
Defined in:
app/models/tokite/hook.rb

Constant Summary collapse

HOOK_EVENTS =
{
  "pull_request" => HookEvent::PullRequest,
  "issues" => HookEvent::Issues,
  "issue_comment" => HookEvent::IssueComment,
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event) ⇒ Hook

Returns a new instance of Hook.



16
17
18
# File 'app/models/tokite/hook.rb', line 16

def initialize(event)
  @event = event
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



3
4
5
# File 'app/models/tokite/hook.rb', line 3

def event
  @event
end

Class Method Details

.fire!(github_event, hook_params) ⇒ Object



11
12
13
14
# File 'app/models/tokite/hook.rb', line 11

def self.fire!(github_event, hook_params)
  event_class = HOOK_EVENTS[github_event]
  Hook.new(event_class.new(hook_params)).fire! if event_class
end

Instance Method Details

#fire!Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/tokite/hook.rb', line 20

def fire!
  return unless event.notify?
  Rule.matched_rules(event).each do |rule|
    attachment = event.slack_attachment
    attachment[:fallback] += "\n\n#{rule.slack_attachment_fallback}"
    attachment[:text] += "\n\n#{rule.slack_attachment_text}"
    emoji = rule.icon_emoji.chomp.presence
    additional_text = rule.additional_text
  
    notify!(channel: rule.channel, text: event.slack_text, icon_emoji: emoji, attachments: [attachment])
    notify!(channel: rule.channel, text: additional_text, icon_emoji: emoji, parse: "full") if additional_text.present?
  end
end

#notify!(payload) ⇒ Object



34
35
36
# File 'app/models/tokite/hook.rb', line 34

def notify!(payload)
  NotifyGithubHookEventJob.perform_now(payload.compact)
end