Commit 6eb31056 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Find referable for each ref found in references rewriter

parent 323d328c
...@@ -49,7 +49,7 @@ module Gitlab ...@@ -49,7 +49,7 @@ module Gitlab
def unfold_reference(reference, match, target_project) def unfold_reference(reference, match, target_project)
before = @text[0...match.begin(0)] before = @text[0...match.begin(0)]
after = @text[match.end(0)..-1] after = @text[match.end(0)..-1]
referable = find_local_referable(reference) referable = find_referable(reference)
return reference unless referable return reference unless referable
cross_reference = referable.to_reference(target_project) cross_reference = referable.to_reference(target_project)
...@@ -58,17 +58,11 @@ module Gitlab ...@@ -58,17 +58,11 @@ module Gitlab
substitution_valid?(new_text) ? cross_reference : reference substitution_valid?(new_text) ? cross_reference : reference
end end
def referables def find_referable(reference)
return @referables if @referables
extractor = Gitlab::ReferenceExtractor.new(@source_project, extractor = Gitlab::ReferenceExtractor.new(@source_project,
@current_user) @current_user)
extractor.analyze(@text) extractor.analyze(reference)
@referables = extractor.all extractor.all.first
end
def find_local_referable(reference)
referables.find { |ref| ref.to_reference == reference }
end end
def substitution_valid?(substituted) def substitution_valid?(substituted)
......
...@@ -61,6 +61,21 @@ describe Gitlab::Gfm::ReferenceRewriter do ...@@ -61,6 +61,21 @@ describe Gitlab::Gfm::ReferenceRewriter do
it { is_expected.to eq "#{ref}, `#1`, #{ref}, `#1`" } it { is_expected.to eq "#{ref}, `#1`, #{ref}, `#1`" }
end end
context 'description with labels' do
let!(:label) { create(:label, id: 123, name: 'test', project: old_project) }
let(:project_ref) { old_project.to_reference }
context 'label referenced by id' do
let(:text) { '#1 and ~123' }
it { is_expected.to eq %Q{#{project_ref}#1 and #{project_ref}~123} }
end
context 'label referenced by text' do
let(:text) { '#1 and ~"test"' }
it { is_expected.to eq %Q{#{project_ref}#1 and #{project_ref}~123} }
end
end
end end
context 'reference contains milestone' do context 'reference contains milestone' do
......
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