Commit 36eafbed authored by Balasankar "Balu" C's avatar Balasankar "Balu" C

Add option to configure branches to send emails on push

Signed-off-by: default avatarBalasankar "Balu" C <balasankarc@autistici.org>
parent 13dfcf8e
# frozen_string_literal: true # frozen_string_literal: true
class EmailsOnPushService < Service class EmailsOnPushService < Service
include NotificationBranchSelection
boolean_accessor :send_from_committer_email boolean_accessor :send_from_committer_email
boolean_accessor :disable_diffs boolean_accessor :disable_diffs
prop_accessor :recipients prop_accessor :recipients, :branches_to_be_notified
validates :recipients, presence: true, if: :valid_recipients? validates :recipients, presence: true, if: :valid_recipients?
def title def title
...@@ -22,9 +24,17 @@ class EmailsOnPushService < Service ...@@ -22,9 +24,17 @@ class EmailsOnPushService < Service
%w(push tag_push) %w(push tag_push)
end end
def initialize_properties
if properties.nil?
self.properties = {}
self.branches_to_be_notified ||= "all"
end
end
def execute(push_data) def execute(push_data)
return unless supported_events.include?(push_data[:object_kind]) return unless supported_events.include?(push_data[:object_kind])
return if project.emails_disabled? return if project.emails_disabled?
return unless notify_for_ref?(push_data)
EmailsOnPushWorker.perform_async( EmailsOnPushWorker.perform_async(
project_id, project_id,
...@@ -35,6 +45,13 @@ class EmailsOnPushService < Service ...@@ -35,6 +45,13 @@ class EmailsOnPushService < Service
) )
end end
def notify_for_ref?(push_data)
return true if push_data[:object_kind] == 'tag_push'
return true if push_data.dig(:object_attributes, :tag)
notify_for_branch?(push_data)
end
def send_from_committer_email? def send_from_committer_email?
Gitlab::Utils.to_boolean(self.send_from_committer_email) Gitlab::Utils.to_boolean(self.send_from_committer_email)
end end
...@@ -50,6 +67,7 @@ class EmailsOnPushService < Service ...@@ -50,6 +67,7 @@ class EmailsOnPushService < Service
help: s_("EmailsOnPushService|Send notifications from the committer's email address if the domain is part of the domain GitLab is running on (e.g. %{domains}).") % { domains: domains } }, help: s_("EmailsOnPushService|Send notifications from the committer's email address if the domain is part of the domain GitLab is running on (e.g. %{domains}).") % { domains: domains } },
{ type: 'checkbox', name: 'disable_diffs', title: s_("EmailsOnPushService|Disable code diffs"), { type: 'checkbox', name: 'disable_diffs', title: s_("EmailsOnPushService|Disable code diffs"),
help: s_("EmailsOnPushService|Don't include possibly sensitive code diffs in notification body.") }, help: s_("EmailsOnPushService|Don't include possibly sensitive code diffs in notification body.") },
{ type: 'select', name: 'branches_to_be_notified', choices: BRANCH_CHOICES },
{ type: 'textarea', name: 'recipients', placeholder: s_('EmailsOnPushService|Emails separated by whitespace') } { type: 'textarea', name: 'recipients', placeholder: s_('EmailsOnPushService|Emails separated by whitespace') }
] ]
end end
......
---
title: Add option to configure branches for which to send emails on push
merge_request: 22196
author:
type: added
...@@ -365,6 +365,12 @@ module API ...@@ -365,6 +365,12 @@ module API
name: :send_from_committer_email, name: :send_from_committer_email,
type: Boolean, type: Boolean,
desc: 'Send from committer' desc: 'Send from committer'
},
{
required: false,
name: :branches_to_be_notified,
type: String,
desc: 'Branches for which notifications are to be sent'
} }
], ],
'external-wiki' => [ 'external-wiki' => [
......
...@@ -25,19 +25,113 @@ describe EmailsOnPushService do ...@@ -25,19 +25,113 @@ describe EmailsOnPushService do
let(:push_data) { { object_kind: 'push' } } let(:push_data) { { object_kind: 'push' } }
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
let(:service) { create(:emails_on_push_service, project: project) } let(:service) { create(:emails_on_push_service, project: project) }
let(:recipients) { 'test@gitlab.com' }
it 'does not send emails when disabled' do before do
expect(project).to receive(:emails_disabled?).and_return(true) subject.recipients = recipients
expect(EmailsOnPushWorker).not_to receive(:perform_async) end
shared_examples 'sending email' do |branches_to_be_notified|
before do
subject.branches_to_be_notified = branches_to_be_notified
end
it 'sends email' do
expect(EmailsOnPushWorker).not_to receive(:perform_async)
service.execute(push_data) service.execute(push_data)
end
end end
it 'does send emails when enabled' do shared_examples 'not sending email' do |branches_to_be_notified|
expect(project).to receive(:emails_disabled?).and_return(false) before do
expect(EmailsOnPushWorker).to receive(:perform_async) subject.branches_to_be_notified = branches_to_be_notified
end
it 'does not send email' do
expect(EmailsOnPushWorker).not_to receive(:perform_async)
service.execute(push_data)
end
end
context 'when emails are disabled on the project' do
it 'does not send emails' do
expect(project).to receive(:emails_disabled?).and_return(true)
expect(EmailsOnPushWorker).not_to receive(:perform_async)
service.execute(push_data)
end
end
context 'when emails are enabled on the project' do
before do
expect(project).to receive(:emails_disabled?).and_return(true)
end
service.execute(push_data) context 'pushing to the default branch' do
let(:push_data) { { object_kind: 'push', object_attributes: { ref: project.default_branch } } }
context 'when configured to send email on pushes to any branch' do
it_behaves_like 'sending email', branches_to_be_notified: "all"
end
context 'when configured to send email on pushes to default branch' do
it_behaves_like 'sending email', branches_to_be_notified: "default"
end
context 'when configured to send email on pushes to protected branches only' do
it_behaves_like 'not sending email', branches_to_be_notified: "protected"
end
context 'when configured to send email on pushes to default and protected branches only' do
it_behaves_like 'sending email', branches_to_be_notified: "default_and_protected"
end
end
context 'pushing to a protected branch' do
before do
create(:protected_branch, project: project, name: 'a-protected-branch')
end
let(:push_data) { { object_kind: 'push', object_attributes: { ref: 'a-protected-branch' } } }
context 'when configured to send email on pushes to any branch' do
it_behaves_like 'sending email', branches_to_be_notified: "all"
end
context 'when configured to send email on pushes to default branch' do
it_behaves_like 'not sending email', branches_to_be_notified: "default"
end
context 'when configured to send email on pushes to protected branches only' do
it_behaves_like 'sending email', branches_to_be_notified: "protected"
end
context 'when configured to send email on pushes to default and protected branches only' do
it_behaves_like 'sending email', branches_to_be_notified: "default_and_protected"
end
end
context 'pushing to a random branch' do
let(:push_data) { { object_kind: 'push', object_attributes: { ref: 'a-random-branch' } } }
context 'when configured to send email on pushes to any branch' do
it_behaves_like 'sending email', branches_to_be_notified: "all"
end
context 'when configured to send email on pushes to default branch' do
it_behaves_like 'not sending email', branches_to_be_notified: "default"
end
context 'when configured to send email on pushes to protected branches only' do
it_behaves_like 'not sending email', branches_to_be_notified: "protected"
end
context 'when configured to send email on pushes to default and protected branches only' do
it_behaves_like 'not sending email', branches_to_be_notified: "default_and_protected"
end
end
end 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