This instance will be upgraded to Heptapod 0.31.0rc on 2022-05-19 at 11:00 UTC+2 (a few minutes of down time)

service.rb 5.41 KB
Newer Older
1
2
3
4
# == Schema Information
#
# Table name: services
#
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
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's avatar
Stan Hu committed
18
#  note_events           :boolean          default(TRUE), not null
Stan Hu's avatar
Stan Hu committed
19
#  build_events          :boolean          default(FALSE), not null
20
#
21

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

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
28
  default_value_for :active, false
29
30
31
32
  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
33
  default_value_for :note_events, true
34
  default_value_for :build_events, true
35
36

  after_initialize :initialize_properties
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
37

38
39
  after_commit :reset_updated_properties

40
41
42
  belongs_to :project
  has_one :service_hook

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

45
  scope :visible, -> { where.not(type: ['GitlabIssueTrackerService', 'GitlabCiService']) }
46
47
48
  scope :issue_trackers, -> { where(category: 'issue_tracker') }
  scope :active, -> { where(active: true) }
  scope :without_defaults, -> { where(default: false) }
49

50
51
52
53
  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) }
54
  scope :note_hooks, -> { where(note_events: true, active: true) }
55
  scope :build_hooks, -> { where(build_events: true, active: true) }
56

57
58
  default_value_for :category, 'common'

59
60
61
  def activated?
    active
  end
62

63
64
65
66
  def template?
    template
  end

67
  def category
68
    read_attribute(:category).to_sym
69
70
  end

71
72
73
74
  def initialize_properties
    self.properties = {} if properties.nil?
  end

75
  def title
76
    # implement inside child
77
78
79
  end

  def description
80
    # implement inside child
81
82
  end

83
84
85
86
  def help
    # implement inside child
  end

87
  def to_param
88
    # implement inside child
89
90
91
  end

  def fields
92
    # implement inside child
93
94
    []
  end
95

96
97
98
99
  def supported_events
    %w(push tag_push issue merge_request)
  end

100
  def execute(data)
101
102
    # implement inside child
  end
103

104
105
106
107
108
109
  def test(data)
    # default implementation
    result = execute(data)
    { success: result.present?, result: result }
  end

110
111
112
  def can_test?
    !project.empty_repo?
  end
Drew Blessing's avatar
Drew Blessing committed
113
114
115

  # Provide convenient accessor methods
  # for each serialized property.
116
  # Also keep track of updated properties in a similar way as ActiveModel::Dirty
Drew Blessing's avatar
Drew Blessing committed
117
118
119
120
121
122
123
124
  def self.prop_accessor(*args)
    args.each do |arg|
      class_eval %{
        def #{arg}
          properties['#{arg}']
        end

        def #{arg}=(value)
125
          updated_properties['#{arg}'] = #{arg} unless #{arg}_changed?
Drew Blessing's avatar
Drew Blessing committed
126
127
          self.properties['#{arg}'] = value
        end
128
129
130
131
132
133
134
135
136
137
138
139

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

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

        def #{arg}_was
          updated_properties['#{arg}']
        end
Drew Blessing's avatar
Drew Blessing committed
140
141
142
      }
    end
  end
143

144
145
146
147
148
149
150
151
  # 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 %{
152
153
154
155
        def #{arg}?
          ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(#{arg})
        end
      }
156
157
158
    end
  end

159
160
  # Returns a hash of the properties that have been assigned a new value since last save,
  # indicating their original values (attr => original value).
161
  # ActiveRecord does not provide a mechanism to track changes in serialized keys,
162
163
164
165
166
  # 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
167
168
  end

169
170
171
  def reset_updated_properties
    @updated_properties = nil
  end
172

173
  def async_execute(data)
174
    return unless supported_events.include?(data[:object_kind])
175

176
177
    Sidekiq::Client.enqueue(ProjectServiceWorker, id, data)
  end
178
179
180
181
182

  def issue_tracker?
    self.category == :issue_tracker
  end

183
  def self.available_services_names
184
185
    %w(
      asana
186
187
      assembla
      bamboo
188
      buildkite
189
      builds_email
190
191
      campfire
      custom_issue_tracker
Kirilll Zaitsev's avatar
Kirilll Zaitsev committed
192
      drone_ci
193
      emails_on_push
194
195
      external_wiki
      flowdock
196
      gemnasium
197
198
      hipchat
      irker
199
      jira
200
201
      pivotaltracker
      pushover
202
      redmine
203
204
      slack
      teamcity
205
    )
206
207
  end

208
209
210
211
212
  def self.create_from_template(project_id, template)
    service = template.dup
    service.template = false
    service.project_id = project_id
    service if service.save
213
  end
214
end