diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2016-09-13 19:09:53 +0800 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2016-09-13 19:09:53 +0800 |
commit | 4add6ca6ecb42fe08c0c1fbb1e7d5cf437e8ee3b (patch) | |
tree | 8e368eb60bd8a05d8553508563333ac0b153f352 | |
parent | 1b1c6ebf49c7ea9f838bff0f13f53808845d8979 (diff) | |
download | gitlab-ce-4add6ca6ecb42fe08c0c1fbb1e7d5cf437e8ee3b.tar.gz |
Try to integrate the email into notification system
-rw-r--r-- | app/mailers/emails/pipelines.rb | 14 | ||||
-rw-r--r-- | app/models/ci/pipeline.rb | 9 | ||||
-rw-r--r-- | app/models/notification_setting.rb | 4 | ||||
-rw-r--r-- | app/models/project_services/pipelines_email_service.rb | 3 | ||||
-rw-r--r-- | app/services/ci/send_pipeline_notification_service.rb | 13 | ||||
-rw-r--r-- | app/services/notification_service.rb | 18 | ||||
-rw-r--r-- | app/views/notify/pipeline_success_email.html.haml (renamed from app/views/notify/pipeline_succeeded_email.html.haml) | 0 | ||||
-rw-r--r-- | app/views/notify/pipeline_success_email.text.erb (renamed from app/views/notify/pipeline_succeeded_email.text.erb) | 0 | ||||
-rw-r--r-- | app/workers/pipeline_email_worker.rb | 39 |
9 files changed, 51 insertions, 49 deletions
diff --git a/app/mailers/emails/pipelines.rb b/app/mailers/emails/pipelines.rb index 643b947a049..e132105807a 100644 --- a/app/mailers/emails/pipelines.rb +++ b/app/mailers/emails/pipelines.rb @@ -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)) diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index 0b1df9f4294..b9c46202e5e 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -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)") diff --git a/app/models/notification_setting.rb b/app/models/notification_setting.rb index 121b598b8f3..43fc218de2b 100644 --- a/app/models/notification_setting.rb +++ b/app/models/notification_setting.rb @@ -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 diff --git a/app/models/project_services/pipelines_email_service.rb b/app/models/project_services/pipelines_email_service.rb index 1f852445c1c..7ce0aa7c16e 100644 --- a/app/models/project_services/pipelines_email_service.rb +++ b/app/models/project_services/pipelines_email_service.rb @@ -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? diff --git a/app/services/ci/send_pipeline_notification_service.rb b/app/services/ci/send_pipeline_notification_service.rb new file mode 100644 index 00000000000..d330fa1a73c --- /dev/null +++ b/app/services/ci/send_pipeline_notification_service.rb @@ -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 diff --git a/app/services/notification_service.rb b/app/services/notification_service.rb index 6139ed56e25..de60c49ba84 100644 --- a/app/services/notification_service.rb +++ b/app/services/notification_service.rb @@ -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 diff --git a/app/views/notify/pipeline_succeeded_email.html.haml b/app/views/notify/pipeline_success_email.html.haml index 64cf7cfe103..64cf7cfe103 100644 --- a/app/views/notify/pipeline_succeeded_email.html.haml +++ b/app/views/notify/pipeline_success_email.html.haml diff --git a/app/views/notify/pipeline_succeeded_email.text.erb b/app/views/notify/pipeline_success_email.text.erb index 6821d1f4459..6821d1f4459 100644 --- a/app/views/notify/pipeline_succeeded_email.text.erb +++ b/app/views/notify/pipeline_success_email.text.erb diff --git a/app/workers/pipeline_email_worker.rb b/app/workers/pipeline_email_worker.rb deleted file mode 100644 index 2160207c901..00000000000 --- a/app/workers/pipeline_email_worker.rb +++ /dev/null @@ -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 |