service.rb 6.63 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
  serialize :properties, JSON # rubocop:disable Cop/ActiveRecordSerialize
6

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
7
  default_value_for :active, false
8 9
  default_value_for :push_events, true
  default_value_for :issues_events, true
10
  default_value_for :confidential_issues_events, true
11
  default_value_for :commit_events, true
12 13
  default_value_for :merge_requests_events, true
  default_value_for :tag_push_events, true
14
  default_value_for :note_events, true
15
  default_value_for :job_events, true
16
  default_value_for :pipeline_events, true
17
  default_value_for :wiki_page_events, true
18 19

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

21
  after_commit :reset_updated_properties
22
  after_commit :cache_project_has_external_issue_tracker
23
  after_commit :cache_project_has_external_wiki
24

25
  belongs_to :project, inverse_of: :services
26 27
  has_one :service_hook

28
  validates :project_id, presence: true, unless: proc { |service| service.template? }
Tiago Botelho's avatar
Tiago Botelho committed
29
  validates :type, presence: true
30

31
  scope :visible, -> { where.not(type: 'GitlabIssueTrackerService') }
32
  scope :issue_trackers, -> { where(category: 'issue_tracker') }
33
  scope :external_wikis, -> { where(type: 'ExternalWikiService').active }
34 35
  scope :active, -> { where(active: true) }
  scope :without_defaults, -> { where(default: false) }
36

37 38 39
  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) }
40
  scope :confidential_issue_hooks, -> { where(confidential_issues_events: true, active: true) }
41
  scope :merge_request_hooks, -> { where(merge_requests_events: true, active: true) }
42
  scope :note_hooks, -> { where(note_events: true, active: true) }
43
  scope :job_hooks, -> { where(job_events: true, active: true) }
44
  scope :pipeline_hooks, -> { where(pipeline_events: true, active: true) }
45
  scope :wiki_page_hooks, -> { where(wiki_page_events: true, active: true) }
46
  scope :external_issue_trackers, -> { issue_trackers.active.without_defaults }
47

48 49
  default_value_for :category, 'common'

50 51 52
  def activated?
    active
  end
53

54 55 56 57 58 59 60 61
  def show_active_box?
    true
  end

  def editable?
    true
  end

62 63 64 65
  def template?
    template
  end

66
  def category
67
    read_attribute(:category).to_sym
68 69
  end

70 71 72 73
  def initialize_properties
    self.properties = {} if properties.nil?
  end

74
  def title
75
    # implement inside child
76 77 78
  end

  def description
79
    # implement inside child
80 81
  end

82 83 84 85
  def help
    # implement inside child
  end

86
  def to_param
87
    # implement inside child
88
    self.class.to_param
89 90
  end

Tiago Botelho's avatar
Tiago Botelho committed
91 92 93 94
  def self.to_param
    raise NotImplementedError
  end

95
  def fields
96
    # implement inside child
97 98
    []
  end
99

100
  def test_data(project, user)
101
    Gitlab::DataBuilder::Push.build_sample(project, user)
102 103
  end

104 105 106 107
  def event_channel_names
    []
  end

108
  def event_names
109
    self.class.event_names
110 111
  end

Tiago Botelho's avatar
Tiago Botelho committed
112 113 114 115
  def self.event_names
    self.supported_events.map { |event| "#{event}_events" }
  end

116 117 118 119 120 121 122 123
  def event_field(event)
    nil
  end

  def global_fields
    fields
  end

124
  def supported_events
125
    self.class.supported_events
126 127
  end

Tiago Botelho's avatar
Tiago Botelho committed
128 129 130 131
  def self.supported_events
    %w(push tag_push issue confidential_issue merge_request wiki_page)
  end

132
  def execute(data)
133 134
    # implement inside child
  end
135

136 137 138 139 140 141
  def test(data)
    # default implementation
    result = execute(data)
    { success: result.present?, result: result }
  end

142
  def can_test?
143
    true
144
  end
145

146 147 148 149 150
  # reason why service cannot be tested
  def disabled_title
    "Please setup a project repository."
  end

151 152
  # Provide convenient accessor methods
  # for each serialized property.
153
  # Also keep track of updated properties in a similar way as ActiveModel::Dirty
154 155 156 157 158 159 160 161
  def self.prop_accessor(*args)
    args.each do |arg|
      class_eval %{
        def #{arg}
          properties['#{arg}']
        end

        def #{arg}=(value)
162
          self.properties ||= {}
163
          updated_properties['#{arg}'] = #{arg} unless #{arg}_changed?
164 165
          self.properties['#{arg}'] = value
        end
166 167 168 169 170 171 172 173 174 175 176 177

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

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

        def #{arg}_was
          updated_properties['#{arg}']
        end
178 179 180
      }
    end
  end
181

182 183 184 185 186 187 188 189
  # 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 %{
190 191 192 193
        def #{arg}?
          ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(#{arg})
        end
      }
194 195 196
    end
  end

197 198
  # Returns a hash of the properties that have been assigned a new value since last save,
  # indicating their original values (attr => original value).
199
  # ActiveRecord does not provide a mechanism to track changes in serialized keys,
200 201 202 203 204
  # 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
205 206
  end

207 208 209
  def reset_updated_properties
    @updated_properties = nil
  end
210

211
  def async_execute(data)
212
    return unless supported_events.include?(data[:object_kind])
213

214
    ProjectServiceWorker.perform_async(id, data)
215
  end
216 217 218 219 220

  def issue_tracker?
    self.category == :issue_tracker
  end

221
  def self.available_services_names
222
    service_names = %w[
223
      asana
224 225
      assembla
      bamboo
226
      buildkite
227
      bugzilla
228 229
      campfire
      custom_issue_tracker
Kirilll Zaitsev's avatar
Kirilll Zaitsev committed
230
      drone_ci
231
      emails_on_push
232 233
      external_wiki
      flowdock
234
      gemnasium
235 236
      hipchat
      irker
237
      jira
238
      kubernetes
239
      mattermost_slash_commands
240
      mattermost
241
      packagist
242
      pipelines_email
243
      pivotaltracker
244
      prometheus
245
      pushover
246
      redmine
247
      slack_slash_commands
248
      slack
249
      teamcity
250
      microsoft_teams
Lin Jen-Shin's avatar
Lin Jen-Shin committed
251
    ]
252 253 254
    if Rails.env.development?
      service_names += %w[mock_ci mock_deployment mock_monitoring]
    end
255 256

    service_names.sort_by(&:downcase)
257 258
  end

259
  def self.build_from_template(project_id, template)
260 261 262
    service = template.dup
    service.template = false
    service.project_id = project_id
263
    service
264
  end
265 266 267 268 269 270 271 272

  private

  def cache_project_has_external_issue_tracker
    if project && !project.destroyed?
      project.cache_has_external_issue_tracker
    end
  end
273 274 275 276 277 278

  def cache_project_has_external_wiki
    if project && !project.destroyed?
      project.cache_has_external_wiki
    end
  end
279
end