BigW Consortium Gitlab

issue_tracker_service.rb 2.36 KB
Newer Older
1
class IssueTrackerService < Service
2
  default_value_for :category, 'issue_tracker'
3

4 5 6 7 8 9
  # Pattern used to extract links from comments
  # Override this method on services that uses different patterns
  def reference_pattern
    @reference_pattern ||= %r{(\b[A-Z][A-Z0-9_]+-|#{Issue.reference_prefix})(?<issue>\d+)}
  end

10
  def default?
11
    default
12 13
  end

14 15 16 17
  def issue_url(iid)
    self.issues_url.gsub(':id', iid.to_s)
  end

18 19 20 21 22 23 24 25 26 27 28 29
  def project_path
    project_url
  end

  def new_issue_path
    new_issue_url
  end

  def issue_path(iid)
    issue_url(iid)
  end

30 31 32 33
  def fields
    [
      { type: 'text', name: 'description', placeholder: description },
      { type: 'text', name: 'project_url', placeholder: 'Project url' },
34 35
      { type: 'text', name: 'issues_url', placeholder: 'Issue url' },
      { type: 'text', name: 'new_issue_url', placeholder: 'New Issue url' }
36 37 38
    ]
  end

39 40 41 42 43 44 45 46 47
  # Initialize with default properties values
  # or receive a block with custom properties
  def initialize_properties(&block)
    return unless properties.nil?

    if enabled_in_gitlab_config
      if block_given?
        yield
      else
48 49
        self.properties = {
          title: issues_tracker['title'],
50 51 52
          project_url: issues_tracker['project_url'],
          issues_url: issues_tracker['issues_url'],
          new_issue_url: issues_tracker['new_issue_url']
53 54
        }
      end
55 56
    else
      self.properties = {}
57 58 59
    end
  end

60 61 62 63
  def supported_events
    %w(push)
  end

64
  def execute(data)
65
    return unless supported_events.include?(data[:object_kind])
66

67 68 69 70
    message = "#{self.type} was unable to reach #{self.project_url}. Check the url and try again."
    result = false

    begin
71
      response = HTTParty.head(self.project_url, verify: true)
72

73 74 75
      if response
        message = "#{self.type} received response #{response.code} when attempting to connect to #{self.project_url}"
        result = true
76
      end
77
    rescue HTTParty::Error, Timeout::Error, SocketError, Errno::ECONNRESET, Errno::ECONNREFUSED => error
78 79 80 81 82 83
      message = "#{self.type} had an error when trying to connect to #{self.project_url}: #{error.message}"
    end
    Rails.logger.info(message)
    result
  end

84 85 86 87 88 89 90 91 92 93 94
  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
95
end