Class: Service

Inherits:
ApplicationRecord show all
Includes:
DataFields, IgnorableColumns, Importable, ProjectServicesLoggable, Sortable
Defined in:
app/models/service.rb

Overview

To add new service you should build a class inherited from Service and implement a set of methods

Constant Summary collapse

SERVICE_NAMES =
%w[
  alerts asana assembla bamboo bugzilla buildkite campfire confluence custom_issue_tracker discord
  drone_ci emails_on_push ewm external_wiki flowdock hangouts_chat hipchat irker jira
  mattermost mattermost_slash_commands microsoft_teams packagist pipelines_email
  pivotaltracker prometheus pushover redmine slack slack_slash_commands teamcity unify_circuit webex_teams youtrack
].freeze
DEV_SERVICE_NAMES =
%w[
  mock_ci mock_deployment mock_monitoring
].freeze

Instance Attribute Summary

Attributes included from Importable

#imported, #importing

Class Method Summary collapse

Instance Method Summary collapse

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

.available_services_namesObject


195
196
197
198
199
200
# File 'app/models/service.rb', line 195

def self.available_services_names
  service_names = services_names
  service_names += dev_services_names

  service_names.sort_by(&:downcase)
end

.available_services_typesObject


212
213
214
# File 'app/models/service.rb', line 212

def self.available_services_types
  available_services_names.map { |service_name| "#{service_name}_service".camelize }
end

.boolean_accessor(*args) ⇒ Object

Provide convenient boolean accessor methods for each serialized property. Also keep track of updated properties in a similar way as ActiveModel::Dirty


120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/models/service.rb', line 120

def self.boolean_accessor(*args)
  self.prop_accessor(*args)

  args.each do |arg|
    class_eval <<~RUBY, __FILE__, __LINE__ + 1
      def #{arg}?
        # '!!' is used because nil or empty string is converted to nil
        !!ActiveRecord::Type::Boolean.new.cast(#{arg})
      end
    RUBY
  end
end

.build_from_integration(project_id, integration) ⇒ Object


220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'app/models/service.rb', line 220

def self.build_from_integration(project_id, integration)
  service = integration.dup

  if integration.supports_data_fields?
    data_fields = integration.data_fields.dup
    data_fields.service = service
  end

  service.template = false
  service.instance = false
  service.inherit_from_id = integration.id if integration.instance?
  service.project_id = project_id
  service.active = false if service.invalid?
  service
end

.default_integration(type, scope) ⇒ Object


240
241
242
# File 'app/models/service.rb', line 240

def self.default_integration(type, scope)
  closest_group_integration(type, scope) || instance_level_integration(type)
end

.dev_services_namesObject


206
207
208
209
210
# File 'app/models/service.rb', line 206

def self.dev_services_names
  return [] unless Rails.env.development?

  DEV_SERVICE_NAMES
end

.event_description(event) ⇒ Object


149
150
151
# File 'app/models/service.rb', line 149

def self.event_description(event)
  ServicesHelper.service_event_description(event)
end

.event_namesObject


137
138
139
# File 'app/models/service.rb', line 137

def self.event_names
  self.supported_events.map { |event| ServicesHelper.service_event_field_name(event) }
end

.find_or_create_templatesObject


153
154
155
156
# File 'app/models/service.rb', line 153

def self.find_or_create_templates
  create_nonexistent_templates
  for_template
end

.find_or_initialize_all(scope) ⇒ Object


177
178
179
# File 'app/models/service.rb', line 177

def self.find_or_initialize_all(scope)
  scope + build_nonexistent_services_for(scope)
end

.find_or_initialize_integration(name, instance: false, group_id: nil) ⇒ Object


171
172
173
174
175
# File 'app/models/service.rb', line 171

def self.find_or_initialize_integration(name, instance: false, group_id: nil)
  if name.in?(available_services_names)
    "#{name}_service".camelize.constantize.find_or_initialize_by(instance: instance, group_id: group_id)
  end
end

.instance_exists_for?(type) ⇒ Boolean

Returns:

  • (Boolean)

236
237
238
# File 'app/models/service.rb', line 236

def self.instance_exists_for?(type)
  exists?(instance: true, type: type)
end

.prop_accessor(*args) ⇒ Object

Provide convenient accessor methods for each serialized property. Also keep track of updated properties in a similar way as ActiveModel::Dirty


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/models/service.rb', line 88

def self.prop_accessor(*args)
  args.each do |arg|
    class_eval <<~RUBY, __FILE__, __LINE__ + 1
      unless method_defined?(arg)
        def #{arg}
          properties['#{arg}']
        end
      end

      def #{arg}=(value)
        self.properties ||= {}
        updated_properties['#{arg}'] = #{arg} unless #{arg}_changed?
        self.properties['#{arg}'] = value
      end

      def #{arg}_changed?
        #{arg}_touched? && #{arg} != #{arg}_was
      end

      def #{arg}_touched?
        updated_properties.include?('#{arg}')
      end

      def #{arg}_was
        updated_properties['#{arg}']
      end
    RUBY
  end
end

.services_namesObject


202
203
204
# File 'app/models/service.rb', line 202

def self.services_names
  SERVICE_NAMES
end

.services_typesObject


216
217
218
# File 'app/models/service.rb', line 216

def self.services_types
  services_names.map { |service_name| "#{service_name}_service".camelize }
end

.supported_event_actionsObject


141
142
143
# File 'app/models/service.rb', line 141

def self.supported_event_actions
  %w[]
end

.supported_eventsObject


145
146
147
# File 'app/models/service.rb', line 145

def self.supported_events
  %w[commit push tag_push issue confidential_issue merge_request wiki_page]
end

.to_paramObject

Raises:

  • (NotImplementedError)

133
134
135
# File 'app/models/service.rb', line 133

def self.to_param
  raise NotImplementedError
end

Instance Method Details

#activated?Boolean

Returns:

  • (Boolean)

259
260
261
# File 'app/models/service.rb', line 259

def activated?
  active
end

#api_field_namesObject


332
333
334
335
# File 'app/models/service.rb', line 332

def api_field_names
  fields.map { |field| field[:name] }
    .reject { |field_name| field_name =~ /(password|token|key|title|description)/ }
end

#async_execute(data) ⇒ Object


390
391
392
393
394
# File 'app/models/service.rb', line 390

def async_execute(data)
  return unless supported_events.include?(data[:object_kind])

  ProjectServiceWorker.perform_async(id, data)
end

#can_test?Boolean

Disable test for instance-level and group-level services. gitlab.com/gitlab-org/gitlab/-/issues/213138

Returns:

  • (Boolean)

372
373
374
# File 'app/models/service.rb', line 372

def can_test?
  !instance? && !group_id
end

#categoryObject


275
276
277
# File 'app/models/service.rb', line 275

def category
  read_attribute(:category).to_sym
end

#configurable_event_actionsObject


352
353
354
# File 'app/models/service.rb', line 352

def configurable_event_actions
  self.class.supported_event_actions
end

#configurable_eventsObject


341
342
343
344
345
346
347
348
349
350
# File 'app/models/service.rb', line 341

def configurable_events
  events = supported_events

  # No need to disable individual triggers when there is only one
  if events.count == 1
    []
  else
    events
  end
end

#descriptionObject


287
288
289
# File 'app/models/service.rb', line 287

def description
  # implement inside child
end

#editable?Boolean

Returns:

  • (Boolean)

271
272
273
# File 'app/models/service.rb', line 271

def editable?
  true
end

#event_channel_namesObject


320
321
322
# File 'app/models/service.rb', line 320

def event_channel_names
  []
end

#event_field(event) ⇒ Object


328
329
330
# File 'app/models/service.rb', line 328

def event_field(event)
  nil
end

#event_namesObject


324
325
326
# File 'app/models/service.rb', line 324

def event_names
  self.class.event_names
end

#execute(data) ⇒ Object


360
361
362
# File 'app/models/service.rb', line 360

def execute(data)
  # implement inside child
end

#fieldsObject


300
301
302
303
# File 'app/models/service.rb', line 300

def fields
  # implement inside child
  []
end

#global_fieldsObject


337
338
339
# File 'app/models/service.rb', line 337

def global_fields
  fields
end

#helpObject


291
292
293
# File 'app/models/service.rb', line 291

def help
  # implement inside child
end

#initialize_propertiesObject


279
280
281
# File 'app/models/service.rb', line 279

def initialize_properties
  self.properties = {} if properties.nil?
end

#issue_tracker?Boolean

Returns:

  • (Boolean)

396
397
398
# File 'app/models/service.rb', line 396

def issue_tracker?
  self.category == :issue_tracker
end

#json_fieldsObject

Expose a list of fields in the JSON endpoint.

This list is used in `Service#as_json(only: json_fields)`.


308
309
310
# File 'app/models/service.rb', line 308

def json_fields
  %w[active]
end

#operating?Boolean

Returns:

  • (Boolean)

263
264
265
# File 'app/models/service.rb', line 263

def operating?
  active && persisted?
end

#reset_updated_propertiesObject


386
387
388
# File 'app/models/service.rb', line 386

def reset_updated_properties
  @updated_properties = nil
end

#show_active_box?Boolean

Returns:

  • (Boolean)

267
268
269
# File 'app/models/service.rb', line 267

def show_active_box?
  true
end

#supported_eventsObject


356
357
358
# File 'app/models/service.rb', line 356

def supported_events
  self.class.supported_events
end

#supports_data_fields?Boolean

override if needed

Returns:

  • (Boolean)

401
402
403
# File 'app/models/service.rb', line 401

def supports_data_fields?
  false
end

#test(data) ⇒ Object


364
365
366
367
368
# File 'app/models/service.rb', line 364

def test(data)
  # default implementation
  result = execute(data)
  { success: result.present?, result: result }
end

#titleObject


283
284
285
# File 'app/models/service.rb', line 283

def title
  # implement inside child
end

#to_data_fields_hashObject


316
317
318
# File 'app/models/service.rb', line 316

def to_data_fields_hash
  data_fields.as_json(only: data_fields.class.column_names).except('id', 'service_id')
end

#to_paramObject


295
296
297
298
# File 'app/models/service.rb', line 295

def to_param
  # implement inside child
  self.class.to_param
end

#to_service_hashObject


312
313
314
# File 'app/models/service.rb', line 312

def to_service_hash
  as_json(methods: :type, except: %w[id template instance project_id])
end

#updated_propertiesObject

Returns a hash of the properties that have been assigned a new value since last save, indicating their original values (attr => original value). ActiveRecord does not provide a mechanism to track changes in serialized keys, so we need a specific implementation for service properties. This allows to track changes to properties set with the accessor methods, but not direct manipulation of properties hash.


382
383
384
# File 'app/models/service.rb', line 382

def updated_properties
  @updated_properties ||= ActiveSupport::HashWithIndifferentAccess.new
end