2015-04-03 00:46:43 +00:00
|
|
|
module Gitlab
|
|
|
|
module Markdown
|
|
|
|
# HTML filter that replaces merge request references with links. References
|
2015-04-14 22:34:01 +00:00
|
|
|
# to merge requests that do not exist are ignored.
|
2015-04-03 00:46:43 +00:00
|
|
|
#
|
|
|
|
# This filter supports cross-project references.
|
2015-04-14 22:34:01 +00:00
|
|
|
class MergeRequestReferenceFilter < ReferenceFilter
|
2015-04-03 00:46:43 +00:00
|
|
|
include CrossProjectReference
|
|
|
|
|
|
|
|
# Public: Find `!123` merge request references in text
|
|
|
|
#
|
|
|
|
# MergeRequestReferenceFilter.references_in(text) do |match, merge_request, project_ref|
|
|
|
|
# "<a href=...>##{merge_request}</a>"
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# text - String text to search.
|
|
|
|
#
|
|
|
|
# Yields the String match, the Integer merge request ID, and an optional
|
|
|
|
# String of the external project reference.
|
|
|
|
#
|
|
|
|
# Returns a String replaced with the return of the block.
|
|
|
|
def self.references_in(text)
|
2015-05-14 20:59:39 +00:00
|
|
|
text.gsub(MergeRequest.reference_pattern) do |match|
|
2015-04-03 00:46:43 +00:00
|
|
|
yield match, $~[:merge_request].to_i, $~[:project]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
2015-05-14 20:59:39 +00:00
|
|
|
replace_text_nodes_matching(MergeRequest.reference_pattern) do |content|
|
2015-04-14 22:34:01 +00:00
|
|
|
merge_request_link_filter(content)
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Replace `!123` merge request references in text with links to the
|
|
|
|
# referenced merge request's details page.
|
|
|
|
#
|
|
|
|
# text - String text to replace references in.
|
|
|
|
#
|
|
|
|
# Returns a String with `!123` references replaced with links. All links
|
|
|
|
# have `gfm` and `gfm-merge_request` class names attached for styling.
|
|
|
|
def merge_request_link_filter(text)
|
|
|
|
self.class.references_in(text) do |match, id, project_ref|
|
|
|
|
project = self.project_from_ref(project_ref)
|
|
|
|
|
2015-04-16 18:06:05 +00:00
|
|
|
if project && merge_request = project.merge_requests.find_by(iid: id)
|
2015-04-15 19:56:45 +00:00
|
|
|
push_result(:merge_request, merge_request)
|
|
|
|
|
2015-04-16 16:41:59 +00:00
|
|
|
title = escape_once("Merge Request: #{merge_request.title}")
|
2015-04-14 22:34:01 +00:00
|
|
|
klass = reference_class(:merge_request)
|
2015-08-07 03:56:09 +00:00
|
|
|
data = data_attribute(project.id)
|
2015-04-03 00:46:43 +00:00
|
|
|
|
|
|
|
url = url_for_merge_request(merge_request, project)
|
|
|
|
|
2015-08-07 03:56:09 +00:00
|
|
|
%(<a href="#{url}" #{data}
|
2015-04-03 00:46:43 +00:00
|
|
|
title="#{title}"
|
2015-05-21 20:35:15 +00:00
|
|
|
class="#{klass}">#{match}</a>)
|
2015-04-03 00:46:43 +00:00
|
|
|
else
|
|
|
|
match
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def url_for_merge_request(mr, project)
|
|
|
|
h = Rails.application.routes.url_helpers
|
|
|
|
h.namespace_project_merge_request_url(project.namespace, project, mr,
|
|
|
|
only_path: context[:only_path])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|