Try to integrate the email into notification system
This commit is contained in:
parent
1b1c6ebf49
commit
4add6ca6ec
9 changed files with 51 additions and 49 deletions
|
@ -1,18 +1,18 @@
|
|||
module Emails
|
||||
module Pipelines
|
||||
def pipeline_succeeded_email(params, to)
|
||||
pipeline_mail(params, to, 'succeeded')
|
||||
def pipeline_success_email(pipeline, to)
|
||||
pipeline_mail(pipeline, to, 'succeeded')
|
||||
end
|
||||
|
||||
def pipeline_failed_email(params, to)
|
||||
pipeline_mail(params, to, 'failed')
|
||||
def pipeline_failed_email(pipeline, to)
|
||||
pipeline_mail(pipeline, to, 'failed')
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def pipeline_mail(params, to, status)
|
||||
@project = params.project
|
||||
@pipeline = params.pipeline
|
||||
def pipeline_mail(pipeline, to, status)
|
||||
@project = pipeline.project
|
||||
@pipeline = pipeline
|
||||
add_headers
|
||||
|
||||
mail(to: to, subject: pipeline_subject(status))
|
||||
|
|
|
@ -62,6 +62,10 @@ module Ci
|
|||
after_transition do |pipeline, transition|
|
||||
pipeline.execute_hooks unless transition.loopback?
|
||||
end
|
||||
|
||||
after_transition any => [:success, :failed] do |pipeline, transition|
|
||||
SendPipelineNotificationService.new(pipeline).execute
|
||||
end
|
||||
end
|
||||
|
||||
# ref can't be HEAD or SHA, can only be branch/tag name
|
||||
|
@ -90,6 +94,11 @@ module Ci
|
|||
project.id
|
||||
end
|
||||
|
||||
# For now the only user who participants is the user who triggered
|
||||
def participants(current_user = nil)
|
||||
[user]
|
||||
end
|
||||
|
||||
def valid_commit_sha
|
||||
if self.sha == Gitlab::Git::BLANK_SHA
|
||||
self.errors.add(:sha, " cant be 00000000 (branch removal)")
|
||||
|
|
|
@ -32,7 +32,9 @@ class NotificationSetting < ActiveRecord::Base
|
|||
:reopen_merge_request,
|
||||
:close_merge_request,
|
||||
:reassign_merge_request,
|
||||
:merge_merge_request
|
||||
:merge_merge_request,
|
||||
:failed_pipeline,
|
||||
:success_pipeline
|
||||
]
|
||||
|
||||
store :events, accessors: EMAIL_EVENTS, coder: JSON
|
||||
|
|
|
@ -34,7 +34,8 @@ class PipelinesEmailService < Service
|
|||
|
||||
return unless all_recipients.any?
|
||||
|
||||
PipelineEmailWorker.perform_async(data, all_recipients)
|
||||
pipeline = Ci::Pipeline.find(data[:object_attributes][:id])
|
||||
Ci::SendPipelineNotificationService.new(pipeline).execute(all_recipients)
|
||||
end
|
||||
|
||||
def can_test?
|
||||
|
|
13
app/services/ci/send_pipeline_notification_service.rb
Normal file
13
app/services/ci/send_pipeline_notification_service.rb
Normal file
|
@ -0,0 +1,13 @@
|
|||
module Ci
|
||||
class SendPipelineNotificationService < BaseService
|
||||
attr_reader :pipeline
|
||||
|
||||
def initialize(new_pipeline)
|
||||
@pipeline = new_pipeline
|
||||
end
|
||||
|
||||
def execute(recipients = nil)
|
||||
notification_service.pipeline_finished(pipeline, recipients)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -311,6 +311,22 @@ class NotificationService
|
|||
mailer.project_was_not_exported_email(current_user, project, errors).deliver_later
|
||||
end
|
||||
|
||||
def pipeline_finished(pipeline, recipients = nil)
|
||||
email_template = "pipeline_#{pipeline.status}_email"
|
||||
|
||||
return unless mailer.respond_to?(email_template)
|
||||
|
||||
recipients ||= build_recipients(
|
||||
pipeline,
|
||||
pipeline.project,
|
||||
pipeline.user,
|
||||
action: pipeline.status)
|
||||
|
||||
recipients.each do |to|
|
||||
Notify.public_send(email_template, pipeline, to).deliver_later
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
# Get project/group users with CUSTOM notification level
|
||||
|
@ -613,6 +629,6 @@ class NotificationService
|
|||
# Build event key to search on custom notification level
|
||||
# Check NotificationSetting::EMAIL_EVENTS
|
||||
def build_custom_key(action, object)
|
||||
"#{action}_#{object.class.name.underscore}".to_sym
|
||||
"#{action}_#{object.class.model_name.name.underscore}".to_sym
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,39 +0,0 @@
|
|||
class PipelineEmailWorker
|
||||
include Sidekiq::Worker
|
||||
|
||||
ParamsStruct = Struct.new(:pipeline, :project, :email_template)
|
||||
class Params < ParamsStruct
|
||||
def initialize(pipeline_id)
|
||||
self.pipeline = Ci::Pipeline.find(pipeline_id)
|
||||
self.project = pipeline.project
|
||||
self.email_template = case pipeline.status
|
||||
when 'success'
|
||||
:pipeline_succeeded_email
|
||||
when 'failed'
|
||||
:pipeline_failed_email
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def perform(data, recipients)
|
||||
params = Params.new(data['object_attributes']['id'])
|
||||
|
||||
return unless params.email_template
|
||||
|
||||
recipients.each do |to|
|
||||
deliver(params, to) do
|
||||
Notify.public_send(params.email_template, params, to).deliver_now
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def deliver(params, to)
|
||||
yield
|
||||
# These are input errors and won't be corrected even if Sidekiq retries
|
||||
rescue Net::SMTPFatalError, Net::SMTPSyntaxError => e
|
||||
project_name = params.project.path_with_namespace
|
||||
logger.info("Failed to send email for #{project_name} to #{to}: #{e}")
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue