issue_tracker_service.rb 3.12 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
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
#  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)
14
15
16
17
#  push_events           :boolean          default(TRUE)
#  issues_events         :boolean          default(TRUE)
#  merge_requests_events :boolean          default(TRUE)
#  tag_push_events       :boolean          default(TRUE)
18
#  note_events           :boolean          default(TRUE), not null
Stan Hu's avatar
Stan Hu committed
19
#  build_events          :boolean          default(FALSE), not null
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
20
21
#

Marin Jankovski's avatar
Marin Jankovski committed
22
23
class IssueTrackerService < Service

24
25
  validates :project_url, :issues_url, :new_issue_url, presence: true, if: :activated?

26
  default_value_for :category, 'issue_tracker'
27

28
  def default?
29
    default
30
31
  end

32
33
34
35
  def issue_url(iid)
    self.issues_url.gsub(':id', iid.to_s)
  end

36
37
38
39
40
41
42
43
44
45
46
47
  def project_path
    project_url
  end

  def new_issue_path
    new_issue_url
  end

  def issue_path(iid)
    issue_url(iid)
  end

48
49
50
51
  def fields
    [
      { type: 'text', name: 'description', placeholder: description },
      { type: 'text', name: 'project_url', placeholder: 'Project url' },
52
53
      { type: 'text', name: 'issues_url', placeholder: 'Issue url' },
      { type: 'text', name: 'new_issue_url', placeholder: 'New Issue url' }
54
55
56
57
58
59
60
61
    ]
  end

  def initialize_properties
    if properties.nil?
      if enabled_in_gitlab_config
        self.properties = {
          title: issues_tracker['title'],
62
63
64
          project_url: add_issues_tracker_id(issues_tracker['project_url']),
          issues_url: add_issues_tracker_id(issues_tracker['issues_url']),
          new_issue_url: add_issues_tracker_id(issues_tracker['new_issue_url'])
65
        }
66
67
      else
        self.properties = {}
68
69
70
71
      end
    end
  end

72
73
74
75
  def supported_events
    %w(push)
  end

76
  def execute(data)
77
    return unless supported_events.include?(data[:object_kind])
78

79
80
81
82
    message = "#{self.type} was unable to reach #{self.project_url}. Check the url and try again."
    result = false

    begin
83
      response = HTTParty.head(self.project_url, verify: true)
84

85
86
87
      if response
        message = "#{self.type} received response #{response.code} when attempting to connect to #{self.project_url}"
        result = true
88
      end
89
    rescue HTTParty::Error, Timeout::Error, SocketError, Errno::ECONNRESET, Errno::ECONNREFUSED => error
90
91
92
93
94
95
      message = "#{self.type} had an error when trying to connect to #{self.project_url}: #{error.message}"
    end
    Rails.logger.info(message)
    result
  end

96
97
98
99
100
101
102
103
104
105
106
107
  private

  def enabled_in_gitlab_config
    Gitlab.config.issues_tracker &&
    Gitlab.config.issues_tracker.values.any? &&
    issues_tracker
  end

  def issues_tracker
    Gitlab.config.issues_tracker[to_param]
  end

108
  def add_issues_tracker_id(url)
109
110
    if self.project
      id = self.project.issues_tracker_id
111

112
      if id
113
        url = url.gsub(":issues_tracker_id", id)
114
      end
115
    end
116

117
    url
118
  end
Marin Jankovski's avatar
Marin Jankovski committed
119
end