Commit e0c186c3 authored by Douwe Maan's avatar Douwe Maan

Add option to send EmailsOnPush from committer email if domain matches.

See #1809.
parent 0e7d1fd4
...@@ -45,7 +45,8 @@ class Admin::ServicesController < Admin::ApplicationController ...@@ -45,7 +45,8 @@ class Admin::ServicesController < Admin::ApplicationController
:room, :recipients, :project_url, :webhook, :room, :recipients, :project_url, :webhook,
:user_key, :device, :priority, :sound, :bamboo_url, :username, :password, :user_key, :device, :priority, :sound, :bamboo_url, :username, :password,
:build_key, :server, :teamcity_url, :build_type, :build_key, :server, :teamcity_url, :build_type,
:description, :issues_url, :new_issue_url, :restrict_to_branch :description, :issues_url, :new_issue_url, :restrict_to_branch,
:send_from_committer_email
]) ])
end end
end end
...@@ -50,7 +50,8 @@ class Projects::ServicesController < Projects::ApplicationController ...@@ -50,7 +50,8 @@ class Projects::ServicesController < Projects::ApplicationController
:room, :recipients, :project_url, :webhook, :room, :recipients, :project_url, :webhook,
:user_key, :device, :priority, :sound, :bamboo_url, :username, :password, :user_key, :device, :priority, :sound, :bamboo_url, :username, :password,
:build_key, :server, :teamcity_url, :build_type, :build_key, :server, :teamcity_url, :build_type,
:description, :issues_url, :new_issue_url, :restrict_to_branch :description, :issues_url, :new_issue_url, :restrict_to_branch,
:send_from_committer_email
) )
end end
end end
...@@ -16,13 +16,13 @@ module Emails ...@@ -16,13 +16,13 @@ module Emails
subject: subject("Project was moved")) subject: subject("Project was moved"))
end end
def repository_push_email(project_id, recipient, author_id, branch, compare) def repository_push_email(project_id, recipient, author_id, branch, compare, send_from_committer_email = false)
@project = Project.find(project_id) @project = Project.find(project_id)
@author = User.find(author_id) @author = User.find(author_id)
@compare = compare @compare = compare
@commits = Commit.decorate(compare.commits) @commits = Commit.decorate(compare.commits)
@diffs = compare.diffs @diffs = compare.diffs
@branch = branch @branch = branch.gsub("refs/heads/", "")
@subject = "[#{@project.path_with_namespace}][#{@branch}] " @subject = "[#{@project.path_with_namespace}][#{@branch}] "
...@@ -40,7 +40,7 @@ module Emails ...@@ -40,7 +40,7 @@ module Emails
@disable_footer = true @disable_footer = true
mail(from: sender(author_id), mail(from: sender(author_id, send_from_committer_email),
to: recipient, to: recipient,
subject: @subject) subject: @subject)
end end
......
...@@ -45,10 +45,15 @@ class Notify < ActionMailer::Base ...@@ -45,10 +45,15 @@ class Notify < ActionMailer::Base
# 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, send_from_user_email = false)
if sender = User.find(sender_id) if sender = User.find(sender_id)
address = default_sender_address address = default_sender_address
address.display_name = sender.name address.display_name = sender.name
if send_from_user_email && sender.email.end_with?("@#{Gitlab.config.gitlab.host}")
address.address = sender.email
end
address.format address.format
end end
end end
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
# #
class EmailsOnPushService < Service class EmailsOnPushService < Service
prop_accessor :send_from_committer_email
prop_accessor :recipients prop_accessor :recipients
validates :recipients, presence: true, if: :activated? validates :recipients, presence: true, if: :activated?
...@@ -29,12 +30,17 @@ class EmailsOnPushService < Service ...@@ -29,12 +30,17 @@ class EmailsOnPushService < Service
'emails_on_push' 'emails_on_push'
end end
def send_from_committer_email?
self.send_from_committer_email == "1"
end
def execute(push_data) def execute(push_data)
EmailsOnPushWorker.perform_async(project_id, recipients, push_data) EmailsOnPushWorker.perform_async(project_id, recipients, push_data, self.send_from_committer_email?)
end end
def fields def fields
[ [
{ type: 'checkbox', name: 'send_from_committer_email', title: "Send from committer email if domain matches" },
{ type: 'textarea', name: 'recipients', placeholder: 'Emails separated by whitespace' }, { type: 'textarea', name: 'recipients', placeholder: 'Emails separated by whitespace' },
] ]
end end
......
class EmailsOnPushWorker class EmailsOnPushWorker
include Sidekiq::Worker include Sidekiq::Worker
def perform(project_id, recipients, push_data) def perform(project_id, recipients, push_data, send_from_committer_email = false)
project = Project.find(project_id) project = Project.find(project_id)
before_sha = push_data["before"] before_sha = push_data["before"]
after_sha = push_data["after"] after_sha = push_data["after"]
...@@ -19,7 +19,7 @@ class EmailsOnPushWorker ...@@ -19,7 +19,7 @@ class EmailsOnPushWorker
return false unless compare && compare.commits.present? return false unless compare && compare.commits.present?
recipients.split(" ").each do |recipient| recipients.split(" ").each do |recipient|
Notify.repository_push_email(project_id, recipient, author_id, branch, compare).deliver Notify.repository_push_email(project_id, recipient, author_id, branch, compare, send_from_committer_email).deliver
end end
ensure ensure
compare = nil compare = nil
......
...@@ -569,8 +569,9 @@ describe Notify do ...@@ -569,8 +569,9 @@ describe Notify do
let(:compare) { Gitlab::Git::Compare.new(project.repository.raw_repository, sample_image_commit.id, sample_commit.id) } let(:compare) { Gitlab::Git::Compare.new(project.repository.raw_repository, sample_image_commit.id, sample_commit.id) }
let(:commits) { Commit.decorate(compare.commits) } let(:commits) { Commit.decorate(compare.commits) }
let(:diff_path) { namespace_project_compare_path(project.namespace, project, from: commits.first, to: commits.last) } let(:diff_path) { namespace_project_compare_path(project.namespace, project, from: commits.first, to: commits.last) }
let(:send_from_committer_email) { false }
subject { Notify.repository_push_email(project.id, 'devs@company.name', user.id, 'master', compare) } subject { Notify.repository_push_email(project.id, 'devs@company.name', user.id, 'master', compare, send_from_committer_email) }
it 'is sent as the author' do it 'is sent as the author' do
sender = subject.header[:from].addrs[0] sender = subject.header[:from].addrs[0]
...@@ -601,6 +602,33 @@ describe Notify do ...@@ -601,6 +602,33 @@ describe Notify do
it 'doesn not contain the misleading footer' do it 'doesn not contain the misleading footer' do
is_expected.not_to have_body_text /you are a member of/ is_expected.not_to have_body_text /you are a member of/
end end
context "when set to send from committer email if domain matches" do
let(:send_from_committer_email) { true }
context "when the committer email domain matches" do
before do
allow(Gitlab.config.gitlab).to receive(:host).and_return("gitlab.dev")
user.update_attribute(:email, "user@#{Gitlab.config.gitlab.host}")
user.confirm!
end
it "is sent from the committer email" do
sender = subject.header[:from].addrs[0]
expect(sender.address).to eq(user.email)
end
end
context "when the committer email doesn't match" do
it "is sent from the default email" do
sender = subject.header[:from].addrs[0]
expect(sender.address).to eq(gitlab_sender)
end
end
end
end end
describe 'email on push with a single commit' do describe 'email on push with a single commit' do
......
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