Commit be1edb93 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'reference-pattern-word-boundary' into 'master'

Improve reference pattern with word boundary

See merge request gitlab-org/gitlab!25266
parents a82496ba 3fd2a90a
...@@ -128,12 +128,12 @@ class Issue < ApplicationRecord ...@@ -128,12 +128,12 @@ class Issue < ApplicationRecord
def self.reference_pattern def self.reference_pattern
@reference_pattern ||= %r{ @reference_pattern ||= %r{
(#{Project.reference_pattern})? (#{Project.reference_pattern})?
#{Regexp.escape(reference_prefix)}(?<issue>\d+) #{Regexp.escape(reference_prefix)}#{Gitlab::Regex.issue}
}x }x
end end
def self.link_reference_pattern def self.link_reference_pattern
@link_reference_pattern ||= super("issues", /(?<issue>\d+)/) @link_reference_pattern ||= super("issues", Gitlab::Regex.issue)
end end
def self.reference_valid?(reference) def self.reference_valid?(reference)
......
...@@ -19,9 +19,9 @@ class IssueTrackerService < Service ...@@ -19,9 +19,9 @@ class IssueTrackerService < Service
# 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_]*-)#{Gitlab::Regex.issue}/
else else
/(\b[A-Z][A-Z0-9_]*-|#{Issue.reference_prefix})(?<issue>\d+)/ /(\b[A-Z][A-Z0-9_]*-|#{Issue.reference_prefix})#{Gitlab::Regex.issue}/
end end
end end
......
...@@ -8,7 +8,7 @@ class YoutrackService < IssueTrackerService ...@@ -8,7 +8,7 @@ class YoutrackService < IssueTrackerService
if only_long if only_long
/(?<issue>\b[A-Za-z][A-Za-z0-9_]*-\d+\b)/ /(?<issue>\b[A-Za-z][A-Za-z0-9_]*-\d+\b)/
else else
/(?<issue>\b[A-Za-z][A-Za-z0-9_]*-\d+\b)|(#{Issue.reference_prefix}(?<issue>\d+))/ /(?<issue>\b[A-Za-z][A-Za-z0-9_]*-\d+\b)|(#{Issue.reference_prefix}#{Gitlab::Regex.issue})/
end end
end end
......
...@@ -165,7 +165,7 @@ module DesignManagement ...@@ -165,7 +165,7 @@ module DesignManagement
def self.link_reference_pattern def self.link_reference_pattern
@link_reference_pattern ||= begin @link_reference_pattern ||= begin
exts = SAFE_IMAGE_EXT + DANGEROUS_IMAGE_EXT 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 filename_pattern = %r{(?<simple_file_name>[a-z0-9_=-]+\.(#{exts.join('|')}))}i
super(path_segment, filename_pattern) super(path_segment, filename_pattern)
......
...@@ -112,7 +112,7 @@ module Gitlab ...@@ -112,7 +112,7 @@ module Gitlab
# Based on Jira's project key format # Based on Jira's project key format
# https://confluence.atlassian.com/adminjiraserver073/changing-the-project-key-format-861253229.html # https://confluence.atlassian.com/adminjiraserver073/changing-the-project-key-format-861253229.html
def jira_issue_key_regex def jira_issue_key_regex
@jira_issue_key_regex ||= /[A-Z][A-Z_0-9]+-\d+/ @jira_issue_key_regex ||= /[A-Z][A-Z_0-9]+-\d+\b/
end end
def jira_transition_id_regex def jira_transition_id_regex
...@@ -144,6 +144,10 @@ module Gitlab ...@@ -144,6 +144,10 @@ module Gitlab
def utc_date_regex def utc_date_regex
@utc_date_regex ||= /\A[0-9]{4}-[0-9]{2}-[0-9]{2}\z/.freeze @utc_date_regex ||= /\A[0-9]{4}-[0-9]{2}-[0-9]{2}\z/.freeze
end end
def issue
@issue ||= /(?<issue>\d+\b)/
end
end end
end end
......
...@@ -38,8 +38,8 @@ describe YoutrackService do ...@@ -38,8 +38,8 @@ describe YoutrackService do
expect(described_class.reference_pattern.match('YT-123')[:issue]).to eq('YT-123') expect(described_class.reference_pattern.match('YT-123')[:issue]).to eq('YT-123')
end end
it 'does not allow issue number to be followed by a letter' do it 'allows lowercase project key on the reference' do
expect(described_class.reference_pattern.match('YT-123A')).to eq(nil) expect(described_class.reference_pattern.match('yt-123')[:issue]).to eq('yt-123')
end end
end end
......
...@@ -21,4 +21,8 @@ RSpec.shared_examples 'allows project key on reference pattern' do |url_attr| ...@@ -21,4 +21,8 @@ RSpec.shared_examples 'allows project key on reference pattern' do |url_attr|
expect(described_class.reference_pattern.match('3EXT_EXT-1234')).to eq nil expect(described_class.reference_pattern.match('3EXT_EXT-1234')).to eq nil
expect(described_class.reference_pattern.match('EXT_EXT-1234')[0]).to eq 'EXT_EXT-1234' expect(described_class.reference_pattern.match('EXT_EXT-1234')[0]).to eq 'EXT_EXT-1234'
end end
it 'does not allow issue number to finish with a letter' do
expect(described_class.reference_pattern.match('EXT-123A')).to eq(nil)
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