Commit 3fd2a90a authored by Arturo Herrero's avatar Arturo Herrero

Extract regex into a common method

The regex (?<issue>\d+\b) is used in several places and here we are
abstracting into a common reference.
parent 23358f55
......@@ -128,12 +128,12 @@ class Issue < ApplicationRecord
def self.reference_pattern
@reference_pattern ||= %r{
(#{Project.reference_pattern})?
#{Regexp.escape(reference_prefix)}(?<issue>\d+)
#{Regexp.escape(reference_prefix)}#{Gitlab::Regex.issue}
}x
end
def self.link_reference_pattern
@link_reference_pattern ||= super("issues", /(?<issue>\d+)/)
@link_reference_pattern ||= super("issues", Gitlab::Regex.issue)
end
def self.reference_valid?(reference)
......
......@@ -19,9 +19,9 @@ class IssueTrackerService < Service
# overridden patterns. See ReferenceRegexes.external_pattern
def self.reference_pattern(only_long: false)
if only_long
/(\b[A-Z][A-Z0-9_]*-)(?<issue>\d+\b)/
/(\b[A-Z][A-Z0-9_]*-)#{Gitlab::Regex.issue}/
else
/(\b[A-Z][A-Z0-9_]*-|#{Issue.reference_prefix})(?<issue>\d+\b)/
/(\b[A-Z][A-Z0-9_]*-|#{Issue.reference_prefix})#{Gitlab::Regex.issue}/
end
end
......
......@@ -8,7 +8,7 @@ class YoutrackService < IssueTrackerService
if only_long
/(?<issue>\b[A-Za-z][A-Za-z0-9_]*-\d+\b)/
else
/(?<issue>\b[A-Za-z][A-Za-z0-9_]*-\d+\b)|(#{Issue.reference_prefix}(?<issue>\d+\b))/
/(?<issue>\b[A-Za-z][A-Za-z0-9_]*-\d+\b)|(#{Issue.reference_prefix}#{Gitlab::Regex.issue})/
end
end
......
......@@ -165,7 +165,7 @@ module DesignManagement
def self.link_reference_pattern
@link_reference_pattern ||= begin
exts = SAFE_IMAGE_EXT + DANGEROUS_IMAGE_EXT
path_segment = %r{issues/(?<issue>\d+)/designs}
path_segment = %r{issues/#{Gitlab::Regex.issue}/designs}
filename_pattern = %r{(?<simple_file_name>[a-z0-9_=-]+\.(#{exts.join('|')}))}i
super(path_segment, filename_pattern)
......
......@@ -144,6 +144,10 @@ module Gitlab
def utc_date_regex
@utc_date_regex ||= /\A[0-9]{4}-[0-9]{2}-[0-9]{2}\z/.freeze
end
def issue
@issue ||= /(?<issue>\d+\b)/
end
end
end
......
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