debian-mirror-gitlab/app/models/project_services/issue_tracker_service.rb

116 lines
3.1 KiB
Ruby
Raw Normal View History

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