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
class EmailsOnPushService < Service
include NotificationBranchSelection
boolean_accessor :send_from_committer_email
boolean_accessor :disable_diffs
prop_accessor :recipients
prop_accessor :recipients, :branches_to_be_notified
validates :recipients, presence: true, if: :valid_recipients?
def title
......@@ -22,9 +24,17 @@ class EmailsOnPushService < Service
%w(push tag_push)
end
def initialize_properties
if properties.nil?
self.properties = {}
self.branches_to_be_notified ||= "all"
end
end
def execute(push_data)
return unless supported_events.include?(push_data[:object_kind])
return if project.emails_disabled?
return unless notify_for_ref?(push_data)
EmailsOnPushWorker.perform_async(
project_id,
......@@ -35,6 +45,13 @@ class EmailsOnPushService < Service
)
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?
Gitlab::Utils.to_boolean(self.send_from_committer_email)
end
......@@ -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 } },
{ type: 'checkbox', name: 'disable_diffs', title: s_("EmailsOnPushService|Disable code diffs"),
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') }
]
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
name: :send_from_committer_email,
type: Boolean,
desc: 'Send from committer'
},
{
required: false,
name: :branches_to_be_notified,
type: String,
desc: 'Branches for which notifications are to be sent'
}
],
'external-wiki' => [
......
......@@ -25,19 +25,113 @@ describe EmailsOnPushService do
let(:push_data) { { object_kind: 'push' } }
let(:project) { create(:project, :repository) }
let(:service) { create(:emails_on_push_service, project: project) }
let(:recipients) { 'test@gitlab.com' }
it 'does not send emails when disabled' do
expect(project).to receive(:emails_disabled?).and_return(true)
expect(EmailsOnPushWorker).not_to receive(:perform_async)
before do
subject.recipients = recipients
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
it 'does send emails when enabled' do
expect(project).to receive(:emails_disabled?).and_return(false)
expect(EmailsOnPushWorker).to receive(:perform_async)
shared_examples 'not sending email' do |branches_to_be_notified|
before do
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
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