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

279 lines
6.6 KiB
Ruby
Raw Normal View History

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
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
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) }
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 }
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
def global_fields
fields
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
2016-08-24 12:49:21 +05:30
# reason why service cannot be tested
def disabled_title
"Please setup a project repository."
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 %{
def #{arg}
properties['#{arg}']
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}?
ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(#{arg})
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])
Sidekiq::Client.enqueue(ProjectServiceWorker, id, data)
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
2015-09-25 12:07:36 +05:30
drone_ci
2015-04-26 12:48:37 +05:30
emails_on_push
external_wiki
flowdock
gemnasium
hipchat
irker
jira
2017-08-17 22:00:37 +05:30
kubernetes
mattermost_slash_commands
mattermost
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
]
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
2017-08-17 22:00:37 +05:30
service
2015-04-26 12:48:37 +05:30
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
2014-09-02 18:07:02 +05:30
end