BigW Consortium Gitlab

Commit 4add6ca6 by Lin Jen-Shin

Try to integrate the email into notification system

parent 1b1c6ebf
module Emails module Emails
module Pipelines module Pipelines
def pipeline_succeeded_email(params, to) def pipeline_success_email(pipeline, to)
pipeline_mail(params, to, 'succeeded') pipeline_mail(pipeline, to, 'succeeded')
end end
def pipeline_failed_email(params, to) def pipeline_failed_email(pipeline, to)
pipeline_mail(params, to, 'failed') pipeline_mail(pipeline, to, 'failed')
end end
private private
def pipeline_mail(params, to, status) def pipeline_mail(pipeline, to, status)
@project = params.project @project = pipeline.project
@pipeline = params.pipeline @pipeline = pipeline
add_headers add_headers
mail(to: to, subject: pipeline_subject(status)) mail(to: to, subject: pipeline_subject(status))
......
...@@ -62,6 +62,10 @@ module Ci ...@@ -62,6 +62,10 @@ module Ci
after_transition do |pipeline, transition| after_transition do |pipeline, transition|
pipeline.execute_hooks unless transition.loopback? pipeline.execute_hooks unless transition.loopback?
end end
after_transition any => [:success, :failed] do |pipeline, transition|
SendPipelineNotificationService.new(pipeline).execute
end
end end
# ref can't be HEAD or SHA, can only be branch/tag name # ref can't be HEAD or SHA, can only be branch/tag name
...@@ -90,6 +94,11 @@ module Ci ...@@ -90,6 +94,11 @@ module Ci
project.id project.id
end end
# For now the only user who participants is the user who triggered
def participants(current_user = nil)
[user]
end
def valid_commit_sha def valid_commit_sha
if self.sha == Gitlab::Git::BLANK_SHA if self.sha == Gitlab::Git::BLANK_SHA
self.errors.add(:sha, " cant be 00000000 (branch removal)") self.errors.add(:sha, " cant be 00000000 (branch removal)")
......
...@@ -32,7 +32,9 @@ class NotificationSetting < ActiveRecord::Base ...@@ -32,7 +32,9 @@ class NotificationSetting < ActiveRecord::Base
:reopen_merge_request, :reopen_merge_request,
:close_merge_request, :close_merge_request,
:reassign_merge_request, :reassign_merge_request,
:merge_merge_request :merge_merge_request,
:failed_pipeline,
:success_pipeline
] ]
store :events, accessors: EMAIL_EVENTS, coder: JSON store :events, accessors: EMAIL_EVENTS, coder: JSON
......
...@@ -34,7 +34,8 @@ class PipelinesEmailService < Service ...@@ -34,7 +34,8 @@ class PipelinesEmailService < Service
return unless all_recipients.any? 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 end
def can_test? def can_test?
......
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 ...@@ -311,6 +311,22 @@ class NotificationService
mailer.project_was_not_exported_email(current_user, project, errors).deliver_later mailer.project_was_not_exported_email(current_user, project, errors).deliver_later
end 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 protected
# Get project/group users with CUSTOM notification level # Get project/group users with CUSTOM notification level
...@@ -613,6 +629,6 @@ class NotificationService ...@@ -613,6 +629,6 @@ class NotificationService
# Build event key to search on custom notification level # Build event key to search on custom notification level
# Check NotificationSetting::EMAIL_EVENTS # Check NotificationSetting::EMAIL_EVENTS
def build_custom_key(action, object) def build_custom_key(action, object)
"#{action}_#{object.class.name.underscore}".to_sym "#{action}_#{object.class.model_name.name.underscore}".to_sym
end end
end end
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment