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
|
|
|
|
|
2019-09-16 11:06:26 -04:00
|
|
|
# TODO: we can probably just delegate as part of
|
2019-10-07 11:05:59 -04:00
|
|
|
# https://gitlab.com/gitlab-org/gitlab/issues/29404
|
2019-09-16 11:06:26 -04:00
|
|
|
data_field :project_url, :issues_url, :new_issue_url
|
|
|
|
|
2016-01-19 07:48:07 -05:00
|
|
|
default_value_for :category, 'issue_tracker'
|
2015-01-23 13:28:38 -05:00
|
|
|
|
2019-09-16 11:06:26 -04:00
|
|
|
before_validation :handle_properties
|
|
|
|
before_validation :set_default_data, on: :create
|
2019-06-26 10:03:57 -04: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
|
2020-02-18 07:09:15 -05:00
|
|
|
/(\b[A-Z][A-Z0-9_]*-)#{Gitlab::Regex.issue}/
|
2017-07-10 03:38:42 -04:00
|
|
|
else
|
2020-02-18 07:09:15 -05:00
|
|
|
/(\b[A-Z][A-Z0-9_]*-|#{Issue.reference_prefix})#{Gitlab::Regex.issue}/
|
2017-07-10 03:38:42 -04:00
|
|
|
end
|
2016-10-06 18:05:27 -04:00
|
|
|
end
|
|
|
|
|
2019-10-07 11:05:59 -04:00
|
|
|
# this will be removed as part of https://gitlab.com/gitlab-org/gitlab/issues/29404
|
2019-06-26 10:03:57 -04:00
|
|
|
def title
|
|
|
|
if title_attribute = read_attribute(:title)
|
|
|
|
title_attribute
|
|
|
|
elsif self.properties && self.properties['title'].present?
|
|
|
|
self.properties['title']
|
|
|
|
else
|
|
|
|
default_title
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-07 11:05:59 -04:00
|
|
|
# this will be removed as part of https://gitlab.com/gitlab-org/gitlab/issues/29404
|
2019-06-26 10:03:57 -04:00
|
|
|
def description
|
|
|
|
if description_attribute = read_attribute(:description)
|
|
|
|
description_attribute
|
|
|
|
elsif self.properties && self.properties['description'].present?
|
|
|
|
self.properties['description']
|
|
|
|
else
|
|
|
|
default_description
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_properties
|
2019-09-16 11:06:26 -04:00
|
|
|
# this has been moved from initialize_properties and should be improved
|
2019-10-07 11:05:59 -04:00
|
|
|
# as part of https://gitlab.com/gitlab-org/gitlab/issues/29404
|
2019-09-16 11:06:26 -04:00
|
|
|
return unless properties
|
|
|
|
|
|
|
|
@legacy_properties_data = properties.dup
|
|
|
|
data_values = properties.slice!('title', 'description')
|
|
|
|
properties.each do |key, _|
|
2019-06-26 10:03:57 -04:00
|
|
|
current_value = self.properties.delete(key)
|
|
|
|
value = attribute_changed?(key) ? attribute_change(key).last : current_value
|
|
|
|
|
|
|
|
write_attribute(key, value)
|
|
|
|
end
|
2019-09-16 11:06:26 -04:00
|
|
|
|
|
|
|
data_values.reject! { |key| data_fields.changed.include?(key) }
|
2019-10-01 08:05:59 -04:00
|
|
|
data_values.slice!(*data_fields.attributes.keys)
|
2019-09-16 11:06:26 -04:00
|
|
|
data_fields.assign_attributes(data_values) if data_values.present?
|
|
|
|
|
|
|
|
self.properties = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def legacy_properties_data
|
|
|
|
@legacy_properties_data ||= {}
|
|
|
|
end
|
|
|
|
|
2019-10-01 08:05:59 -04:00
|
|
|
def supports_data_fields?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2019-09-16 11:06:26 -04:00
|
|
|
def data_fields
|
|
|
|
issue_tracker_data || self.build_issue_tracker_data
|
2019-06-26 10:03:57 -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)
|
2019-09-16 11:06:26 -04:00
|
|
|
issues_url.gsub(':id', iid.to_s)
|
2015-01-28 16:19:32 -05:00
|
|
|
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
|
|
|
|
|
2019-09-16 11:06:26 -04:00
|
|
|
def initialize_properties
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
2016-09-29 17:11:32 -04:00
|
|
|
# Initialize with default properties values
|
2019-09-16 11:06:26 -04:00
|
|
|
def set_default_data
|
|
|
|
return unless issues_tracker.present?
|
|
|
|
|
|
|
|
self.title ||= issues_tracker['title']
|
|
|
|
|
|
|
|
# we don't want to override if we have set something
|
|
|
|
return if project_url || issues_url || new_issue_url
|
|
|
|
|
|
|
|
data_fields.project_url = issues_tracker['project_url']
|
|
|
|
data_fields.issues_url = issues_tracker['issues_url']
|
|
|
|
data_fields.new_issue_url = issues_tracker['new_issue_url']
|
2015-01-26 19:24:11 -05:00
|
|
|
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
|
2020-02-11 13:08:58 -05:00
|
|
|
return if template?
|
2016-10-08 12:51:44 -04:00
|
|
|
return if project.blank?
|
|
|
|
|
|
|
|
if project.services.external_issue_trackers.where.not(id: id).any?
|
2020-03-03 04:07:54 -05:00
|
|
|
errors.add(:base, _('Another issue tracker is already in use. Only one issue tracker service can be active at a time'))
|
2016-10-08 12:51:44 -04:00
|
|
|
end
|
|
|
|
end
|
2015-01-20 19:46:27 -05:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
IssueTrackerService.prepend_if_ee('EE::IssueTrackerService')
|