Class: EmailsOnPushService

Inherits:
Service show all
Includes:
NotificationBranchSelection
Defined in:
app/models/project_services/emails_on_push_service.rb

Constant Summary

Constants inherited from Service

Service::DEV_SERVICE_NAMES, Service::SERVICE_NAMES

Instance Attribute Summary

Attributes included from Importable

#imported, #importing

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NotificationBranchSelection

#branch_choices, #notify_for_branch?

Methods inherited from Service

#activated?, #api_field_names, #async_execute, available_services_names, available_services_types, boolean_accessor, build_from_integration, #can_test?, #category, #configurable_event_actions, #configurable_events, default_integration, dev_services_names, #editable?, #event_channel_names, event_description, #event_field, event_names, #event_names, find_or_create_templates, find_or_initialize_all, find_or_initialize_integration, #global_fields, #help, instance_exists_for?, #issue_tracker?, #json_fields, #operating?, prop_accessor, #reset_updated_properties, services_names, services_types, #show_active_box?, supported_event_actions, #supported_events, #supports_data_fields?, #test, #to_data_fields_hash, #to_param, #to_service_hash, #updated_properties

Methods included from ProjectServicesLoggable

#build_message, #log_error, #log_info, #logger

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Class Method Details

.supported_eventsObject


23
24
25
# File 'app/models/project_services/emails_on_push_service.rb', line 23

def self.supported_events
  %w(push tag_push)
end

.to_paramObject


19
20
21
# File 'app/models/project_services/emails_on_push_service.rb', line 19

def self.to_param
  'emails_on_push'
end

Instance Method Details

#descriptionObject


15
16
17
# File 'app/models/project_services/emails_on_push_service.rb', line 15

def description
  s_('EmailsOnPushService|Email the commits and diff of each push to a list of recipients.')
end

#disable_diffs?Boolean

Returns:

  • (Boolean)

58
59
60
# File 'app/models/project_services/emails_on_push_service.rb', line 58

def disable_diffs?
  Gitlab::Utils.to_boolean(self.disable_diffs)
end

#execute(push_data) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/project_services/emails_on_push_service.rb', line 33

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,
    recipients,
    push_data,
    send_from_committer_email: send_from_committer_email?,
    disable_diffs:             disable_diffs?
  )
end

#fieldsObject


62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/project_services/emails_on_push_service.rb', line 62

def fields
  domains = Notify.allowed_email_domains.map { |domain| "[email protected]#{domain}" }.join(", ")
  [
    { 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.") },
    { type: 'select', name: 'branches_to_be_notified', choices: branch_choices },
    { type: 'textarea', name: 'recipients', placeholder: s_('EmailsOnPushService|Emails separated by whitespace') }
  ]
end

#initialize_propertiesObject


27
28
29
30
31
# File 'app/models/project_services/emails_on_push_service.rb', line 27

def initialize_properties
  super

  self.branches_to_be_notified = 'all' if branches_to_be_notified.nil?
end

#notify_for_ref?(push_data) ⇒ Boolean

Returns:

  • (Boolean)

47
48
49
50
51
52
# File 'app/models/project_services/emails_on_push_service.rb', line 47

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

#send_from_committer_email?Boolean

Returns:

  • (Boolean)

54
55
56
# File 'app/models/project_services/emails_on_push_service.rb', line 54

def send_from_committer_email?
  Gitlab::Utils.to_boolean(self.send_from_committer_email)
end

#titleObject


11
12
13
# File 'app/models/project_services/emails_on_push_service.rb', line 11

def title
  s_('EmailsOnPushService|Emails on push')
end