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