Commit 9aa705dd authored by Yorick Peterse's avatar Yorick Peterse

Turn reference regex constants into methods

`Mentionable::ReferenceRegexes` used to define the following two
constants:

1. DEFAULT_PATTERN
2. EXTERNAL_PATTERN

These two constants were built using some of the class methods that
reside in this same module. In EE we redefine one of these methods by
using `prepend` at the start of the `ReferenceRegexes` module. This
poses a problem: we can not move the `prepend` to the end of the file,
because the constants later on depend on it.

To resolve this problem, this commit turns these constants into class
methods that memoize their results. This allows EE to redefine the
appropriate methods before these two class methods are used, in turn
allowing us to move the `prepend` to the end of the file.

See https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/8198 for more
information.
parent f5e3ce5e
...@@ -97,9 +97,9 @@ module Mentionable ...@@ -97,9 +97,9 @@ module Mentionable
# Allows heavy processing to be skipped # Allows heavy processing to be skipped
def matches_cross_reference_regex? def matches_cross_reference_regex?
reference_pattern = if !project || project.default_issues_tracker? reference_pattern = if !project || project.default_issues_tracker?
ReferenceRegexes::DEFAULT_PATTERN ReferenceRegexes.default_pattern
else else
ReferenceRegexes::EXTERNAL_PATTERN ReferenceRegexes.external_pattern
end end
self.class.mentionable_attrs.any? do |attr, _| self.class.mentionable_attrs.any? do |attr, _|
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
module Mentionable module Mentionable
module ReferenceRegexes module ReferenceRegexes
extend Gitlab::Utils::StrongMemoize
def self.reference_pattern(link_patterns, issue_pattern) def self.reference_pattern(link_patterns, issue_pattern)
Regexp.union(link_patterns, Regexp.union(link_patterns,
issue_pattern, issue_pattern,
...@@ -15,16 +17,20 @@ module Mentionable ...@@ -15,16 +17,20 @@ module Mentionable
] ]
end end
DEFAULT_PATTERN = begin def self.default_pattern
issue_pattern = Issue.reference_pattern strong_memoize(:default_pattern) do
link_patterns = Regexp.union([Issue, Commit, MergeRequest, Epic].map(&:link_reference_pattern).compact) issue_pattern = Issue.reference_pattern
reference_pattern(link_patterns, issue_pattern) link_patterns = Regexp.union([Issue, Commit, MergeRequest, Epic].map(&:link_reference_pattern).compact)
reference_pattern(link_patterns, issue_pattern)
end
end end
EXTERNAL_PATTERN = begin def self.external_pattern
issue_pattern = IssueTrackerService.reference_pattern strong_memoize(:external_pattern) do
link_patterns = URI.regexp(%w(http https)) issue_pattern = IssueTrackerService.reference_pattern
reference_pattern(link_patterns, issue_pattern) link_patterns = URI.regexp(%w(http https))
reference_pattern(link_patterns, issue_pattern)
end
end end
end end
end end
...@@ -9,7 +9,7 @@ class IssueTrackerService < Service ...@@ -9,7 +9,7 @@ class IssueTrackerService < Service
# Override this method on services that uses different patterns # Override this method on services that uses different patterns
# This pattern does not support cross-project references # This pattern does not support cross-project references
# The other code assumes that this pattern is a superset of all # The other code assumes that this pattern is a superset of all
# overridden patterns. See ReferenceRegexes::EXTERNAL_PATTERN # overridden patterns. See ReferenceRegexes.external_pattern
def self.reference_pattern(only_long: false) def self.reference_pattern(only_long: false)
if only_long if only_long
/(\b[A-Z][A-Z0-9_]*-)(?<issue>\d+)/ /(\b[A-Z][A-Z0-9_]*-)(?<issue>\d+)/
......
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