2013-05-22 13:58:44 +00:00
|
|
|
# To add new service you should build a class inherited from Service
|
|
|
|
# and implement a set of methods
|
2012-11-19 18:14:05 +00:00
|
|
|
class Service < ActiveRecord::Base
|
2015-02-05 22:20:55 +00:00
|
|
|
include Sortable
|
2014-09-08 00:54:18 +00:00
|
|
|
serialize :properties, JSON
|
|
|
|
|
2014-02-26 08:41:44 +00:00
|
|
|
default_value_for :active, false
|
2015-02-19 05:02:57 +00:00
|
|
|
default_value_for :push_events, true
|
|
|
|
default_value_for :issues_events, true
|
2016-08-30 21:39:25 +00:00
|
|
|
default_value_for :confidential_issues_events, true
|
2015-02-19 05:02:57 +00:00
|
|
|
default_value_for :merge_requests_events, true
|
|
|
|
default_value_for :tag_push_events, true
|
2015-03-05 18:38:23 +00:00
|
|
|
default_value_for :note_events, true
|
2015-12-07 12:23:23 +00:00
|
|
|
default_value_for :build_events, true
|
2016-09-09 12:32:06 +00:00
|
|
|
default_value_for :pipeline_events, true
|
2016-02-28 07:26:52 +00:00
|
|
|
default_value_for :wiki_page_events, true
|
2014-09-11 15:48:29 +00:00
|
|
|
|
|
|
|
after_initialize :initialize_properties
|
2014-02-26 08:41:44 +00:00
|
|
|
|
2015-10-15 07:09:01 +00:00
|
|
|
after_commit :reset_updated_properties
|
2016-06-03 08:21:18 +00:00
|
|
|
after_commit :cache_project_has_external_issue_tracker
|
2016-07-17 14:32:11 +00:00
|
|
|
after_commit :cache_project_has_external_wiki
|
2015-10-15 07:09:01 +00:00
|
|
|
|
2016-06-08 16:10:46 +00:00
|
|
|
belongs_to :project, inverse_of: :services
|
2012-11-19 18:14:05 +00:00
|
|
|
has_one :service_hook
|
|
|
|
|
2015-02-12 01:34:41 +00:00
|
|
|
validates :project_id, presence: true, unless: Proc.new { |service| service.template? }
|
2013-01-03 07:52:14 +00:00
|
|
|
|
2015-12-04 11:55:23 +00:00
|
|
|
scope :visible, -> { where.not(type: ['GitlabIssueTrackerService', 'GitlabCiService']) }
|
2016-01-19 12:48:07 +00:00
|
|
|
scope :issue_trackers, -> { where(category: 'issue_tracker') }
|
2016-07-21 08:36:02 +00:00
|
|
|
scope :external_wikis, -> { where(type: 'ExternalWikiService').active }
|
2016-01-19 12:48:07 +00:00
|
|
|
scope :active, -> { where(active: true) }
|
|
|
|
scope :without_defaults, -> { where(default: false) }
|
2015-01-28 22:25:55 +00:00
|
|
|
|
2015-02-19 05:02:57 +00: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 22:11:46 +00:00
|
|
|
scope :confidential_issue_hooks, -> { where(confidential_issues_events: true, active: true) }
|
2015-02-19 05:02:57 +00:00
|
|
|
scope :merge_request_hooks, -> { where(merge_requests_events: true, active: true) }
|
2015-03-05 18:38:23 +00:00
|
|
|
scope :note_hooks, -> { where(note_events: true, active: true) }
|
2015-12-07 12:23:23 +00:00
|
|
|
scope :build_hooks, -> { where(build_events: true, active: true) }
|
2016-08-02 10:06:31 +00:00
|
|
|
scope :pipeline_hooks, -> { where(pipeline_events: true, active: true) }
|
2016-02-28 07:26:52 +00:00
|
|
|
scope :wiki_page_hooks, -> { where(wiki_page_events: true, active: true) }
|
2016-06-03 08:21:18 +00:00
|
|
|
scope :external_issue_trackers, -> { issue_trackers.active.without_defaults }
|
2015-02-19 05:02:57 +00:00
|
|
|
|
2016-01-19 12:48:07 +00:00
|
|
|
default_value_for :category, 'common'
|
|
|
|
|
2013-01-03 07:52:14 +00:00
|
|
|
def activated?
|
|
|
|
active
|
|
|
|
end
|
2013-05-22 13:58:44 +00:00
|
|
|
|
2015-02-12 01:34:41 +00:00
|
|
|
def template?
|
|
|
|
template
|
|
|
|
end
|
|
|
|
|
2014-05-28 08:35:43 +00:00
|
|
|
def category
|
2016-01-19 12:48:07 +00:00
|
|
|
read_attribute(:category).to_sym
|
2014-05-28 08:35:43 +00:00
|
|
|
end
|
|
|
|
|
2014-09-11 15:48:29 +00:00
|
|
|
def initialize_properties
|
|
|
|
self.properties = {} if properties.nil?
|
|
|
|
end
|
|
|
|
|
2013-05-22 13:58:44 +00:00
|
|
|
def title
|
2013-05-22 14:59:43 +00:00
|
|
|
# implement inside child
|
2013-05-22 13:58:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
2013-05-22 14:59:43 +00:00
|
|
|
# implement inside child
|
2013-05-22 13:58:44 +00:00
|
|
|
end
|
|
|
|
|
2014-05-28 08:35:43 +00:00
|
|
|
def help
|
|
|
|
# implement inside child
|
|
|
|
end
|
|
|
|
|
2013-05-22 13:58:44 +00:00
|
|
|
def to_param
|
2013-05-22 14:59:43 +00:00
|
|
|
# implement inside child
|
2013-05-22 13:58:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def fields
|
2013-05-22 14:59:43 +00:00
|
|
|
# implement inside child
|
2013-05-22 13:58:44 +00:00
|
|
|
[]
|
|
|
|
end
|
2013-05-22 14:59:43 +00:00
|
|
|
|
2016-07-12 21:19:47 +00:00
|
|
|
def test_data(project, user)
|
2016-08-12 08:09:29 +00:00
|
|
|
Gitlab::DataBuilder::Push.build_sample(project, user)
|
2016-07-12 21:19:47 +00:00
|
|
|
end
|
|
|
|
|
2016-07-12 20:00:49 +00:00
|
|
|
def event_channel_names
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2016-08-02 10:06:31 +00:00
|
|
|
def event_names
|
|
|
|
supported_events.map { |event| "#{event}_events" }
|
|
|
|
end
|
|
|
|
|
2016-07-13 19:49:47 +00:00
|
|
|
def event_field(event)
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def global_fields
|
|
|
|
fields
|
|
|
|
end
|
|
|
|
|
2015-02-28 16:33:18 +00:00
|
|
|
def supported_events
|
2016-08-30 21:39:25 +00:00
|
|
|
%w(push tag_push issue confidential_issue merge_request wiki_page)
|
2015-02-28 16:33:18 +00:00
|
|
|
end
|
|
|
|
|
2015-08-12 07:40:54 +00:00
|
|
|
def execute(data)
|
2013-05-22 14:59:43 +00:00
|
|
|
# implement inside child
|
|
|
|
end
|
2013-08-09 18:14:02 +00:00
|
|
|
|
2015-08-12 07:40:54 +00:00
|
|
|
def test(data)
|
|
|
|
# default implementation
|
|
|
|
result = execute(data)
|
|
|
|
{ success: result.present?, result: result }
|
|
|
|
end
|
|
|
|
|
2013-08-09 18:14:02 +00:00
|
|
|
def can_test?
|
|
|
|
!project.empty_repo?
|
|
|
|
end
|
2014-09-08 00:54:18 +00:00
|
|
|
|
2016-07-12 21:19:47 +00:00
|
|
|
# reason why service cannot be tested
|
|
|
|
def disabled_title
|
|
|
|
"Please setup a project repository."
|
|
|
|
end
|
|
|
|
|
2014-09-08 00:54:18 +00:00
|
|
|
# Provide convenient accessor methods
|
|
|
|
# for each serialized property.
|
2015-10-15 07:09:01 +00:00
|
|
|
# Also keep track of updated properties in a similar way as ActiveModel::Dirty
|
2014-09-08 00:54:18 +00:00
|
|
|
def self.prop_accessor(*args)
|
|
|
|
args.each do |arg|
|
|
|
|
class_eval %{
|
|
|
|
def #{arg}
|
|
|
|
properties['#{arg}']
|
|
|
|
end
|
|
|
|
|
|
|
|
def #{arg}=(value)
|
2015-10-15 07:09:01 +00:00
|
|
|
updated_properties['#{arg}'] = #{arg} unless #{arg}_changed?
|
2014-09-08 00:54:18 +00:00
|
|
|
self.properties['#{arg}'] = value
|
|
|
|
end
|
2015-10-15 07:09:01 +00:00
|
|
|
|
|
|
|
def #{arg}_changed?
|
|
|
|
#{arg}_touched? && #{arg} != #{arg}_was
|
|
|
|
end
|
|
|
|
|
|
|
|
def #{arg}_touched?
|
|
|
|
updated_properties.include?('#{arg}')
|
|
|
|
end
|
|
|
|
|
|
|
|
def #{arg}_was
|
|
|
|
updated_properties['#{arg}']
|
|
|
|
end
|
2014-09-08 00:54:18 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2014-12-07 10:29:37 +00:00
|
|
|
|
2015-12-07 12:23:23 +00:00
|
|
|
# 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 %{
|
2015-12-11 17:01:57 +00:00
|
|
|
def #{arg}?
|
|
|
|
ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(#{arg})
|
|
|
|
end
|
|
|
|
}
|
2015-12-07 12:23:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-15 07:09:01 +00: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 12:48:07 +00:00
|
|
|
# ActiveRecord does not provide a mechanism to track changes in serialized keys,
|
2015-10-15 07:09:01 +00:00
|
|
|
# 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
|
2015-10-09 10:20:38 +00:00
|
|
|
end
|
|
|
|
|
2015-10-15 07:09:01 +00:00
|
|
|
def reset_updated_properties
|
|
|
|
@updated_properties = nil
|
|
|
|
end
|
2016-01-19 12:48:07 +00:00
|
|
|
|
2014-12-07 10:29:37 +00:00
|
|
|
def async_execute(data)
|
2015-02-28 16:33:18 +00:00
|
|
|
return unless supported_events.include?(data[:object_kind])
|
2015-03-18 02:06:43 +00:00
|
|
|
|
2014-12-07 10:29:37 +00:00
|
|
|
Sidekiq::Client.enqueue(ProjectServiceWorker, id, data)
|
|
|
|
end
|
2015-01-23 19:55:41 +00:00
|
|
|
|
|
|
|
def issue_tracker?
|
|
|
|
self.category == :issue_tracker
|
|
|
|
end
|
|
|
|
|
2015-02-12 17:19:55 +00:00
|
|
|
def self.available_services_names
|
2015-03-18 02:06:43 +00:00
|
|
|
%w(
|
|
|
|
asana
|
2015-04-09 20:37:07 +00:00
|
|
|
assembla
|
|
|
|
bamboo
|
2015-04-11 18:02:13 +00:00
|
|
|
buildkite
|
2015-12-07 12:23:23 +00:00
|
|
|
builds_email
|
2016-06-26 17:36:43 +00:00
|
|
|
bugzilla
|
2015-04-09 20:37:07 +00:00
|
|
|
campfire
|
|
|
|
custom_issue_tracker
|
2015-08-26 23:58:49 +00:00
|
|
|
drone_ci
|
2015-03-18 02:06:43 +00:00
|
|
|
emails_on_push
|
2015-04-09 20:37:07 +00:00
|
|
|
external_wiki
|
|
|
|
flowdock
|
2015-03-18 02:06:43 +00:00
|
|
|
gemnasium
|
2015-04-09 20:37:07 +00:00
|
|
|
hipchat
|
|
|
|
irker
|
2015-03-18 02:06:43 +00:00
|
|
|
jira
|
2015-04-09 20:37:07 +00:00
|
|
|
pivotaltracker
|
|
|
|
pushover
|
2015-03-18 02:06:43 +00:00
|
|
|
redmine
|
2015-04-09 20:37:07 +00:00
|
|
|
slack
|
|
|
|
teamcity
|
2015-03-18 02:06:43 +00:00
|
|
|
)
|
2015-02-12 17:19:55 +00:00
|
|
|
end
|
|
|
|
|
2015-02-12 01:34:41 +00:00
|
|
|
def self.create_from_template(project_id, template)
|
|
|
|
service = template.dup
|
|
|
|
service.template = false
|
|
|
|
service.project_id = project_id
|
|
|
|
service if service.save
|
2015-01-23 19:55:41 +00:00
|
|
|
end
|
2016-06-03 08:21:18 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def cache_project_has_external_issue_tracker
|
|
|
|
if project && !project.destroyed?
|
|
|
|
project.cache_has_external_issue_tracker
|
|
|
|
end
|
|
|
|
end
|
2016-07-17 14:32:11 +00:00
|
|
|
|
|
|
|
def cache_project_has_external_wiki
|
|
|
|
if project && !project.destroyed?
|
|
|
|
project.cache_has_external_wiki
|
|
|
|
end
|
|
|
|
end
|
2012-11-19 18:14:05 +00:00
|
|
|
end
|