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,
  "pull_request_review" => HookEvent::PullRequestReview,
  "pull_request_review_comment" => HookEvent::PullRequestReviewComment,
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event) ⇒ Hook

Returns a new instance of Hook.



18
19
20
# File 'app/models/tokite/hook.rb', line 18

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



13
14
15
16
# File 'app/models/tokite/hook.rb', line 13

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



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
# File 'app/models/tokite/hook.rb', line 22

def fire!
  return unless event.notify?
  payloads = []
  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

    if payloads.none? {|payload| payload[:channel] == rule.channel && payload[:emoji] == emoji && payload[:additional_text] == additional_text }
      payloads << {
        channel: rule.channel,
        text: event.slack_text,
        emoji: emoji,
        additional_text: additional_text,
        attachments: [attachment],
      }
    end
  end
  payloads.each do |payload|
    notify!(channel: payload[:channel], text: payload[:text], icon_emoji: payload[:emoji], attachments: payload[:attachments])
    notify!(channel: payload[:channel], text: payload[:additional_text], icon_emoji: payload[:emoji], parse: "full") if payload[:additional_text].present?
  end
end

#notify!(payload) ⇒ Object



48
49
50
# File 'app/models/tokite/hook.rb', line 48

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