2016-08-25 08:01:10 -04:00
|
|
|
class PipelinesEmailService < Service
|
|
|
|
prop_accessor :recipients
|
|
|
|
boolean_accessor :notify_only_broken_pipelines
|
2018-01-26 08:48:47 -05:00
|
|
|
validates :recipients, presence: true, if: :valid_recipients?
|
2016-08-25 08:01:10 -04:00
|
|
|
|
|
|
|
def initialize_properties
|
2016-09-13 11:38:46 -04:00
|
|
|
self.properties ||= { notify_only_broken_pipelines: true }
|
2016-08-25 08:01:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def title
|
|
|
|
'Pipelines emails'
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
'Email the pipelines status to a list of recipients.'
|
|
|
|
end
|
|
|
|
|
2016-12-27 07:44:24 -05:00
|
|
|
def self.to_param
|
2016-08-25 08:01:10 -04:00
|
|
|
'pipelines_email'
|
|
|
|
end
|
|
|
|
|
2016-12-27 07:44:24 -05:00
|
|
|
def self.supported_events
|
2016-08-25 08:01:10 -04:00
|
|
|
%w[pipeline]
|
|
|
|
end
|
|
|
|
|
2016-09-14 12:00:02 -04:00
|
|
|
def execute(data, force: false)
|
2016-08-25 08:01:10 -04:00
|
|
|
return unless supported_events.include?(data[:object_kind])
|
2016-09-13 11:38:46 -04:00
|
|
|
return unless force || should_pipeline_be_notified?(data)
|
2016-08-25 08:01:10 -04:00
|
|
|
|
|
|
|
all_recipients = retrieve_recipients(data)
|
|
|
|
|
|
|
|
return unless all_recipients.any?
|
|
|
|
|
2016-09-28 05:22:06 -04:00
|
|
|
pipeline_id = data[:object_attributes][:id]
|
2016-10-17 06:12:07 -04:00
|
|
|
PipelineNotificationWorker.new.perform(pipeline_id, all_recipients)
|
2016-08-25 08:01:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_test?
|
2016-10-12 13:12:00 -04:00
|
|
|
project.pipelines.any?
|
2016-08-25 08:01:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_data(project, user)
|
|
|
|
data = Gitlab::DataBuilder::Pipeline.build(project.pipelines.last)
|
|
|
|
data[:user] = user.hook_attrs
|
|
|
|
data
|
|
|
|
end
|
|
|
|
|
|
|
|
def fields
|
|
|
|
[
|
|
|
|
{ type: 'textarea',
|
|
|
|
name: 'recipients',
|
2017-05-22 06:07:12 -04:00
|
|
|
placeholder: 'Emails separated by comma',
|
|
|
|
required: true },
|
2016-08-25 08:01:10 -04:00
|
|
|
{ type: 'checkbox',
|
2017-05-03 07:22:03 -04:00
|
|
|
name: 'notify_only_broken_pipelines' }
|
2016-08-25 08:01:10 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test(data)
|
2016-09-14 12:00:02 -04:00
|
|
|
result = execute(data, force: true)
|
2016-08-25 08:01:10 -04:00
|
|
|
|
|
|
|
{ success: true, result: result }
|
|
|
|
rescue StandardError => error
|
|
|
|
{ success: false, result: error }
|
|
|
|
end
|
|
|
|
|
2016-09-13 11:38:46 -04:00
|
|
|
def should_pipeline_be_notified?(data)
|
2016-08-25 08:01:10 -04:00
|
|
|
case data[:object_attributes][:status]
|
|
|
|
when 'success'
|
|
|
|
!notify_only_broken_pipelines?
|
2016-09-13 11:38:46 -04:00
|
|
|
when 'failed'
|
|
|
|
true
|
2016-08-25 08:01:10 -04:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def retrieve_recipients(data)
|
2017-09-13 08:24:19 -04:00
|
|
|
recipients.to_s.split(/[,(?:\r?\n) ]+/).reject(&:empty?)
|
2016-08-25 08:01:10 -04:00
|
|
|
end
|
|
|
|
end
|