debian-mirror-gitlab/lib/banzai/filter/external_issue_reference_filter.rb

107 lines
3.5 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2015-12-23 02:04:40 +05:30
module Banzai
module Filter
2015-09-11 14:41:01 +05:30
# HTML filter that replaces external issue tracker references with links.
# References are ignored if the project doesn't use an external issue
# tracker.
2017-08-17 22:00:37 +05:30
#
# This filter does not support cross-project references.
2015-09-11 14:41:01 +05:30
class ExternalIssueReferenceFilter < ReferenceFilter
self.reference_type = :external_issue
2015-09-11 14:41:01 +05:30
# Public: Find `JIRA-123` issue references in text
#
2016-11-03 12:29:30 +05:30
# ExternalIssueReferenceFilter.references_in(text, pattern) do |match, issue|
2015-09-11 14:41:01 +05:30
# "<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-11-03 12:29:30 +05:30
def self.references_in(text, pattern)
text.gsub(pattern) do |match|
2015-09-11 14:41:01 +05:30
yield match, $~[:issue]
end
end
def call
# Early return if the project isn't using an external tracker
2016-06-02 11:05:42 +05:30
return doc if project.nil? || default_issues_tracker?
2015-09-11 14:41:01 +05:30
2016-11-03 12:29:30 +05:30
ref_pattern = issue_reference_pattern
2016-06-02 11:05:42 +05:30
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
2015-12-23 02:04:40 +05:30
2016-06-02 11:05:42 +05:30
elsif element_node?(node)
2017-08-17 22:00:37 +05:30
yield_valid_link(node) do |link, inner_html|
2016-06-02 11:05:42 +05:30
if link =~ ref_start_pattern
replace_link_node_with_href(node, link) do
2017-08-17 22:00:37 +05:30
issue_link_filter(link, link_content: inner_html)
2016-06-02 11:05:42 +05:30
end
end
end
end
2015-12-23 02:04:40 +05:30
end
2016-06-02 11:05:42 +05:30
doc
2015-09-11 14:41:01 +05:30
end
# Replace `JIRA-123` issue references in text with links to the referenced
# issue's details page.
#
# text - String text to replace references in.
2017-08-17 22:00:37 +05:30
# link_content - Original content of the link being replaced.
2015-09-11 14:41:01 +05:30
#
# Returns a String with `JIRA-123` references replaced with links. All
# links have `gfm` and `gfm-issue` class names attached for styling.
2017-08-17 22:00:37 +05:30
def issue_link_filter(text, link_content: nil)
2015-09-11 14:41:01 +05:30
project = context[:project]
2016-11-03 12:29:30 +05:30
self.class.references_in(text, issue_reference_pattern) do |match, id|
2015-12-23 02:04:40 +05:30
ExternalIssue.new(id, project)
url = url_for_issue(id, project, only_path: context[:only_path])
2015-09-11 14:41:01 +05:30
title = "Issue in #{project.external_issue_tracker.title}"
2015-09-11 14:41:01 +05:30
klass = reference_class(:issue)
2015-12-23 02:04:40 +05:30
data = data_attribute(project: project.id, external_issue: id)
2017-08-17 22:00:37 +05:30
content = link_content || match
2015-09-11 14:41:01 +05:30
2015-10-24 18:46:33 +05:30
%(<a href="#{url}" #{data}
title="#{escape_once(title)}"
2017-08-17 22:00:37 +05:30
class="#{klass}">#{content}</a>)
2015-09-11 14:41:01 +05:30
end
end
def url_for_issue(*args)
IssuesHelper.url_for_issue(*args)
end
2016-06-02 11:05:42 +05:30
def default_issues_tracker?
2016-11-03 12:29:30 +05:30
external_issues_cached(:default_issues_tracker?)
end
def issue_reference_pattern
2017-08-17 22:00:37 +05:30
external_issues_cached(:external_issue_reference_pattern)
2016-06-02 11:05:42 +05:30
end
private
2016-11-03 12:29:30 +05:30
def external_issues_cached(attribute)
2018-12-05 23:21:45 +05:30
cached_attributes = Gitlab::SafeRequestStore[:banzai_external_issues_tracker_attributes] ||= Hash.new { |h, k| h[k] = {} }
2018-03-17 18:26:18 +05:30
cached_attributes[project.id][attribute] = project.public_send(attribute) if cached_attributes[project.id][attribute].nil? # rubocop:disable GitlabSecurity/PublicSend
2016-11-03 12:29:30 +05:30
cached_attributes[project.id][attribute]
2016-06-02 11:05:42 +05:30
end
2015-09-11 14:41:01 +05:30
end
end
end