BigW Consortium Gitlab

service.rb 5.06 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
  after_commit :reset_updated_properties
19
  after_commit :cache_project_has_external_issue_tracker
20

21
  belongs_to :project, inverse_of: :services
22 23
  has_one :service_hook

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

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

31 32 33 34
  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) }
35
  scope :note_hooks, -> { where(note_events: true, active: true) }
36
  scope :build_hooks, -> { where(build_events: true, active: true) }
37
  scope :wiki_page_hooks, -> { where(wiki_page_events: true, active: true) }
38
  scope :external_issue_trackers, -> { issue_trackers.active.without_defaults }
39

40 41
  default_value_for :category, 'common'

42 43 44
  def activated?
    active
  end
45

46 47 48 49
  def template?
    template
  end

50
  def category
51
    read_attribute(:category).to_sym
52 53
  end

54 55 56 57
  def initialize_properties
    self.properties = {} if properties.nil?
  end

58
  def title
59
    # implement inside child
60 61 62
  end

  def description
63
    # implement inside child
64 65
  end

66 67 68 69
  def help
    # implement inside child
  end

70
  def to_param
71
    # implement inside child
72 73 74
  end

  def fields
75
    # implement inside child
76 77
    []
  end
78

79
  def supported_events
80
    %w(push tag_push issue merge_request wiki_page)
81 82
  end

83
  def execute(data)
84 85
    # implement inside child
  end
86

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

93 94 95
  def can_test?
    !project.empty_repo?
  end
96 97 98

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

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

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

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

        def #{arg}_was
          updated_properties['#{arg}']
        end
123 124 125
      }
    end
  end
126

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

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

152 153 154
  def reset_updated_properties
    @updated_properties = nil
  end
155

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

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

  def issue_tracker?
    self.category == :issue_tracker
  end

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

192 193 194 195 196
  def self.create_from_template(project_id, template)
    service = template.dup
    service.template = false
    service.project_id = project_id
    service if service.save
197
  end
198 199 200 201 202 203 204 205

  private

  def cache_project_has_external_issue_tracker
    if project && !project.destroyed?
      project.cache_has_external_issue_tracker
    end
  end
206
end