debian-mirror-gitlab/app/models/project_services/pipelines_email_service.rb

86 lines
1.8 KiB
Ruby
Raw Normal View History

2016-11-03 12:29:30 +05:30
class PipelinesEmailService < Service
prop_accessor :recipients
boolean_accessor :notify_only_broken_pipelines
2018-03-17 18:26:18 +05:30
validates :recipients, presence: true, if: :valid_recipients?
2016-11-03 12:29:30 +05:30
def initialize_properties
self.properties ||= { notify_only_broken_pipelines: true }
end
def title
'Pipelines emails'
end
def description
'Email the pipelines status to a list of recipients.'
end
2017-08-17 22:00:37 +05:30
def self.to_param
2016-11-03 12:29:30 +05:30
'pipelines_email'
end
2017-08-17 22:00:37 +05:30
def self.supported_events
2016-11-03 12:29:30 +05:30
%w[pipeline]
end
def execute(data, force: false)
return unless supported_events.include?(data[:object_kind])
return unless force || should_pipeline_be_notified?(data)
all_recipients = retrieve_recipients(data)
return unless all_recipients.any?
2017-08-17 22:00:37 +05:30
pipeline_id = data[:object_attributes][:id]
PipelineNotificationWorker.new.perform(pipeline_id, all_recipients)
2016-11-03 12:29:30 +05:30
end
def can_test?
project.pipelines.any?
end
def disabled_title
'Please setup a pipeline on your repository.'
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-09-10 17:25:29 +05:30
placeholder: 'Emails separated by comma',
required: true },
2016-11-03 12:29:30 +05:30
{ type: 'checkbox',
2017-09-10 17:25:29 +05:30
name: 'notify_only_broken_pipelines' }
2016-11-03 12:29:30 +05:30
]
end
def test(data)
result = execute(data, force: true)
{ success: true, result: result }
rescue StandardError => error
{ success: false, result: error }
end
def should_pipeline_be_notified?(data)
case data[:object_attributes][:status]
when 'success'
!notify_only_broken_pipelines?
when 'failed'
true
else
false
end
end
def retrieve_recipients(data)
2018-03-17 18:26:18 +05:30
recipients.to_s.split(/[,(?:\r?\n) ]+/).reject(&:empty?)
2016-11-03 12:29:30 +05:30
end
end