service.rb 8.72 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
# To add new service you should build a class inherited from Service
# and implement a set of methods
5
class Service < ApplicationRecord
6
  include Sortable
7
  include Importable
8
  include ProjectServicesLoggable
9

10
  serialize :properties, JSON # rubocop:disable Cop/ActiveRecordSerialize
11

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
12
  default_value_for :active, false
13 14
  default_value_for :push_events, true
  default_value_for :issues_events, true
15
  default_value_for :confidential_issues_events, true
16
  default_value_for :commit_events, true
17 18
  default_value_for :merge_requests_events, true
  default_value_for :tag_push_events, true
19
  default_value_for :note_events, true
20
  default_value_for :confidential_note_events, true
21
  default_value_for :job_events, true
22
  default_value_for :pipeline_events, true
23
  default_value_for :wiki_page_events, true
24 25

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

27
  after_commit :reset_updated_properties
28
  after_commit :cache_project_has_external_issue_tracker
29
  after_commit :cache_project_has_external_wiki
30

31
  belongs_to :project, inverse_of: :services
32 33
  has_one :service_hook

34
  validates :project_id, presence: true, unless: proc { |service| service.template? }
Tiago Botelho's avatar
Tiago Botelho committed
35
  validates :type, presence: true
36

37
  scope :visible, -> { where.not(type: 'GitlabIssueTrackerService') }
38
  scope :issue_trackers, -> { where(category: 'issue_tracker') }
39
  scope :external_wikis, -> { where(type: 'ExternalWikiService').active }
40 41
  scope :active, -> { where(active: true) }
  scope :without_defaults, -> { where(default: false) }
42

43 44 45
  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) }
46
  scope :confidential_issue_hooks, -> { where(confidential_issues_events: true, active: true) }
47
  scope :merge_request_hooks, -> { where(merge_requests_events: true, active: true) }
48
  scope :note_hooks, -> { where(note_events: true, active: true) }
49
  scope :confidential_note_hooks, -> { where(confidential_note_events: true, active: true) }
50
  scope :job_hooks, -> { where(job_events: true, active: true) }
51
  scope :pipeline_hooks, -> { where(pipeline_events: true, active: true) }
52
  scope :wiki_page_hooks, -> { where(wiki_page_events: true, active: true) }
53
  scope :deployment_hooks, -> { where(deployment_events: true, active: true) }
54
  scope :external_issue_trackers, -> { issue_trackers.active.without_defaults }
55
  scope :deployment, -> { where(category: 'deployment') }
56

57 58
  default_value_for :category, 'common'

59 60 61
  def activated?
    active
  end
62

63 64 65 66 67 68 69 70
  def show_active_box?
    true
  end

  def editable?
    true
  end

71 72 73 74
  def template?
    template
  end

75
  def category
76
    read_attribute(:category).to_sym
77 78
  end

79 80 81 82
  def initialize_properties
    self.properties = {} if properties.nil?
  end

83
  def title
84
    # implement inside child
85 86 87
  end

  def description
88
    # implement inside child
89 90
  end

91 92 93 94
  def help
    # implement inside child
  end

95
  def to_param
96
    # implement inside child
97
    self.class.to_param
98 99
  end

Tiago Botelho's avatar
Tiago Botelho committed
100 101 102 103
  def self.to_param
    raise NotImplementedError
  end

104
  def fields
105
    # implement inside child
106 107
    []
  end
108

109
  def test_data(project, user)
110
    Gitlab::DataBuilder::Push.build_sample(project, user)
111 112
  end

113 114 115 116
  def event_channel_names
    []
  end

117
  def event_names
118
    self.class.event_names
119 120
  end

Tiago Botelho's avatar
Tiago Botelho committed
121 122 123 124
  def self.event_names
    self.supported_events.map { |event| "#{event}_events" }
  end

Felipe's avatar
Felipe committed
125 126 127 128
  def event_field(event)
    nil
  end

Stan Hu's avatar
Stan Hu committed
129 130 131 132 133
  def api_field_names
    fields.map { |field| field[:name] }
      .reject { |field_name| field_name =~ /(password|token|key)/ }
  end

Felipe's avatar
Felipe committed
134 135 136 137
  def global_fields
    fields
  end

138 139 140 141 142 143 144 145 146 147 148
  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

149
  def supported_events
150
    self.class.supported_events
151 152
  end

Tiago Botelho's avatar
Tiago Botelho committed
153 154 155 156
  def self.supported_events
    %w(push tag_push issue confidential_issue merge_request wiki_page)
  end

157
  def execute(data)
158 159
    # implement inside child
  end
160

161 162 163 164 165 166
  def test(data)
    # default implementation
    result = execute(data)
    { success: result.present?, result: result }
  end

167
  def can_test?
168
    true
169
  end
170 171 172

  # Provide convenient accessor methods
  # for each serialized property.
173
  # Also keep track of updated properties in a similar way as ActiveModel::Dirty
174 175 176
  def self.prop_accessor(*args)
    args.each do |arg|
      class_eval %{
177 178 179 180
        unless method_defined?(arg)
          def #{arg}
            properties['#{arg}']
          end
181 182 183
        end

        def #{arg}=(value)
184
          self.properties ||= {}
185
          updated_properties['#{arg}'] = #{arg} unless #{arg}_changed?
186 187
          self.properties['#{arg}'] = value
        end
188 189 190 191 192 193 194 195 196 197 198 199

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

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

        def #{arg}_was
          updated_properties['#{arg}']
        end
200 201 202
      }
    end
  end
203

204 205 206 207 208 209 210 211
  # 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 %{
212
        def #{arg}?
213
          # '!!' is used because nil or empty string is converted to nil
214
          !!ActiveRecord::Type::Boolean.new.cast(#{arg})
215 216
        end
      }
217 218 219
    end
  end

220 221
  # Returns a hash of the properties that have been assigned a new value since last save,
  # indicating their original values (attr => original value).
222
  # ActiveRecord does not provide a mechanism to track changes in serialized keys,
223 224 225 226 227
  # 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
228 229
  end

230 231 232
  def reset_updated_properties
    @updated_properties = nil
  end
233

234
  def async_execute(data)
235
    return unless supported_events.include?(data[:object_kind])
236

237
    ProjectServiceWorker.perform_async(id, data)
238
  end
239 240 241 242 243

  def issue_tracker?
    self.category == :issue_tracker
  end

244
  def self.available_services_names
245
    service_names = %w[
246
      asana
247 248
      assembla
      bamboo
249
      buildkite
250
      bugzilla
251 252
      campfire
      custom_issue_tracker
blackst0ne's avatar
blackst0ne committed
253
      discord
Kirill Zaitsev's avatar
Kirill Zaitsev committed
254
      drone_ci
255
      emails_on_push
256 257
      external_wiki
      flowdock
258
      hangouts_chat
259
      hipchat
260
      irker
261
      jira
262
      kubernetes
263
      mattermost_slash_commands
264
      mattermost
265
      packagist
266
      pipelines_email
267
      pivotaltracker
268
      prometheus
269
      pushover
270
      redmine
271
      youtrack
272
      slack_slash_commands
273
      slack
274
      teamcity
275
      microsoft_teams
Lin Jen-Shin's avatar
Lin Jen-Shin committed
276
    ]
277

278 279 280
    if Rails.env.development?
      service_names += %w[mock_ci mock_deployment mock_monitoring]
    end
281 282

    service_names.sort_by(&:downcase)
283 284
  end

285
  def self.build_from_template(project_id, template)
286 287 288
    service = template.dup
    service.template = false
    service.project_id = project_id
289
    service.active = false if service.active? && !service.valid?
290
    service
291
  end
292

293 294 295 296 297 298 299 300
  def deprecated?
    false
  end

  def deprecation_message
    nil
  end

301 302 303 304
  def self.find_by_template
    find_by(template: true)
  end

305 306 307 308 309 310 311
  private

  def cache_project_has_external_issue_tracker
    if project && !project.destroyed?
      project.cache_has_external_issue_tracker
    end
  end
312 313 314 315 316 317

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

Felipe's avatar
Felipe committed
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
  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"
339 340
    when "deployment"
      "Event will be triggered when a deployment finishes"
Felipe's avatar
Felipe committed
341 342 343
    end
  end

344 345 346
  def valid_recipients?
    activated? && !importing?
  end
347
end