service.rb 3.35 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)
18
#
19

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

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

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

35
36
37
  belongs_to :project
  has_one :service_hook

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

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

42
43
44
45
  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) }
46
  scope :note_hooks, -> { where(note_events: true, active: true) }
47

48
49
50
  def activated?
    active
  end
51

52
53
54
55
  def template?
    template
  end

56
57
58
59
  def category
    :common
  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
86
87
88
  def supported_events
    %w(push tag_push issue merge_request)
  end

89
90
91
  def execute
    # implement inside child
  end
92
93
94
95

  def can_test?
    !project.empty_repo?
  end
Drew Blessing's avatar
Drew Blessing committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

  # Provide convenient accessor methods
  # for each serialized property.
  def self.prop_accessor(*args)
    args.each do |arg|
      class_eval %{
        def #{arg}
          properties['#{arg}']
        end

        def #{arg}=(value)
          self.properties['#{arg}'] = value
        end
      }
    end
  end
112
113

  def async_execute(data)
114
    return unless supported_events.include?(data[:object_kind])
115

116
117
    Sidekiq::Client.enqueue(ProjectServiceWorker, id, data)
  end
118
119
120
121
122

  def issue_tracker?
    self.category == :issue_tracker
  end

123
  def self.available_services_names
124
125
    %w(
      asana
126
127
      assembla
      bamboo
128
      buildkite
129
130
      campfire
      custom_issue_tracker
131
      emails_on_push
132
133
      external_wiki
      flowdock
134
      gemnasium
135
136
137
      gitlab_ci
      hipchat
      irker
138
      jira
139
140
      pivotaltracker
      pushover
141
      redmine
142
143
      slack
      teamcity
144
    )
145
146
  end

147
148
149
150
151
  def self.create_from_template(project_id, template)
    service = template.dup
    service.template = false
    service.project_id = project_id
    service if service.save
152
  end
153
end