Class: Integration

Overview

To add new integration you should build a class inherited from Integration and implement a set of methods

Constant Summary collapse

UnknownType =
Class.new(StandardError)
INTEGRATION_NAMES =
%w[
  asana assembla bamboo bugzilla buildkite campfire confluence custom_issue_tracker datadog discord
  drone_ci emails_on_push ewm external_wiki flowdock hangouts_chat harbor 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 zentao
].freeze
PROJECT_SPECIFIC_INTEGRATION_NAMES =

TODO Shimo is temporary disabled on group and instance-levels. See: gitlab.com/gitlab-org/gitlab/-/issues/345677

%w[
  jenkins shimo
].freeze
DEV_INTEGRATION_NAMES =

Fake integrations to help with local development.

%w[
  mock_ci mock_monitoring
].freeze
BASE_CLASSES =

Base classes which aren't actual integrations.

%w[
  Integrations::BaseChatNotification
  Integrations::BaseCi
  Integrations::BaseIssueTracker
  Integrations::BaseMonitoring
  Integrations::BaseSlashCommands
].freeze
SECTION_TYPE_CONNECTION =
'connection'

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 Gitlab::Utils::Override

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

Methods included from Integrations::ResetSecretFields

#exposing_secrets_fields

Methods included from Integrations::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

.available_integration_names(include_project_specific: true, include_dev: true) ⇒ Object

Deprecated.

Returns a list of available integration names. Example: [“asana”, …]


269
270
271
272
273
274
275
# File 'app/models/integration.rb', line 269

def self.available_integration_names(include_project_specific: true, include_dev: true)
  names = integration_names
  names += project_specific_integration_names if include_project_specific
  names += dev_integration_names if include_dev

  names.sort_by(&:downcase)
end

.available_integration_types(include_project_specific: true, include_dev: true) ⇒ Object

Returns a list of available integration types. Example: [“Integrations::Asana”, …]


293
294
295
296
297
# File 'app/models/integration.rb', line 293

def self.available_integration_types(include_project_specific: true, include_dev: true)
  available_integration_names(include_project_specific: include_project_specific, include_dev: include_dev).map do
    integration_name_to_type(_1)
  end
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


201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'app/models/integration.rb', line 201

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

  args.each do |arg|
    class_eval <<~RUBY, __FILE__, __LINE__ + 1
      def #{arg}
        return if properties.blank?

        Gitlab::Utils.to_boolean(properties['#{arg}'])
      end

      def #{arg}?
        # '!!' is used because nil or empty string is converted to nil
        !!#{arg}
      end
    RUBY
  end
end

.build_from_integration(integration, project_id: nil, group_id: nil) ⇒ Object


324
325
326
327
328
329
330
331
332
# File 'app/models/integration.rb', line 324

def self.build_from_integration(integration, project_id: nil, group_id: nil)
  new_integration = integration.dup

  new_integration.instance = false
  new_integration.project_id = project_id
  new_integration.group_id = group_id
  new_integration.inherit_from_id = integration.id if integration.inheritable?
  new_integration
end

.create_from_active_default_integrations(owner, association) ⇒ Object

Returns the number of successfully saved integrations Duplicate integrations are excluded from this count by their validations.


377
378
379
380
381
382
383
384
385
386
# File 'app/models/integration.rb', line 377

def self.create_from_active_default_integrations(owner, association)
  group_ids = sorted_ancestors(owner).select(:id)
  array = group_ids.to_sql.present? ? "array(#{group_ids.to_sql})" : 'ARRAY[]'
  order = Arel.sql("type_new ASC, array_position(#{array}::bigint[], #{table_name}.group_id), instance DESC")

  from_union([active.where(instance: true), active.where(group_id: group_ids, inherit_from_id: nil)])
    .order(order)
    .group_by(&:type)
    .count { |type, parents| build_from_integration(parents.first, association => owner.id).save }
end

.default_integration(type, scope) ⇒ Object


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

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

.default_test_eventObject


232
233
234
# File 'app/models/integration.rb', line 232

def self.default_test_event
  'push'
end

.dev_integration_namesObject


281
282
283
284
285
# File 'app/models/integration.rb', line 281

def self.dev_integration_names
  return [] unless Gitlab.dev_or_test_env?

  DEV_INTEGRATION_NAMES
end

.event_description(event) ⇒ Object


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

def self.event_description(event)
  IntegrationsHelper.integration_event_description(event)
end

.event_namesObject


224
225
226
# File 'app/models/integration.rb', line 224

def self.event_names
  self.supported_events.map { |event| IntegrationsHelper.integration_event_field_name(event) }
end

.field(name, storage: field_storage, **attrs) ⇒ Object

:nocov: Tested on subclasses.


145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/models/integration.rb', line 145

def self.field(name, storage: field_storage, **attrs)
  fields << ::Integrations::Field.new(name: name, **attrs)

  case storage
  when :properties
    prop_accessor(name)
  when :data_fields
    data_field(name)
  else
    raise ArgumentError, "Unknown field storage: #{storage}"
  end
end

.fieldsObject

:nocov:


159
160
161
# File 'app/models/integration.rb', line 159

def self.fields
  @fields ||= []
end

.find_or_initialize_all_non_project_specific(scope) ⇒ Object


246
247
248
# File 'app/models/integration.rb', line 246

def self.find_or_initialize_all_non_project_specific(scope)
  scope + build_nonexistent_integrations_for(scope)
end

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


240
241
242
243
244
# File 'app/models/integration.rb', line 240

def self.find_or_initialize_non_project_specific_integration(name, instance: false, group_id: nil)
  return unless name.in?(available_integration_names(include_project_specific: false))

  integration_name_to_model(name).find_or_initialize_by(instance: instance, group_id: group_id)
end

.inherited_descendants_from_self_or_ancestors_from(integration) ⇒ Object


388
389
390
391
392
393
394
395
396
397
# File 'app/models/integration.rb', line 388

def self.inherited_descendants_from_self_or_ancestors_from(integration)
  inherit_from_ids =
    where(type: integration.type, group: integration.group.self_and_ancestors)
      .or(where(type: integration.type, instance: true)).select(:id)

  from_union([
    where(type: integration.type, inherit_from_id: inherit_from_ids, group: integration.group.descendants),
    where(type: integration.type, inherit_from_id: inherit_from_ids, project: Project.in_namespace(integration.group.self_and_descendants))
  ])
end

.instance_exists_for?(type) ⇒ Boolean

Returns:

  • (Boolean)

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

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

.integration_name_to_model(name) ⇒ Object

Returns the model for the given integration name. Example: :asana => Integrations::Asana


301
302
303
304
# File 'app/models/integration.rb', line 301

def self.integration_name_to_model(name)
  type = integration_name_to_type(name)
  integration_type_to_model(type)
end

.integration_name_to_type(name) ⇒ Object

Returns the STI type for the given integration name. Example: “asana” => “Integrations::Asana”


308
309
310
311
312
313
314
315
# File 'app/models/integration.rb', line 308

def self.integration_name_to_type(name)
  name = name.to_s
  if available_integration_names.exclude?(name)
    Gitlab::ErrorTracking.track_and_raise_for_dev_exception(UnknownType.new(name.inspect))
  else
    "Integrations::#{name.camelize}"
  end
end

.integration_namesObject


277
278
279
# File 'app/models/integration.rb', line 277

def self.integration_names
  INTEGRATION_NAMES
end

.project_specific_integration_namesObject


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

def self.project_specific_integration_names
  PROJECT_SPECIFIC_INTEGRATION_NAMES
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


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'app/models/integration.rb', line 169

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

      def #{arg}=(value)
        self.properties ||= {}
        updated_properties['#{arg}'] = #{arg} unless #{arg}_changed?
        self.properties = self.properties.merge('#{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

.supported_eventsObject


228
229
230
# File 'app/models/integration.rb', line 228

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

.to_paramObject

Raises:

  • (NotImplementedError)

220
221
222
# File 'app/models/integration.rb', line 220

def self.to_param
  raise NotImplementedError
end

Instance Method Details

#activate_disabled_reasonObject


415
416
417
# File 'app/models/integration.rb', line 415

def activate_disabled_reason
  nil
end

#activated?Boolean

Returns:

  • (Boolean)

399
400
401
# File 'app/models/integration.rb', line 399

def activated?
  active
end

#api_field_namesObject


503
504
505
506
507
508
# File 'app/models/integration.rb', line 503

def api_field_names
  fields
    .reject { _1[:type] == 'password' }
    .pluck(:name)
    .grep_v(/password|token|key/)
end

#async_execute(data) ⇒ Object


579
580
581
582
583
# File 'app/models/integration.rb', line 579

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

  ProjectServiceWorker.perform_async(id, data)
end

#attributesObject


464
465
466
# File 'app/models/integration.rb', line 464

def attributes
  super.except('properties')
end

#categoryObject


419
420
421
# File 'app/models/integration.rb', line 419

def category
  read_attribute(:category).to_sym
end

#configurable_eventsObject


514
515
516
517
518
519
520
521
522
523
# File 'app/models/integration.rb', line 514

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

#default_test_eventObject


529
530
531
# File 'app/models/integration.rb', line 529

def default_test_event
  self.class.default_test_event
end

#descriptionObject


431
432
433
# File 'app/models/integration.rb', line 431

def description
  # implement inside child
end

#dupObject


336
337
338
339
340
341
342
343
344
345
346
# File 'app/models/integration.rb', line 336

def dup
  new_integration = super
  new_integration.assign_attributes(reencrypt_properties)

  if supports_data_fields?
    fields = data_fields.dup
    fields.integration = new_integration
  end

  new_integration
end

#editable?Boolean

Returns:

  • (Boolean)

411
412
413
# File 'app/models/integration.rb', line 411

def editable?
  true
end

#event_channel_namesObject


491
492
493
# File 'app/models/integration.rb', line 491

def event_channel_names
  []
end

#event_field(event) ⇒ Object


499
500
501
# File 'app/models/integration.rb', line 499

def event_field(event)
  nil
end

#event_namesObject


495
496
497
# File 'app/models/integration.rb', line 495

def event_names
  self.class.event_names
end

#execute(data) ⇒ Object


533
534
535
# File 'app/models/integration.rb', line 533

def execute(data)
  # implement inside child
end

#fieldsObject


163
164
165
# File 'app/models/integration.rb', line 163

def fields
  self.class.fields.dup
end

#global_fieldsObject


510
511
512
# File 'app/models/integration.rb', line 510

def global_fields
  fields
end

#group_level?Boolean

Returns:

  • (Boolean)

553
554
555
# File 'app/models/integration.rb', line 553

def group_level?
  group_id.present?
end

#helpObject


435
436
437
# File 'app/models/integration.rb', line 435

def help
  # implement inside child
end

#inheritable?Boolean

Returns:

  • (Boolean)

348
349
350
# File 'app/models/integration.rb', line 348

def inheritable?
  instance_level? || group_level?
end

#initialize_propertiesObject


423
424
425
# File 'app/models/integration.rb', line 423

def initialize_properties
  self.properties = {} if has_attribute?(:encrypted_properties) && encrypted_properties.nil?
end

#instance_level?Boolean

Returns:

  • (Boolean)

557
558
559
# File 'app/models/integration.rb', line 557

def instance_level?
  instance?
end

#json_fieldsObject

Expose a list of fields in the JSON endpoint.

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


458
459
460
# File 'app/models/integration.rb', line 458

def json_fields
  %w[active]
end

#operating?Boolean

Returns:

  • (Boolean)

403
404
405
# File 'app/models/integration.rb', line 403

def operating?
  active && persisted?
end

#parentObject


561
562
563
# File 'app/models/integration.rb', line 561

def parent
  project || group
end

#project_level?Boolean

Returns:

  • (Boolean)

549
550
551
# File 'app/models/integration.rb', line 549

def project_level?
  project_id.present?
end

#properties=(props) ⇒ Object


66
67
68
# File 'app/models/integration.rb', line 66

def properties=(props)
  self.attr_encrypted_props = props&.with_indifferent_access&.freeze
end

#reencrypt_propertiesObject


477
478
479
480
481
482
483
484
485
# File 'app/models/integration.rb', line 477

def reencrypt_properties
  unless properties.nil? || properties.empty?
    alg = self.class.encrypted_attributes[:properties][:algorithm]
    iv = generate_iv(alg)
    ep = self.class.encrypt(:properties, properties, { iv: iv })
  end

  { 'encrypted_properties' => ep, 'encrypted_properties_iv' => iv }
end

#reset_updated_propertiesObject


575
576
577
# File 'app/models/integration.rb', line 575

def reset_updated_properties
  @updated_properties = nil
end

#secret_fieldsObject

TODO: Once all integrations use `Integrations::Field` we can use `#secret?` here. See: gitlab.com/groups/gitlab-org/-/epics/7652


451
452
453
# File 'app/models/integration.rb', line 451

def secret_fields
  fields.select { |f| f[:type] == 'password' }.pluck(:name)
end

#sectionsObject


444
445
446
# File 'app/models/integration.rb', line 444

def sections
  []
end

#show_active_box?Boolean

Returns:

  • (Boolean)

407
408
409
# File 'app/models/integration.rb', line 407

def show_active_box?
  true
end

#supported_eventsObject


525
526
527
# File 'app/models/integration.rb', line 525

def supported_events
  self.class.supported_events
end

#supports_data_fields?Boolean

override if needed

Returns:

  • (Boolean)

586
587
588
# File 'app/models/integration.rb', line 586

def supports_data_fields?
  false
end

#test(data) ⇒ Object


537
538
539
540
541
# File 'app/models/integration.rb', line 537

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

#testable?Boolean

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

Returns:

  • (Boolean)

545
546
547
# File 'app/models/integration.rb', line 545

def testable?
  project_level?
end

#titleObject


427
428
429
# File 'app/models/integration.rb', line 427

def title
  # implement inside child
end

#to_data_fields_hashObject


487
488
489
# File 'app/models/integration.rb', line 487

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

#to_integration_hashObject

return a hash of columns => values suitable for passing to insert_all


469
470
471
472
473
474
475
# File 'app/models/integration.rb', line 469

def to_integration_hash
  column = self.class.attribute_aliases.fetch('type', 'type')

  as_json(except: %w[id instance project_id group_id])
    .merge(column => type)
    .merge(reencrypt_properties)
end

#to_paramObject


439
440
441
442
# File 'app/models/integration.rb', line 439

def to_param
  # implement inside child
  self.class.to_param
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 integration properties. This allows to track changes to properties set with the accessor methods, but not direct manipulation of properties hash.


571
572
573
# File 'app/models/integration.rb', line 571

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