service.rb 6.96 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
  scope :deployment, -> { where(category: 'deployment') }
48

49 50
  default_value_for :category, 'common'

51 52 53
  def activated?
    active
  end
54

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

  def editable?
    true
  end

63 64 65 66
  def template?
    template
  end

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

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

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

  def description
80
    # implement inside child
81 82
  end

83 84 85 86
  def help
    # implement inside child
  end

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

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

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

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

105 106 107 108
  def event_channel_names
    []
  end

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

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

117 118 119 120
  def event_field(event)
    nil
  end

Stan Hu's avatar
Stan Hu committed
121 122 123 124 125
  def api_field_names
    fields.map { |field| field[:name] }
      .reject { |field_name| field_name =~ /(password|token|key)/ }
  end

126 127 128 129
  def global_fields
    fields
  end

130
  def supported_events
131
    self.class.supported_events
132 133
  end

Tiago Botelho's avatar
Tiago Botelho committed
134 135 136 137
  def self.supported_events
    %w(push tag_push issue confidential_issue merge_request wiki_page)
  end

138
  def execute(data)
139 140
    # implement inside child
  end
141

142 143 144 145 146 147
  def test(data)
    # default implementation
    result = execute(data)
    { success: result.present?, result: result }
  end

148
  def can_test?
149
    true
150
  end
151

152 153 154 155 156
  # reason why service cannot be tested
  def disabled_title
    "Please setup a project repository."
  end

157 158
  # Provide convenient accessor methods
  # for each serialized property.
159
  # Also keep track of updated properties in a similar way as ActiveModel::Dirty
160 161 162 163 164 165 166 167
  def self.prop_accessor(*args)
    args.each do |arg|
      class_eval %{
        def #{arg}
          properties['#{arg}']
        end

        def #{arg}=(value)
168
          self.properties ||= {}
169
          updated_properties['#{arg}'] = #{arg} unless #{arg}_changed?
170 171
          self.properties['#{arg}'] = value
        end
172 173 174 175 176 177 178 179 180 181 182 183

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

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

        def #{arg}_was
          updated_properties['#{arg}']
        end
184 185 186
      }
    end
  end
187

188 189 190 191 192 193 194 195
  # 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 %{
196 197 198 199
        def #{arg}?
          ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(#{arg})
        end
      }
200 201 202
    end
  end

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

213 214 215
  def reset_updated_properties
    @updated_properties = nil
  end
216

217
  def async_execute(data)
218
    return unless supported_events.include?(data[:object_kind])
219

220
    ProjectServiceWorker.perform_async(id, data)
221
  end
222 223 224 225 226

  def issue_tracker?
    self.category == :issue_tracker
  end

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

259 260 261
    if Rails.env.development?
      service_names += %w[mock_ci mock_deployment mock_monitoring]
    end
262 263

    service_names.sort_by(&:downcase)
264 265
  end

266
  def self.build_from_template(project_id, template)
267 268 269
    service = template.dup
    service.template = false
    service.project_id = project_id
270
    service
271
  end
272

273 274 275 276 277 278 279 280
  def deprecated?
    false
  end

  def deprecation_message
    nil
  end

281 282 283 284
  def self.find_by_template
    find_by(template: true)
  end

285 286 287 288 289 290 291
  private

  def cache_project_has_external_issue_tracker
    if project && !project.destroyed?
      project.cache_has_external_issue_tracker
    end
  end
292 293 294 295 296 297

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