service.rb 5.26 KB
Newer Older
1 2
# To add new service you should build a class inherited from Service
# and implement a set of methods
3
class Service < ActiveRecord::Base
4
  include Sortable
5 6
  serialize :properties, JSON

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
7
  default_value_for :active, false
8 9 10 11
  default_value_for :push_events, true
  default_value_for :issues_events, true
  default_value_for :merge_requests_events, true
  default_value_for :tag_push_events, true
12
  default_value_for :note_events, true
13
  default_value_for :build_events, true
14
  default_value_for :wiki_page_events, true
15 16

  after_initialize :initialize_properties
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
17

18
  after_commit :reset_updated_properties
19
  after_commit :cache_project_has_external_issue_tracker
20

21
  belongs_to :project, inverse_of: :services
22 23
  has_one :service_hook

24
  validates :project_id, presence: true, unless: Proc.new { |service| service.template? }
25

26
  scope :visible, -> { where.not(type: ['GitlabIssueTrackerService', 'GitlabCiService']) }
27 28 29
  scope :issue_trackers, -> { where(category: 'issue_tracker') }
  scope :active, -> { where(active: true) }
  scope :without_defaults, -> { where(default: false) }
30

31 32 33 34
  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) }
  scope :merge_request_hooks, -> { where(merge_requests_events: true, active: true) }
35
  scope :note_hooks, -> { where(note_events: true, active: true) }
36
  scope :build_hooks, -> { where(build_events: true, active: true) }
37
  scope :wiki_page_hooks, -> { where(wiki_page_events: true, active: true) }
38
  scope :external_issue_trackers, -> { issue_trackers.active.without_defaults }
39

40 41
  default_value_for :category, 'common'

42 43 44
  def activated?
    active
  end
45

46 47 48 49
  def template?
    template
  end

50
  def category
51
    read_attribute(:category).to_sym
52 53
  end

54 55 56 57
  def initialize_properties
    self.properties = {} if properties.nil?
  end

58
  def title
59
    # implement inside child
60 61 62
  end

  def description
63
    # implement inside child
64 65
  end

66 67 68 69
  def help
    # implement inside child
  end

70
  def to_param
71
    # implement inside child
72 73 74
  end

  def fields
75
    # implement inside child
76 77
    []
  end
78

79 80 81 82
  def test_data(project, user)
    Gitlab::PushDataBuilder.build_sample(project, user)
  end

83
  def supported_events
84
    %w(push tag_push issue merge_request wiki_page)
85 86
  end

87
  def execute(data)
88 89
    # implement inside child
  end
90

91 92 93 94 95 96
  def test(data)
    # default implementation
    result = execute(data)
    { success: result.present?, result: result }
  end

97 98 99
  def can_test?
    !project.empty_repo?
  end
100

101 102 103 104 105
  # reason why service cannot be tested
  def disabled_title
    "Please setup a project repository."
  end

106 107
  # Provide convenient accessor methods
  # for each serialized property.
108
  # Also keep track of updated properties in a similar way as ActiveModel::Dirty
109 110 111 112 113 114 115 116
  def self.prop_accessor(*args)
    args.each do |arg|
      class_eval %{
        def #{arg}
          properties['#{arg}']
        end

        def #{arg}=(value)
117
          updated_properties['#{arg}'] = #{arg} unless #{arg}_changed?
118 119
          self.properties['#{arg}'] = value
        end
120 121 122 123 124 125 126 127 128 129 130 131

        def #{arg}_changed?
          #{arg}_touched? && #{arg} != #{arg}_was
        end

        def #{arg}_touched?
          updated_properties.include?('#{arg}')
        end

        def #{arg}_was
          updated_properties['#{arg}']
        end
132 133 134
      }
    end
  end
135

136 137 138 139 140 141 142 143
  # 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 %{
144 145 146 147
        def #{arg}?
          ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(#{arg})
        end
      }
148 149 150
    end
  end

151 152
  # Returns a hash of the properties that have been assigned a new value since last save,
  # indicating their original values (attr => original value).
153
  # ActiveRecord does not provide a mechanism to track changes in serialized keys,
154 155 156 157 158
  # 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
159 160
  end

161 162 163
  def reset_updated_properties
    @updated_properties = nil
  end
164

165
  def async_execute(data)
166
    return unless supported_events.include?(data[:object_kind])
167

168 169
    Sidekiq::Client.enqueue(ProjectServiceWorker, id, data)
  end
170 171 172 173 174

  def issue_tracker?
    self.category == :issue_tracker
  end

175
  def self.available_services_names
176 177
    %w(
      asana
178 179
      assembla
      bamboo
180
      buildkite
181
      builds_email
182
      bugzilla
183 184
      campfire
      custom_issue_tracker
Kirilll Zaitsev's avatar
Kirilll Zaitsev committed
185
      drone_ci
186
      emails_on_push
187 188
      external_wiki
      flowdock
189
      gemnasium
190 191
      hipchat
      irker
192
      jira
193 194
      pivotaltracker
      pushover
195
      redmine
196 197
      slack
      teamcity
198
    )
199 200
  end

201 202 203 204 205
  def self.create_from_template(project_id, template)
    service = template.dup
    service.template = false
    service.project_id = project_id
    service if service.save
206
  end
207 208 209 210 211 212 213 214

  private

  def cache_project_has_external_issue_tracker
    if project && !project.destroyed?
      project.cache_has_external_issue_tracker
    end
  end
215
end