Commit 0cc8cee6 authored by Robert Speicher's avatar Robert Speicher

Merge branch 'fix/using-uploads-in-global-snippets' into 'master'

Fix using link to uploads in global snippets

Closes #17342, closes #17363

See merge request !4085
parents 98d8e3fe 535be93a
Please view this file on the master branch, on stable branches it's out of date. Please view this file on the master branch, on stable branches it's out of date.
v 8.8.0 (unreleased) v 8.8.0 (unreleased)
- Fix error when using link to uploads in global snippets
- Assign labels and milestone to target project when moving issue. !3934 (Long Nguyen) - Assign labels and milestone to target project when moving issue. !3934 (Long Nguyen)
- Use a case-insensitive comparison in sanitizing URI schemes - Use a case-insensitive comparison in sanitizing URI schemes
- Project#open_branches has been cleaned up and no longer loads entire records into memory. - Project#open_branches has been cleaned up and no longer loads entire records into memory.
......
...@@ -8,6 +8,8 @@ module Banzai ...@@ -8,6 +8,8 @@ module Banzai
# #
class UploadLinkFilter < HTML::Pipeline::Filter class UploadLinkFilter < HTML::Pipeline::Filter
def call def call
return doc unless project
doc.search('a').each do |el| doc.search('a').each do |el|
process_link_attr el.attribute('href') process_link_attr el.attribute('href')
end end
...@@ -31,7 +33,11 @@ module Banzai ...@@ -31,7 +33,11 @@ module Banzai
end end
def build_url(uri) def build_url(uri)
File.join(Gitlab.config.gitlab.url, context[:project].path_with_namespace, uri) File.join(Gitlab.config.gitlab.url, project.path_with_namespace, uri)
end
def project
context[:project]
end end
# Ensure that a :project key exists in context # Ensure that a :project key exists in context
......
...@@ -8,6 +8,10 @@ describe Banzai::Filter::UploadLinkFilter, lib: true do ...@@ -8,6 +8,10 @@ describe Banzai::Filter::UploadLinkFilter, lib: true do
project: project project: project
}) })
raw_filter(doc, contexts)
end
def raw_filter(doc, contexts = {})
described_class.call(doc, contexts) described_class.call(doc, contexts)
end end
...@@ -70,4 +74,18 @@ describe Banzai::Filter::UploadLinkFilter, lib: true do ...@@ -70,4 +74,18 @@ describe Banzai::Filter::UploadLinkFilter, lib: true do
expect(doc.at_css('img')['src']).to match "#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/uploads/%ED%95%9C%EA%B8%80.png" expect(doc.at_css('img')['src']).to match "#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/uploads/%ED%95%9C%EA%B8%80.png"
end end
end end
context 'when project context does not exist' do
let(:upload_link) { link('/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg') }
it 'does not raise error' do
expect { raw_filter(upload_link, project: nil) }.not_to raise_error
end
it 'does not rewrite link' do
doc = raw_filter(upload_link, project: nil)
expect(doc.to_html).to eq upload_link
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