BigW Consortium Gitlab

service.rb 6.02 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 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 12
  default_value_for :merge_requests_events, true
  default_value_for :tag_push_events, true
13
  default_value_for :note_events, true
14
  default_value_for :build_events, true
15
  default_value_for :pipeline_events, true
16
  default_value_for :wiki_page_events, true
17 18

  after_initialize :initialize_properties
Dmitriy Zaporozhets committed
19

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

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

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

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

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

46 47
  default_value_for :category, 'common'

48 49 50
  def activated?
    active
  end
51

52 53 54 55
  def template?
    template
  end

56
  def category
57
    read_attribute(:category).to_sym
58 59
  end

60 61 62 63
  def initialize_properties
    self.properties = {} if properties.nil?
  end

64
  def title
65
    # implement inside child
66 67 68
  end

  def description
69
    # implement inside child
70 71
  end

72 73 74 75
  def help
    # implement inside child
  end

76
  def to_param
77
    # implement inside child
78 79 80
  end

  def fields
81
    # implement inside child
82 83
    []
  end
84

85
  def test_data(project, user)
86
    Gitlab::DataBuilder::Push.build_sample(project, user)
87 88
  end

89 90 91 92
  def event_channel_names
    []
  end

93 94 95 96
  def event_names
    supported_events.map { |event| "#{event}_events" }
  end

97 98 99 100 101 102 103 104
  def event_field(event)
    nil
  end

  def global_fields
    fields
  end

105
  def supported_events
106
    %w(push tag_push issue confidential_issue merge_request wiki_page)
107 108
  end

109
  def execute(data)
110 111
    # implement inside child
  end
112

113 114 115 116 117 118
  def test(data)
    # default implementation
    result = execute(data)
    { success: result.present?, result: result }
  end

119 120 121
  def can_test?
    !project.empty_repo?
  end
122

123 124 125 126 127
  # reason why service cannot be tested
  def disabled_title
    "Please setup a project repository."
  end

128 129
  # Provide convenient accessor methods
  # for each serialized property.
130
  # Also keep track of updated properties in a similar way as ActiveModel::Dirty
131 132 133 134 135 136 137 138
  def self.prop_accessor(*args)
    args.each do |arg|
      class_eval %{
        def #{arg}
          properties['#{arg}']
        end

        def #{arg}=(value)
139
          self.properties ||= {}
140
          updated_properties['#{arg}'] = #{arg} unless #{arg}_changed?
141 142
          self.properties['#{arg}'] = value
        end
143 144 145 146 147 148 149 150 151 152 153 154

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

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

        def #{arg}_was
          updated_properties['#{arg}']
        end
155 156 157
      }
    end
  end
158

159 160 161 162 163 164 165 166
  # 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 %{
167 168 169 170
        def #{arg}?
          ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(#{arg})
        end
      }
171 172 173
    end
  end

174 175
  # Returns a hash of the properties that have been assigned a new value since last save,
  # indicating their original values (attr => original value).
176
  # ActiveRecord does not provide a mechanism to track changes in serialized keys,
177 178 179 180 181
  # 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
182 183
  end

184 185 186
  def reset_updated_properties
    @updated_properties = nil
  end
187

188
  def async_execute(data)
189
    return unless supported_events.include?(data[:object_kind])
190

191 192
    Sidekiq::Client.enqueue(ProjectServiceWorker, id, data)
  end
193 194 195 196 197

  def issue_tracker?
    self.category == :issue_tracker
  end

198
  def self.available_services_names
Lin Jen-Shin committed
199
    %w[
200
      asana
201 202
      assembla
      bamboo
203
      buildkite
204
      builds_email
205
      pipelines_email
206
      bugzilla
207 208
      campfire
      custom_issue_tracker
Kirilll Zaitsev committed
209
      drone_ci
210
      emails_on_push
211 212
      external_wiki
      flowdock
213
      gemnasium
214 215
      hipchat
      irker
216
      jira
217 218
      pivotaltracker
      pushover
219
      redmine
220 221
      slack
      teamcity
Lin Jen-Shin committed
222
    ]
223 224
  end

225
  def self.build_from_template(project_id, template)
226 227 228
    service = template.dup
    service.template = false
    service.project_id = project_id
229
    service
230
  end
231 232 233 234 235 236 237 238

  private

  def cache_project_has_external_issue_tracker
    if project && !project.destroyed?
      project.cache_has_external_issue_tracker
    end
  end
239 240 241 242 243 244

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