Class: PipelinesEmailService

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

Constant Summary

Constants inherited from Service

Service::DEV_SERVICE_NAMES, Service::PROJECT_SPECIFIC_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, #category, #configurable_event_actions, #configurable_events, create_from_active_default_integrations, default_integration, #default_test_event, dev_services_names, #editable?, #event_channel_names, event_description, #event_field, event_names, #event_names, #external_issue_tracker?, #external_wiki?, find_or_create_templates, find_or_initialize_all_non_project_specific, find_or_initialize_non_project_specific_integration, #global_fields, #help, inherited_descendants_from_self_or_ancestors_from, instance_exists_for?, #json_fields, #operating?, project_specific_services_names, prop_accessor, #reset_updated_properties, services_names, #show_active_box?, supported_event_actions, #supported_events, #supports_data_fields?, #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, where_exists, with_fast_statement_timeout, without_order

Class Method Details

.default_test_eventObject


43
44
45
# File 'app/models/project_services/pipelines_email_service.rb', line 43

def self.default_test_event
  'pipeline'
end

.supported_eventsObject


39
40
41
# File 'app/models/project_services/pipelines_email_service.rb', line 39

def self.supported_events
  %w[pipeline]
end

.to_paramObject


35
36
37
# File 'app/models/project_services/pipelines_email_service.rb', line 35

def self.to_param
  'pipelines_email'
end

Instance Method Details

#can_test?Boolean

Returns:

  • (Boolean)

59
60
61
# File 'app/models/project_services/pipelines_email_service.rb', line 59

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

#descriptionObject


31
32
33
# File 'app/models/project_services/pipelines_email_service.rb', line 31

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

#execute(data, force: false) ⇒ Object


47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/project_services/pipelines_email_service.rb', line 47

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(data)

  return unless all_recipients.any?

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

#fieldsObject


63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/project_services/pipelines_email_service.rb', line 63

def fields
  [
    { type: 'textarea',
      name: 'recipients',
      placeholder: _('Emails separated by comma'),
      required: true },
    { type: 'checkbox',
      name: 'notify_only_broken_pipelines' },
    { type: 'select',
      name: 'branches_to_be_notified',
      choices: branch_choices }
  ]
end

#initialize_propertiesObject


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/project_services/pipelines_email_service.rb', line 10

def initialize_properties
  if properties.nil?
    self.properties = {}
    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)

89
90
91
92
93
94
95
96
97
98
# File 'app/models/project_services/pipelines_email_service.rb', line 89

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

#retrieve_recipients(data) ⇒ Object


100
101
102
# File 'app/models/project_services/pipelines_email_service.rb', line 100

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

#should_pipeline_be_notified?(data) ⇒ Boolean

Returns:

  • (Boolean)

85
86
87
# File 'app/models/project_services/pipelines_email_service.rb', line 85

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

#test(data) ⇒ Object


77
78
79
80
81
82
83
# File 'app/models/project_services/pipelines_email_service.rb', line 77

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

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

#titleObject


27
28
29
# File 'app/models/project_services/pipelines_email_service.rb', line 27

def title
  _('Pipelines emails')
end