2018-08-10 02:45:01 -04:00
# frozen_string_literal: true
2013-12-17 05:42:40 -05:00
class EmailsOnPushService < Service
2019-12-31 10:08:53 -05:00
include NotificationBranchSelection
2016-12-05 09:40:53 -05:00
boolean_accessor :send_from_committer_email
boolean_accessor :disable_diffs
2019-12-31 10:08:53 -05:00
prop_accessor :recipients , :branches_to_be_notified
2018-01-26 08:48:47 -05:00
validates :recipients , presence : true , if : :valid_recipients?
2013-12-17 05:42:40 -05:00
def title
2019-04-12 08:28:07 -04:00
s_ ( 'EmailsOnPushService|Emails on push' )
2013-12-17 05:42:40 -05:00
end
def description
2019-04-12 08:28:07 -04:00
s_ ( 'EmailsOnPushService|Email the commits and diff of each push to a list of recipients.' )
2013-12-17 05:42:40 -05:00
end
2016-12-27 07:44:24 -05:00
def self . to_param
2013-12-17 05:42:40 -05:00
'emails_on_push'
end
2016-12-27 07:44:24 -05:00
def self . supported_events
2015-03-17 08:55:39 -04:00
%w( push tag_push )
2015-02-28 11:33:18 -05:00
end
2019-12-31 10:08:53 -05:00
def initialize_properties
2020-02-03 04:08:42 -05:00
super
self . branches_to_be_notified = 'all' if branches_to_be_notified . nil?
2019-12-31 10:08:53 -05:00
end
2015-03-08 19:49:11 -04:00
def execute ( push_data )
return unless supported_events . include? ( push_data [ :object_kind ] )
2019-08-15 13:37:36 -04:00
return if project . emails_disabled?
2019-12-31 10:08:53 -05:00
return unless notify_for_ref? ( push_data )
2015-02-19 00:02:57 -05:00
2015-03-23 10:00:12 -04:00
EmailsOnPushWorker . perform_async (
2016-12-05 09:40:53 -05:00
project_id ,
recipients ,
push_data ,
send_from_committer_email : send_from_committer_email? ,
disable_diffs : disable_diffs?
2015-03-23 10:00:12 -04:00
)
2015-03-08 19:49:11 -04:00
end
2019-12-31 10:08:53 -05:00
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
2015-02-25 08:05:45 -05:00
def send_from_committer_email?
2016-12-05 09:40:53 -05:00
Gitlab :: Utils . to_boolean ( self . send_from_committer_email )
2015-02-25 08:05:45 -05:00
end
2015-02-25 08:29:49 -05:00
def disable_diffs?
2016-12-05 09:40:53 -05:00
Gitlab :: Utils . to_boolean ( self . disable_diffs )
2013-12-17 05:42:40 -05:00
end
def fields
2015-02-25 09:56:42 -05:00
domains = Notify . allowed_email_domains . map { | domain | " user@ #{ domain } " } . join ( " , " )
2013-12-17 05:42:40 -05:00
[
2019-04-12 08:28:07 -04:00
{ type : 'checkbox' , name : 'send_from_committer_email' , title : s_ ( " EmailsOnPushService|Send from committer " ) ,
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. " ) } ,
2020-04-21 11:21:10 -04:00
{ type : 'select' , name : 'branches_to_be_notified' , choices : branch_choices } ,
2019-04-12 08:28:07 -04:00
{ type : 'textarea' , name : 'recipients' , placeholder : s_ ( 'EmailsOnPushService|Emails separated by whitespace' ) }
2013-12-17 05:42:40 -05:00
]
end
end