2018-08-10 02:45:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-25 08:01:10 -04:00
|
|
|
class PipelinesEmailService < Service
|
2019-09-13 14:06:03 -04:00
|
|
|
include NotificationBranchSelection
|
|
|
|
|
|
|
|
prop_accessor :recipients, :branches_to_be_notified
|
2019-05-12 05:19:02 -04:00
|
|
|
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?
|
2016-08-25 08:01:10 -04:00
|
|
|
|
|
|
|
def initialize_properties
|
2019-09-13 14:06:03 -04:00
|
|
|
if properties.nil?
|
|
|
|
self.properties = {}
|
|
|
|
self.notify_only_broken_pipelines = true
|
|
|
|
self.branches_to_be_notified = "default"
|
|
|
|
elsif !self.notify_only_default_branch.nil?
|
|
|
|
# In older versions, there was only a boolean property named
|
|
|
|
# `notify_only_default_branch`. Now we have a string property named
|
|
|
|
# `branches_to_be_notified`. Instead of doing a background migration, we
|
|
|
|
# opted to set a value for the new property based on the old one, if
|
|
|
|
# users hasn't specified one already. When users edit the service and
|
|
|
|
# selects a value for this new property, it will override everything.
|
|
|
|
|
|
|
|
self.branches_to_be_notified ||= notify_only_default_branch? ? "default" : "all"
|
|
|
|
end
|
2016-08-25 08:01:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def title
|
2019-04-12 08:28:07 -04:00
|
|
|
_('Pipelines emails')
|
2016-08-25 08:01:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
2019-04-12 08:28:07 -04:00
|
|
|
_('Email the pipelines status to a list of recipients.')
|
2016-08-25 08:01:10 -04:00
|
|
|
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]
|
2020-02-27 13:09:21 -05:00
|
|
|
PipelineNotificationWorker.new.perform(pipeline_id, recipients: all_recipients)
|
2016-08-25 08:01:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_test?
|
2020-04-03 20:09:37 -04:00
|
|
|
project&.ci_pipelines&.any?
|
2016-08-25 08:01:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_data(project, user)
|
2018-12-05 09:39:15 -05:00
|
|
|
data = Gitlab::DataBuilder::Pipeline.build(project.ci_pipelines.last)
|
2016-08-25 08:01:10 -04:00
|
|
|
data[:user] = user.hook_attrs
|
|
|
|
data
|
|
|
|
end
|
|
|
|
|
|
|
|
def fields
|
|
|
|
[
|
|
|
|
{ type: 'textarea',
|
|
|
|
name: 'recipients',
|
2019-04-12 08:28:07 -04:00
|
|
|
placeholder: _('Emails separated by comma'),
|
2017-05-22 06:07:12 -04:00
|
|
|
required: true },
|
2016-08-25 08:01:10 -04:00
|
|
|
{ type: 'checkbox',
|
2019-05-12 05:19:02 -04:00
|
|
|
name: 'notify_only_broken_pipelines' },
|
2019-09-13 14:06:03 -04:00
|
|
|
{ type: 'select',
|
|
|
|
name: 'branches_to_be_notified',
|
2020-04-21 11:21:10 -04:00
|
|
|
choices: branch_choices }
|
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)
|
2019-09-13 14:06:03 -04:00
|
|
|
notify_for_branch?(data) && notify_for_pipeline?(data)
|
2019-05-12 05:19:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def notify_for_pipeline?(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)
|
2020-01-20 07:09:02 -05:00
|
|
|
recipients.to_s.split(/[,\r\n ]+/).reject(&:empty?)
|
2016-08-25 08:01:10 -04:00
|
|
|
end
|
|
|
|
end
|