BigW Consortium Gitlab

service.rb 6.56 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 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 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? }
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
  def template?
    template
  end

58
  def category
59
    read_attribute(:category).to_sym
60 61
  end

62 63 64 65
  def initialize_properties
    self.properties = {} if properties.nil?
  end

66
  def title
67
    # implement inside child
68 69 70
  end

  def description
71
    # implement inside child
72 73
  end

74 75 76 77
  def help
    # implement inside child
  end

78
  def to_param
79
    # implement inside child
80
    self.class.to_param
81 82
  end

Tiago Botelho committed
83 84 85 86
  def self.to_param
    raise NotImplementedError
  end

87
  def fields
88
    # implement inside child
89 90
    []
  end
91

92
  def test_data(project, user)
93
    Gitlab::DataBuilder::Push.build_sample(project, user)
94 95
  end

96 97 98 99
  def event_channel_names
    []
  end

100
  def event_names
101
    self.class.event_names
102 103
  end

Tiago Botelho committed
104 105 106 107
  def self.event_names
    self.supported_events.map { |event| "#{event}_events" }
  end

108 109 110 111 112 113 114 115
  def event_field(event)
    nil
  end

  def global_fields
    fields
  end

116
  def supported_events
117
    self.class.supported_events
118 119
  end

Tiago Botelho committed
120 121 122 123
  def self.supported_events
    %w(push tag_push issue confidential_issue merge_request wiki_page)
  end

124
  def execute(data)
125 126
    # implement inside child
  end
127

128 129 130 131 132 133
  def test(data)
    # default implementation
    result = execute(data)
    { success: result.present?, result: result }
  end

134
  def can_test?
135
    true
136
  end
137

138 139 140 141 142
  # reason why service cannot be tested
  def disabled_title
    "Please setup a project repository."
  end

143 144
  # Provide convenient accessor methods
  # for each serialized property.
145
  # Also keep track of updated properties in a similar way as ActiveModel::Dirty
146 147 148 149 150 151 152 153
  def self.prop_accessor(*args)
    args.each do |arg|
      class_eval %{
        def #{arg}
          properties['#{arg}']
        end

        def #{arg}=(value)
154
          self.properties ||= {}
155
          updated_properties['#{arg}'] = #{arg} unless #{arg}_changed?
156 157
          self.properties['#{arg}'] = value
        end
158 159 160 161 162 163 164 165 166 167 168 169

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

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

        def #{arg}_was
          updated_properties['#{arg}']
        end
170 171 172
      }
    end
  end
173

174 175 176 177 178 179 180 181
  # 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 %{
182 183 184 185
        def #{arg}?
          ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(#{arg})
        end
      }
186 187 188
    end
  end

189 190
  # Returns a hash of the properties that have been assigned a new value since last save,
  # indicating their original values (attr => original value).
191
  # ActiveRecord does not provide a mechanism to track changes in serialized keys,
192 193 194 195 196
  # 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
197 198
  end

199 200 201
  def reset_updated_properties
    @updated_properties = nil
  end
202

203
  def async_execute(data)
204
    return unless supported_events.include?(data[:object_kind])
205

206 207
    Sidekiq::Client.enqueue(ProjectServiceWorker, id, data)
  end
208 209 210 211 212

  def issue_tracker?
    self.category == :issue_tracker
  end

213
  def self.available_services_names
214
    service_names = %w[
215
      asana
216 217
      assembla
      bamboo
218
      buildkite
219
      bugzilla
220 221
      campfire
      custom_issue_tracker
Kirilll Zaitsev committed
222
      drone_ci
223
      emails_on_push
224 225
      external_wiki
      flowdock
226
      gemnasium
227 228
      hipchat
      irker
229
      jira
230
      kubernetes
231
      mattermost_slash_commands
232
      mattermost
233
      pipelines_email
234
      pivotaltracker
235
      prometheus
236
      pushover
237
      redmine
238
      slack_slash_commands
239
      slack
240
      teamcity
241
      microsoft_teams
Lin Jen-Shin committed
242
    ]
243 244 245
    if Rails.env.development?
      service_names += %w[mock_ci mock_deployment mock_monitoring]
    end
246 247

    service_names.sort_by(&:downcase)
248 249
  end

250
  def self.build_from_template(project_id, template)
251 252 253
    service = template.dup
    service.template = false
    service.project_id = project_id
254
    service
255
  end
256 257 258 259 260 261 262 263

  private

  def cache_project_has_external_issue_tracker
    if project && !project.destroyed?
      project.cache_has_external_issue_tracker
    end
  end
264 265 266 267 268 269

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