2018-08-10 02:45:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-01-20 19:46:27 -05:00
|
|
|
class IssueTrackerService < Service
|
2016-10-08 12:51:44 -04:00
|
|
|
validate :one_issue_tracker, if: :activated?, on: :manual_change
|
|
|
|
|
2016-01-19 07:48:07 -05:00
|
|
|
default_value_for :category, 'issue_tracker'
|
2015-01-23 13:28:38 -05:00
|
|
|
|
2016-10-06 18:05:27 -04:00
|
|
|
# Pattern used to extract links from comments
|
|
|
|
# Override this method on services that uses different patterns
|
2017-06-30 08:47:53 -04:00
|
|
|
# This pattern does not support cross-project references
|
|
|
|
# The other code assumes that this pattern is a superset of all
|
2018-11-12 07:50:31 -05:00
|
|
|
# overridden patterns. See ReferenceRegexes.external_pattern
|
2017-07-10 03:38:42 -04:00
|
|
|
def self.reference_pattern(only_long: false)
|
|
|
|
if only_long
|
2018-10-31 11:04:34 -04:00
|
|
|
/(\b[A-Z][A-Z0-9_]*-)(?<issue>\d+)/
|
2017-07-10 03:38:42 -04:00
|
|
|
else
|
2018-10-31 11:04:34 -04:00
|
|
|
/(\b[A-Z][A-Z0-9_]*-|#{Issue.reference_prefix})(?<issue>\d+)/
|
2017-07-10 03:38:42 -04:00
|
|
|
end
|
2016-10-06 18:05:27 -04:00
|
|
|
end
|
|
|
|
|
2015-01-28 12:28:17 -05:00
|
|
|
def default?
|
2016-01-19 07:48:07 -05:00
|
|
|
default
|
2015-01-28 12:28:17 -05:00
|
|
|
end
|
|
|
|
|
2015-01-28 16:19:32 -05:00
|
|
|
def issue_url(iid)
|
|
|
|
self.issues_url.gsub(':id', iid.to_s)
|
|
|
|
end
|
|
|
|
|
2017-07-17 14:56:35 -04:00
|
|
|
def issue_tracker_path
|
|
|
|
project_url
|
2015-03-25 05:03:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def new_issue_path
|
|
|
|
new_issue_url
|
|
|
|
end
|
|
|
|
|
|
|
|
def issue_path(iid)
|
|
|
|
issue_url(iid)
|
|
|
|
end
|
|
|
|
|
2015-01-26 19:24:11 -05:00
|
|
|
def fields
|
|
|
|
[
|
|
|
|
{ type: 'text', name: 'description', placeholder: description },
|
2017-05-22 06:07:12 -04:00
|
|
|
{ type: 'text', name: 'project_url', placeholder: 'Project url', required: true },
|
|
|
|
{ type: 'text', name: 'issues_url', placeholder: 'Issue url', required: true },
|
|
|
|
{ type: 'text', name: 'new_issue_url', placeholder: 'New Issue url', required: true }
|
2015-01-26 19:24:11 -05:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2016-09-29 17:11:32 -04:00
|
|
|
# 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
|
2015-01-26 19:24:11 -05:00
|
|
|
self.properties = {
|
|
|
|
title: issues_tracker['title'],
|
2016-06-10 15:53:20 -04:00
|
|
|
project_url: issues_tracker['project_url'],
|
|
|
|
issues_url: issues_tracker['issues_url'],
|
|
|
|
new_issue_url: issues_tracker['new_issue_url']
|
2015-01-26 19:24:11 -05:00
|
|
|
}
|
|
|
|
end
|
2016-09-29 17:11:32 -04:00
|
|
|
else
|
|
|
|
self.properties = {}
|
2015-01-26 19:24:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-27 07:44:24 -05:00
|
|
|
def self.supported_events
|
2015-02-28 11:33:18 -05:00
|
|
|
%w(push)
|
|
|
|
end
|
|
|
|
|
2015-02-12 16:02:58 -05:00
|
|
|
def execute(data)
|
2015-02-28 11:33:18 -05:00
|
|
|
return unless supported_events.include?(data[:object_kind])
|
2015-02-19 00:02:57 -05:00
|
|
|
|
2015-02-12 16:02:58 -05:00
|
|
|
message = "#{self.type} was unable to reach #{self.project_url}. Check the url and try again."
|
|
|
|
result = false
|
|
|
|
|
|
|
|
begin
|
2018-03-13 18:38:25 -04:00
|
|
|
response = Gitlab::HTTP.head(self.project_url, verify: true)
|
2015-02-12 16:02:58 -05:00
|
|
|
|
2015-06-04 15:08:35 -04:00
|
|
|
if response
|
|
|
|
message = "#{self.type} received response #{response.code} when attempting to connect to #{self.project_url}"
|
|
|
|
result = true
|
2015-02-12 16:02:58 -05:00
|
|
|
end
|
2018-03-13 18:38:25 -04:00
|
|
|
rescue Gitlab::HTTP::Error, Timeout::Error, SocketError, Errno::ECONNRESET, Errno::ECONNREFUSED, OpenSSL::SSL::SSLError => error
|
2015-02-12 16:02:58 -05:00
|
|
|
message = "#{self.type} had an error when trying to connect to #{self.project_url}: #{error.message}"
|
|
|
|
end
|
2018-08-20 14:34:07 -04:00
|
|
|
log_info(message)
|
2015-02-12 16:02:58 -05:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2015-01-26 19:24:11 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def enabled_in_gitlab_config
|
|
|
|
Gitlab.config.issues_tracker &&
|
2016-12-15 17:14:20 -05:00
|
|
|
Gitlab.config.issues_tracker.values.any? &&
|
|
|
|
issues_tracker
|
2015-01-26 19:24:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def issues_tracker
|
|
|
|
Gitlab.config.issues_tracker[to_param]
|
|
|
|
end
|
2016-10-08 12:51:44 -04:00
|
|
|
|
|
|
|
def one_issue_tracker
|
|
|
|
return if template?
|
|
|
|
return if project.blank?
|
|
|
|
|
|
|
|
if project.services.external_issue_trackers.where.not(id: id).any?
|
|
|
|
errors.add(:base, 'Another issue tracker is already in use. Only one issue tracker service can be active at a time')
|
|
|
|
end
|
|
|
|
end
|
2015-01-20 19:46:27 -05:00
|
|
|
end
|