Send notification emails to the "project", and put people in Cc

This fixes email threading in Mail.app, that doesn't like when a thread
doesn't have stable recipients.

For instance, here is a possible sender-recipient combinations before:

From: A
To: Me
New issue

From: B
To: Me
Reply on new issue

From: A
To: Me
Another reply

Mail.app doesn't see B as a participant to the original email thread,
and decides to break the thread: it will group all messages from A
together, and separately all messages from B.

This commit makes the thread look like this:

From: A
To: gitlab/project
Cc: Me
New issue

From: B
To: gitlab/project
Cc: Me
Reply on new issue

From: A
To: gitlab/project
Cc: Me
Another reply

Mail.app sees a common recipient, and group the thread correctly.
parent 772f2f1a
...@@ -4,7 +4,7 @@ module Emails ...@@ -4,7 +4,7 @@ module Emails
@membership = UsersGroup.find(user_group_id) @membership = UsersGroup.find(user_group_id)
@group = @membership.group @group = @membership.group
@target_url = group_url(@group) @target_url = group_url(@group)
mail(to: @membership.user.email, mail(cc: @membership.user.email,
subject: subject("Access to group was granted")) subject: subject("Access to group was granted"))
end end
end end
......
...@@ -6,7 +6,7 @@ module Emails ...@@ -6,7 +6,7 @@ module Emails
@target_url = project_issue_url(@project, @issue) @target_url = project_issue_url(@project, @issue)
set_message_id("issue_#{issue_id}") set_message_id("issue_#{issue_id}")
mail(from: sender(@issue.author_id), mail(from: sender(@issue.author_id),
to: recipient(recipient_id), cc: recipient(recipient_id),
subject: subject("#{@issue.title} (##{@issue.iid})")) subject: subject("#{@issue.title} (##{@issue.iid})"))
end end
...@@ -17,7 +17,7 @@ module Emails ...@@ -17,7 +17,7 @@ module Emails
@target_url = project_issue_url(@project, @issue) @target_url = project_issue_url(@project, @issue)
set_reference("issue_#{issue_id}") set_reference("issue_#{issue_id}")
mail(from: sender(updated_by_user_id), mail(from: sender(updated_by_user_id),
to: recipient(recipient_id), cc: recipient(recipient_id),
subject: subject("#{@issue.title} (##{@issue.iid})")) subject: subject("#{@issue.title} (##{@issue.iid})"))
end end
...@@ -28,7 +28,7 @@ module Emails ...@@ -28,7 +28,7 @@ module Emails
@target_url = project_issue_url(@project, @issue) @target_url = project_issue_url(@project, @issue)
set_reference("issue_#{issue_id}") set_reference("issue_#{issue_id}")
mail(from: sender(updated_by_user_id), mail(from: sender(updated_by_user_id),
to: recipient(recipient_id), cc: recipient(recipient_id),
subject: subject("#{@issue.title} (##{@issue.iid})")) subject: subject("#{@issue.title} (##{@issue.iid})"))
end end
...@@ -40,7 +40,7 @@ module Emails ...@@ -40,7 +40,7 @@ module Emails
@target_url = project_issue_url(@project, @issue) @target_url = project_issue_url(@project, @issue)
set_reference("issue_#{issue_id}") set_reference("issue_#{issue_id}")
mail(from: sender(updated_by_user_id), mail(from: sender(updated_by_user_id),
to: recipient(recipient_id), cc: recipient(recipient_id),
subject: subject("#{@issue.title} (##{@issue.iid})")) subject: subject("#{@issue.title} (##{@issue.iid})"))
end end
end end
......
...@@ -6,7 +6,7 @@ module Emails ...@@ -6,7 +6,7 @@ module Emails
@target_url = project_merge_request_url(@project, @merge_request) @target_url = project_merge_request_url(@project, @merge_request)
set_message_id("merge_request_#{merge_request_id}") set_message_id("merge_request_#{merge_request_id}")
mail(from: sender(@merge_request.author_id), mail(from: sender(@merge_request.author_id),
to: recipient(recipient_id), cc: recipient(recipient_id),
subject: subject("#{@merge_request.title} (##{@merge_request.iid})")) subject: subject("#{@merge_request.title} (##{@merge_request.iid})"))
end end
...@@ -17,7 +17,7 @@ module Emails ...@@ -17,7 +17,7 @@ module Emails
@target_url = project_merge_request_url(@project, @merge_request) @target_url = project_merge_request_url(@project, @merge_request)
set_reference("merge_request_#{merge_request_id}") set_reference("merge_request_#{merge_request_id}")
mail(from: sender(updated_by_user_id), mail(from: sender(updated_by_user_id),
to: recipient(recipient_id), cc: recipient(recipient_id),
subject: subject("#{@merge_request.title} (##{@merge_request.iid})")) subject: subject("#{@merge_request.title} (##{@merge_request.iid})"))
end end
...@@ -28,7 +28,7 @@ module Emails ...@@ -28,7 +28,7 @@ module Emails
@target_url = project_merge_request_url(@project, @merge_request) @target_url = project_merge_request_url(@project, @merge_request)
set_reference("merge_request_#{merge_request_id}") set_reference("merge_request_#{merge_request_id}")
mail(from: sender(updated_by_user_id), mail(from: sender(updated_by_user_id),
to: recipient(recipient_id), cc: recipient(recipient_id),
subject: subject("#{@merge_request.title} (##{@merge_request.iid})")) subject: subject("#{@merge_request.title} (##{@merge_request.iid})"))
end end
...@@ -38,7 +38,7 @@ module Emails ...@@ -38,7 +38,7 @@ module Emails
@target_url = project_merge_request_url(@project, @merge_request) @target_url = project_merge_request_url(@project, @merge_request)
set_reference("merge_request_#{merge_request_id}") set_reference("merge_request_#{merge_request_id}")
mail(from: sender(updated_by_user_id), mail(from: sender(updated_by_user_id),
to: recipient(recipient_id), cc: recipient(recipient_id),
subject: subject("#{@merge_request.title} (##{@merge_request.iid})")) subject: subject("#{@merge_request.title} (##{@merge_request.iid})"))
end end
end end
......
...@@ -6,7 +6,7 @@ module Emails ...@@ -6,7 +6,7 @@ module Emails
@project = @note.project @project = @note.project
@target_url = project_commit_url(@project, @commit, anchor: "note_#{@note.id}") @target_url = project_commit_url(@project, @commit, anchor: "note_#{@note.id}")
mail(from: sender(@note.author_id), mail(from: sender(@note.author_id),
to: recipient(recipient_id), cc: recipient(recipient_id),
subject: subject("#{@commit.title} (#{@commit.short_id})")) subject: subject("#{@commit.title} (#{@commit.short_id})"))
end end
...@@ -17,7 +17,7 @@ module Emails ...@@ -17,7 +17,7 @@ module Emails
@target_url = project_issue_url(@project, @issue, anchor: "note_#{@note.id}") @target_url = project_issue_url(@project, @issue, anchor: "note_#{@note.id}")
set_reference("issue_#{@issue.id}") set_reference("issue_#{@issue.id}")
mail(from: sender(@note.author_id), mail(from: sender(@note.author_id),
to: recipient(recipient_id), cc: recipient(recipient_id),
subject: subject("#{@issue.title} (##{@issue.iid})")) subject: subject("#{@issue.title} (##{@issue.iid})"))
end end
...@@ -28,7 +28,7 @@ module Emails ...@@ -28,7 +28,7 @@ module Emails
@target_url = project_merge_request_url(@project, @merge_request, anchor: "note_#{@note.id}") @target_url = project_merge_request_url(@project, @merge_request, anchor: "note_#{@note.id}")
set_reference("merge_request_#{@merge_request.id}") set_reference("merge_request_#{@merge_request.id}")
mail(from: sender(@note.author_id), mail(from: sender(@note.author_id),
to: recipient(recipient_id), cc: recipient(recipient_id),
subject: subject("#{@merge_request.title} (##{@merge_request.iid})")) subject: subject("#{@merge_request.title} (##{@merge_request.iid})"))
end end
...@@ -37,7 +37,7 @@ module Emails ...@@ -37,7 +37,7 @@ module Emails
@project = @note.project @project = @note.project
@target_url = project_wall_url(@note.project, anchor: "note_#{@note.id}") @target_url = project_wall_url(@note.project, anchor: "note_#{@note.id}")
mail(from: sender(@note.author_id), mail(from: sender(@note.author_id),
to: recipient(recipient_id), cc: recipient(recipient_id),
subject: subject("Note on wall")) subject: subject("Note on wall"))
end end
end end
......
...@@ -4,7 +4,7 @@ module Emails ...@@ -4,7 +4,7 @@ module Emails
@users_project = UsersProject.find user_project_id @users_project = UsersProject.find user_project_id
@project = @users_project.project @project = @users_project.project
@target_url = project_url(@project) @target_url = project_url(@project)
mail(to: @users_project.user.email, mail(cc: @users_project.user.email,
subject: subject("Access to project was granted")) subject: subject("Access to project was granted"))
end end
...@@ -12,7 +12,7 @@ module Emails ...@@ -12,7 +12,7 @@ module Emails
@user = User.find user_id @user = User.find user_id
@project = Project.find project_id @project = Project.find project_id
@target_url = project_url(@project) @target_url = project_url(@project)
mail(to: @user.email, mail(cc: @user.email,
subject: subject("Project was moved")) subject: subject("Project was moved"))
end end
...@@ -30,7 +30,7 @@ module Emails ...@@ -30,7 +30,7 @@ module Emails
end end
mail(from: sender(author_id), mail(from: sender(author_id),
to: recipient, cc: recipient,
subject: subject("New push to repository")) subject: subject("New push to repository"))
end end
end end
......
class Notify < ActionMailer::Base class Notify < ActionMailer::Base
include ActionDispatch::Routing::PolymorphicRoutes
include Emails::Issues include Emails::Issues
include Emails::MergeRequests include Emails::MergeRequests
include Emails::Notes include Emails::Notes
...@@ -16,6 +18,7 @@ class Notify < ActionMailer::Base ...@@ -16,6 +18,7 @@ class Notify < ActionMailer::Base
default_url_options[:script_name] = Gitlab.config.gitlab.relative_url_root default_url_options[:script_name] = Gitlab.config.gitlab.relative_url_root
default from: Proc.new { default_sender_address.format } default from: Proc.new { default_sender_address.format }
default to: Proc.new { project_sender_address.format }
default reply_to: "noreply@#{Gitlab.config.gitlab.host}" default reply_to: "noreply@#{Gitlab.config.gitlab.host}"
# Just send email with 2 seconds delay # Just send email with 2 seconds delay
...@@ -32,6 +35,17 @@ class Notify < ActionMailer::Base ...@@ -32,6 +35,17 @@ class Notify < ActionMailer::Base
address address
end end
# The default email address to send emails to. Includes the project name if possible.
def project_sender_address
if @project
address = default_sender_address
address.display_name = @project.name_with_namespace
address
else
default_sender_address
end
end
# Return an email address that displays the name of the sender. # Return an email address that displays the name of the sender.
# Only the displayed name changes; the actual email address is always the same. # Only the displayed name changes; the actual email address is always the same.
def sender(sender_id) def sender(sender_id)
......
...@@ -10,7 +10,7 @@ describe Notify do ...@@ -10,7 +10,7 @@ describe Notify do
shared_examples 'a multiple recipients email' do shared_examples 'a multiple recipients email' do
it 'is sent to the given recipient' do it 'is sent to the given recipient' do
should deliver_to recipient.email should cc_to recipient.email
end end
end end
...@@ -141,7 +141,7 @@ describe Notify do ...@@ -141,7 +141,7 @@ describe Notify do
end end
it 'is sent to the assignee' do it 'is sent to the assignee' do
should deliver_to assignee.email should cc_to assignee.email
end end
end end
...@@ -394,7 +394,7 @@ describe Notify do ...@@ -394,7 +394,7 @@ describe Notify do
end end
it 'is sent to the given recipient' do it 'is sent to the given recipient' do
should deliver_to recipient.email should cc_to recipient.email
end end
it 'contains the message from the note' do it 'contains the message from the note' do
...@@ -538,7 +538,7 @@ describe Notify do ...@@ -538,7 +538,7 @@ describe Notify do
end end
it 'is sent to recipient' do it 'is sent to recipient' do
should deliver_to 'devs@company.name' should cc_to 'devs@company.name'
end end
it 'has the correct subject' do it 'has the correct subject' do
......
...@@ -22,7 +22,7 @@ describe Issues::CloseService do ...@@ -22,7 +22,7 @@ describe Issues::CloseService do
it 'should send email to user2 about assign of new issue' do it 'should send email to user2 about assign of new issue' do
email = ActionMailer::Base.deliveries.last email = ActionMailer::Base.deliveries.last
email.to.first.should == user2.email email.cc.first.should == user2.email
email.subject.should include(issue.title) email.subject.should include(issue.title)
end end
......
...@@ -31,7 +31,7 @@ describe Issues::UpdateService do ...@@ -31,7 +31,7 @@ describe Issues::UpdateService do
it 'should send email to user2 about assign of new issue' do it 'should send email to user2 about assign of new issue' do
email = ActionMailer::Base.deliveries.last email = ActionMailer::Base.deliveries.last
email.to.first.should == user2.email email.cc.first.should == user2.email
email.subject.should include(issue.title) email.subject.should include(issue.title)
end end
......
...@@ -22,7 +22,7 @@ describe MergeRequests::CloseService do ...@@ -22,7 +22,7 @@ describe MergeRequests::CloseService do
it 'should send email to user2 about assign of new merge_request' do it 'should send email to user2 about assign of new merge_request' do
email = ActionMailer::Base.deliveries.last email = ActionMailer::Base.deliveries.last
email.to.first.should == user2.email email.cc.first.should == user2.email
email.subject.should include(merge_request.title) email.subject.should include(merge_request.title)
end end
......
...@@ -31,7 +31,7 @@ describe MergeRequests::UpdateService do ...@@ -31,7 +31,7 @@ describe MergeRequests::UpdateService do
it 'should send email to user2 about assign of new merge_request' do it 'should send email to user2 about assign of new merge_request' do
email = ActionMailer::Base.deliveries.last email = ActionMailer::Base.deliveries.last
email.to.first.should == user2.email email.cc.first.should == user2.email
email.subject.should include(merge_request.title) email.subject.should include(merge_request.title)
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