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

344 lines
8.5 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
class Service < ActiveRecord::Base
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
2018-03-17 18:26:18 +05:30
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
2017-08-17 22:00:37 +05:30
validates :project_id, presence: true, unless: proc { |service| service.template? }
validates :type, presence: true
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) }
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) }
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
2015-04-26 12:48:37 +05:30
def template?
template
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
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
self.supported_events.map { |event| "#{event}_events" }
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] }
.reject { |field_name| field_name =~ /(password|token|key)/ }
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
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
2016-09-29 09:46:39 +05:30
%w(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
2014-09-02 18:07:02 +05:30
def can_test?
2017-08-17 22:00:37 +05:30
true
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|
class_eval %{
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
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|
class_eval %{
def #{arg}?
2018-11-08 19:23:39 +05:30
# '!!' is used because nil or empty string is converted to nil
2019-02-13 22:33:31 +05:30
!!ActiveRecord::Type::Boolean.new.cast(#{arg})
2015-12-23 02:04:40 +05:30
end
}
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
def self.available_services_names
2017-08-17 22:00:37 +05:30
service_names = %w[
2015-04-26 12:48:37 +05:30
asana
assembla
bamboo
buildkite
2016-08-24 12:49:21 +05:30
bugzilla
2015-04-26 12:48:37 +05:30
campfire
custom_issue_tracker
2019-02-13 22:33:31 +05:30
discord
2015-09-25 12:07:36 +05:30
drone_ci
2015-04-26 12:48:37 +05:30
emails_on_push
external_wiki
flowdock
2018-11-18 11:00:15 +05:30
hangouts_chat
2015-04-26 12:48:37 +05:30
hipchat
irker
jira
2017-08-17 22:00:37 +05:30
kubernetes
mattermost_slash_commands
mattermost
2018-03-17 18:26:18 +05:30
packagist
2017-08-17 22:00:37 +05:30
pipelines_email
2015-04-26 12:48:37 +05:30
pivotaltracker
2017-08-17 22:00:37 +05:30
prometheus
2015-04-26 12:48:37 +05:30
pushover
redmine
2017-08-17 22:00:37 +05:30
slack_slash_commands
2015-04-26 12:48:37 +05:30
slack
teamcity
2017-08-17 22:00:37 +05:30
microsoft_teams
2016-11-03 12:29:30 +05:30
]
2018-03-17 18:26:18 +05:30
2017-08-17 22:00:37 +05:30
if Rails.env.development?
service_names += %w[mock_ci mock_deployment mock_monitoring]
end
service_names.sort_by(&:downcase)
2015-04-26 12:48:37 +05:30
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
service.template = false
service.project_id = project_id
2018-11-08 19:23:39 +05:30
service.active = false if service.active? && !service.valid?
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
def self.find_by_template
find_by(template: true)
end
private
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"
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