2015-12-15 14:51:16 +00:00
|
|
|
module Banzai
|
|
|
|
module Filter
|
2015-04-03 00:46:43 +00:00
|
|
|
# HTML filter that replaces external issue tracker references with links.
|
2015-04-14 23:01:09 +00:00
|
|
|
# References are ignored if the project doesn't use an external issue
|
|
|
|
# tracker.
|
2017-06-30 12:47:53 +00:00
|
|
|
#
|
|
|
|
# This filter does not support cross-project references.
|
2015-04-14 22:34:01 +00:00
|
|
|
class ExternalIssueReferenceFilter < ReferenceFilter
|
2016-05-26 11:16:43 +00:00
|
|
|
self.reference_type = :external_issue
|
|
|
|
|
2015-04-03 00:46:43 +00:00
|
|
|
# Public: Find `JIRA-123` issue references in text
|
|
|
|
#
|
2016-10-06 22:05:27 +00:00
|
|
|
# ExternalIssueReferenceFilter.references_in(text, pattern) do |match, issue|
|
2015-04-03 00:46:43 +00:00
|
|
|
# "<a href=...>##{issue}</a>"
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# text - String text to search.
|
|
|
|
#
|
|
|
|
# Yields the String match and the String issue reference.
|
|
|
|
#
|
|
|
|
# Returns a String replaced with the return of the block.
|
2016-10-06 22:05:27 +00:00
|
|
|
def self.references_in(text, pattern)
|
|
|
|
text.gsub(pattern) do |match|
|
2015-04-03 00:46:43 +00:00
|
|
|
yield match, $~[:issue]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
2015-04-15 17:04:04 +00:00
|
|
|
# Early return if the project isn't using an external tracker
|
2016-03-30 12:04:28 +00:00
|
|
|
return doc if project.nil? || default_issues_tracker?
|
2015-04-14 23:01:09 +00:00
|
|
|
|
2016-10-06 22:05:27 +00:00
|
|
|
ref_pattern = issue_reference_pattern
|
2016-03-24 14:57:22 +00:00
|
|
|
ref_start_pattern = /\A#{ref_pattern}\z/
|
|
|
|
|
|
|
|
each_node do |node|
|
|
|
|
if text_node?(node)
|
|
|
|
replace_text_when_pattern_matches(node, ref_pattern) do |content|
|
|
|
|
issue_link_filter(content)
|
|
|
|
end
|
|
|
|
|
|
|
|
elsif element_node?(node)
|
2016-09-30 09:03:16 +00:00
|
|
|
yield_valid_link(node) do |link, inner_html|
|
2016-03-24 14:57:22 +00:00
|
|
|
if link =~ ref_start_pattern
|
|
|
|
replace_link_node_with_href(node, link) do
|
2016-09-30 09:03:16 +00:00
|
|
|
issue_link_filter(link, link_content: inner_html)
|
2016-03-24 14:57:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
2015-12-01 15:25:56 +00:00
|
|
|
|
2016-03-24 14:57:22 +00:00
|
|
|
doc
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Replace `JIRA-123` issue references in text with links to the referenced
|
|
|
|
# issue's details page.
|
|
|
|
#
|
|
|
|
# text - String text to replace references in.
|
2016-09-30 09:03:16 +00:00
|
|
|
# link_content - Original content of the link being replaced.
|
2015-04-03 00:46:43 +00:00
|
|
|
#
|
|
|
|
# Returns a String with `JIRA-123` references replaced with links. All
|
|
|
|
# links have `gfm` and `gfm-issue` class names attached for styling.
|
2016-09-30 09:03:16 +00:00
|
|
|
def issue_link_filter(text, link_content: nil)
|
2015-04-03 00:46:43 +00:00
|
|
|
project = context[:project]
|
|
|
|
|
2016-10-06 22:05:27 +00:00
|
|
|
self.class.references_in(text, issue_reference_pattern) do |match, id|
|
2015-12-17 22:08:14 +00:00
|
|
|
ExternalIssue.new(id, project)
|
|
|
|
|
|
|
|
url = url_for_issue(id, project, only_path: context[:only_path])
|
2015-04-03 00:46:43 +00:00
|
|
|
|
2015-12-24 20:36:33 +00:00
|
|
|
title = "Issue in #{project.external_issue_tracker.title}"
|
2015-04-14 22:34:01 +00:00
|
|
|
klass = reference_class(:issue)
|
2015-12-17 22:08:14 +00:00
|
|
|
data = data_attribute(project: project.id, external_issue: id)
|
2015-04-03 00:46:43 +00:00
|
|
|
|
2016-09-30 09:03:16 +00:00
|
|
|
content = link_content || match
|
2015-12-01 15:25:56 +00:00
|
|
|
|
2015-10-07 15:00:48 +00:00
|
|
|
%(<a href="#{url}" #{data}
|
2015-12-24 20:36:33 +00:00
|
|
|
title="#{escape_once(title)}"
|
2016-09-30 09:03:16 +00:00
|
|
|
class="#{klass}">#{content}</a>)
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def url_for_issue(*args)
|
|
|
|
IssuesHelper.url_for_issue(*args)
|
|
|
|
end
|
2016-03-30 12:04:28 +00:00
|
|
|
|
|
|
|
def default_issues_tracker?
|
2016-10-06 22:05:27 +00:00
|
|
|
external_issues_cached(:default_issues_tracker?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def issue_reference_pattern
|
2017-06-30 12:47:53 +00:00
|
|
|
external_issues_cached(:external_issue_reference_pattern)
|
2016-03-30 12:04:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-10-06 22:05:27 +00:00
|
|
|
def external_issues_cached(attribute)
|
|
|
|
return project.public_send(attribute) unless RequestStore.active?
|
|
|
|
|
|
|
|
cached_attributes = RequestStore[:banzai_external_issues_tracker_attributes] ||= Hash.new { |h, k| h[k] = {} }
|
|
|
|
cached_attributes[project.id][attribute] = project.public_send(attribute) if cached_attributes[project.id][attribute].nil?
|
|
|
|
cached_attributes[project.id][attribute]
|
2016-03-30 12:04:28 +00:00
|
|
|
end
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|