File: integration.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (821 lines) | stat: -rw-r--r-- 29,724 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
# frozen_string_literal: true

module Integrations
  module Base
    module Integration
      extend ActiveSupport::Concern

      UnknownType = Class.new(StandardError)

      INTEGRATION_NAMES = %w[
        asana assembla bamboo bugzilla buildkite campfire clickup confluence custom_issue_tracker
        datadog diffblue_cover discord drone_ci emails_on_push ewm external_wiki
        gitlab_slack_application hangouts_chat harbor irker jira matrix
        mattermost mattermost_slash_commands microsoft_teams packagist phorge pipelines_email
        pivotaltracker prometheus pumble pushover redmine slack slack_slash_commands squash_tm teamcity telegram
        unify_circuit webex_teams youtrack zentao
      ].freeze

      # Integrations that can only be enabled on the instance-level
      INSTANCE_LEVEL_ONLY_INTEGRATION_NAMES = %w[
        beyond_identity
      ].freeze

      # Integrations that can only be enabled on the project-level
      PROJECT_LEVEL_ONLY_INTEGRATION_NAMES = %w[
        apple_app_store google_play jenkins
      ].freeze

      # Integrations that cannot be enabled on the instance-level
      PROJECT_AND_GROUP_LEVEL_ONLY_INTEGRATION_NAMES = %w[
        jira_cloud_app
      ].freeze

      # Fake integrations to help with local development.
      DEV_INTEGRATION_NAMES = %w[
        mock_ci mock_monitoring
      ].freeze

      # Base classes which aren't actual integrations.
      BASE_CLASSES = %w[
        Integrations::BaseChatNotification
        Integrations::BaseCi
        Integrations::BaseIssueTracker
        Integrations::BaseMonitoring
        Integrations::BaseSlackNotification
        Integrations::BaseSlashCommands
        Integrations::BaseThirdPartyWiki
      ].freeze

      BASE_ATTRIBUTES = %w[id instance project_id group_id created_at updated_at
        encrypted_properties encrypted_properties_iv properties].freeze

      SECTION_TYPE_CONFIGURATION = 'configuration'
      SECTION_TYPE_CONNECTION = 'connection'
      SECTION_TYPE_TRIGGER = 'trigger'

      SNOWPLOW_EVENT_ACTION = 'perform_integrations_action'
      SNOWPLOW_EVENT_LABEL = 'redis_hll_counters.ecosystem.ecosystem_total_unique_counts_monthly'

      included do
        include Sortable
        include Importable
        include Integrations::Loggable
        include Integrations::HasDataFields
        include Integrations::ResetSecretFields
        include FromUnion
        include EachBatch
        extend SafeFormatHelper
        extend ::Gitlab::Utils::Override

        self.allow_legacy_sti_class = true
        self.inheritance_column = :type_new # rubocop:disable Database/AvoidInheritanceColumn -- existing code moved as is

        attr_encrypted :properties,
          mode: :per_attribute_iv,
          key: Settings.attr_encrypted_db_key_base_32,
          algorithm: 'aes-256-gcm',
          marshal: true,
          marshaler: ::Gitlab::Json,
          encode: false,
          encode_iv: false

        alias_attribute :name, :title
        # Handle assignment of props with symbol keys.
        # To do this correctly, we need to call the method generated by attr_encrypted.
        alias_method :attr_encrypted_props=, :properties=
        private :attr_encrypted_props=

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

        alias_attribute :type, :type_new

        attribute :active, default: false
        attribute :alert_events, default: true
        attribute :incident_events, default: false
        attribute :category, default: 'common'
        attribute :commit_events, default: true
        attribute :confidential_issues_events, default: true
        attribute :confidential_note_events, default: true
        attribute :deployment_events, default: false
        attribute :issues_events, default: true
        attribute :job_events, default: true
        attribute :merge_requests_events, default: true
        attribute :note_events, default: true
        attribute :pipeline_events, default: true
        attribute :push_events, default: true
        attribute :tag_push_events, default: true
        attribute :wiki_page_events, default: true
        attribute :group_mention_events, default: false
        attribute :group_confidential_mention_events, default: false

        after_initialize :initialize_properties

        after_commit :reset_updated_properties

        belongs_to :project, inverse_of: :integrations
        belongs_to :group, inverse_of: :integrations

        validates :project_id, presence: true, unless: -> { instance_level? || group_level? }
        validates :group_id, presence: true, unless: -> { instance_level? || project_level? }
        validates :project_id, :group_id, absence: true, if: -> { instance_level? }
        validates :type, presence: true, exclusion: BASE_CLASSES
        validates :type, uniqueness: { scope: :instance }, if: :instance_level?
        validates :type, uniqueness: { scope: :project_id }, if: :project_level?
        validates :type, uniqueness: { scope: :group_id }, if: :group_level?
        validate :validate_belongs_to_project_or_group

        scope :external_issue_trackers, -> { where(category: 'issue_tracker').active }
        scope :third_party_wikis, -> { where(category: 'third_party_wiki').active }
        scope :by_name, ->(name) { by_type(integration_name_to_type(name)) }
        scope :external_wikis, -> { by_name(:external_wiki).active }
        scope :active, -> { where(active: true) }
        scope :by_type, ->(type) { where(type: type) } # INTERNAL USE ONLY: use by_name instead
        scope :by_active_flag, ->(flag) { where(active: flag) }
        scope :inherit_from_id, ->(id) { where(inherit_from_id: id) }
        scope :with_default_settings, -> { where.not(inherit_from_id: nil) }
        scope :with_custom_settings, -> { where(inherit_from_id: nil) }
        scope :for_group, ->(group) {
          types = available_integration_types(include_project_specific: false)
          where(group_id: group, type: types)
        }

        scope :for_instance, -> {
          types = available_integration_types(include_project_specific: false, include_group_specific: false)
          where(instance: true, type: types)
        }

        scope :push_hooks, -> { where(push_events: true, active: true) }
        scope :tag_push_hooks, -> { where(tag_push_events: true, active: true) }
        scope :issue_hooks, -> { where(issues_events: true, active: true) }
        scope :confidential_issue_hooks, -> { where(confidential_issues_events: true, active: true) }
        scope :merge_request_hooks, -> { where(merge_requests_events: true, active: true) }
        scope :note_hooks, -> { where(note_events: true, active: true) }
        scope :confidential_note_hooks, -> { where(confidential_note_events: true, active: true) }
        scope :job_hooks, -> { where(job_events: true, active: true) }
        scope :archive_trace_hooks, -> { where(archive_trace_events: true, active: true) }
        scope :pipeline_hooks, -> { where(pipeline_events: true, active: true) }
        scope :wiki_page_hooks, -> { where(wiki_page_events: true, active: true) }
        scope :deployment_hooks, -> { where(deployment_events: true, active: true) }
        scope :alert_hooks, -> { where(alert_events: true, active: true) }
        scope :incident_hooks, -> { where(incident_events: true, active: true) }
        scope :deployment, -> { where(category: 'deployment') }
        scope :group_mention_hooks, -> { where(group_mention_events: true, active: true) }
        scope :group_confidential_mention_hooks, -> { where(group_confidential_mention_events: true, active: true) }
        scope :exclusions_for_project, ->(project) { where(project: project, active: false) }

        class << self
          private

          attr_writer :field_storage

          def field_storage
            @field_storage || :properties
          end
        end

        # rubocop:disable Gitlab/NoCodeCoverageComment -- existing code moved as is
        # :nocov: Tested on subclasses.
        def self.field(name, storage: field_storage, **attrs)
          fields << ::Integrations::Field.new(name: name, integration_class: self, **attrs)

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

          boolean_accessor(name) if attrs[:type] == :checkbox && storage != :attribute
        end
        # :nocov:
        # rubocop:enable Gitlab/NoCodeCoverageComment

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

        def fields
          self.class.fields.dup
        end

        # Provide convenient accessor methods for each serialized property.
        # Also keep track of updated properties in a similar way as ActiveModel::Dirty
        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

        # Provide convenient boolean accessor methods for each serialized property.
        # Also keep track of updated properties in a similar way as ActiveModel::Dirty
        def self.boolean_accessor(*args)
          args.each do |arg|
            class_eval <<~RUBY, __FILE__, __LINE__ + 1
              # Make the original getter available as a private method.
              alias_method :#{arg}_before_type_cast, :#{arg}
              private(:#{arg}_before_type_cast)

              def #{arg}
                Gitlab::Utils.to_boolean(#{arg}_before_type_cast)
              end

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

        def self.title
          raise NotImplementedError
        end

        def self.description
          raise NotImplementedError
        end

        def self.help
          # no-op
        end

        def self.to_param
          raise NotImplementedError
        end

        def self.attribution_notice
          # no-op
        end

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

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

        def self.default_test_event
          'push'
        end

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

        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,
            include_group_specific: group_id.present?,
            include_instance_specific: instance))

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

        def self.find_or_initialize_all_non_project_specific(scope, include_instance_specific: false)
          scope + build_nonexistent_integrations_for(scope,
            include_group_specific: !include_instance_specific,
            include_instance_specific: include_instance_specific)
        end

        def self.build_nonexistent_integrations_for(...)
          nonexistent_integration_types_for(...).map do |type|
            integration_type_to_model(type).new
          end
        end
        private_class_method :build_nonexistent_integrations_for

        # Returns a list of integration types that do not exist in the given scope.
        # Example: ["AsanaService", ...]
        def self.nonexistent_integration_types_for(
          scope,
          include_group_specific: false,
          include_instance_specific: false)
          # Using #map instead of #pluck to save one query count. This is because
          # ActiveRecord loaded the object here, so we don't need to query again later.
          available_integration_types(
            include_project_specific: false,
            include_group_specific: include_group_specific,
            include_instance_specific: include_instance_specific
          ) - scope.map(&:type)
        end
        private_class_method :nonexistent_integration_types_for

        # Returns a list of available integration names.
        # Example: ["asana", ...]
        def self.available_integration_names(
          include_project_specific: true,
          include_group_specific: true,
          include_instance_specific: true,
          include_dev: true,
          include_disabled: false)
          names = integration_names.dup
          names.concat(project_specific_integration_names) if include_project_specific
          names.concat(dev_integration_names) if include_dev
          names.concat(instance_specific_integration_names) if include_instance_specific

          if include_project_specific || include_group_specific
            names.concat(project_and_group_specific_integration_names)
          end

          names -= disabled_integration_names unless include_disabled

          names.sort_by(&:downcase)
        end

        def self.integration_names
          names = INTEGRATION_NAMES.dup

          unless Feature.enabled?(:gitlab_for_slack_app_instance_and_group_level, type: :beta) && # rubocop:disable Gitlab/FeatureFlagWithoutActor -- existing code moved as is
              (Gitlab::CurrentSettings.slack_app_enabled || Gitlab.dev_or_test_env?)
            names.delete('gitlab_slack_application')
          end

          names
        end

        def self.instance_specific_integration_names
          INSTANCE_LEVEL_ONLY_INTEGRATION_NAMES
        end

        def self.instance_specific_integration_types
          instance_specific_integration_names.map { |name| integration_name_to_type(name) }
        end

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

          DEV_INTEGRATION_NAMES
        end

        def self.project_specific_integration_names
          names = PROJECT_LEVEL_ONLY_INTEGRATION_NAMES.dup

          if Feature.disabled?(:gitlab_for_slack_app_instance_and_group_level, type: :beta) && # rubocop:disable Gitlab/FeatureFlagWithoutActor -- existing code moved as is
              (Gitlab::CurrentSettings.slack_app_enabled || Gitlab.dev_or_test_env?)
            names << 'gitlab_slack_application'
          end

          names
        end

        def self.project_and_group_specific_integration_names
          PROJECT_AND_GROUP_LEVEL_ONLY_INTEGRATION_NAMES.dup
        end
        private_class_method :project_and_group_specific_integration_names

        # Returns a list of available integration types.
        # Example: ["Integrations::Asana", ...]
        def self.available_integration_types(...)
          available_integration_names(...).map do # rubocop:disable Style/NumberedParameters -- existing code moved as is
            integration_name_to_type(_1)
          end
        end

        # Returns a list of disabled integration names.
        # Example: ["gitlab_slack_application", ...]
        def self.disabled_integration_names
          # The GitLab for Slack app integration is only available when enabled through settings.
          # The Slack Slash Commands integration is only available for customers
          # who cannot use the GitLab for Slack app.
          disabled = Gitlab::CurrentSettings.slack_app_enabled ? ['slack_slash_commands'] : ['gitlab_slack_application']
          disabled += ['jira_cloud_app'] unless Gitlab::CurrentSettings.jira_connect_application_key.present?
          disabled
        end
        private_class_method :disabled_integration_names

        # Returns the model for the given integration name.
        # Example: :asana => Integrations::Asana
        def self.integration_name_to_model(name)
          type = integration_name_to_type(name)
          integration_type_to_model(type)
        end

        # Returns the STI type for the given integration name.
        # Example: "asana" => "Integrations::Asana"
        def self.integration_name_to_type(name)
          name = name.to_s
          if available_integration_names(include_disabled: true).exclude?(name)
            Gitlab::ErrorTracking.track_and_raise_for_dev_exception(UnknownType.new(name.inspect))
          else
            "Integrations::#{name.camelize}"
          end
        end

        # Returns the model for the given STI type.
        # Example: "Integrations::Asana" => Integrations::Asana
        def self.integration_type_to_model(type)
          type.constantize
        end
        private_class_method :integration_type_to_model

        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

        # Duplicating an integration also duplicates the data fields. Duped records have different ciphertexts.
        override :dup
        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

        def inheritable?
          instance_level? || group_level?
        end

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

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

        def self.closest_group_integration(type, scope)
          group_ids = scope.ancestors(hierarchy_order: :asc).reselect(:id)
          array = group_ids.to_sql.present? ? "array(#{group_ids.to_sql})" : 'ARRAY[]'

          where(type: type, group_id: group_ids, inherit_from_id: nil)
            .order(Arel.sql("array_position(#{array}::bigint[], #{table_name}.group_id)"))
            .first
        end
        private_class_method :closest_group_integration

        def self.instance_level_integration(type)
          find_by(type: type, instance: true)
        end
        private_class_method :instance_level_integration

        def self.default_integrations(owner, scope)
          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([scope.where(instance: true), scope.where(group_id: group_ids, inherit_from_id: nil)])
            .order(order)
            .group_by(&:type)
            .transform_values(&:first)
        end
        private_class_method :default_integrations

        def self.create_from_default_integrations(owner, association)
          active_default_count = create_from_active_default_integrations(owner, association)
          default_instance_specific_count = create_from_default_instance_specific_integrations(owner, association)
          active_default_count + default_instance_specific_count
        end

        # Returns the number of successfully saved integrations
        # Duplicate integrations are excluded from this count by their validations.
        def self.create_from_active_default_integrations(owner, association)
          default_integrations(
            owner,
            active.where.not(type: instance_specific_integration_types)
          ).count { |_type, integration| build_from_integration(integration, association => owner.id).save }
        end

        def self.create_from_default_instance_specific_integrations(owner, association)
          default_integrations(
            owner,
            where(type: instance_specific_integration_types)
          ).count { |_type, integration| build_from_integration(integration, association => owner.id).save }
        end

        def self.descendants_from_self_or_ancestors_from(integration)
          scope = where(type: integration.type)
          from_union([
            scope.where(group: integration.group.descendants),
            scope.where(project: Project.in_namespace(integration.group.self_and_descendants))
          ])
        end

        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

        def activated?
          active
        end

        def operating?
          active && persisted?
        end

        def manual_activation?
          true
        end

        def editable?
          true
        end

        def activate_disabled_reason
          nil
        end

        def category
          read_attribute(:category).to_sym
        end

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

        def title
          self.class.title
        end

        def description
          self.class.description
        end

        def help
          self.class.help
        end

        def to_param
          self.class.to_param
        end

        def attribution_notice
          self.class.attribution_notice
        end

        def sections
          []
        end

        def secret_fields
          fields.select(&:secret?).pluck(:name) # rubocop:disable Database/AvoidUsingPluckWithoutLimit -- existing code moved as is
        end

        # Expose a list of fields in the JSON endpoint.
        #
        # This list is used in `Integration#as_json(only: json_fields)`.
        def json_fields
          %w[active]
        end

        # properties is always nil - ignore it.
        override :attributes
        def attributes
          super.except('properties')
        end

        # Returns a hash of attributes (columns => values) used for inserting into the database.
        def to_database_hash
          column = self.class.attribute_aliases.fetch('type', 'type')

          attributes_for_database.except(*BASE_ATTRIBUTES)
                                 .merge(column => type)
                                 .merge(reencrypt_properties)
        end

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

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

        def event_channel_names
          []
        end

        def event_names
          self.class.event_names
        end

        def api_field_names
          fields # rubocop:disable Style/NumberedParameters -- existing code moved as is
            .reject { _1[:type] == :password || _1[:name] == 'webhook' || (_1.key?(:if) && _1[:if] != true) } # rubocop:disable Style/NumberedParameters -- existing code moved as is
            .pluck(:name) # rubocop:disable Database/AvoidUsingPluckWithoutLimit -- existing code moved as is
        end

        def self.api_arguments
          fields.filter_map do |field|
            next if field.if != true

            {
              required: field.required?,
              name: field.name.to_sym,
              type: field.api_type,
              desc: field.description
            }
          end
        end

        def self.instance_specific?
          false
        end

        def self.pluck_group_id
          pluck(:group_id) # rubocop:disable Database/AvoidUsingPluckWithoutLimit -- existing code moved as is
        end

        def form_fields
          fields.reject { _1[:api_only] == true || (_1.key?(:if) && _1[:if] != true) } # rubocop:disable Style/NumberedParameters -- existing code moved as is
        end

        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

        def supported_events
          self.class.supported_events
        end

        def default_test_event
          self.class.default_test_event
        end

        def execute(data)
          # implement inside child
        end

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

        # Disable test for instance-level and group-level integrations.
        # https://gitlab.com/gitlab-org/gitlab/-/issues/213138
        def testable?
          project_level?
        end

        def project_level?
          project_id.present?
        end

        def group_level?
          group_id.present?
        end

        def instance_level?
          instance?
        end

        def parent
          project || group
        end

        # 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.
        def updated_properties
          @updated_properties ||= ActiveSupport::HashWithIndifferentAccess.new
        end

        def reset_updated_properties
          @updated_properties = nil
        end

        def async_execute(data)
          return if ::Gitlab::SilentMode.enabled?

          # Temporarily log when we return within this method to gather data for
          # https://gitlab.com/gitlab-org/gitlab/-/issues/382999
          unless supported_events.include?(data[:object_kind])
            log_info(
              'async_execute did nothing due to event not being supported',
              event: data[:object_kind]
            )
            return
          end

          Integrations::ExecuteWorker.perform_async(id, data.deep_stringify_keys)
        end

        # override if needed
        def supports_data_fields?
          false
        end

        def chat?
          category == :chat
        end

        def ci?
          category == :ci
        end

        def deactivate!
          update(active: false)
        end

        def activate!
          update(active: true)
        end

        def toggle!
          active? ? deactivate! : activate!
        end

        private

        def self.build_help_page_url(url_path, help_text, link_text = _("Learn More"), options = {})
          docs_link = ActionController::Base.helpers.link_to(
            '',
            Rails.application.routes.url_helpers.help_page_url(url_path, **options), # rubocop:disable Gitlab/DocumentationLinks/Link: -- existing code moved as is
            target: '_blank',
            rel: 'noopener noreferrer'
          )
          tag_pair_docs_link = tag_pair(docs_link, :link_start, :link_end)

          safe_format(help_text + " %{link_start}#{link_text}%{link_end}.", tag_pair_docs_link)
        end

        # Ancestors sorted by hierarchy depth in bottom-top order.
        def self.sorted_ancestors(scope)
          if scope.root_ancestor.use_traversal_ids?
            Namespace.from(scope.ancestors(hierarchy_order: :asc))
          else
            scope.ancestors
          end
        end

        def validate_belongs_to_project_or_group
          return unless project_level? && group_level?

          errors.add(:project_id, 'The integration cannot belong to both a project and a group')
        end

        def validate_recipients?
          activated? && !importing?
        end
      end
    end
  end
end

Integration.prepend_mod_with('Integration')