BigW Consortium Gitlab

service.rb 4.74 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 10 11
  default_value_for :push_events, true
  default_value_for :issues_events, true
  default_value_for :merge_requests_events, true
  default_value_for :tag_push_events, true
12
  default_value_for :note_events, true
13
  default_value_for :build_events, true
14
  default_value_for :wiki_page_events, true
15 16

  after_initialize :initialize_properties
Dmitriy Zaporozhets committed
17

18 19
  after_commit :reset_updated_properties

20 21 22
  belongs_to :project
  has_one :service_hook

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

25
  scope :visible, -> { where.not(type: ['GitlabIssueTrackerService', 'GitlabCiService']) }
26 27 28
  scope :issue_trackers, -> { where(category: 'issue_tracker') }
  scope :active, -> { where(active: true) }
  scope :without_defaults, -> { where(default: false) }
29

30 31 32 33
  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) }
  scope :merge_request_hooks, -> { where(merge_requests_events: true, active: true) }
34
  scope :note_hooks, -> { where(note_events: true, active: true) }
35
  scope :build_hooks, -> { where(build_events: true, active: true) }
36
  scope :wiki_page_hooks, -> { where(wiki_page_events: true, active: true) }
37

38 39
  default_value_for :category, 'common'

40 41 42
  def activated?
    active
  end
43

44 45 46 47
  def template?
    template
  end

48
  def category
49
    read_attribute(:category).to_sym
50 51
  end

52 53 54 55
  def initialize_properties
    self.properties = {} if properties.nil?
  end

56
  def title
57
    # implement inside child
58 59 60
  end

  def description
61
    # implement inside child
62 63
  end

64 65 66 67
  def help
    # implement inside child
  end

68
  def to_param
69
    # implement inside child
70 71 72
  end

  def fields
73
    # implement inside child
74 75
    []
  end
76

77
  def supported_events
78
    %w(push tag_push issue merge_request wiki_page)
79 80
  end

81
  def execute(data)
82 83
    # implement inside child
  end
84

85 86 87 88 89 90
  def test(data)
    # default implementation
    result = execute(data)
    { success: result.present?, result: result }
  end

91 92 93
  def can_test?
    !project.empty_repo?
  end
94 95 96

  # Provide convenient accessor methods
  # for each serialized property.
97
  # Also keep track of updated properties in a similar way as ActiveModel::Dirty
98 99 100 101 102 103 104 105
  def self.prop_accessor(*args)
    args.each do |arg|
      class_eval %{
        def #{arg}
          properties['#{arg}']
        end

        def #{arg}=(value)
106
          updated_properties['#{arg}'] = #{arg} unless #{arg}_changed?
107 108
          self.properties['#{arg}'] = value
        end
109 110 111 112 113 114 115 116 117 118 119 120

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

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

        def #{arg}_was
          updated_properties['#{arg}']
        end
121 122 123
      }
    end
  end
124

125 126 127 128 129 130 131 132
  # 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 %{
133 134 135 136
        def #{arg}?
          ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(#{arg})
        end
      }
137 138 139
    end
  end

140 141
  # Returns a hash of the properties that have been assigned a new value since last save,
  # indicating their original values (attr => original value).
142
  # ActiveRecord does not provide a mechanism to track changes in serialized keys,
143 144 145 146 147
  # 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
148 149
  end

150 151 152
  def reset_updated_properties
    @updated_properties = nil
  end
153

154
  def async_execute(data)
155
    return unless supported_events.include?(data[:object_kind])
156

157 158
    Sidekiq::Client.enqueue(ProjectServiceWorker, id, data)
  end
159 160 161 162 163

  def issue_tracker?
    self.category == :issue_tracker
  end

164
  def self.available_services_names
165 166
    %w(
      asana
167 168
      assembla
      bamboo
169
      buildkite
170
      builds_email
171 172
      campfire
      custom_issue_tracker
Kirilll Zaitsev committed
173
      drone_ci
174
      emails_on_push
175 176
      external_wiki
      flowdock
177
      gemnasium
178 179
      hipchat
      irker
180
      jira
181 182
      pivotaltracker
      pushover
183
      redmine
184 185
      slack
      teamcity
186
    )
187 188
  end

189 190 191 192 193
  def self.create_from_template(project_id, template)
    service = template.dup
    service.template = false
    service.project_id = project_id
    service if service.save
194
  end
195
end