gitlab-org--gitlab-foss/app/models/project_services/pipelines_email_service.rb

96 lines
2.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class PipelinesEmailService < Service
prop_accessor :recipients
boolean_accessor :notify_only_broken_pipelines, :notify_only_default_branch
2018-01-26 08:48:47 -05:00
validates :recipients, presence: true, if: :valid_recipients?
def initialize_properties
self.properties ||= { notify_only_broken_pipelines: true, notify_only_default_branch: false }
end
def title
_('Pipelines emails')
end
def description
_('Email the pipelines status to a list of recipients.')
end
def self.to_param
'pipelines_email'
end
def self.supported_events
%w[pipeline]
end
def execute(data, force: false)
return unless supported_events.include?(data[:object_kind])
2016-09-13 11:38:46 -04:00
return unless force || should_pipeline_be_notified?(data)
all_recipients = retrieve_recipients(data)
return unless all_recipients.any?
pipeline_id = data[:object_attributes][:id]
PipelineNotificationWorker.new.perform(pipeline_id, all_recipients)
end
def can_test?
2018-12-05 09:39:15 -05:00
project.ci_pipelines.any?
end
def test_data(project, user)
2018-12-05 09:39:15 -05:00
data = Gitlab::DataBuilder::Pipeline.build(project.ci_pipelines.last)
data[:user] = user.hook_attrs
data
end
def fields
[
{ type: 'textarea',
name: 'recipients',
placeholder: _('Emails separated by comma'),
required: true },
{ type: 'checkbox',
name: 'notify_only_broken_pipelines' },
{ type: 'checkbox',
name: 'notify_only_default_branch' }
]
end
def test(data)
result = execute(data, force: true)
{ 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)
notify_for_pipeline_branch?(data) && notify_for_pipeline?(data)
end
def notify_for_pipeline_branch?(data)
return true unless notify_only_default_branch?
data[:object_attributes][:ref] == data[:project][:default_branch]
end
def notify_for_pipeline?(data)
case data[:object_attributes][:status]
when 'success'
!notify_only_broken_pipelines?
2016-09-13 11:38:46 -04:00
when 'failed'
true
else
false
end
end
def retrieve_recipients(data)
recipients.to_s.split(/[,(?:\r?\n) ]+/).reject(&:empty?)
end
end