BigW Consortium Gitlab

service.rb 4.59 KB
Newer Older
1 2 3 4
# == Schema Information
#
# Table name: services
#
5 6 7 8 9 10 11 12 13 14 15 16 17
#  id                    :integer          not null, primary key
#  type                  :string(255)
#  title                 :string(255)
#  project_id            :integer
#  created_at            :datetime
#  updated_at            :datetime
#  active                :boolean          default(FALSE), not null
#  properties            :text
#  template              :boolean          default(FALSE)
#  push_events           :boolean          default(TRUE)
#  issues_events         :boolean          default(TRUE)
#  merge_requests_events :boolean          default(TRUE)
#  tag_push_events       :boolean          default(TRUE)
Stan Hu committed
18
#  note_events           :boolean          default(TRUE), not null
19
#
20

21 22
# To add new service you should build a class inherited from Service
# and implement a set of methods
23
class Service < ActiveRecord::Base
24
  include Sortable
25 26
  serialize :properties, JSON

Dmitriy Zaporozhets committed
27
  default_value_for :active, false
28 29 30 31
  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
32
  default_value_for :note_events, true
33 34

  after_initialize :initialize_properties
Dmitriy Zaporozhets committed
35

36 37
  after_commit :reset_updated_properties

38 39 40
  belongs_to :project
  has_one :service_hook

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

43 44
  scope :visible, -> { where.not(type: 'GitlabIssueTrackerService') }

45 46 47 48
  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) }
49
  scope :note_hooks, -> { where(note_events: true, active: true) }
50

51 52 53
  def activated?
    active
  end
54

55 56 57 58
  def template?
    template
  end

59 60 61 62
  def category
    :common
  end

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

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

  def description
72
    # implement inside child
73 74
  end

75 76 77 78
  def help
    # implement inside child
  end

79
  def to_param
80
    # implement inside child
81 82 83
  end

  def fields
84
    # implement inside child
85 86
    []
  end
87

88 89 90 91
  def supported_events
    %w(push tag_push issue merge_request)
  end

92
  def execute(data)
93 94
    # implement inside child
  end
95

96 97 98 99 100 101
  def test(data)
    # default implementation
    result = execute(data)
    { success: result.present?, result: result }
  end

102 103 104
  def can_test?
    !project.empty_repo?
  end
105 106 107

  # Provide convenient accessor methods
  # for each serialized property.
108
  # Also keep track of updated properties in a similar way as ActiveModel::Dirty
109 110 111 112 113 114 115 116
  def self.prop_accessor(*args)
    args.each do |arg|
      class_eval %{
        def #{arg}
          properties['#{arg}']
        end

        def #{arg}=(value)
117
          updated_properties['#{arg}'] = #{arg} unless #{arg}_changed?
118 119
          self.properties['#{arg}'] = value
        end
120 121 122 123 124 125 126 127 128 129 130 131

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

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

        def #{arg}_was
          updated_properties['#{arg}']
        end
132 133 134
      }
    end
  end
135

136 137 138 139 140 141 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).
  # ActiveRecord does not provide a mechanism to track changes in serialized keys, 
  # 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
144 145
  end

146 147 148 149
  def reset_updated_properties
    @updated_properties = nil
  end
  
150
  def async_execute(data)
151
    return unless supported_events.include?(data[:object_kind])
152

153 154
    Sidekiq::Client.enqueue(ProjectServiceWorker, id, data)
  end
155 156 157 158 159

  def issue_tracker?
    self.category == :issue_tracker
  end

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

185 186 187 188 189
  def self.create_from_template(project_id, template)
    service = template.dup
    service.template = false
    service.project_id = project_id
    service if service.save
190
  end
191
end