Commit f2674c7b authored by Grzegorz Bizon's avatar Grzegorz Bizon

Refactor uploads rewriter used when moving issue

parent 0b8cefd3
...@@ -62,26 +62,15 @@ module Issues ...@@ -62,26 +62,15 @@ module Issues
end end
def rewrite_content(content) def rewrite_content(content)
rewrite_uploads(
unfold_references(content)
)
end
def unfold_references(content)
return unless content return unless content
rewriter = Gitlab::Gfm::ReferenceRewriter.new(content, @old_project, rewriters = [Gitlab::Gfm::ReferenceRewriter,
@current_user) Gitlab::Gfm::UploadsRewriter]
rewriter.rewrite(@new_project)
end
def rewrite_uploads(content)
return unless content
rewriter = Gitlab::Gfm::UploadsRewriter.new(content, @old_project, rewriters.inject(content) do |text, klass|
@current_user) rewriter = klass.new(text, @old_project, @current_user)
return content unless rewriter.has_uploads? rewriter.needs_rewrite? ? rewriter.rewrite(@new_project) : text
rewriter.rewrite(@new_project) end
end end
def close_issue def close_issue
......
...@@ -34,16 +34,19 @@ module Gitlab ...@@ -34,16 +34,19 @@ module Gitlab
@source_project = source_project @source_project = source_project
@current_user = current_user @current_user = current_user
@original_html = markdown(text) @original_html = markdown(text)
@pattern = Gitlab::ReferenceExtractor.references_pattern
end end
def rewrite(target_project) def rewrite(target_project)
pattern = Gitlab::ReferenceExtractor.references_pattern @text.gsub(@pattern) do |reference|
@text.gsub(pattern) do |reference|
unfold_reference(reference, Regexp.last_match, target_project) unfold_reference(reference, Regexp.last_match, target_project)
end end
end end
def needs_rewrite?
!(@text =~ @pattern).nil?
end
private private
def unfold_reference(reference, match, target_project) def unfold_reference(reference, match, target_project)
......
...@@ -3,8 +3,9 @@ module Gitlab ...@@ -3,8 +3,9 @@ module Gitlab
## ##
# Class that rewrites markdown links for uploads # Class that rewrites markdown links for uploads
# #
# Using a pattern defined in `FileUploader` copies files to a new project # Using a pattern defined in `FileUploader` it copies files to a new
# and rewrites all links to uploads in ain a given text. # project and rewrites all links to uploads in in a given text.
#
# #
class UploadsRewriter class UploadsRewriter
def initialize(text, source_project, _current_user) def initialize(text, source_project, _current_user)
...@@ -17,17 +18,17 @@ module Gitlab ...@@ -17,17 +18,17 @@ module Gitlab
return unless @text return unless @text
new_uploader = file_uploader(target_project) new_uploader = file_uploader(target_project)
@text.gsub(@pattern) do |markdown_link| @text.gsub(@pattern) do |markdown|
old_file = find_file(@source_project, $~[:secret], $~[:file]) file = find_file(@source_project, $~[:secret], $~[:file])
return markdown_link unless old_file.exists? return markdown unless file.try(:exists?)
new_uploader.store!(old_file) new_uploader.store!(file)
new_uploader.to_h[:markdown] new_uploader.to_h[:markdown]
end end
end end
def has_uploads? def needs_rewrite?
!(@text =~ @pattern).nil? files.any?
end end
def files def files
...@@ -46,8 +47,8 @@ module Gitlab ...@@ -46,8 +47,8 @@ module Gitlab
uploader.file uploader.file
end end
def file_uploader(*args) def file_uploader(project, secret = nil)
uploader = FileUploader.new(*args) uploader = FileUploader.new(project, secret)
uploader.define_singleton_method(:move_to_store) { false } uploader.define_singleton_method(:move_to_store) { false }
uploader uploader
end end
......
...@@ -33,8 +33,8 @@ describe Gitlab::Gfm::UploadsRewriter do ...@@ -33,8 +33,8 @@ describe Gitlab::Gfm::UploadsRewriter do
end end
end end
describe '#has_uploads?' do describe '#needs_rewrite?' do
subject { rewriter.has_uploads? } subject { rewriter.needs_rewrite? }
it { is_expected.to eq true } it { is_expected.to eq true }
end end
......
...@@ -162,12 +162,9 @@ describe Issues::MoveService, services: true do ...@@ -162,12 +162,9 @@ describe Issues::MoveService, services: true do
end end
context 'issue description with uploads' do context 'issue description with uploads' do
let(:path) { Rails.root + 'spec/fixtures/rails_sample.jpg' } let(:uploader) { build(:file_uploader, project: old_project) }
let(:file) { fixture_file_upload(path, 'image/jpg') }
let(:uploader) { FileUploader.new(old_project) }
let!(:store) { uploader.store!(file) }
let(:markdown) { uploader.to_h[:markdown] } let(:markdown) { uploader.to_h[:markdown] }
let(:description) { "Text and #{markdown}"} let(:description) { "Text and #{markdown}" }
include_context 'issue move executed' include_context 'issue move executed'
...@@ -176,8 +173,6 @@ describe Issues::MoveService, services: true do ...@@ -176,8 +173,6 @@ describe Issues::MoveService, services: true do
expect(new_issue.description) expect(new_issue.description)
.to match(/Text and #{FileUploader::MARKDOWN_PATTERN}/) .to match(/Text and #{FileUploader::MARKDOWN_PATTERN}/)
end end
after { uploader.remove! }
end 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