2019-11-18 03:06:28 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-03-07 12:09:53 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe Gitlab::Gfm::ReferenceRewriter do
|
2020-04-03 18:10:03 +00:00
|
|
|
let_it_be(:group) { create(:group) }
|
|
|
|
let_it_be(:user) { create(:user) }
|
2016-03-19 17:58:52 +00:00
|
|
|
|
2020-04-03 18:10:03 +00:00
|
|
|
let(:new_project) { create(:project, name: 'new-project', group: group) }
|
|
|
|
let(:old_project) { create(:project, name: 'old-project', group: group) }
|
2020-01-29 12:09:08 +00:00
|
|
|
let(:old_project_ref) { old_project.to_reference_base(new_project) }
|
2017-09-20 09:55:54 +00:00
|
|
|
let(:text) { 'some text' }
|
2022-06-02 21:09:25 +00:00
|
|
|
let(:note) { create(:note, note: text, project: old_project) }
|
2017-09-20 09:55:54 +00:00
|
|
|
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
2017-12-22 08:18:28 +00:00
|
|
|
old_project.add_reporter(user)
|
2017-06-14 18:18:56 +00:00
|
|
|
end
|
2016-03-07 12:09:53 +00:00
|
|
|
|
2016-03-20 09:11:26 +00:00
|
|
|
describe '#rewrite' do
|
2016-03-19 17:58:52 +00:00
|
|
|
subject do
|
2022-06-02 21:09:25 +00:00
|
|
|
described_class.new(note.note, note.note_html, old_project, user).rewrite(new_project)
|
2016-03-19 17:58:52 +00:00
|
|
|
end
|
2016-03-07 12:09:53 +00:00
|
|
|
|
|
|
|
context 'multiple issues and merge requests referenced' do
|
|
|
|
let!(:issue_first) { create(:issue, project: old_project) }
|
|
|
|
let!(:issue_second) { create(:issue, project: old_project) }
|
|
|
|
let!(:merge_request) { create(:merge_request, source_project: old_project) }
|
|
|
|
|
|
|
|
context 'plain text description' do
|
|
|
|
let(:text) { 'Description that references #1, #2 and !1' }
|
|
|
|
|
|
|
|
it { is_expected.to include issue_first.to_reference(new_project) }
|
|
|
|
it { is_expected.to include issue_second.to_reference(new_project) }
|
|
|
|
it { is_expected.to include merge_request.to_reference(new_project) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'description with ignored elements' do
|
|
|
|
let(:text) do
|
2020-01-23 15:08:46 +00:00
|
|
|
"Hi. This references #1, but not `#2`\n" \
|
2016-12-15 22:14:20 +00:00
|
|
|
'<pre>and not !1</pre>'
|
2016-03-07 12:09:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to include issue_first.to_reference(new_project) }
|
2016-05-23 23:37:59 +00:00
|
|
|
it { is_expected.not_to include issue_second.to_reference(new_project) }
|
|
|
|
it { is_expected.not_to include merge_request.to_reference(new_project) }
|
2016-03-07 12:09:53 +00:00
|
|
|
end
|
|
|
|
|
2017-09-20 09:55:54 +00:00
|
|
|
context 'rewrite ambigous references' do
|
2016-03-11 10:49:04 +00:00
|
|
|
context 'url' do
|
|
|
|
let(:url) { 'http://gitlab.com/#1' }
|
|
|
|
let(:text) { "This references #1, but not #{url}" }
|
2016-03-07 12:09:53 +00:00
|
|
|
|
2016-03-11 10:49:04 +00:00
|
|
|
it { is_expected.to include url }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'code' do
|
|
|
|
let(:text) { "#1, but not `[#1]`" }
|
2019-12-12 12:07:33 +00:00
|
|
|
|
2016-03-11 10:49:04 +00:00
|
|
|
it { is_expected.to eq "#{issue_first.to_reference(new_project)}, but not `[#1]`" }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'code reverse' do
|
|
|
|
let(:text) { "not `#1`, but #1" }
|
2019-12-12 12:07:33 +00:00
|
|
|
|
2016-03-11 10:49:04 +00:00
|
|
|
it { is_expected.to eq "not `#1`, but #{issue_first.to_reference(new_project)}" }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'code in random order' do
|
|
|
|
let(:text) { "#1, `#1`, #1, `#1`" }
|
|
|
|
let(:ref) { issue_first.to_reference(new_project) }
|
|
|
|
|
|
|
|
it { is_expected.to eq "#{ref}, `#1`, #{ref}, `#1`" }
|
|
|
|
end
|
2016-03-20 09:52:01 +00:00
|
|
|
|
2016-09-29 03:21:47 +00:00
|
|
|
context 'description with project labels' do
|
2016-03-20 09:52:01 +00:00
|
|
|
let!(:label) { create(:label, id: 123, name: 'test', project: old_project) }
|
|
|
|
|
|
|
|
context 'label referenced by id' do
|
|
|
|
let(:text) { '#1 and ~123' }
|
2019-12-12 12:07:33 +00:00
|
|
|
|
2017-09-20 09:55:54 +00:00
|
|
|
it { is_expected.to eq %Q{#{old_project_ref}#1 and #{old_project_ref}~123} }
|
2016-03-20 09:52:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'label referenced by text' do
|
|
|
|
let(:text) { '#1 and ~"test"' }
|
2019-12-12 12:07:33 +00:00
|
|
|
|
2017-09-20 09:55:54 +00:00
|
|
|
it { is_expected.to eq %Q{#{old_project_ref}#1 and #{old_project_ref}~123} }
|
2016-03-20 09:52:01 +00:00
|
|
|
end
|
|
|
|
end
|
2016-09-29 03:21:47 +00:00
|
|
|
|
|
|
|
context 'description with group labels' do
|
|
|
|
let(:old_group) { create(:group) }
|
|
|
|
let!(:group_label) { create(:group_label, id: 321, name: 'group label', group: old_group) }
|
|
|
|
|
|
|
|
before do
|
2022-01-17 18:16:07 +00:00
|
|
|
old_project.update!(namespace: old_group)
|
2016-09-29 03:21:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'label referenced by id' do
|
|
|
|
let(:text) { '#1 and ~321' }
|
2019-12-12 12:07:33 +00:00
|
|
|
|
2017-09-20 09:55:54 +00:00
|
|
|
it { is_expected.to eq %Q{#{old_project_ref}#1 and #{old_project_ref}~321} }
|
2016-09-29 03:21:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'label referenced by text' do
|
|
|
|
let(:text) { '#1 and ~"group label"' }
|
2019-12-12 12:07:33 +00:00
|
|
|
|
2017-09-20 09:55:54 +00:00
|
|
|
it { is_expected.to eq %Q{#{old_project_ref}#1 and #{old_project_ref}~321} }
|
2016-09-29 03:21:47 +00:00
|
|
|
end
|
|
|
|
end
|
2016-03-11 10:49:04 +00:00
|
|
|
end
|
2017-09-20 09:55:54 +00:00
|
|
|
end
|
|
|
|
|
2020-08-11 15:10:08 +00:00
|
|
|
context 'when description contains a local reference' do
|
|
|
|
let(:local_issue) { create(:issue, project: old_project) }
|
|
|
|
let(:text) { "See ##{local_issue.iid}" }
|
|
|
|
|
|
|
|
it { is_expected.to eq("See #{old_project.path}##{local_issue.iid}") }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when description contains a cross reference' do
|
|
|
|
let(:merge_request) { create(:merge_request) }
|
|
|
|
let(:text) { "See #{merge_request.project.full_path}!#{merge_request.iid}" }
|
|
|
|
|
|
|
|
it { is_expected.to eq(text) }
|
|
|
|
end
|
|
|
|
|
2019-08-28 05:48:24 +00:00
|
|
|
context 'with a commit' do
|
|
|
|
let(:old_project) { create(:project, :repository, name: 'old-project', group: group) }
|
|
|
|
let(:commit) { old_project.commit }
|
|
|
|
|
|
|
|
context 'reference to an absolute URL to a commit' do
|
|
|
|
let(:text) { Gitlab::UrlBuilder.build(commit) }
|
|
|
|
|
|
|
|
it { is_expected.to eq(text) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'reference to a commit' do
|
|
|
|
let(:text) { commit.id }
|
|
|
|
|
|
|
|
it { is_expected.to eq("#{old_project_ref}@#{text}") }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-20 09:55:54 +00:00
|
|
|
context 'reference contains project milestone' do
|
|
|
|
let!(:milestone) do
|
|
|
|
create(:milestone, title: '9.0', project: old_project)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:text) { 'milestone: %"9.0"' }
|
|
|
|
|
|
|
|
it { is_expected.to eq %Q[milestone: #{old_project_ref}%"9.0"] }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when referring to group milestone' do
|
|
|
|
let!(:milestone) do
|
|
|
|
create(:milestone, title: '10.0', group: group)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:text) { 'milestone %"10.0"' }
|
|
|
|
|
|
|
|
it { is_expected.to eq text }
|
|
|
|
end
|
|
|
|
|
2020-04-24 00:09:28 +00:00
|
|
|
context 'when referring to a group' do
|
|
|
|
let(:text) { "group @#{group.full_path}" }
|
|
|
|
|
|
|
|
it { is_expected.to eq text }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when referring to a user' do
|
|
|
|
let(:text) { "user @#{user.full_path}" }
|
|
|
|
|
|
|
|
it { is_expected.to eq text }
|
|
|
|
end
|
|
|
|
|
2017-09-20 09:55:54 +00:00
|
|
|
context 'when referable has a nil reference' do
|
|
|
|
before do
|
|
|
|
create(:milestone, title: '9.0', project: old_project)
|
|
|
|
|
|
|
|
allow_any_instance_of(Milestone)
|
|
|
|
.to receive(:to_reference)
|
|
|
|
.and_return(nil)
|
|
|
|
end
|
2016-03-11 10:49:04 +00:00
|
|
|
|
2017-09-20 09:55:54 +00:00
|
|
|
let(:text) { 'milestone: %"9.0"' }
|
2016-03-11 10:49:04 +00:00
|
|
|
|
2017-09-20 09:55:54 +00:00
|
|
|
it 'raises an error that should be fixed' do
|
|
|
|
expect { subject }.to raise_error(
|
|
|
|
described_class::RewriteError,
|
|
|
|
'Unspecified reference detected for Milestone'
|
|
|
|
)
|
2016-03-07 12:09:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|