debian-mirror-gitlab/app/models/integrations/base_issue_tracker.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

158 lines
4.3 KiB
Ruby
Raw Normal View History

2021-09-04 01:27:46 +05:30
# frozen_string_literal: true
module Integrations
class BaseIssueTracker < Integration
validate :one_issue_tracker, if: :activated?, on: :manual_change
2023-01-13 00:05:48 +05:30
attribute :category, default: 'issue_tracker'
2021-09-04 01:27:46 +05:30
before_validation :handle_properties
before_validation :set_default_data, on: :create
# Pattern used to extract links from comments
# Override this method on services that uses different patterns
# This pattern does not support cross-project references
# The other code assumes that this pattern is a superset of all
# overridden patterns. See ReferenceRegexes.external_pattern
2023-06-20 00:43:36 +05:30
def self.base_reference_pattern(only_long: false)
2021-09-04 01:27:46 +05:30
if only_long
/(\b[A-Z][A-Z0-9_]*-)#{Gitlab::Regex.issue}/
else
/(\b[A-Z][A-Z0-9_]*-|#{Issue.reference_prefix})#{Gitlab::Regex.issue}/
end
end
2023-06-20 00:43:36 +05:30
def reference_pattern(only_long: false)
self.class.base_reference_pattern(only_long: only_long)
end
2021-09-04 01:27:46 +05:30
def handle_properties
# this has been moved from initialize_properties and should be improved
# as part of https://gitlab.com/gitlab-org/gitlab/issues/29404
2022-06-21 17:19:12 +05:30
return unless properties.present?
safe_keys = data_fields.attributes.keys.grep_v(/encrypted/) - %w[id service_id created_at]
2021-09-04 01:27:46 +05:30
@legacy_properties_data = properties.dup
2022-06-21 17:19:12 +05:30
data_values = properties.slice(*safe_keys)
2021-09-04 01:27:46 +05:30
data_values.reject! { |key| data_fields.changed.include?(key) }
2022-06-21 17:19:12 +05:30
2021-09-04 01:27:46 +05:30
data_fields.assign_attributes(data_values) if data_values.present?
self.properties = {}
end
def legacy_properties_data
@legacy_properties_data ||= {}
end
def supports_data_fields?
true
end
def data_fields
issue_tracker_data || self.build_issue_tracker_data
end
def default?
default
end
def issue_url(iid)
issues_url.gsub(':id', iid.to_s)
end
def issue_tracker_path
project_url
end
def new_issue_path
new_issue_url
end
def issue_path(iid)
issue_url(iid)
end
# Initialize with default properties values
def set_default_data
return unless issues_tracker.present?
# 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']
end
def self.supported_events
%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
2022-08-13 15:12:31 +05:30
response = Gitlab::HTTP.head(self.project_url, verify: true)
2021-09-04 01:27:46 +05:30
if response
message = "#{self.type} received response #{response.code} when attempting to connect to #{self.project_url}"
result = true
end
2022-08-27 11:52:29 +05:30
rescue Gitlab::HTTP::Error, Timeout::Error, SocketError, Errno::ECONNRESET, Errno::ECONNREFUSED, OpenSSL::SSL::SSLError => e
message = "#{self.type} had an error when trying to connect to #{self.project_url}: #{e.message}"
2021-09-04 01:27:46 +05:30
end
log_info(message)
result
end
def support_close_issue?
false
end
def support_cross_reference?
false
end
2022-01-26 12:08:38 +05:30
def create_cross_reference_note(external_issue, mentioned_in, author)
2021-09-04 01:27:46 +05:30
# implement inside child
end
2022-05-07 20:08:51 +05:30
def activate_disabled_reason
{ trackers: other_external_issue_trackers } if other_external_issue_trackers.any?
end
2021-09-04 01:27:46 +05:30
private
2022-05-07 20:08:51 +05:30
def other_external_issue_trackers
return [] unless project_level?
@other_external_issue_trackers ||= project.integrations.external_issue_trackers.where.not(id: id)
end
2021-09-04 01:27:46 +05:30
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
def one_issue_tracker
2022-05-07 20:08:51 +05:30
return if instance?
2021-09-04 01:27:46 +05:30
return if project.blank?
2022-05-07 20:08:51 +05:30
if other_external_issue_trackers.any?
2021-09-04 01:27:46 +05:30
errors.add(:base, _('Another issue tracker is already in use. Only one issue tracker service can be active at a time'))
end
end
end
end