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

100 lines
2.6 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2016-11-03 12:29:30 +05:30
class PipelinesEmailService < Service
2019-12-04 20:38:33 +05:30
include NotificationBranchSelection
prop_accessor :recipients, :branches_to_be_notified
2019-09-04 21:01:54 +05:30
boolean_accessor :notify_only_broken_pipelines, :notify_only_default_branch
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
2019-12-04 20:38:33 +05:30
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-11-03 12:29:30 +05:30
end
def title
2019-07-31 22:56:46 +05:30
_('Pipelines emails')
2016-11-03 12:29:30 +05:30
end
def description
2019-07-31 22:56:46 +05:30
_('Email the pipelines status to a list of recipients.')
2016-11-03 12:29:30 +05:30
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]
2020-04-08 14:13:33 +05:30
PipelineNotificationWorker.new.perform(pipeline_id, recipients: all_recipients)
2016-11-03 12:29:30 +05:30
end
def can_test?
2020-04-22 19:07:51 +05:30
project&.ci_pipelines&.any?
2016-11-03 12:29:30 +05:30
end
def fields
[
{ type: 'textarea',
name: 'recipients',
2019-07-31 22:56:46 +05:30
placeholder: _('Emails separated by comma'),
2017-09-10 17:25:29 +05:30
required: true },
2016-11-03 12:29:30 +05:30
{ type: 'checkbox',
2019-09-04 21:01:54 +05:30
name: 'notify_only_broken_pipelines' },
2019-12-04 20:38:33 +05:30
{ type: 'select',
name: 'branches_to_be_notified',
2020-04-22 19:07:51 +05:30
choices: branch_choices }
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)
2019-12-04 20:38:33 +05:30
notify_for_branch?(data) && notify_for_pipeline?(data)
2019-09-04 21:01:54 +05:30
end
def notify_for_pipeline?(data)
2016-11-03 12:29:30 +05:30
case data[:object_attributes][:status]
when 'success'
!notify_only_broken_pipelines?
when 'failed'
true
else
false
end
end
def retrieve_recipients(data)
2020-03-13 15:44:24 +05:30
recipients.to_s.split(/[,\r\n ]+/).reject(&:empty?)
2016-11-03 12:29:30 +05:30
end
end