Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
36eafbed
Commit
36eafbed
authored
Dec 22, 2019
by
Balasankar "Balu" C
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add option to configure branches to send emails on push
Signed-off-by:
Balasankar "Balu" C
<
balasankarc@autistici.org
>
parent
13dfcf8e
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
132 additions
and
9 deletions
+132
-9
app/models/project_services/emails_on_push_service.rb
app/models/project_services/emails_on_push_service.rb
+19
-1
changelogs/unreleased/email-on-push-select-branches.yml
changelogs/unreleased/email-on-push-select-branches.yml
+5
-0
lib/api/helpers/services_helpers.rb
lib/api/helpers/services_helpers.rb
+6
-0
spec/models/project_services/emails_on_push_service_spec.rb
spec/models/project_services/emails_on_push_service_spec.rb
+102
-8
No files found.
app/models/project_services/emails_on_push_service.rb
View file @
36eafbed
# 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
...
...
changelogs/unreleased/email-on-push-select-branches.yml
0 → 100644
View file @
36eafbed
---
title
:
Add option to configure branches for which to send emails on push
merge_request
:
22196
author
:
type
:
added
lib/api/helpers/services_helpers.rb
View file @
36eafbed
...
...
@@ -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'
=>
[
...
...
spec/models/project_services/emails_on_push_service_spec.rb
View file @
36eafbed
...
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment