Commit 3c493c24 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add reference unfold pipeline used when moving issue

parent efd82517
...@@ -86,10 +86,13 @@ module Issues ...@@ -86,10 +86,13 @@ module Issues
:snippets, :commits, :commit_ranges] :snippets, :commits, :commit_ranges]
cross_project_mentionables.each do |type| cross_project_mentionables.each do |type|
references.public_send(type).each do |mentionable| referables = references.public_send(type)
new_content.gsub!(mentionable.to_reference,
mentionable.to_reference(@project_new)) context = { objects: referables, project: @project_new,
end pipeline: :reference_unfold }
new_content = Banzai.render_result(new_content, context)
new_content = new_content[:output].to_s
end end
new_content new_content
......
module Banzai
module Filter
##
# Filter than unfolds local references.
#
# Replaces all local references with project cross reference version
# in all objects passed to this filter in context.
#
# Requires objects array with each element implementing `Referable`.
#
class ReferenceUnfoldFilter < ReferenceFilter
def initialize(*)
super
@objects = context[:objects]
@project = context[:project]
unless @objects.all? { |object| object.respond_to?(:to_reference) }
raise StandardError, "No `to_reference` method implemented in one of the objects !"
end
unless @project.kind_of?(Project)
raise StandardError, 'No valid project passed in context!'
end
end
def call
@objects.each do |object|
pattern = /#{Regexp.escape(object.to_reference)}/
replace_text_nodes_matching(pattern) do |content|
content.gsub(pattern, object.to_reference(@project))
end
end
doc
end
private
def validate
needs :project
needs :objects
end
end
end
end
module Banzai
module Pipeline
class ReferenceUnfoldPipeline < BasePipeline
def self.filters
[Filter::ReferenceUnfoldFilter]
end
end
end
end
require 'spec_helper'
describe Banzai::Pipeline::ReferenceUnfoldPipeline do
let(:text) { 'some text' }
let(:project) { create(:project) }
let(:objects) { [] }
let(:result) do
described_class.to_html(text, project: project, objects: objects)
end
context 'invalid initializers' do
subject { -> { result } }
context 'project context is invalid' do
let(:project) { nil }
it { is_expected.to raise_error StandardError, /No valid project/ }
end
context 'objects context is invalid' do
let(:objects) { ['issue'] }
it { is_expected.to raise_error StandardError, /No `to_reference` method/ }
end
end
context 'multiple issues and merge requests referenced' do
subject { result }
let(:main_project) { create(:project) }
let(:issue_first) { create(:issue, project: main_project) }
let(:issue_second) { create(:issue, project: main_project) }
let(:merge_request) { create(:merge_request, source_project: main_project) }
let(:objects) { [issue_first, issue_second, merge_request] }
context 'plain text description' do
let(:text) { 'Description that references #1, #2 and !1' }
it { is_expected.to include issue_first.to_reference(project) }
it { is_expected.to include issue_second.to_reference(project) }
it { is_expected.to include merge_request.to_reference(project) }
end
context 'description with ignored elements' do
let(:text) do
<<-EOF
Hi. This references #1, but not `#2`
<pre>and not !1</pre>
EOF
end
it { is_expected.to include issue_first.to_reference(project) }
it { is_expected.to_not include issue_second.to_reference(project) }
it { is_expected.to_not include merge_request.to_reference(project) }
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