Class: Integrations::PipelinesEmail

Inherits:
Integration show all
Includes:
NotificationBranchSelection
Defined in:
app/models/integrations/pipelines_email.rb

Constant Summary collapse

RECIPIENTS_LIMIT =
30

Constants inherited from Integration

Integration::BASE_CLASSES, Integration::DEV_INTEGRATION_NAMES, Integration::INTEGRATION_NAMES, Integration::PROJECT_SPECIFIC_INTEGRATION_NAMES, Integration::SECTION_TYPE_CONNECTION, Integration::UnknownType

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

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 Integration

#activate_disabled_reason, #activated?, #api_field_names, #async_execute, #attributes, available_integration_names, available_integration_types, boolean_accessor, build_from_integration, #category, #configurable_events, create_from_active_default_integrations, default_integration, #default_test_event, dev_integration_names, #dup, #editable?, #event_channel_names, event_description, #event_field, event_names, #event_names, field, fields, find_or_initialize_all_non_project_specific, find_or_initialize_non_project_specific_integration, #global_fields, #group_level?, #help, #inheritable?, inherited_descendants_from_self_or_ancestors_from, instance_exists_for?, #instance_level?, integration_name_to_model, integration_name_to_type, integration_names, #json_fields, #operating?, #parent, #project_level?, project_specific_integration_names, prop_accessor, #properties=, #reencrypt_properties, #reset_updated_properties, #secret_fields, #sections, #show_active_box?, #supported_events, #supports_data_fields?, #to_data_fields_hash, #to_integration_hash, #to_param, #updated_properties

Methods included from Gitlab::Utils::Override

#extended, extensions, #included, #method_added, #override, #prepended, #queue_verification, verify!

Methods included from ResetSecretFields

#exposing_secrets_fields

Methods included from Loggable

#build_message, #log_error, #log_exception, #log_info, #logger

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Class Method Details

.default_test_eventObject


48
49
50
# File 'app/models/integrations/pipelines_email.rb', line 48

def self.default_test_event
  'pipeline'
end

.supported_eventsObject


44
45
46
# File 'app/models/integrations/pipelines_email.rb', line 44

def self.supported_events
  %w[pipeline]
end

.to_paramObject


40
41
42
# File 'app/models/integrations/pipelines_email.rb', line 40

def self.to_param
  'pipelines_email'
end

Instance Method Details

#descriptionObject


36
37
38
# File 'app/models/integrations/pipelines_email.rb', line 36

def description
  _('Email the pipeline status to a list of recipients.')
end

#execute(data, force: false) ⇒ Object


52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/integrations/pipelines_email.rb', line 52

def execute(data, force: false)
  return unless supported_events.include?(data[:object_kind])
  return unless force || should_pipeline_be_notified?(data)

  all_recipients = retrieve_recipients

  return unless all_recipients.any?

  pipeline_id = data[:object_attributes][:id]
  PipelineNotificationWorker.new.perform(pipeline_id, recipients: all_recipients)
end

#fieldsObject


68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/integrations/pipelines_email.rb', line 68

def fields
  [
    { type: 'textarea',
      name: 'recipients',
      help: _('Comma-separated list of email addresses.'),
      required: true },
    { type: 'checkbox',
      name: 'notify_only_broken_pipelines' },
    { type: 'select',
      name: 'branches_to_be_notified',
      title: s_('Integrations|Branches for which notifications are to be sent'),
      choices: branch_choices }
  ]
end

#initialize_propertiesObject


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/integrations/pipelines_email.rb', line 14

def initialize_properties
  super

  if properties.blank?
    self.notify_only_broken_pipelines = true
    self.branches_to_be_notified = "default"
  elsif !self.notify_only_default_branch.nil?
    # In older versions, there was only a boolean property named
    # `notify_only_default_branch`. Now we have a string property named
    # `branches_to_be_notified`. Instead of doing a background migration, we
    # opted to set a value for the new property based on the old one, if
    # users hasn't specified one already. When users edit the service and
    # selects a value for this new property, it will override everything.

    self.branches_to_be_notified ||= notify_only_default_branch? ? "default" : "all"
  end
end

#notify_for_pipeline?(data) ⇒ Boolean

Returns:

  • (Boolean)

95
96
97
98
99
100
101
102
103
104
# File 'app/models/integrations/pipelines_email.rb', line 95

def notify_for_pipeline?(data)
  case data[:object_attributes][:status]
  when 'success'
    !notify_only_broken_pipelines?
  when 'failed'
    true
  else
    false
  end
end

#retrieve_recipientsObject


106
107
108
# File 'app/models/integrations/pipelines_email.rb', line 106

def retrieve_recipients
  recipients.to_s.split(/[,\r\n ]+/).reject(&:empty?)
end

#should_pipeline_be_notified?(data) ⇒ Boolean

Returns:

  • (Boolean)

91
92
93
# File 'app/models/integrations/pipelines_email.rb', line 91

def should_pipeline_be_notified?(data)
  notify_for_branch?(data) && notify_for_pipeline?(data)
end

#test(data) ⇒ Object


83
84
85
86
87
88
89
# File 'app/models/integrations/pipelines_email.rb', line 83

def test(data)
  result = execute(data, force: true)

  { success: true, result: result }
rescue StandardError => error
  { success: false, result: error }
end

#testable?Boolean

Returns:

  • (Boolean)

64
65
66
# File 'app/models/integrations/pipelines_email.rb', line 64

def testable?
  project&.ci_pipelines&.any?
end

#titleObject


32
33
34
# File 'app/models/integrations/pipelines_email.rb', line 32

def title
  _('Pipeline status emails')
end