2015-12-07 12:23:23 +00:00
|
|
|
class BuildsEmailService < Service
|
|
|
|
prop_accessor :recipients
|
|
|
|
boolean_accessor :add_pusher
|
|
|
|
boolean_accessor :notify_only_broken_builds
|
2016-04-14 08:24:09 +00:00
|
|
|
validates :recipients, presence: true, if: ->(s) { s.activated? && !s.add_pusher? }
|
2015-12-07 12:23:23 +00:00
|
|
|
|
2015-12-11 12:28:40 +00:00
|
|
|
def initialize_properties
|
|
|
|
if properties.nil?
|
|
|
|
self.properties = {}
|
|
|
|
self.notify_only_broken_builds = true
|
|
|
|
end
|
|
|
|
end
|
2015-12-10 13:08:09 +00:00
|
|
|
|
2015-12-07 12:23:23 +00:00
|
|
|
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])
|
2016-04-05 16:15:13 +00:00
|
|
|
return unless should_build_be_notified?(push_data)
|
2015-12-07 12:23:23 +00:00
|
|
|
|
2016-04-05 16:15:13 +00:00
|
|
|
recipients = all_recipients(push_data)
|
|
|
|
|
|
|
|
if recipients.any?
|
2015-12-07 12:23:23 +00:00
|
|
|
BuildEmailWorker.perform_async(
|
|
|
|
push_data[:build_id],
|
2016-04-05 16:15:13 +00:00
|
|
|
recipients,
|
|
|
|
push_data
|
2015-12-07 12:23:23 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-12 21:19:47 +00:00
|
|
|
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)
|
2016-08-12 08:09:29 +00:00
|
|
|
Gitlab::DataBuilder::Build.build(project.builds.last)
|
2016-07-12 21:19:47 +00:00
|
|
|
end
|
|
|
|
|
2015-12-07 12:23:23 +00:00
|
|
|
def fields
|
|
|
|
[
|
2015-12-09 15:31:42 +00:00
|
|
|
{ type: 'textarea', name: 'recipients', placeholder: 'Emails separated by comma' },
|
2015-12-07 12:23:23 +00:00
|
|
|
{ type: 'checkbox', name: 'add_pusher', label: 'Add pusher to recipients list' },
|
|
|
|
{ type: 'checkbox', name: 'notify_only_broken_builds' },
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2016-07-12 21:19:47 +00:00
|
|
|
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
|
|
|
|
|
2015-12-07 12:23:23 +00:00
|
|
|
def should_build_be_notified?(data)
|
|
|
|
case data[:build_status]
|
|
|
|
when 'success'
|
|
|
|
!notify_only_broken_builds?
|
|
|
|
when 'failed'
|
2015-12-22 20:15:06 +00:00
|
|
|
!allow_failure?(data)
|
2015-12-07 12:23:23 +00:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-22 20:15:06 +00:00
|
|
|
def allow_failure?(data)
|
|
|
|
data[:build_allow_failure] == true
|
|
|
|
end
|
|
|
|
|
2015-12-07 12:23:23 +00:00
|
|
|
def all_recipients(data)
|
2016-04-14 08:24:09 +00:00
|
|
|
all_recipients = []
|
|
|
|
|
|
|
|
unless recipients.blank?
|
|
|
|
all_recipients += recipients.split(',').compact.reject(&:blank?)
|
|
|
|
end
|
2015-12-09 15:31:42 +00:00
|
|
|
|
2015-12-07 12:23:23 +00:00
|
|
|
if add_pusher? && data[:user][:email]
|
2016-04-14 08:24:09 +00:00
|
|
|
all_recipients << data[:user][:email]
|
2015-12-07 12:23:23 +00:00
|
|
|
end
|
2015-12-09 15:31:42 +00:00
|
|
|
|
|
|
|
all_recipients
|
2015-12-07 12:23:23 +00:00
|
|
|
end
|
|
|
|
end
|