Commit 323df002 authored by Etienne Baqué's avatar Etienne Baqué

Merge branch '15694-support-expanded-references-for-urls' into 'master'

Support expanded reference formats for URLs

See merge request gitlab-org/gitlab!75719
parents 2f81cd23 175b336a
......@@ -506,12 +506,12 @@ class MergeRequest < ApplicationRecord
def self.reference_pattern
@reference_pattern ||= %r{
(#{Project.reference_pattern})?
#{Regexp.escape(reference_prefix)}(?<merge_request>\d+)(?<format>\+)?
#{Regexp.escape(reference_prefix)}#{Gitlab::Regex.merge_request}
}x
end
def self.link_reference_pattern
@link_reference_pattern ||= super("merge_requests", /(?<merge_request>\d+)/)
@link_reference_pattern ||= super("merge_requests", Gitlab::Regex.merge_request)
end
def self.reference_valid?(reference)
......
......@@ -562,7 +562,7 @@ To include the title in the rendered link of an issue, merge request, or epic, a
at the end of the reference. For example, a reference like `#123+` is rendered as
`The issue title (#123)`.
Expanding titles does not apply to URL references, like `https://gitlab.com/gitlab-org/gitlab/-/issues/1234`.
URL references like `https://gitlab.com/gitlab-org/gitlab/-/issues/1234+` are also expanded.
### Embedding metrics in GitLab Flavored Markdown
......
......@@ -221,7 +221,7 @@ module EE
}x
%r{
(#{group_regexp})?
(?:#{combined_prefix})(?<epic>\d+)(?<format>\+)?
(?:#{combined_prefix})#{::Gitlab::Regex.epic}
}x
end
end
......@@ -236,7 +236,7 @@ module EE
#{Regexp.escape(::Gitlab.config.gitlab.url)}
\/groups\/(?<group>#{::Gitlab::PathRegex::FULL_NAMESPACE_FORMAT_REGEX})
\/-\/epics
\/(?<epic>\d+)
\/#{::Gitlab::Regex.epic}
(?<path>
(\/[a-z0-9_=-]+)*\/*
)?
......
# frozen_string_literal: true
module EE
module Gitlab
module Regex
extend ActiveSupport::Concern
class_methods do
def epic
@epic ||= /(?<epic>\d+)(?<format>\+)?/
end
end
end
end
end
......@@ -72,6 +72,13 @@ RSpec.describe Banzai::Filter::References::EpicReferenceFilter do
expect(link.attr('data-reference-format')).to eq('+')
end
it 'includes a data-reference-format attribute for URL references' do
link = doc("#{urls.group_epic_url(group, epic)}+").css('a').first
expect(link).to have_attribute('data-reference-format')
expect(link.attr('data-reference-format')).to eq('+')
end
it 'ignores invalid epic IIDs' do
text = "Check &#{non_existing_record_iid}"
......
......@@ -184,7 +184,9 @@ module Banzai
end
def unescape_link(href)
CGI.unescape(href)
# We cannot use CGI.unescape here because it also converts `+` to spaces.
# We need to keep the `+` for expanded reference formats.
Addressable::URI.unescape(href)
end
def unescape_html_entities(text)
......
......@@ -427,6 +427,10 @@ module Gitlab
@issue ||= /(?<issue>\d+)(?<format>\+)?(?=\W|\z)/
end
def merge_request
@merge_request ||= /(?<merge_request>\d+)(?<format>\+)?/
end
def base64_regex
@base64_regex ||= %r{(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?}.freeze
end
......@@ -441,3 +445,5 @@ module Gitlab
end
end
end
Gitlab::Regex.prepend_mod
......@@ -124,6 +124,14 @@ RSpec.describe Banzai::Filter::References::IssueReferenceFilter do
expect(link.attr('data-reference-format')).to eq('+')
end
it 'includes a data-reference-format attribute for URL references' do
doc = reference_filter("Issue #{issue_url}+")
link = doc.css('a').first
expect(link).to have_attribute('data-reference-format')
expect(link.attr('data-reference-format')).to eq('+')
end
it 'supports an :only_path context' do
doc = reference_filter("Issue #{reference}", only_path: true)
link = doc.css('a').first.attr('href')
......
......@@ -117,6 +117,14 @@ RSpec.describe Banzai::Filter::References::MergeRequestReferenceFilter do
expect(link.attr('data-reference-format')).to eq('+')
end
it 'includes a data-reference-format attribute for URL references' do
doc = reference_filter("Merge #{urls.project_merge_request_url(project, merge)}+")
link = doc.css('a').first
expect(link).to have_attribute('data-reference-format')
expect(link.attr('data-reference-format')).to eq('+')
end
it 'supports an :only_path context' do
doc = reference_filter("Merge #{reference}", only_path: true)
link = doc.css('a').first.attr('href')
......
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