Commit 045c6715 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Use bcc for pipeline emails because:

We use bcc here because we don't want to generate this emails for a
thousand times. This could be potentially expensive in a loop, and
recipients would contain all project watchers so it could be a lot.
parent 12ef494d
module Emails module Emails
module Pipelines module Pipelines
def pipeline_success_email(pipeline, to) def pipeline_success_email(pipeline, recipients)
pipeline_mail(pipeline, to, 'succeeded') pipeline_mail(pipeline, recipients, 'succeeded')
end end
def pipeline_failed_email(pipeline, to) def pipeline_failed_email(pipeline, recipients)
pipeline_mail(pipeline, to, 'failed') pipeline_mail(pipeline, recipients, 'failed')
end end
private private
def pipeline_mail(pipeline, to, status) def pipeline_mail(pipeline, recipients, status)
@project = pipeline.project @project = pipeline.project
@pipeline = pipeline @pipeline = pipeline
@merge_request = pipeline.merge_requests.first @merge_request = pipeline.merge_requests.first
add_headers add_headers
mail(to: to, subject: pipeline_subject(status), skip_premailer: true) do |format| # We use bcc here because we don't want to generate this emails for a
# thousand times. This could be potentially expensive in a loop, and
# recipients would contain all project watchers so it could be a lot.
mail(bcc: recipients,
subject: pipeline_subject(status),
skip_premailer: true) do |format|
format.html { render layout: false } format.html { render layout: false }
format.text format.text
end end
......
...@@ -323,9 +323,7 @@ class NotificationService ...@@ -323,9 +323,7 @@ class NotificationService
nil, # The acting user, who won't be added to recipients nil, # The acting user, who won't be added to recipients
action: pipeline.status).map(&:email) action: pipeline.status).map(&:email)
recipients.each do |to| mailer.public_send(email_template, pipeline, recipients).deliver_later
mailer.public_send(email_template, pipeline, to).deliver_later
end
end end
protected protected
......
...@@ -536,7 +536,7 @@ describe Ci::Pipeline, models: true do ...@@ -536,7 +536,7 @@ describe Ci::Pipeline, models: true do
shared_examples 'sending a notification' do shared_examples 'sending a notification' do
it 'sends an email' do it 'sends an email' do
should_only_email(pipeline.user) should_only_email(pipeline.user, kind: :bcc)
end end
end end
......
...@@ -7,8 +7,8 @@ module EmailHelpers ...@@ -7,8 +7,8 @@ module EmailHelpers
ActionMailer::Base.deliveries.clear ActionMailer::Base.deliveries.clear
end end
def should_only_email(*users) def should_only_email(*users, kind: :to)
recipients = email_recipients recipients = email_recipients(kind: kind)
users.each { |user| should_email(user, recipients) } users.each { |user| should_email(user, recipients) }
...@@ -27,7 +27,7 @@ module EmailHelpers ...@@ -27,7 +27,7 @@ module EmailHelpers
expect(ActionMailer::Base.deliveries).to be_empty expect(ActionMailer::Base.deliveries).to be_empty
end end
def email_recipients def email_recipients(kind: :to)
ActionMailer::Base.deliveries.flat_map(&:to) ActionMailer::Base.deliveries.flat_map(&kind)
end end
end end
...@@ -26,15 +26,13 @@ describe PipelineNotificationWorker do ...@@ -26,15 +26,13 @@ describe PipelineNotificationWorker do
subject.perform(pipeline.id) subject.perform(pipeline.id)
end end
expected_receivers = [pusher, watcher].uniq.sort_by(&:email) emails = ActionMailer::Base.deliveries
actual = ActionMailer::Base.deliveries.sort_by(&:to) actual = emails.flat_map(&:bcc).sort
expected_receivers = [pusher, watcher].map(&:email).uniq.sort
expect(expected_receivers.size).to eq(actual.size) expect(actual).to eq(expected_receivers)
expect(emails.size).to eq(1)
actual.zip(expected_receivers).each do |(email, receiver)| expect(emails.last.subject).to include(email_subject)
expect(email.subject).to include(email_subject)
expect(email.to).to eq([receiver.email])
end
end end
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