2013-05-30 23:16:49 +00:00
|
|
|
# Specifications for behavior common to all Mentionable implementations.
|
|
|
|
# Requires a shared context containing:
|
2015-04-16 20:25:25 +00:00
|
|
|
# - subject { "the mentionable implementation" }
|
2013-05-30 23:16:49 +00:00
|
|
|
# - let(:backref_text) { "the way that +subject+ should refer to itself in backreferences " }
|
|
|
|
# - let(:set_mentionable_text) { lambda { |txt| "block that assigns txt to the subject's mentionable_text" } }
|
|
|
|
|
|
|
|
def common_mentionable_setup
|
2015-04-16 20:26:28 +00:00
|
|
|
let(:project) { create :project }
|
|
|
|
let(:author) { subject.author }
|
|
|
|
|
|
|
|
let(:mentioned_issue) { create(:issue, project: project) }
|
2015-08-11 08:28:42 +00:00
|
|
|
let(:mentioned_mr) { create(:merge_request, :simple, source_project: project) }
|
2015-05-03 03:44:46 +00:00
|
|
|
let(:mentioned_commit) { project.commit }
|
2015-04-16 20:26:28 +00:00
|
|
|
|
|
|
|
let(:ext_proj) { create(:project, :public) }
|
|
|
|
let(:ext_issue) { create(:issue, project: ext_proj) }
|
|
|
|
let(:ext_mr) { create(:merge_request, :simple, source_project: ext_proj) }
|
2015-05-03 03:44:46 +00:00
|
|
|
let(:ext_commit) { ext_proj.commit }
|
2014-10-03 05:48:35 +00:00
|
|
|
|
2013-05-30 23:16:49 +00:00
|
|
|
# Override to add known commits to the repository stub.
|
|
|
|
let(:extra_commits) { [] }
|
|
|
|
|
|
|
|
# A string that mentions each of the +mentioned_.*+ objects above. Mentionables should add a self-reference
|
|
|
|
# to this string and place it in their +mentionable_text+.
|
|
|
|
let(:ref_string) do
|
2015-04-16 20:26:28 +00:00
|
|
|
<<-MSG.strip_heredoc
|
|
|
|
These references are new:
|
2015-05-03 03:44:46 +00:00
|
|
|
Issue: #{mentioned_issue.to_reference}
|
|
|
|
Merge: #{mentioned_mr.to_reference}
|
|
|
|
Commit: #{mentioned_commit.to_reference}
|
2015-04-16 20:26:28 +00:00
|
|
|
|
|
|
|
This reference is a repeat and should only be mentioned once:
|
2015-05-03 03:44:46 +00:00
|
|
|
Repeat: #{mentioned_issue.to_reference}
|
2015-04-16 20:26:28 +00:00
|
|
|
|
|
|
|
These references are cross-referenced:
|
2015-05-03 03:44:46 +00:00
|
|
|
Issue: #{ext_issue.to_reference(project)}
|
|
|
|
Merge: #{ext_mr.to_reference(project)}
|
|
|
|
Commit: #{ext_commit.to_reference(project)}
|
2015-04-16 20:26:28 +00:00
|
|
|
|
|
|
|
This is a self-reference and should not be mentioned at all:
|
|
|
|
Self: #{backref_text}
|
|
|
|
MSG
|
2013-05-30 23:16:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2015-04-16 20:26:28 +00:00
|
|
|
# Wire the project's repository to return the mentioned commit, and +nil+
|
|
|
|
# for any unrecognized commits.
|
|
|
|
commitmap = {
|
|
|
|
mentioned_commit.id => mentioned_commit
|
|
|
|
}
|
2014-10-10 12:39:48 +00:00
|
|
|
extra_commits.each { |c| commitmap[c.short_id] = c }
|
2015-04-16 20:26:28 +00:00
|
|
|
|
2015-04-23 16:46:52 +00:00
|
|
|
allow(project.repository).to receive(:commit) { |sha| commitmap[sha] }
|
2015-04-15 20:13:56 +00:00
|
|
|
|
2013-05-30 23:16:49 +00:00
|
|
|
set_mentionable_text.call(ref_string)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'a mentionable' do
|
|
|
|
common_mentionable_setup
|
|
|
|
|
|
|
|
it 'generates a descriptive back-reference' do
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(subject.gfm_reference).to eq(backref_text)
|
2013-05-30 23:16:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "extracts references from its reference property" do
|
|
|
|
# De-duplicate and omit itself
|
2015-04-16 20:26:28 +00:00
|
|
|
refs = subject.references(project)
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(refs.size).to eq(6)
|
|
|
|
expect(refs).to include(mentioned_issue)
|
|
|
|
expect(refs).to include(mentioned_mr)
|
|
|
|
expect(refs).to include(mentioned_commit)
|
|
|
|
expect(refs).to include(ext_issue)
|
|
|
|
expect(refs).to include(ext_mr)
|
|
|
|
expect(refs).to include(ext_commit)
|
2013-05-30 23:16:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates cross-reference notes' do
|
2014-10-03 05:48:35 +00:00
|
|
|
mentioned_objects = [mentioned_issue, mentioned_mr, mentioned_commit,
|
|
|
|
ext_issue, ext_mr, ext_commit]
|
|
|
|
|
|
|
|
mentioned_objects.each do |referenced|
|
2015-06-18 00:59:19 +00:00
|
|
|
expect(SystemNoteService).to receive(:cross_reference).
|
2015-04-21 13:19:23 +00:00
|
|
|
with(referenced, subject.local_reference, author)
|
2013-05-30 23:16:49 +00:00
|
|
|
end
|
|
|
|
|
2015-04-16 20:26:28 +00:00
|
|
|
subject.create_cross_references!(project, author)
|
2013-05-30 23:16:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'detects existing cross-references' do
|
2015-06-18 00:59:19 +00:00
|
|
|
SystemNoteService.cross_reference(mentioned_issue, subject.local_reference, author)
|
2013-05-30 23:16:49 +00:00
|
|
|
|
2015-04-16 20:34:01 +00:00
|
|
|
expect(subject).to have_mentioned(mentioned_issue)
|
|
|
|
expect(subject).not_to have_mentioned(mentioned_mr)
|
2013-05-30 23:16:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'an editable mentionable' do
|
|
|
|
common_mentionable_setup
|
|
|
|
|
|
|
|
it_behaves_like 'a mentionable'
|
|
|
|
|
2015-04-16 20:26:28 +00:00
|
|
|
let(:new_issues) do
|
|
|
|
[create(:issue, project: project), create(:issue, project: ext_proj)]
|
|
|
|
end
|
|
|
|
|
2013-05-30 23:16:49 +00:00
|
|
|
it 'creates new cross-reference notes when the mentionable text is edited' do
|
2015-04-23 16:46:52 +00:00
|
|
|
subject.save
|
|
|
|
|
2015-06-02 13:00:51 +00:00
|
|
|
new_text = <<-MSG.strip_heredoc
|
2015-04-16 20:26:28 +00:00
|
|
|
These references already existed:
|
2015-06-02 13:00:51 +00:00
|
|
|
|
|
|
|
Issue: #{mentioned_issue.to_reference}
|
|
|
|
|
|
|
|
Commit: #{mentioned_commit.to_reference}
|
|
|
|
|
|
|
|
---
|
2015-04-16 20:26:28 +00:00
|
|
|
|
|
|
|
This cross-project reference already existed:
|
2015-06-02 13:00:51 +00:00
|
|
|
|
|
|
|
Issue: #{ext_issue.to_reference(project)}
|
|
|
|
|
|
|
|
---
|
2015-04-16 20:26:28 +00:00
|
|
|
|
|
|
|
These two references are introduced in an edit:
|
2015-06-02 13:00:51 +00:00
|
|
|
|
|
|
|
Issue: #{new_issues[0].to_reference}
|
|
|
|
|
|
|
|
Cross: #{new_issues[1].to_reference(project)}
|
2015-04-16 20:26:28 +00:00
|
|
|
MSG
|
2013-05-30 23:16:49 +00:00
|
|
|
|
2015-04-16 20:26:28 +00:00
|
|
|
# These three objects were already referenced, and should not receive new
|
|
|
|
# notes
|
2014-10-03 05:48:35 +00:00
|
|
|
[mentioned_issue, mentioned_commit, ext_issue].each do |oldref|
|
2015-06-18 00:59:19 +00:00
|
|
|
expect(SystemNoteService).not_to receive(:cross_reference).
|
2015-04-16 20:26:28 +00:00
|
|
|
with(oldref, any_args)
|
2013-05-30 23:16:49 +00:00
|
|
|
end
|
|
|
|
|
2015-04-16 20:26:28 +00:00
|
|
|
# These two issues are new and should receive reference notes
|
|
|
|
new_issues.each do |newref|
|
2015-06-18 00:59:19 +00:00
|
|
|
expect(SystemNoteService).to receive(:cross_reference).
|
2015-04-21 13:19:23 +00:00
|
|
|
with(newref, subject.local_reference, author)
|
2014-10-03 05:48:35 +00:00
|
|
|
end
|
2013-05-30 23:16:49 +00:00
|
|
|
|
|
|
|
set_mentionable_text.call(new_text)
|
2015-07-13 22:33:50 +00:00
|
|
|
subject.create_new_cross_references!(project, author)
|
2013-05-30 23:16:49 +00:00
|
|
|
end
|
|
|
|
end
|