BigW Consortium Gitlab

issue_tracker_service.rb 3.17 KB
Newer Older
Dmitriy Zaporozhets committed
1 2 3 4
# == Schema Information
#
# Table name: services
#
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
Dmitriy Zaporozhets committed
19 20
#

21 22
class IssueTrackerService < Service

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

25 26 27 28
  def category
    :issue_tracker
  end

29 30 31 32
  def default?
    false
  end

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

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

  def new_issue_path
    new_issue_url
  end

  def issue_path(iid)
    issue_url(iid)
  end

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

  def initialize_properties
    if properties.nil?
      if enabled_in_gitlab_config
        self.properties = {
          title: issues_tracker['title'],
63 64 65
          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'])
66
        }
67 68
      else
        self.properties = {}
69 70 71 72
      end
    end
  end

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

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

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

    begin
      url = URI.parse(self.project_url)

      if url.host && url.port
Marin Jankovski committed
87
        http = Net::HTTP.start(url.host, url.port, { open_timeout: 5, read_timeout: 5 })
88 89 90 91 92 93 94 95 96 97 98 99 100 101
        response = http.head("/")

        if response
          message = "#{self.type} received response #{response.code} when attempting to connect to #{self.project_url}"
          result = true
        end
      end
    rescue Timeout::Error, SocketError, Errno::ECONNRESET, Errno::ECONNREFUSED => error
      message = "#{self.type} had an error when trying to connect to #{self.project_url}: #{error.message}"
    end
    Rails.logger.info(message)
    result
  end

102 103 104 105 106 107 108 109 110 111 112 113
  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

114
  def add_issues_tracker_id(url)
115 116
    if self.project
      id = self.project.issues_tracker_id
117

118
      if id
119
        url = url.gsub(":issues_tracker_id", id)
120
      end
121
    end
122

123
    url
124
  end
125
end