diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index 4fdb5fef4fbe72b13b465d0b35a98e48e2f2ca95..c293096f5c9972699bf2fe5ddb1221db70420bdc 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -82,6 +82,10 @@ module Ci PipelineHooksWorker.perform_async(id) end 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 @@ -110,6 +114,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 121b598b8f3839f67a9963bc31dfb3ad9b79f597..43fc218de2b5d458ab85add171d173ab8d2c0880 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/services/ci/send_pipeline_notification_service.rb b/app/services/ci/send_pipeline_notification_service.rb index ceb182801f7f5a788acb0ac475172897578b0a62..cfbcad5dadfe719bb431998a435fb8af3b93b367 100644 --- a/app/services/ci/send_pipeline_notification_service.rb +++ b/app/services/ci/send_pipeline_notification_service.rb @@ -6,14 +6,8 @@ module Ci @pipeline = new_pipeline end - def execute(recipients) - email_template = "pipeline_#{pipeline.status}_email" - - return unless Notify.respond_to?(email_template) - - recipients.each do |to| - Notify.public_send(email_template, pipeline, to).deliver_later - 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 72712afc07e529cc7190c5abc247e1a70ac3438b..65091c2b8af32d73aa3674cd490542bf91a5efb5 100644 --- a/app/services/notification_service.rb +++ b/app/services/notification_service.rb @@ -312,6 +312,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, + nil, # The acting user, who won't be added to recipients + action: pipeline.status) + + recipients.each do |to| + mailer.public_send(email_template, pipeline, to.email).deliver_later + end + end + protected # Get project/group users with CUSTOM notification level diff --git a/spec/services/ci/send_pipeline_notification_service_spec.rb b/spec/services/ci/send_pipeline_notification_service_spec.rb index 9c840967aedc247fab806d6d2cade824d9a428f0..110e16410c5729a002246c19c180b5b4d78f7d3c 100644 --- a/spec/services/ci/send_pipeline_notification_service_spec.rb +++ b/spec/services/ci/send_pipeline_notification_service_spec.rb @@ -23,7 +23,7 @@ describe Ci::SendPipelineNotificationService, services: true do shared_examples 'sending emails' do it 'sends emails' do perform_enqueued_jobs do - subject.execute([user.email]) + subject.execute end expected_receivers = [pusher, watcher].uniq.sort_by(&:email)