BigW Consortium Gitlab

builds_email_service.rb 1.71 KB
Newer Older
1 2 3 4
class BuildsEmailService < Service
  prop_accessor :recipients
  boolean_accessor :add_pusher
  boolean_accessor :notify_only_broken_builds
5
  validates :recipients, presence: true, if: ->(s) { s.activated? && !s.add_pusher? }
6

7 8 9 10 11 12
  def initialize_properties
    if properties.nil?
      self.properties = {}
      self.notify_only_broken_builds = true
    end
  end
13

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  def title
    'Builds emails'
  end

  def description
    'Email the builds status to a list of recipients.'
  end

  def to_param
    'builds_email'
  end

  def supported_events
    %w(build)
  end

  def execute(push_data)
    return unless supported_events.include?(push_data[:object_kind])
32
    return unless should_build_be_notified?(push_data)
33

34 35 36
    recipients = all_recipients(push_data)

    if recipients.any?
37 38
      BuildEmailWorker.perform_async(
        push_data[:build_id],
39 40
        recipients,
        push_data
41 42 43 44 45 46
      )
    end
  end

  def fields
    [
47
      { type: 'textarea', name: 'recipients', placeholder: 'Emails separated by comma' },
48 49 50 51 52 53 54 55 56 57
      { type: 'checkbox', name: 'add_pusher', label: 'Add pusher to recipients list' },
      { type: 'checkbox', name: 'notify_only_broken_builds' },
    ]
  end

  def should_build_be_notified?(data)
    case data[:build_status]
    when 'success'
      !notify_only_broken_builds?
    when 'failed'
58
      !allow_failure?(data)
59 60 61 62 63
    else
      false
    end
  end

64 65 66 67
  def allow_failure?(data)
    data[:build_allow_failure] == true
  end

68
  def all_recipients(data)
69 70 71 72 73
    all_recipients = []

    unless recipients.blank?
      all_recipients += recipients.split(',').compact.reject(&:blank?)
    end
74

75
    if add_pusher? && data[:user][:email]
76
      all_recipients << data[:user][:email]
77
    end
78 79

    all_recipients
80 81
  end
end