2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
# To add new integration you should build a class inherited from Integration
|
2014-09-02 18:07:02 +05:30
|
|
|
# and implement a set of methods
|
2021-06-08 01:23:25 +05:30
|
|
|
class Integration < ApplicationRecord
|
2015-04-26 12:48:37 +05:30
|
|
|
include Sortable
|
2018-03-17 18:26:18 +05:30
|
|
|
include Importable
|
2018-11-20 20:47:30 +05:30
|
|
|
include ProjectServicesLoggable
|
2021-09-04 01:27:46 +05:30
|
|
|
include Integrations::HasDataFields
|
2021-01-03 14:25:43 +05:30
|
|
|
include FromUnion
|
2021-01-29 00:20:46 +05:30
|
|
|
include EachBatch
|
2022-05-07 20:08:51 +05:30
|
|
|
include IgnorableColumns
|
2022-06-21 17:19:12 +05:30
|
|
|
extend ::Gitlab::Utils::Override
|
2022-05-07 20:08:51 +05:30
|
|
|
|
|
|
|
ignore_column :template, remove_with: '15.0', remove_after: '2022-04-22'
|
|
|
|
ignore_column :type, remove_with: '15.0', remove_after: '2022-04-22'
|
2022-06-21 17:19:12 +05:30
|
|
|
ignore_column :properties, remove_with: '15.1', remove_after: '2022-05-22'
|
2022-05-07 20:08:51 +05:30
|
|
|
|
|
|
|
UnknownType = Class.new(StandardError)
|
|
|
|
|
|
|
|
self.inheritance_column = :type_new
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
INTEGRATION_NAMES = %w[
|
2021-09-30 23:02:18 +05:30
|
|
|
asana assembla bamboo bugzilla buildkite campfire confluence custom_issue_tracker datadog discord
|
2022-05-07 20:08:51 +05:30
|
|
|
drone_ci emails_on_push ewm external_wiki flowdock hangouts_chat harbor irker jira
|
2020-04-22 19:07:51 +05:30
|
|
|
mattermost mattermost_slash_commands microsoft_teams packagist pipelines_email
|
2022-01-26 12:08:38 +05:30
|
|
|
pivotaltracker prometheus pushover redmine slack slack_slash_commands teamcity unify_circuit webex_teams youtrack zentao
|
2020-04-22 19:07:51 +05:30
|
|
|
].freeze
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
# TODO Shimo is temporary disabled on group and instance-levels.
|
|
|
|
# See: https://gitlab.com/gitlab-org/gitlab/-/issues/345677
|
2021-06-08 01:23:25 +05:30
|
|
|
PROJECT_SPECIFIC_INTEGRATION_NAMES = %w[
|
2022-01-26 12:08:38 +05:30
|
|
|
jenkins shimo
|
2021-02-22 17:27:13 +05:30
|
|
|
].freeze
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
# Fake integrations to help with local development.
|
|
|
|
DEV_INTEGRATION_NAMES = %w[
|
2021-02-22 17:27:13 +05:30
|
|
|
mock_ci mock_monitoring
|
2020-04-22 19:07:51 +05:30
|
|
|
].freeze
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
# Base classes which aren't actual integrations.
|
|
|
|
BASE_CLASSES = %w[
|
|
|
|
Integrations::BaseChatNotification
|
|
|
|
Integrations::BaseCi
|
|
|
|
Integrations::BaseIssueTracker
|
|
|
|
Integrations::BaseMonitoring
|
|
|
|
Integrations::BaseSlashCommands
|
|
|
|
].freeze
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
SECTION_TYPE_CONNECTION = 'connection'
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
attr_encrypted :properties,
|
2022-05-07 20:08:51 +05:30
|
|
|
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
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
# 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
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
alias_attribute :type, :type_new
|
2021-06-08 01:23:25 +05:30
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
default_value_for :active, false
|
2020-06-23 00:09:42 +05:30
|
|
|
default_value_for :alert_events, true
|
2020-11-24 15:15:51 +05:30
|
|
|
default_value_for :category, 'common'
|
2017-08-17 22:00:37 +05:30
|
|
|
default_value_for :commit_events, true
|
2020-11-24 15:15:51 +05:30
|
|
|
default_value_for :confidential_issues_events, true
|
2018-04-05 14:03:07 +05:30
|
|
|
default_value_for :confidential_note_events, true
|
2020-11-24 15:15:51 +05:30
|
|
|
default_value_for :issues_events, true
|
2017-09-10 17:25:29 +05:30
|
|
|
default_value_for :job_events, true
|
2020-11-24 15:15:51 +05:30
|
|
|
default_value_for :merge_requests_events, true
|
|
|
|
default_value_for :note_events, true
|
2016-09-29 09:46:39 +05:30
|
|
|
default_value_for :pipeline_events, true
|
2020-11-24 15:15:51 +05:30
|
|
|
default_value_for :push_events, true
|
|
|
|
default_value_for :tag_push_events, true
|
2016-06-02 11:05:42 +05:30
|
|
|
default_value_for :wiki_page_events, true
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
after_initialize :initialize_properties
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
after_commit :reset_updated_properties
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
belongs_to :project, inverse_of: :integrations
|
|
|
|
belongs_to :group, inverse_of: :integrations
|
|
|
|
has_one :service_hook, inverse_of: :integration, foreign_key: :service_id
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
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? }
|
2021-09-04 01:27:46 +05:30
|
|
|
validates :type, presence: true, exclusion: BASE_CLASSES
|
2021-06-08 01:23:25 +05:30
|
|
|
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?
|
2020-10-24 23:57:45 +05:30
|
|
|
validate :validate_belongs_to_project_or_group
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
scope :external_issue_trackers, -> { where(category: 'issue_tracker').active }
|
2022-06-21 17:19:12 +05:30
|
|
|
# TODO: Will be modified in 15.0
|
|
|
|
# Details: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/74501#note_744393645
|
|
|
|
scope :third_party_wikis, -> { where(type: %w[Integrations::Confluence Integrations::Shimo]).active }
|
2022-05-07 20:08:51 +05:30
|
|
|
scope :by_name, ->(name) { by_type(integration_name_to_type(name)) }
|
|
|
|
scope :external_wikis, -> { by_name(:external_wiki).active }
|
2016-01-29 22:53:50 +05:30
|
|
|
scope :active, -> { where(active: true) }
|
2022-05-07 20:08:51 +05:30
|
|
|
scope :by_type, ->(type) { where(type: type) } # INTERNAL USE ONLY: use by_name instead
|
2020-04-22 19:07:51 +05:30
|
|
|
scope :by_active_flag, -> (flag) { where(active: flag) }
|
2021-01-03 14:25:43 +05:30
|
|
|
scope :inherit_from_id, -> (id) { where(inherit_from_id: id) }
|
2021-10-27 15:23:28 +05:30
|
|
|
scope :with_default_settings, -> { where.not(inherit_from_id: nil) }
|
|
|
|
scope :with_custom_settings, -> { where(inherit_from_id: nil) }
|
2021-09-30 23:02:18 +05:30
|
|
|
scope :for_group, -> (group) { where(group_id: group, type: available_integration_types(include_project_specific: false)) }
|
|
|
|
scope :for_instance, -> { where(instance: true, type: available_integration_types(include_project_specific: false)) }
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
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) }
|
2016-09-29 09:46:39 +05:30
|
|
|
scope :confidential_issue_hooks, -> { where(confidential_issues_events: true, active: true) }
|
2015-04-26 12:48:37 +05:30
|
|
|
scope :merge_request_hooks, -> { where(merge_requests_events: true, active: true) }
|
|
|
|
scope :note_hooks, -> { where(note_events: true, active: true) }
|
2018-04-05 14:03:07 +05:30
|
|
|
scope :confidential_note_hooks, -> { where(confidential_note_events: true, active: true) }
|
2017-09-10 17:25:29 +05:30
|
|
|
scope :job_hooks, -> { where(job_events: true, active: true) }
|
2022-03-02 08:16:31 +05:30
|
|
|
scope :archive_trace_hooks, -> { where(archive_trace_events: true, active: true) }
|
2016-09-13 17:45:13 +05:30
|
|
|
scope :pipeline_hooks, -> { where(pipeline_events: true, active: true) }
|
2016-06-02 11:05:42 +05:30
|
|
|
scope :wiki_page_hooks, -> { where(wiki_page_events: true, active: true) }
|
2019-07-31 22:56:46 +05:30
|
|
|
scope :deployment_hooks, -> { where(deployment_events: true, active: true) }
|
2020-06-23 00:09:42 +05:30
|
|
|
scope :alert_hooks, -> { where(alert_events: true, active: true) }
|
2018-03-17 18:26:18 +05:30
|
|
|
scope :deployment, -> { where(category: 'deployment') }
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
class << self
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_writer :field_storage
|
|
|
|
|
|
|
|
def field_storage
|
|
|
|
@field_storage || :properties
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# :nocov: Tested on subclasses.
|
|
|
|
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
|
|
|
|
# :nocov:
|
|
|
|
|
|
|
|
def self.fields
|
|
|
|
@fields ||= []
|
|
|
|
end
|
|
|
|
|
|
|
|
def fields
|
|
|
|
self.class.fields
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
# 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}
|
2022-06-21 17:19:12 +05:30
|
|
|
properties['#{arg}'] if properties.present?
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def #{arg}=(value)
|
|
|
|
self.properties ||= {}
|
|
|
|
updated_properties['#{arg}'] = #{arg} unless #{arg}_changed?
|
2022-06-21 17:19:12 +05:30
|
|
|
self.properties = self.properties.merge('#{arg}' => value)
|
2020-11-24 15:15:51 +05:30
|
|
|
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)
|
2022-06-21 17:19:12 +05:30
|
|
|
prop_accessor(*args)
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
args.each do |arg|
|
|
|
|
class_eval <<~RUBY, __FILE__, __LINE__ + 1
|
2021-09-30 23:02:18 +05:30
|
|
|
def #{arg}
|
2022-06-21 17:19:12 +05:30
|
|
|
return if properties.blank?
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
Gitlab::Utils.to_boolean(properties['#{arg}'])
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
def #{arg}?
|
|
|
|
# '!!' is used because nil or empty string is converted to nil
|
2021-09-30 23:02:18 +05:30
|
|
|
!!#{arg}
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.to_param
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.event_names
|
2021-09-30 23:02:18 +05:30
|
|
|
self.supported_events.map { |event| IntegrationsHelper.integration_event_field_name(event) }
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def self.supported_events
|
|
|
|
%w[commit push tag_push issue confidential_issue merge_request wiki_page]
|
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def self.default_test_event
|
|
|
|
'push'
|
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
def self.event_description(event)
|
|
|
|
IntegrationsHelper.integration_event_description(event)
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
def self.find_or_initialize_non_project_specific_integration(name, instance: false, group_id: nil)
|
2021-09-30 23:02:18 +05:30
|
|
|
return unless name.in?(available_integration_names(include_project_specific: false))
|
2021-06-08 01:23:25 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
integration_name_to_model(name).find_or_initialize_by(instance: instance, group_id: group_id)
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
def self.find_or_initialize_all_non_project_specific(scope)
|
2021-09-30 23:02:18 +05:30
|
|
|
scope + build_nonexistent_integrations_for(scope)
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
def self.build_nonexistent_integrations_for(scope)
|
|
|
|
nonexistent_integration_types_for(scope).map do |type|
|
|
|
|
integration_type_to_model(type).new
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
end
|
2021-09-30 23:02:18 +05:30
|
|
|
private_class_method :build_nonexistent_integrations_for
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
# Returns a list of integration types that do not exist in the given scope.
|
2021-06-08 01:23:25 +05:30
|
|
|
# Example: ["AsanaService", ...]
|
2021-09-30 23:02:18 +05:30
|
|
|
def self.nonexistent_integration_types_for(scope)
|
2020-11-24 15:15:51 +05:30
|
|
|
# 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.
|
2021-09-30 23:02:18 +05:30
|
|
|
available_integration_types(include_project_specific: false) - scope.map(&:type)
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
2021-09-30 23:02:18 +05:30
|
|
|
private_class_method :nonexistent_integration_types_for
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
# Returns a list of available integration names.
|
2021-06-08 01:23:25 +05:30
|
|
|
# Example: ["asana", ...]
|
2021-09-04 01:27:46 +05:30
|
|
|
# @deprecated
|
2021-09-30 23:02:18 +05:30
|
|
|
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
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
names.sort_by(&:downcase)
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def self.integration_names
|
2021-06-08 01:23:25 +05:30
|
|
|
INTEGRATION_NAMES
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
def self.dev_integration_names
|
2020-11-24 15:15:51 +05:30
|
|
|
return [] unless Rails.env.development?
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
DEV_INTEGRATION_NAMES
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
def self.project_specific_integration_names
|
2021-06-08 01:23:25 +05:30
|
|
|
PROJECT_SPECIFIC_INTEGRATION_NAMES
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
# Returns a list of available integration types.
|
2022-05-07 20:08:51 +05:30
|
|
|
# Example: ["Integrations::Asana", ...]
|
2021-09-30 23:02:18 +05:30
|
|
|
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)
|
2021-01-29 00:20:46 +05:30
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
# Returns the model for the given integration name.
|
2022-05-07 20:08:51 +05:30
|
|
|
# Example: :asana => Integrations::Asana
|
2021-09-04 01:27:46 +05:30
|
|
|
def self.integration_name_to_model(name)
|
|
|
|
type = integration_name_to_type(name)
|
|
|
|
integration_type_to_model(type)
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
# Returns the STI type for the given integration name.
|
2022-05-07 20:08:51 +05:30
|
|
|
# Example: "asana" => "Integrations::Asana"
|
2021-09-04 01:27:46 +05:30
|
|
|
def self.integration_name_to_type(name)
|
2022-05-07 20:08:51 +05:30
|
|
|
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
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the model for the given STI type.
|
2022-05-07 20:08:51 +05:30
|
|
|
# Example: "Integrations::Asana" => Integrations::Asana
|
2021-09-04 01:27:46 +05:30
|
|
|
def self.integration_type_to_model(type)
|
2022-05-07 20:08:51 +05:30
|
|
|
type.constantize
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
private_class_method :integration_type_to_model
|
2021-06-08 01:23:25 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def self.build_from_integration(integration, project_id: nil, group_id: nil)
|
2021-06-08 01:23:25 +05:30
|
|
|
new_integration = integration.dup
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
new_integration.instance = false
|
|
|
|
new_integration.project_id = project_id
|
|
|
|
new_integration.group_id = group_id
|
2022-06-21 17:19:12 +05:30
|
|
|
new_integration.inherit_from_id = integration.id if integration.inheritable?
|
2021-06-08 01:23:25 +05:30
|
|
|
new_integration
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
# 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
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
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)
|
2021-11-11 11:23:49 +05:30
|
|
|
group_ids = scope.ancestors(hierarchy_order: :asc).reselect(:id)
|
2020-11-24 15:15:51 +05:30
|
|
|
array = group_ids.to_sql.present? ? "array(#{group_ids.to_sql})" : 'ARRAY[]'
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
where(type: type, group_id: group_ids, inherit_from_id: nil)
|
2021-09-30 23:02:18 +05:30
|
|
|
.order(Arel.sql("array_position(#{array}::bigint[], #{table_name}.group_id)"))
|
2020-11-24 15:15:51 +05:30
|
|
|
.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
|
2016-01-29 22:53:50 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
# 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)
|
|
|
|
group_ids = sorted_ancestors(owner).select(:id)
|
2021-01-03 14:25:43 +05:30
|
|
|
array = group_ids.to_sql.present? ? "array(#{group_ids.to_sql})" : 'ARRAY[]'
|
2022-06-21 17:19:12 +05:30
|
|
|
order = Arel.sql("type_new ASC, array_position(#{array}::bigint[], #{table_name}.group_id), instance DESC")
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
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 }
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
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
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def activated?
|
|
|
|
active
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def operating?
|
|
|
|
active && persisted?
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def show_active_box?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def editable?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
def activate_disabled_reason
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def category
|
2016-01-29 22:53:50 +05:30
|
|
|
read_attribute(:category).to_sym
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def initialize_properties
|
2022-06-21 17:19:12 +05:30
|
|
|
self.properties = {} if has_attribute?(:encrypted_properties) && encrypted_properties.nil?
|
2022-05-07 20:08:51 +05:30
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def title
|
|
|
|
# implement inside child
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
# implement inside child
|
|
|
|
end
|
|
|
|
|
|
|
|
def help
|
|
|
|
# implement inside child
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_param
|
|
|
|
# implement inside child
|
2017-08-17 22:00:37 +05:30
|
|
|
self.class.to_param
|
|
|
|
end
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
def sections
|
2014-09-02 18:07:02 +05:30
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
# TODO: Once all integrations use `Integrations::Field` we can
|
|
|
|
# use `#secret?` here.
|
|
|
|
def secret_fields
|
2021-11-11 11:23:49 +05:30
|
|
|
fields.select { |f| f[:type] == 'password' }.pluck(:name)
|
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
# Expose a list of fields in the JSON endpoint.
|
|
|
|
#
|
2021-06-08 01:23:25 +05:30
|
|
|
# This list is used in `Integration#as_json(only: json_fields)`.
|
2019-12-04 20:38:33 +05:30
|
|
|
def json_fields
|
2020-11-24 15:15:51 +05:30
|
|
|
%w[active]
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
# properties is always nil - ignore it.
|
|
|
|
override :attributes
|
|
|
|
def attributes
|
|
|
|
super.except('properties')
|
|
|
|
end
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
# return a hash of columns => values suitable for passing to insert_all
|
2021-09-30 23:02:18 +05:30
|
|
|
def to_integration_hash
|
2022-05-07 20:08:51 +05:30
|
|
|
column = self.class.attribute_aliases.fetch('type', 'type')
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
as_json(except: %w[id instance project_id group_id])
|
2022-05-07 20:08:51 +05:30
|
|
|
.merge(column => type)
|
|
|
|
.merge(reencrypt_properties)
|
|
|
|
end
|
|
|
|
|
|
|
|
def reencrypt_properties
|
|
|
|
unless properties.nil? || properties.empty?
|
2022-06-21 17:19:12 +05:30
|
|
|
alg = self.class.encrypted_attributes[:properties][:algorithm]
|
2022-05-07 20:08:51 +05:30
|
|
|
iv = generate_iv(alg)
|
2022-06-21 17:19:12 +05:30
|
|
|
ep = self.class.encrypt(:properties, properties, { iv: iv })
|
2022-05-07 20:08:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
{ 'encrypted_properties' => ep, 'encrypted_properties_iv' => iv }
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def to_data_fields_hash
|
2021-12-11 22:18:48 +05:30
|
|
|
data_fields.as_json(only: data_fields.class.column_names).except('id', 'service_id', 'integration_id')
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def event_channel_names
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def event_names
|
2017-08-17 22:00:37 +05:30
|
|
|
self.class.event_names
|
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def event_field(event)
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def api_field_names
|
2022-05-07 20:08:51 +05:30
|
|
|
fields
|
|
|
|
.reject { _1[:type] == 'password' }
|
|
|
|
.pluck(:name)
|
|
|
|
.grep_v(/password|token|key/)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def global_fields
|
|
|
|
fields
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def configurable_events
|
2020-06-23 00:09:42 +05:30
|
|
|
events = supported_events
|
2018-03-27 19:54:05 +05:30
|
|
|
|
|
|
|
# No need to disable individual triggers when there is only one
|
|
|
|
if events.count == 1
|
|
|
|
[]
|
|
|
|
else
|
|
|
|
events
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def supported_events
|
2017-08-17 22:00:37 +05:30
|
|
|
self.class.supported_events
|
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def default_test_event
|
|
|
|
self.class.default_test_event
|
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
def execute(data)
|
2014-09-02 18:07:02 +05:30
|
|
|
# implement inside child
|
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
def test(data)
|
|
|
|
# default implementation
|
|
|
|
result = execute(data)
|
|
|
|
{ success: result.present?, result: result }
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
# Disable test for instance-level and group-level integrations.
|
2020-04-22 19:07:51 +05:30
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/213138
|
2021-09-30 23:02:18 +05:30
|
|
|
def testable?
|
|
|
|
project_level?
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
def project_level?
|
|
|
|
project_id.present?
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
def group_level?
|
|
|
|
group_id.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def instance_level?
|
|
|
|
instance?
|
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def parent
|
|
|
|
project || group
|
|
|
|
end
|
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
# Returns a hash of the properties that have been assigned a new value since last save,
|
|
|
|
# indicating their original values (attr => original value).
|
2016-01-29 22:53:50 +05:30
|
|
|
# ActiveRecord does not provide a mechanism to track changes in serialized keys,
|
2021-06-08 01:23:25 +05:30
|
|
|
# so we need a specific implementation for integration properties.
|
2015-10-24 18:46:33 +05:30
|
|
|
# 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
|
2016-01-29 22:53:50 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def async_execute(data)
|
|
|
|
return unless supported_events.include?(data[:object_kind])
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
ProjectServiceWorker.perform_async(id, data)
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
# override if needed
|
|
|
|
def supports_data_fields?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
private
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
# 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
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
def validate_belongs_to_project_or_group
|
2021-06-08 01:23:25 +05:30
|
|
|
errors.add(:project_id, 'The service cannot belong to both a project and a group') if project_level? && group_level?
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
def validate_recipients?
|
2018-03-17 18:26:18 +05:30
|
|
|
activated? && !importing?
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
Integration.prepend_mod_with('Integration')
|