chat_notification_service.rb 4.04 KB
Newer Older
1 2 3 4 5 6 7 8
# Base class for Chat notifications services
# This class is not meant to be used directly, but only to inherit from.
class ChatNotificationService < Service
  include ChatMessage

  default_value_for :category, 'chat'

  prop_accessor :webhook, :username, :channel
9
  boolean_accessor :notify_only_broken_pipelines, :notify_only_default_branch
10 11 12 13 14 15 16 17 18 19

  validates :webhook, presence: true, url: true, if: :activated?

  def initialize_properties
    # Custom serialized properties initialization
    self.supported_events.each { |event| self.class.prop_accessor(event_channel_name(event)) }

    if properties.nil?
      self.properties = {}
      self.notify_only_broken_pipelines = true
20
      self.notify_only_default_branch = true
21 22 23 24 25 26 27
    end
  end

  def can_test?
    valid?
  end

28
  def self.supported_events
29
    %w[push issue confidential_issue merge_request note tag_push
30
       pipeline wiki_page]
31 32
  end

33 34 35 36 37 38 39 40 41 42 43 44 45
  def fields
    default_fields + build_event_channels
  end

  def default_fields
    [
      { type: 'text', name: 'webhook', placeholder: "e.g. #{webhook_placeholder}" },
      { type: 'text', name: 'username', placeholder: 'e.g. GitLab' },
      { type: 'checkbox', name: 'notify_only_broken_pipelines' },
      { type: 'checkbox', name: 'notify_only_default_branch' },
    ]
  end

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  def execute(data)
    return unless supported_events.include?(data[:object_kind])
    return unless webhook.present?

    object_kind = data[:object_kind]

    data = data.merge(
      project_url: project_url,
      project_name: project_name
    )

    # WebHook events often have an 'update' event that follows a 'open' or
    # 'close' action. Ignore update events for now to prevent duplicate
    # messages from arriving.

    message = get_message(object_kind, data)

    return false unless message

65
    channel_name = get_channel_field(object_kind).presence || channel
66

67
    opts = {}
68
    opts[:channel] = channel_name if channel_name
69
    opts[:username] = username if username
70

71
    notifier = Slack::Notifier.new(webhook, opts)
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    notifier.ping(message.pretext, attachments: message.attachments, fallback: message.fallback)

    true
  end

  def event_channel_names
    supported_events.map { |event| event_channel_name(event) }
  end

  def event_field(event)
    fields.find { |field| field[:name] == event_channel_name(event) }
  end

  def global_fields
    fields.reject { |field| field[:name].end_with?('channel') }
  end

89
  def default_channel_placeholder
90 91 92 93 94 95 96 97
    raise NotImplementedError
  end

  private

  def get_message(object_kind, data)
    case object_kind
    when "push", "tag_push"
98
      ChatMessage::PushMessage.new(data)
99
    when "issue"
100
      ChatMessage::IssueMessage.new(data) unless is_update?(data)
101
    when "merge_request"
102
      ChatMessage::MergeMessage.new(data) unless is_update?(data)
103
    when "note"
104
      ChatMessage::NoteMessage.new(data)
105
    when "pipeline"
106
      ChatMessage::PipelineMessage.new(data) if should_pipeline_be_notified?(data)
107
    when "wiki_page"
108
      ChatMessage::WikiPageMessage.new(data)
109 110 111 112 113 114 115 116 117 118
    end
  end

  def get_channel_field(event)
    field_name = event_channel_name(event)
    self.public_send(field_name)
  end

  def build_event_channels
    supported_events.reduce([]) do |channels, event|
119
      channels << { type: 'text', name: event_channel_name(event), placeholder: default_channel_placeholder }
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    end
  end

  def event_channel_name(event)
    "#{event}_channel"
  end

  def project_name
    project.name_with_namespace.gsub(/\s/, '')
  end

  def project_url
    project.web_url
  end

  def is_update?(data)
    data[:object_attributes][:action] == 'update'
  end

  def should_pipeline_be_notified?(data)
140
    notify_for_ref?(data) && notify_for_pipeline?(data)
141 142
  end

143 144 145
  def notify_for_ref?(data)
    return true if data[:object_attributes][:tag]
    return true unless notify_only_default_branch
146

147
    data[:object_attributes][:ref] == project.default_branch
148 149
  end

150
  def notify_for_pipeline?(data)
151 152 153 154 155 156 157 158 159 160
    case data[:object_attributes][:status]
    when 'success'
      !notify_only_broken_pipelines?
    when 'failed'
      true
    else
      false
    end
  end
end