summaryrefslogtreecommitdiff
path: root/app/models/project_services/builds_email_service.rb
blob: fa66e5864b8b002efd92d17aa737acc96a947a48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
class BuildsEmailService < Service
  prop_accessor :recipients
  boolean_accessor :add_pusher
  boolean_accessor :notify_only_broken_builds
  validates :recipients, presence: true, if: ->(s) { s.activated? && !s.add_pusher? }

  def initialize_properties
    if properties.nil?
      self.properties = {}
      self.notify_only_broken_builds = true
    end
  end

  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])
    return unless should_build_be_notified?(push_data)

    recipients = all_recipients(push_data)

    if recipients.any?
      BuildEmailWorker.perform_async(
        push_data[:build_id],
        recipients,
        push_data
      )
    end
  end

  def can_test?
    project.builds.count > 0
  end

  def disabled_title
    "Please setup a build on your repository."
  end

  def test_data(project = nil, user = nil)
    Gitlab::DataBuilder::Build.build(project.builds.last)
  end

  def fields
    [
      { type: 'textarea', name: 'recipients', placeholder: 'Emails separated by comma' },
      { type: 'checkbox', name: 'add_pusher', label: 'Add pusher to recipients list' },
      { type: 'checkbox', name: 'notify_only_broken_builds' },
    ]
  end

  def test(data)
    begin
      # bypass build status verification when testing
      data[:build_status] = "failed"
      data[:build_allow_failure] = false

      result = execute(data)
    rescue StandardError => error
      return { success: false, result: error }
    end

    { success: true, result: result }
  end

  def should_build_be_notified?(data)
    case data[:build_status]
    when 'success'
      !notify_only_broken_builds?
    when 'failed'
      !allow_failure?(data)
    else
      false
    end
  end

  def allow_failure?(data)
    data[:build_allow_failure] == true
  end

  def all_recipients(data)
    all_recipients = []

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

    if add_pusher? && data[:user][:email]
      all_recipients << data[:user][:email]
    end

    all_recipients
  end
end