Commit a4ce2d12 authored by Yorick Peterse's avatar Yorick Peterse

Handle external issues in IssueReferenceFilter

IssueReferenceFilter will end up processing internal issue references
when a project uses an external issues tracker while still using
internal issue references (in the form of `#\d+`). This commit ensures
that these links are rendered as external issue links, regardless of
whether the project one currently views uses an internal or external
issues tracker.

Fixes gitlab-org/gitlab-ce#19036, gitlab-com/performance#16
parent bef4294c
...@@ -160,11 +160,7 @@ module Banzai ...@@ -160,11 +160,7 @@ module Banzai
title = object_link_title(object) title = object_link_title(object)
klass = reference_class(object_sym) klass = reference_class(object_sym)
data = data_attribute( data = data_attributes_for(link_text || match, project, object)
original: link_text || match,
project: project.id,
object_sym => object.id
)
if matches.names.include?("url") && matches[:url] if matches.names.include?("url") && matches[:url]
url = matches[:url] url = matches[:url]
...@@ -183,6 +179,14 @@ module Banzai ...@@ -183,6 +179,14 @@ module Banzai
end end
end end
def data_attributes_for(text, project, object)
data_attribute(
original: text,
project: project.id,
object_sym => object.id
)
end
def object_link_text_extras(object, matches) def object_link_text_extras(object, matches)
extras = [] extras = []
......
...@@ -46,6 +46,26 @@ module Banzai ...@@ -46,6 +46,26 @@ module Banzai
end end
end end
def object_link_title(object)
if object.is_a?(ExternalIssue)
"Issue in #{object.project.external_issue_tracker.title}"
else
super
end
end
def data_attributes_for(text, project, object)
if object.is_a?(ExternalIssue)
data_attribute(
project: project.id,
external_issue: object.id,
reference_type: ExternalIssueReferenceFilter.reference_type
)
else
super
end
end
def find_projects_for_paths(paths) def find_projects_for_paths(paths)
super(paths).includes(:gitlab_issue_tracker_service) super(paths).includes(:gitlab_issue_tracker_service)
end end
......
...@@ -29,7 +29,7 @@ module Banzai ...@@ -29,7 +29,7 @@ module Banzai
def data_attribute(attributes = {}) def data_attribute(attributes = {})
attributes = attributes.reject { |_, v| v.nil? } attributes = attributes.reject { |_, v| v.nil? }
attributes[:reference_type] = self.class.reference_type attributes[:reference_type] ||= self.class.reference_type
attributes.delete(:original) if context[:no_original_data] attributes.delete(:original) if context[:no_original_data]
attributes.map { |key, value| %Q(data-#{key.to_s.dasherize}="#{escape_once(value)}") }.join(" ") attributes.map { |key, value| %Q(data-#{key.to_s.dasherize}="#{escape_once(value)}") }.join(" ")
end end
......
...@@ -199,6 +199,19 @@ describe Banzai::Filter::IssueReferenceFilter, lib: true do ...@@ -199,6 +199,19 @@ describe Banzai::Filter::IssueReferenceFilter, lib: true do
end end
end end
context 'referencing external issues' do
let(:project) { create(:redmine_project) }
it 'renders internal issue IDs as external issue links' do
doc = reference_filter('#1')
link = doc.css('a').first
expect(link.attr('data-reference-type')).to eq('external_issue')
expect(link.attr('title')).to eq('Issue in Redmine')
expect(link.attr('data-external-issue')).to eq('1')
end
end
describe '#issues_per_Project' do describe '#issues_per_Project' do
context 'using an internal issue tracker' do context 'using an internal issue tracker' do
it 'returns a Hash containing the issues per project' do it 'returns a Hash containing the issues per project' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment