debian-mirror-gitlab/app/models/service.rb

410 lines
11 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
# To add new service you should build a class inherited from Service
# and implement a set of methods
2019-07-07 11:18:12 +05:30
class Service < 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
2019-09-04 21:01:54 +05:30
include DataFields
2018-03-17 18:26:18 +05:30
2020-04-22 19:07:51 +05:30
SERVICE_NAMES = %w[
alerts asana assembla bamboo bugzilla buildkite campfire custom_issue_tracker discord
drone_ci emails_on_push external_wiki flowdock hangouts_chat hipchat irker jira
mattermost mattermost_slash_commands microsoft_teams packagist pipelines_email
pivotaltracker prometheus pushover redmine slack slack_slash_commands teamcity unify_circuit youtrack
].freeze
DEV_SERVICE_NAMES = %w[
mock_ci mock_deployment mock_monitoring
].freeze
2017-09-10 17:25:29 +05:30
serialize :properties, JSON # rubocop:disable Cop/ActiveRecordSerialize
2015-04-26 12:48:37 +05:30
2014-09-02 18:07:02 +05:30
default_value_for :active, false
2015-04-26 12:48:37 +05:30
default_value_for :push_events, true
default_value_for :issues_events, true
2016-09-29 09:46:39 +05:30
default_value_for :confidential_issues_events, true
2017-08-17 22:00:37 +05:30
default_value_for :commit_events, true
2015-04-26 12:48:37 +05:30
default_value_for :merge_requests_events, true
default_value_for :tag_push_events, true
default_value_for :note_events, true
2018-04-05 14:03:07 +05:30
default_value_for :confidential_note_events, true
2017-09-10 17:25:29 +05:30
default_value_for :job_events, true
2016-09-29 09:46:39 +05:30
default_value_for :pipeline_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
after_commit :cache_project_has_external_issue_tracker
2016-08-24 12:49:21 +05:30
after_commit :cache_project_has_external_wiki
2015-10-24 18:46:33 +05:30
2016-08-24 12:49:21 +05:30
belongs_to :project, inverse_of: :services
2014-09-02 18:07:02 +05:30
has_one :service_hook
2020-04-08 14:13:33 +05:30
validates :project_id, presence: true, unless: -> { template? || instance? }
validates :project_id, absence: true, if: -> { template? || instance? }
2020-04-22 19:07:51 +05:30
validates :type, uniqueness: { scope: :project_id }, unless: -> { template? || instance? }, on: :create
2017-08-17 22:00:37 +05:30
validates :type, presence: true
2020-04-08 14:13:33 +05:30
validates :template, uniqueness: { scope: :type }, if: -> { template? }
validates :instance, uniqueness: { scope: :type }, if: -> { instance? }
validate :validate_is_instance_or_template
2015-04-26 12:48:37 +05:30
2017-08-17 22:00:37 +05:30
scope :visible, -> { where.not(type: 'GitlabIssueTrackerService') }
2016-01-29 22:53:50 +05:30
scope :issue_trackers, -> { where(category: 'issue_tracker') }
2016-08-24 12:49:21 +05:30
scope :external_wikis, -> { where(type: 'ExternalWikiService').active }
2016-01-29 22:53:50 +05:30
scope :active, -> { where(active: true) }
scope :without_defaults, -> { where(default: false) }
2019-12-26 22:10:19 +05:30
scope :by_type, -> (type) { where(type: type) }
2020-04-22 19:07:51 +05:30
scope :by_active_flag, -> (flag) { where(active: flag) }
scope :templates, -> { where(template: true, type: available_services_types) }
scope :instances, -> { where(instance: true, type: available_services_types) }
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) }
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) }
scope :external_issue_trackers, -> { issue_trackers.active.without_defaults }
2018-03-17 18:26:18 +05:30
scope :deployment, -> { where(category: 'deployment') }
2014-09-02 18:07:02 +05:30
2016-01-29 22:53:50 +05:30
default_value_for :category, 'common'
2014-09-02 18:07:02 +05:30
def activated?
active
end
2017-09-10 17:25:29 +05:30
def show_active_box?
true
end
def editable?
true
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
self.properties = {} if properties.nil?
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
def self.to_param
raise NotImplementedError
2014-09-02 18:07:02 +05:30
end
def fields
# implement inside child
[]
end
2019-12-04 20:38:33 +05:30
# Expose a list of fields in the JSON endpoint.
#
# This list is used in `Service#as_json(only: json_fields)`.
def json_fields
%w(active)
end
2016-08-24 12:49:21 +05:30
def test_data(project, user)
2016-09-13 17:45:13 +05:30
Gitlab::DataBuilder::Push.build_sample(project, user)
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
def self.event_names
2019-09-04 21:01:54 +05:30
self.supported_events.map { |event| ServicesHelper.service_event_field_name(event) }
2016-09-13 17:45:13 +05:30
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
fields.map { |field| field[:name] }
2019-09-30 21:07:59 +05:30
.reject { |field_name| field_name =~ /(password|token|key|title|description)/ }
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
events = self.class.supported_events
# No need to disable individual triggers when there is only one
if events.count == 1
[]
else
events
end
end
2020-01-01 13:55:28 +05:30
def configurable_event_actions
self.class.supported_event_actions
end
def self.supported_event_actions
%w()
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
def self.supported_events
2019-09-04 21:01:54 +05:30
%w(commit push tag_push issue confidential_issue merge_request wiki_page)
2015-04-26 12:48:37 +05:30
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
2020-04-22 19:07:51 +05:30
# Disable test for instance-level services.
# https://gitlab.com/gitlab-org/gitlab/-/issues/213138
2014-09-02 18:07:02 +05:30
def can_test?
2020-04-22 19:07:51 +05:30
!instance?
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
# Provide convenient accessor methods
# for each serialized property.
2015-10-24 18:46:33 +05:30
# Also keep track of updated properties in a similar way as ActiveModel::Dirty
2015-04-26 12:48:37 +05:30
def self.prop_accessor(*args)
args.each do |arg|
2019-12-04 20:38:33 +05:30
class_eval <<~RUBY, __FILE__, __LINE__ + 1
2018-04-05 14:03:07 +05:30
unless method_defined?(arg)
def #{arg}
properties['#{arg}']
end
2015-04-26 12:48:37 +05:30
end
def #{arg}=(value)
2016-11-03 12:29:30 +05:30
self.properties ||= {}
2015-10-24 18:46:33 +05:30
updated_properties['#{arg}'] = #{arg} unless #{arg}_changed?
2015-04-26 12:48:37 +05:30
self.properties['#{arg}'] = value
end
2015-10-24 18:46:33 +05:30
def #{arg}_changed?
#{arg}_touched? && #{arg} != #{arg}_was
end
def #{arg}_touched?
updated_properties.include?('#{arg}')
end
def #{arg}_was
updated_properties['#{arg}']
end
2019-12-04 20:38:33 +05:30
RUBY
2015-04-26 12:48:37 +05:30
end
end
2015-12-23 02:04:40 +05:30
# 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|
2019-12-04 20:38:33 +05:30
class_eval <<~RUBY, __FILE__, __LINE__ + 1
2015-12-23 02:04:40 +05:30
def #{arg}?
2018-11-08 19:23:39 +05:30
# '!!' is used because nil or empty string is converted to nil
2019-02-15 15:39:39 +05:30
!!ActiveRecord::Type::Boolean.new.cast(#{arg})
2015-12-23 02:04:40 +05:30
end
2019-12-04 20:38:33 +05:30
RUBY
2015-12-23 02:04:40 +05:30
end
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,
2015-10-24 18:46:33 +05:30
# so we need a specific implementation for service properties.
# This allows to track changes to properties set with the accessor methods,
# but not direct manipulation of properties hash.
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
def issue_tracker?
self.category == :issue_tracker
end
2020-04-22 19:07:51 +05:30
def self.find_or_create_templates
create_nonexistent_templates
templates
end
private_class_method def self.create_nonexistent_templates
nonexistent_services = list_nonexistent_services_for(templates)
return if nonexistent_services.empty?
# Create within a transaction to perform the lowest possible SQL queries.
transaction do
nonexistent_services.each do |service_type|
service_type.constantize.create(template: true)
end
2017-08-17 22:00:37 +05:30
end
2020-04-22 19:07:51 +05:30
end
def self.find_or_initialize_instances
instances + build_nonexistent_instances
end
private_class_method def self.build_nonexistent_instances
list_nonexistent_services_for(instances).map do |service_type|
service_type.constantize.new
end
end
private_class_method def self.list_nonexistent_services_for(scope)
available_services_types - scope.map(&:type)
end
def self.available_services_names
service_names = services_names
service_names += dev_services_names
2017-08-17 22:00:37 +05:30
service_names.sort_by(&:downcase)
2015-04-26 12:48:37 +05:30
end
2020-04-22 19:07:51 +05:30
def self.services_names
SERVICE_NAMES
end
def self.dev_services_names
return [] unless Rails.env.development?
DEV_SERVICE_NAMES
end
def self.available_services_types
available_services_names.map { |service_name| "#{service_name}_service".camelize }
end
def self.services_types
services_names.map { |service_name| "#{service_name}_service".camelize }
end
2017-08-17 22:00:37 +05:30
def self.build_from_template(project_id, template)
2015-04-26 12:48:37 +05:30
service = template.dup
2019-12-21 20:55:43 +05:30
if template.supports_data_fields?
data_fields = template.data_fields.dup
data_fields.service = service
end
2015-04-26 12:48:37 +05:30
service.template = false
service.project_id = project_id
2020-04-22 19:07:51 +05:30
service.active = false if service.active? && service.invalid?
2017-08-17 22:00:37 +05:30
service
2015-04-26 12:48:37 +05:30
end
2018-03-17 18:26:18 +05:30
def deprecated?
false
end
def deprecation_message
nil
end
2019-12-21 20:55:43 +05:30
# override if needed
def supports_data_fields?
false
end
private
2020-04-08 14:13:33 +05:30
def validate_is_instance_or_template
errors.add(:template, 'The service should be a service template or instance-level integration') if template? && instance?
end
def cache_project_has_external_issue_tracker
if project && !project.destroyed?
project.cache_has_external_issue_tracker
end
end
2016-08-24 12:49:21 +05:30
def cache_project_has_external_wiki
if project && !project.destroyed?
project.cache_has_external_wiki
end
end
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
def self.event_description(event)
case event
when "push", "push_events"
"Event will be triggered by a push to the repository"
when "tag_push", "tag_push_events"
"Event will be triggered when a new tag is pushed to the repository"
when "note", "note_events"
"Event will be triggered when someone adds a comment"
when "issue", "issue_events"
"Event will be triggered when an issue is created/updated/closed"
when "confidential_issue", "confidential_issue_events"
"Event will be triggered when a confidential issue is created/updated/closed"
when "merge_request", "merge_request_events"
"Event will be triggered when a merge request is created/updated/merged"
when "pipeline", "pipeline_events"
"Event will be triggered when a pipeline status changes"
when "wiki_page", "wiki_page_events"
"Event will be triggered when a wiki page is created/updated"
when "commit", "commit_events"
"Event will be triggered when a commit is created/updated"
2019-07-31 22:56:46 +05:30
when "deployment"
"Event will be triggered when a deployment finishes"
2018-05-09 12:01:36 +05:30
end
end
2018-03-17 18:26:18 +05:30
def valid_recipients?
activated? && !importing?
end
2014-09-02 18:07:02 +05:30
end
2019-12-04 20:38:33 +05:30
Service.prepend_if_ee('EE::Service')