2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-05-12 08:10:24 -04:00
|
|
|
# To add new integration you should build a class inherited from Integration
|
2013-05-22 09:58:44 -04:00
|
|
|
# and implement a set of methods
|
2021-05-12 08:10:24 -04:00
|
|
|
class Integration < ApplicationRecord
|
2015-02-05 17:20:55 -05:00
|
|
|
include Sortable
|
2018-01-25 10:21:23 -05:00
|
|
|
include Importable
|
2018-08-20 14:34:07 -04:00
|
|
|
include ProjectServicesLoggable
|
2021-05-27 05:10:32 -04:00
|
|
|
include Integrations::HasDataFields
|
2020-09-18 11:09:22 -04:00
|
|
|
include FromUnion
|
2020-10-23 05:08:41 -04:00
|
|
|
include EachBatch
|
2020-06-22 08:08:47 -04:00
|
|
|
|
2021-05-12 08:10:24 -04:00
|
|
|
INTEGRATION_NAMES = %w[
|
2021-06-21 08:07:45 -04:00
|
|
|
asana assembla bamboo bugzilla buildkite campfire confluence custom_issue_tracker datadog discord
|
2021-03-30 08:10:51 -04:00
|
|
|
drone_ci emails_on_push ewm external_wiki flowdock hangouts_chat irker jira
|
2020-04-08 02:09:54 -04:00
|
|
|
mattermost mattermost_slash_commands microsoft_teams packagist pipelines_email
|
2021-10-18 20:10:29 -04:00
|
|
|
pivotaltracker prometheus pushover redmine slack slack_slash_commands teamcity unify_circuit webex_teams youtrack zentao
|
2020-04-08 02:09:54 -04:00
|
|
|
].freeze
|
|
|
|
|
2021-05-12 08:10:24 -04:00
|
|
|
PROJECT_SPECIFIC_INTEGRATION_NAMES = %w[
|
2021-06-21 08:07:45 -04:00
|
|
|
jenkins
|
2020-11-26 10:09:30 -05:00
|
|
|
].freeze
|
|
|
|
|
2021-05-12 08:10:24 -04:00
|
|
|
# Fake integrations to help with local development.
|
|
|
|
DEV_INTEGRATION_NAMES = %w[
|
2020-12-16 07:09:53 -05:00
|
|
|
mock_ci mock_monitoring
|
2020-04-08 02:09:54 -04:00
|
|
|
].freeze
|
|
|
|
|
2021-06-03 23:10:08 -04:00
|
|
|
# Base classes which aren't actual integrations.
|
|
|
|
BASE_CLASSES = %w[
|
|
|
|
Integrations::BaseChatNotification
|
|
|
|
Integrations::BaseCi
|
|
|
|
Integrations::BaseIssueTracker
|
|
|
|
Integrations::BaseMonitoring
|
|
|
|
Integrations::BaseSlashCommands
|
|
|
|
].freeze
|
|
|
|
|
2017-07-03 10:01:41 -04:00
|
|
|
serialize :properties, JSON # rubocop:disable Cop/ActiveRecordSerialize
|
2014-09-07 20:54:18 -04:00
|
|
|
|
2021-05-10 14:10:41 -04:00
|
|
|
attribute :type, Gitlab::Integrations::StiType.new
|
|
|
|
|
2014-02-26 03:41:44 -05:00
|
|
|
default_value_for :active, false
|
2020-06-06 11:08:10 -04:00
|
|
|
default_value_for :alert_events, true
|
2020-08-27 11:10:21 -04:00
|
|
|
default_value_for :category, 'common'
|
2016-11-14 16:30:01 -05:00
|
|
|
default_value_for :commit_events, true
|
2020-08-27 11:10:21 -04:00
|
|
|
default_value_for :confidential_issues_events, true
|
2018-04-03 07:00:33 -04:00
|
|
|
default_value_for :confidential_note_events, true
|
2020-08-27 11:10:21 -04:00
|
|
|
default_value_for :issues_events, true
|
2017-05-15 11:11:45 -04:00
|
|
|
default_value_for :job_events, true
|
2020-08-27 11:10:21 -04:00
|
|
|
default_value_for :merge_requests_events, true
|
|
|
|
default_value_for :note_events, true
|
2016-09-09 08:32:06 -04:00
|
|
|
default_value_for :pipeline_events, true
|
2020-08-27 11:10:21 -04:00
|
|
|
default_value_for :push_events, true
|
|
|
|
default_value_for :tag_push_events, true
|
2016-02-28 02:26:52 -05:00
|
|
|
default_value_for :wiki_page_events, true
|
2014-09-11 11:48:29 -04:00
|
|
|
|
|
|
|
after_initialize :initialize_properties
|
2014-02-26 03:41:44 -05:00
|
|
|
|
2015-10-15 03:09:01 -04:00
|
|
|
after_commit :reset_updated_properties
|
|
|
|
|
2021-05-12 08:10:24 -04:00
|
|
|
belongs_to :project, inverse_of: :integrations
|
|
|
|
belongs_to :group, inverse_of: :integrations
|
|
|
|
has_one :service_hook, inverse_of: :integration, foreign_key: :service_id
|
2012-11-19 13:14:05 -05:00
|
|
|
|
2021-07-21 11:08:52 -04:00
|
|
|
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-06-03 23:10:08 -04:00
|
|
|
validates :type, presence: true, exclusion: BASE_CLASSES
|
2021-04-26 05:09:53 -04:00
|
|
|
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-08-07 14:09:53 -04:00
|
|
|
validate :validate_belongs_to_project_or_group
|
2013-01-03 02:52:14 -05:00
|
|
|
|
2020-08-03 08:09:47 -04:00
|
|
|
scope :external_issue_trackers, -> { where(category: 'issue_tracker').active }
|
2016-07-21 04:36:02 -04:00
|
|
|
scope :external_wikis, -> { where(type: 'ExternalWikiService').active }
|
2016-01-19 07:48:07 -05:00
|
|
|
scope :active, -> { where(active: true) }
|
2019-11-13 07:06:22 -05:00
|
|
|
scope :by_type, -> (type) { where(type: type) }
|
2020-04-08 02:09:54 -04:00
|
|
|
scope :by_active_flag, -> (flag) { where(active: flag) }
|
2020-09-29 11:10:08 -04:00
|
|
|
scope :inherit_from_id, -> (id) { where(inherit_from_id: id) }
|
2021-08-03 05:15:56 -04:00
|
|
|
scope :with_default_settings, -> { where.not(inherit_from_id: nil) }
|
|
|
|
scope :with_custom_settings, -> { where(inherit_from_id: nil) }
|
2021-06-23 14:07:10 -04:00
|
|
|
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-01-28 17:25:55 -05:00
|
|
|
|
2015-02-19 00:02:57 -05:00
|
|
|
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-08-30 18:11:46 -04:00
|
|
|
scope :confidential_issue_hooks, -> { where(confidential_issues_events: true, active: true) }
|
2015-02-19 00:02:57 -05:00
|
|
|
scope :merge_request_hooks, -> { where(merge_requests_events: true, active: true) }
|
2015-03-05 13:38:23 -05:00
|
|
|
scope :note_hooks, -> { where(note_events: true, active: true) }
|
2018-04-03 07:00:33 -04:00
|
|
|
scope :confidential_note_hooks, -> { where(confidential_note_events: true, active: true) }
|
2017-05-15 11:11:45 -04:00
|
|
|
scope :job_hooks, -> { where(job_events: true, active: true) }
|
2016-08-02 06:06:31 -04:00
|
|
|
scope :pipeline_hooks, -> { where(pipeline_events: true, active: true) }
|
2016-02-28 02:26:52 -05:00
|
|
|
scope :wiki_page_hooks, -> { where(wiki_page_events: true, active: true) }
|
2019-04-26 17:08:41 -04:00
|
|
|
scope :deployment_hooks, -> { where(deployment_events: true, active: true) }
|
2020-06-06 11:08:10 -04:00
|
|
|
scope :alert_hooks, -> { where(alert_events: true, active: true) }
|
2018-01-04 17:35:41 -05:00
|
|
|
scope :deployment, -> { where(category: 'deployment') }
|
2015-02-19 00:02:57 -05:00
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
# 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}']
|
|
|
|
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
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
self.prop_accessor(*args)
|
|
|
|
|
|
|
|
args.each do |arg|
|
|
|
|
class_eval <<~RUBY, __FILE__, __LINE__ + 1
|
2021-06-24 11:07:28 -04:00
|
|
|
def #{arg}
|
|
|
|
Gitlab::Utils.to_boolean(properties['#{arg}'])
|
|
|
|
end
|
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
def #{arg}?
|
|
|
|
# '!!' is used because nil or empty string is converted to nil
|
2021-06-24 11:07:28 -04:00
|
|
|
!!#{arg}
|
2020-08-27 11:10:21 -04:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.to_param
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.event_names
|
2021-06-24 11:07:28 -04:00
|
|
|
self.supported_events.map { |event| IntegrationsHelper.integration_event_field_name(event) }
|
2020-08-27 11:10:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.supported_event_actions
|
|
|
|
%w[]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.supported_events
|
|
|
|
%w[commit push tag_push issue confidential_issue merge_request wiki_page]
|
|
|
|
end
|
|
|
|
|
2020-11-26 16:09:22 -05:00
|
|
|
def self.default_test_event
|
|
|
|
'push'
|
|
|
|
end
|
|
|
|
|
2021-07-21 11:08:52 -04:00
|
|
|
def self.event_description(event)
|
|
|
|
IntegrationsHelper.integration_event_description(event)
|
2020-08-27 11:10:21 -04:00
|
|
|
end
|
|
|
|
|
2020-11-16 22:09:06 -05:00
|
|
|
def self.find_or_initialize_non_project_specific_integration(name, instance: false, group_id: nil)
|
2021-06-23 14:07:10 -04:00
|
|
|
return unless name.in?(available_integration_names(include_project_specific: false))
|
2021-05-10 14:10:41 -04:00
|
|
|
|
2021-06-09 14:11:25 -04:00
|
|
|
integration_name_to_model(name).find_or_initialize_by(instance: instance, group_id: group_id)
|
2020-08-27 11:10:21 -04:00
|
|
|
end
|
|
|
|
|
2020-11-16 22:09:06 -05:00
|
|
|
def self.find_or_initialize_all_non_project_specific(scope)
|
2021-06-30 02:07:17 -04:00
|
|
|
scope + build_nonexistent_integrations_for(scope)
|
2020-08-27 11:10:21 -04:00
|
|
|
end
|
|
|
|
|
2021-06-30 02:07:17 -04:00
|
|
|
def self.build_nonexistent_integrations_for(scope)
|
|
|
|
nonexistent_integration_types_for(scope).map do |type|
|
|
|
|
integration_type_to_model(type).new
|
2020-08-27 11:10:21 -04:00
|
|
|
end
|
|
|
|
end
|
2021-06-30 02:07:17 -04:00
|
|
|
private_class_method :build_nonexistent_integrations_for
|
2020-08-27 11:10:21 -04:00
|
|
|
|
2021-06-30 02:07:17 -04:00
|
|
|
# Returns a list of integration types that do not exist in the given scope.
|
2021-05-10 14:10:41 -04:00
|
|
|
# Example: ["AsanaService", ...]
|
2021-06-30 02:07:17 -04:00
|
|
|
def self.nonexistent_integration_types_for(scope)
|
2020-08-27 11:10:21 -04:00
|
|
|
# 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-06-23 14:07:10 -04:00
|
|
|
available_integration_types(include_project_specific: false) - scope.map(&:type)
|
2020-08-27 11:10:21 -04:00
|
|
|
end
|
2021-06-30 02:07:17 -04:00
|
|
|
private_class_method :nonexistent_integration_types_for
|
2020-08-27 11:10:21 -04:00
|
|
|
|
2021-06-23 14:07:10 -04:00
|
|
|
# Returns a list of available integration names.
|
2021-05-10 14:10:41 -04:00
|
|
|
# Example: ["asana", ...]
|
2021-06-09 14:11:25 -04:00
|
|
|
# @deprecated
|
2021-06-23 14:07:10 -04:00
|
|
|
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-08-27 11:10:21 -04:00
|
|
|
|
2021-06-23 14:07:10 -04:00
|
|
|
names.sort_by(&:downcase)
|
2020-08-27 11:10:21 -04:00
|
|
|
end
|
|
|
|
|
2021-06-09 14:11:25 -04:00
|
|
|
def self.integration_names
|
2021-05-12 08:10:24 -04:00
|
|
|
INTEGRATION_NAMES
|
2020-08-27 11:10:21 -04:00
|
|
|
end
|
|
|
|
|
2021-06-23 14:07:10 -04:00
|
|
|
def self.dev_integration_names
|
2020-08-27 11:10:21 -04:00
|
|
|
return [] unless Rails.env.development?
|
|
|
|
|
2021-05-12 08:10:24 -04:00
|
|
|
DEV_INTEGRATION_NAMES
|
2020-08-27 11:10:21 -04:00
|
|
|
end
|
|
|
|
|
2021-06-23 14:07:10 -04:00
|
|
|
def self.project_specific_integration_names
|
2021-05-12 08:10:24 -04:00
|
|
|
PROJECT_SPECIFIC_INTEGRATION_NAMES
|
2020-10-16 05:09:06 -04:00
|
|
|
end
|
|
|
|
|
2021-06-23 14:07:10 -04:00
|
|
|
# Returns a list of available integration types.
|
2021-05-10 14:10:41 -04:00
|
|
|
# Example: ["AsanaService", ...]
|
2021-06-23 14:07:10 -04:00
|
|
|
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)
|
2020-11-16 22:09:06 -05:00
|
|
|
end
|
2020-08-27 11:10:21 -04:00
|
|
|
end
|
|
|
|
|
2021-06-30 02:07:17 -04:00
|
|
|
# Returns the model for the given integration name.
|
2021-05-10 14:10:41 -04:00
|
|
|
# Example: "asana" => Integrations::Asana
|
2021-06-09 14:11:25 -04:00
|
|
|
def self.integration_name_to_model(name)
|
|
|
|
type = integration_name_to_type(name)
|
|
|
|
integration_type_to_model(type)
|
2021-05-10 14:10:41 -04:00
|
|
|
end
|
|
|
|
|
2021-06-30 02:07:17 -04:00
|
|
|
# Returns the STI type for the given integration name.
|
2021-05-10 14:10:41 -04:00
|
|
|
# Example: "asana" => "AsanaService"
|
2021-06-09 14:11:25 -04:00
|
|
|
def self.integration_name_to_type(name)
|
2021-05-10 14:10:41 -04:00
|
|
|
"#{name}_service".camelize
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the model for the given STI type.
|
|
|
|
# Example: "AsanaService" => Integrations::Asana
|
2021-06-09 14:11:25 -04:00
|
|
|
def self.integration_type_to_model(type)
|
2021-05-10 14:10:41 -04:00
|
|
|
Gitlab::Integrations::StiType.new.cast(type).constantize
|
|
|
|
end
|
2021-06-09 14:11:25 -04:00
|
|
|
private_class_method :integration_type_to_model
|
2021-05-10 14:10:41 -04:00
|
|
|
|
2020-09-28 02:09:56 -04:00
|
|
|
def self.build_from_integration(integration, project_id: nil, group_id: nil)
|
2021-05-12 08:10:24 -04:00
|
|
|
new_integration = integration.dup
|
2020-08-27 11:10:21 -04:00
|
|
|
|
|
|
|
if integration.supports_data_fields?
|
|
|
|
data_fields = integration.data_fields.dup
|
2021-05-12 08:10:24 -04:00
|
|
|
data_fields.integration = new_integration
|
2020-08-27 11:10:21 -04:00
|
|
|
end
|
|
|
|
|
2021-05-12 08:10:24 -04:00
|
|
|
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.instance_level? || integration.group_level?
|
|
|
|
new_integration
|
2020-08-27 11:10:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.instance_exists_for?(type)
|
|
|
|
exists?(instance: true, type: type)
|
|
|
|
end
|
|
|
|
|
2020-09-10 20:08:51 -04:00
|
|
|
def self.default_integration(type, scope)
|
|
|
|
closest_group_integration(type, scope) || instance_level_integration(type)
|
2020-08-27 11:10:21 -04:00
|
|
|
end
|
2016-01-19 07:48:07 -05:00
|
|
|
|
2020-09-10 20:08:51 -04:00
|
|
|
def self.closest_group_integration(type, scope)
|
2021-09-01 05:10:58 -04:00
|
|
|
group_ids = scope.ancestors(hierarchy_order: :asc).reselect(:id)
|
2020-09-10 20:08:51 -04:00
|
|
|
array = group_ids.to_sql.present? ? "array(#{group_ids.to_sql})" : 'ARRAY[]'
|
|
|
|
|
2020-10-15 02:09:14 -04:00
|
|
|
where(type: type, group_id: group_ids, inherit_from_id: nil)
|
2021-07-07 11:07:24 -04:00
|
|
|
.order(Arel.sql("array_position(#{array}::bigint[], #{table_name}.group_id)"))
|
2020-09-10 20:08:51 -04:00
|
|
|
.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
|
|
|
|
|
2021-07-21 11:08:52 -04:00
|
|
|
def self.create_from_active_default_integrations(scope, association)
|
2021-05-06 05:18:56 -04:00
|
|
|
group_ids = sorted_ancestors(scope).select(:id)
|
2020-09-28 02:09:56 -04:00
|
|
|
array = group_ids.to_sql.present? ? "array(#{group_ids.to_sql})" : 'ARRAY[]'
|
|
|
|
|
|
|
|
from_union([
|
|
|
|
active.where(instance: true),
|
2020-10-15 02:09:14 -04:00
|
|
|
active.where(group_id: group_ids, inherit_from_id: nil)
|
2021-07-07 11:07:24 -04:00
|
|
|
]).order(Arel.sql("type ASC, array_position(#{array}::bigint[], #{table_name}.group_id), instance DESC")).group_by(&:type).each do |type, records|
|
2020-12-04 04:09:36 -05:00
|
|
|
build_from_integration(records.first, association => scope.id).save
|
2020-09-28 02:09:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-23 14:08:31 -04:00
|
|
|
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
|
|
|
|
|
2013-01-03 02:52:14 -05:00
|
|
|
def activated?
|
|
|
|
active
|
|
|
|
end
|
2013-05-22 09:58:44 -04:00
|
|
|
|
2020-05-15 14:07:52 -04:00
|
|
|
def operating?
|
|
|
|
active && persisted?
|
|
|
|
end
|
|
|
|
|
2017-07-05 08:07:03 -04:00
|
|
|
def show_active_box?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def editable?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2014-05-28 04:35:43 -04:00
|
|
|
def category
|
2016-01-19 07:48:07 -05:00
|
|
|
read_attribute(:category).to_sym
|
2014-05-28 04:35:43 -04:00
|
|
|
end
|
|
|
|
|
2014-09-11 11:48:29 -04:00
|
|
|
def initialize_properties
|
2020-10-23 05:08:41 -04:00
|
|
|
self.properties = {} if has_attribute?(:properties) && properties.nil?
|
2014-09-11 11:48:29 -04:00
|
|
|
end
|
|
|
|
|
2013-05-22 09:58:44 -04:00
|
|
|
def title
|
2013-05-22 10:59:43 -04:00
|
|
|
# implement inside child
|
2013-05-22 09:58:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
2013-05-22 10:59:43 -04:00
|
|
|
# implement inside child
|
2013-05-22 09:58:44 -04:00
|
|
|
end
|
|
|
|
|
2014-05-28 04:35:43 -04:00
|
|
|
def help
|
|
|
|
# implement inside child
|
|
|
|
end
|
|
|
|
|
2013-05-22 09:58:44 -04:00
|
|
|
def to_param
|
2013-05-22 10:59:43 -04:00
|
|
|
# implement inside child
|
2016-12-27 07:44:24 -05:00
|
|
|
self.class.to_param
|
2013-05-22 09:58:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def fields
|
2013-05-22 10:59:43 -04:00
|
|
|
# implement inside child
|
2013-05-22 09:58:44 -04:00
|
|
|
[]
|
|
|
|
end
|
2013-05-22 10:59:43 -04:00
|
|
|
|
2021-08-25 20:09:31 -04:00
|
|
|
def password_fields
|
|
|
|
fields.select { |f| f[:type] == 'password' }.pluck(:name)
|
|
|
|
end
|
|
|
|
|
2019-09-09 16:22:00 -04:00
|
|
|
# Expose a list of fields in the JSON endpoint.
|
|
|
|
#
|
2021-05-12 08:10:24 -04:00
|
|
|
# This list is used in `Integration#as_json(only: json_fields)`.
|
2019-09-09 16:22:00 -04:00
|
|
|
def json_fields
|
2020-08-27 11:10:21 -04:00
|
|
|
%w[active]
|
2019-09-09 16:22:00 -04:00
|
|
|
end
|
|
|
|
|
2021-06-30 02:07:17 -04:00
|
|
|
def to_integration_hash
|
2021-07-21 11:08:52 -04:00
|
|
|
as_json(methods: :type, except: %w[id instance project_id group_id])
|
2020-05-20 14:08:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_data_fields_hash
|
2021-11-09 07:12:15 -05:00
|
|
|
data_fields.as_json(only: data_fields.class.column_names).except('id', 'service_id', 'integration_id')
|
2020-05-20 14:08:00 -04:00
|
|
|
end
|
|
|
|
|
2016-07-12 16:00:49 -04:00
|
|
|
def event_channel_names
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2016-08-02 06:06:31 -04:00
|
|
|
def event_names
|
2016-12-27 07:44:24 -05:00
|
|
|
self.class.event_names
|
2016-08-02 06:06:31 -04:00
|
|
|
end
|
|
|
|
|
2016-07-13 15:49:47 -04:00
|
|
|
def event_field(event)
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2018-01-06 01:18:13 -05:00
|
|
|
def api_field_names
|
|
|
|
fields.map { |field| field[:name] }
|
2019-06-26 10:03:57 -04:00
|
|
|
.reject { |field_name| field_name =~ /(password|token|key|title|description)/ }
|
2018-01-06 01:18:13 -05:00
|
|
|
end
|
|
|
|
|
2016-07-13 15:49:47 -04:00
|
|
|
def global_fields
|
|
|
|
fields
|
|
|
|
end
|
|
|
|
|
2018-03-06 18:20:01 -05:00
|
|
|
def configurable_events
|
2020-06-06 11:08:10 -04:00
|
|
|
events = supported_events
|
2018-03-06 18:20:01 -05:00
|
|
|
|
|
|
|
# No need to disable individual triggers when there is only one
|
|
|
|
if events.count == 1
|
|
|
|
[]
|
|
|
|
else
|
|
|
|
events
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-26 07:06:18 -05:00
|
|
|
def configurable_event_actions
|
|
|
|
self.class.supported_event_actions
|
|
|
|
end
|
|
|
|
|
2015-02-28 11:33:18 -05:00
|
|
|
def supported_events
|
2016-12-27 07:44:24 -05:00
|
|
|
self.class.supported_events
|
2015-02-28 11:33:18 -05:00
|
|
|
end
|
|
|
|
|
2020-11-26 16:09:22 -05:00
|
|
|
def default_test_event
|
|
|
|
self.class.default_test_event
|
|
|
|
end
|
|
|
|
|
2015-08-12 03:40:54 -04:00
|
|
|
def execute(data)
|
2013-05-22 10:59:43 -04:00
|
|
|
# implement inside child
|
|
|
|
end
|
2013-08-09 14:14:02 -04:00
|
|
|
|
2015-08-12 03:40:54 -04:00
|
|
|
def test(data)
|
|
|
|
# default implementation
|
|
|
|
result = execute(data)
|
|
|
|
{ success: result.present?, result: result }
|
|
|
|
end
|
|
|
|
|
2021-05-12 08:10:24 -04:00
|
|
|
# Disable test for instance-level and group-level integrations.
|
2020-04-03 20:09:37 -04:00
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/213138
|
2021-07-08 14:09:32 -04:00
|
|
|
def testable?
|
2021-06-29 23:07:30 -04:00
|
|
|
project_level?
|
2013-08-09 14:14:02 -04:00
|
|
|
end
|
2014-09-07 20:54:18 -04:00
|
|
|
|
2021-03-28 20:09:36 -04:00
|
|
|
def project_level?
|
|
|
|
project_id.present?
|
|
|
|
end
|
|
|
|
|
2021-04-22 11:09:56 -04:00
|
|
|
def group_level?
|
|
|
|
group_id.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def instance_level?
|
|
|
|
instance?
|
|
|
|
end
|
|
|
|
|
2020-12-02 13:09:35 -05:00
|
|
|
def parent
|
|
|
|
project || group
|
|
|
|
end
|
|
|
|
|
2015-10-15 03:09:01 -04:00
|
|
|
# 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-19 07:48:07 -05:00
|
|
|
# ActiveRecord does not provide a mechanism to track changes in serialized keys,
|
2021-05-12 08:10:24 -04:00
|
|
|
# so we need a specific implementation for integration properties.
|
2015-10-15 03:09:01 -04:00
|
|
|
# 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
|
2015-10-09 06:20:38 -04:00
|
|
|
end
|
|
|
|
|
2015-10-15 03:09:01 -04:00
|
|
|
def reset_updated_properties
|
|
|
|
@updated_properties = nil
|
|
|
|
end
|
2016-01-19 07:48:07 -05:00
|
|
|
|
2014-12-07 05:29:37 -05:00
|
|
|
def async_execute(data)
|
2015-02-28 11:33:18 -05:00
|
|
|
return unless supported_events.include?(data[:object_kind])
|
2015-03-17 22:06:43 -04:00
|
|
|
|
2017-11-29 10:30:17 -05:00
|
|
|
ProjectServiceWorker.perform_async(id, data)
|
2014-12-07 05:29:37 -05:00
|
|
|
end
|
2015-01-23 14:55:41 -05:00
|
|
|
|
2019-10-01 08:05:59 -04:00
|
|
|
# override if needed
|
|
|
|
def supports_data_fields?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2016-06-03 04:21:18 -04:00
|
|
|
private
|
|
|
|
|
2021-05-06 05:18:56 -04:00
|
|
|
# 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-08-07 14:09:53 -04:00
|
|
|
def validate_belongs_to_project_or_group
|
2021-04-26 05:09:53 -04:00
|
|
|
errors.add(:project_id, 'The service cannot belong to both a project and a group') if project_level? && group_level?
|
2020-08-07 14:09:53 -04:00
|
|
|
end
|
|
|
|
|
2021-04-06 11:09:23 -04:00
|
|
|
def validate_recipients?
|
2018-01-26 08:48:47 -05:00
|
|
|
activated? && !importing?
|
|
|
|
end
|
2012-11-19 13:14:05 -05:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-12 08:10:24 -04:00
|
|
|
Integration.prepend_mod_with('Integration')
|