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_CONFIGURATION, Integration::SECTION_TYPE_CONNECTION, Integration::SECTION_TYPE_TRIGGER, Integration::SNOWPLOW_EVENT_ACTION, Integration::SNOWPLOW_EVENT_LABEL, Integration::UnknownType

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from ResetOnUnionError

ResetOnUnionError::MAX_RESET_PERIOD

Instance Attribute Summary

Attributes included from Importable

#imported, #importing

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NotificationBranchSelection

#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, #chat?, #ci?, #configurable_events, create_from_active_default_integrations, default_integration, #default_test_event, dev_integration_names, #dup, #editable?, #event_channel_names, event_description, event_names, #event_names, field, #fields, fields, find_or_initialize_all_non_project_specific, find_or_initialize_non_project_specific_integration, #form_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_database_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



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

def self.default_test_event
  'pipeline'
end

.supported_eventsObject



59
60
61
# File 'app/models/integrations/pipelines_email.rb', line 59

def self.supported_events
  %w[pipeline]
end

.to_paramObject



55
56
57
# File 'app/models/integrations/pipelines_email.rb', line 55

def self.to_param
  'pipelines_email'
end

Instance Method Details

#descriptionObject



51
52
53
# File 'app/models/integrations/pipelines_email.rb', line 51

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

#execute(data, force: false) ⇒ Object



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

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

#initialize_propertiesObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/integrations/pipelines_email.rb', line 29

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 => e
  { success: false, result: e }
end

#testable?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'app/models/integrations/pipelines_email.rb', line 79

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

#titleObject



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

def title
  _('Pipeline status emails')
end