Commit b9c25013 authored by Markus Koller's avatar Markus Koller

Use wiki instead of project_wiki in Banzai

This is in preparation for group wikis, we need to remove the coupling
between wikis and projects.

In this step we only change the name inside the Banzai filters and
pipelines, once we port the controllers we can also switch the
controller instance variable from `@project_wiki` to `@wiki`.
parent e437426b
...@@ -32,7 +32,7 @@ module PreviewMarkdown ...@@ -32,7 +32,7 @@ module PreviewMarkdown
def markdown_context_params def markdown_context_params
case controller_name case controller_name
when 'wikis' then { pipeline: :wiki, project_wiki: @project_wiki, page_slug: params[:id] } when 'wikis' then { pipeline: :wiki, wiki: @project_wiki, page_slug: params[:id] }
when 'snippets' then { skip_project_check: true } when 'snippets' then { skip_project_check: true }
when 'groups' then { group: group } when 'groups' then { group: group }
when 'projects' then projects_filter_params when 'projects' then projects_filter_params
......
...@@ -129,7 +129,7 @@ module MarkupHelper ...@@ -129,7 +129,7 @@ module MarkupHelper
context.merge!( context.merge!(
pipeline: :wiki, pipeline: :wiki,
project: @project, project: @project,
project_wiki: @project_wiki, wiki: @project_wiki,
repository: @project_wiki.repository, repository: @project_wiki.repository,
page_slug: wiki_page.slug, page_slug: wiki_page.slug,
issuable_state_filter_enabled: true issuable_state_filter_enabled: true
......
...@@ -30,7 +30,7 @@ module Banzai ...@@ -30,7 +30,7 @@ module Banzai
# Note: the table of contents tag is now handled by TableOfContentsTagFilter # Note: the table of contents tag is now handled by TableOfContentsTagFilter
# #
# Context options: # Context options:
# :project_wiki (required) - Current project wiki. # :wiki [Wiki] (required) - Current wiki instance.
# #
class GollumTagsFilter < HTML::Pipeline::Filter class GollumTagsFilter < HTML::Pipeline::Filter
include ActionView::Helpers::TagHelper include ActionView::Helpers::TagHelper
...@@ -100,8 +100,8 @@ module Banzai ...@@ -100,8 +100,8 @@ module Banzai
if url?(content) if url?(content)
path = content path = content
elsif file = project_wiki.find_file(content) elsif file = wiki.find_file(content)
path = ::File.join project_wiki_base_path, file.path path = ::File.join(wiki_base_path, file.path)
end end
if path if path
...@@ -134,25 +134,25 @@ module Banzai ...@@ -134,25 +134,25 @@ module Banzai
if url?(reference) if url?(reference)
reference reference
else else
::File.join(project_wiki_base_path, reference) ::File.join(wiki_base_path, reference)
end end
content_tag(:a, name || reference, href: href, class: 'gfm') content_tag(:a, name || reference, href: href, class: 'gfm')
end end
def project_wiki def wiki
context[:project_wiki] context[:wiki]
end end
def project_wiki_base_path def wiki_base_path
project_wiki && project_wiki.wiki_base_path wiki&.wiki_base_path
end end
# Ensure that a :project_wiki key exists in context # Ensure that a :wiki key exists in context
# #
# Note that while the key might exist, its value could be nil! # Note that while the key might exist, its value could be nil!
def validate def validate
needs :project_wiki needs :wiki
end end
end end
end end
......
...@@ -10,7 +10,7 @@ module Banzai ...@@ -10,7 +10,7 @@ module Banzai
# :commit # :commit
# :current_user # :current_user
# :project # :project
# :project_wiki # :wiki
# :ref # :ref
# :requested_path # :requested_path
# :system_note # :system_note
...@@ -53,7 +53,7 @@ module Banzai ...@@ -53,7 +53,7 @@ module Banzai
def linkable_files? def linkable_files?
strong_memoize(:linkable_files) do strong_memoize(:linkable_files) do
context[:project_wiki].nil? && repository.try(:exists?) && !repository.empty? context[:wiki].nil? && repository.try(:exists?) && !repository.empty?
end end
end end
......
...@@ -6,12 +6,12 @@ module Banzai ...@@ -6,12 +6,12 @@ module Banzai
# Rewrite rules are documented in the `WikiPipeline` spec. # Rewrite rules are documented in the `WikiPipeline` spec.
# #
# Context options: # Context options:
# :project_wiki # :wiki
class WikiLinkFilter < HTML::Pipeline::Filter class WikiLinkFilter < HTML::Pipeline::Filter
include Gitlab::Utils::SanitizeNodeLink include Gitlab::Utils::SanitizeNodeLink
def call def call
return doc unless project_wiki? return doc unless wiki?
doc.search('a:not(.gfm)').each { |el| process_link(el.attribute('href'), el) } doc.search('a:not(.gfm)').each { |el| process_link(el.attribute('href'), el) }
...@@ -33,8 +33,8 @@ module Banzai ...@@ -33,8 +33,8 @@ module Banzai
remove_unsafe_links({ node: node }, remove_invalid_links: false) remove_unsafe_links({ node: node }, remove_invalid_links: false)
end end
def project_wiki? def wiki?
!context[:project_wiki].nil? !context[:wiki].nil?
end end
def process_link_attr(html_attr) def process_link_attr(html_attr)
...@@ -46,7 +46,7 @@ module Banzai ...@@ -46,7 +46,7 @@ module Banzai
end end
def apply_rewrite_rules(link_string) def apply_rewrite_rules(link_string)
Rewriter.new(link_string, wiki: context[:project_wiki], slug: context[:page_slug]).apply_rules Rewriter.new(link_string, wiki: context[:wiki], slug: context[:page_slug]).apply_rules
end end
end end
end end
......
...@@ -157,7 +157,7 @@ describe 'Copy as GFM', :js do ...@@ -157,7 +157,7 @@ describe 'Copy as GFM', :js do
GFM GFM
pipeline: :wiki, pipeline: :wiki,
project_wiki: @project.wiki wiki: @project.wiki
) )
verify( verify(
......
...@@ -269,15 +269,15 @@ describe 'GitLab Markdown', :aggregate_failures do ...@@ -269,15 +269,15 @@ describe 'GitLab Markdown', :aggregate_failures do
context 'wiki pipeline' do context 'wiki pipeline' do
before do before do
@project_wiki = @feat.project_wiki @wiki = @feat.wiki
@project_wiki_page = @feat.project_wiki_page @wiki_page = @feat.wiki_page
path = 'images/example.jpg' path = 'images/example.jpg'
gitaly_wiki_file = Gitlab::GitalyClient::WikiFile.new(path: path) gitaly_wiki_file = Gitlab::GitalyClient::WikiFile.new(path: path)
expect(@project_wiki).to receive(:find_file).with(path).and_return(Gitlab::Git::WikiFile.new(gitaly_wiki_file)) expect(@wiki).to receive(:find_file).with(path).and_return(Gitlab::Git::WikiFile.new(gitaly_wiki_file))
allow(@project_wiki).to receive(:wiki_base_path) { '/namespace1/gitlabhq/wikis' } allow(@wiki).to receive(:wiki_base_path) { '/namespace1/gitlabhq/wikis' }
@html = markdown(@feat.raw_markdown, { pipeline: :wiki, project_wiki: @project_wiki, page_slug: @project_wiki_page.slug }) @html = markdown(@feat.raw_markdown, { pipeline: :wiki, wiki: @wiki, page_slug: @wiki_page.slug })
end end
it_behaves_like 'all pipelines' it_behaves_like 'all pipelines'
......
...@@ -317,7 +317,7 @@ describe MarkupHelper do ...@@ -317,7 +317,7 @@ describe MarkupHelper do
let(:wiki_repository) { double('Repository') } let(:wiki_repository) { double('Repository') }
let(:context) do let(:context) do
{ {
pipeline: :wiki, project: project, project_wiki: wiki, pipeline: :wiki, project: project, wiki: wiki,
page_slug: 'nested/page', issuable_state_filter_enabled: true, page_slug: 'nested/page', issuable_state_filter_enabled: true,
repository: wiki_repository repository: wiki_repository
} }
......
...@@ -7,11 +7,11 @@ describe Banzai::Filter::GollumTagsFilter do ...@@ -7,11 +7,11 @@ describe Banzai::Filter::GollumTagsFilter do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:user) { double } let(:user) { double }
let(:project_wiki) { ProjectWiki.new(project, user) } let(:wiki) { ProjectWiki.new(project, user) }
describe 'validation' do describe 'validation' do
it 'ensure that a :project_wiki key exists in context' do it 'ensure that a :wiki key exists in context' do
expect { filter("See [[images/image.jpg]]", {}) }.to raise_error ArgumentError, "Missing context keys for Banzai::Filter::GollumTagsFilter: :project_wiki" expect { filter("See [[images/image.jpg]]", {}) }.to raise_error ArgumentError, "Missing context keys for Banzai::Filter::GollumTagsFilter: :wiki"
end end
end end
...@@ -23,19 +23,19 @@ describe Banzai::Filter::GollumTagsFilter do ...@@ -23,19 +23,19 @@ describe Banzai::Filter::GollumTagsFilter do
path: 'images/image.jpg', path: 'images/image.jpg',
raw_data: '') raw_data: '')
wiki_file = Gitlab::Git::WikiFile.new(gollum_file_double) wiki_file = Gitlab::Git::WikiFile.new(gollum_file_double)
expect(project_wiki).to receive(:find_file).with('images/image.jpg').and_return(wiki_file) expect(wiki).to receive(:find_file).with('images/image.jpg').and_return(wiki_file)
tag = '[[images/image.jpg]]' tag = '[[images/image.jpg]]'
doc = filter("See #{tag}", project_wiki: project_wiki) doc = filter("See #{tag}", wiki: wiki)
expect(doc.at_css('img')['data-src']).to eq "#{project_wiki.wiki_base_path}/images/image.jpg" expect(doc.at_css('img')['data-src']).to eq "#{wiki.wiki_base_path}/images/image.jpg"
end end
it 'does not creates img tag if image does not exist' do it 'does not creates img tag if image does not exist' do
expect(project_wiki).to receive(:find_file).with('images/image.jpg').and_return(nil) expect(wiki).to receive(:find_file).with('images/image.jpg').and_return(nil)
tag = '[[images/image.jpg]]' tag = '[[images/image.jpg]]'
doc = filter("See #{tag}", project_wiki: project_wiki) doc = filter("See #{tag}", wiki: wiki)
expect(doc.css('img').size).to eq 0 expect(doc.css('img').size).to eq 0
end end
...@@ -44,14 +44,14 @@ describe Banzai::Filter::GollumTagsFilter do ...@@ -44,14 +44,14 @@ describe Banzai::Filter::GollumTagsFilter do
context 'linking external images' do context 'linking external images' do
it 'creates img tag for valid URL' do it 'creates img tag for valid URL' do
tag = '[[http://example.com/image.jpg]]' tag = '[[http://example.com/image.jpg]]'
doc = filter("See #{tag}", project_wiki: project_wiki) doc = filter("See #{tag}", wiki: wiki)
expect(doc.at_css('img')['data-src']).to eq "http://example.com/image.jpg" expect(doc.at_css('img')['data-src']).to eq "http://example.com/image.jpg"
end end
it 'does not creates img tag for invalid URL' do it 'does not creates img tag for invalid URL' do
tag = '[[http://example.com/image.pdf]]' tag = '[[http://example.com/image.pdf]]'
doc = filter("See #{tag}", project_wiki: project_wiki) doc = filter("See #{tag}", wiki: wiki)
expect(doc.css('img').size).to eq 0 expect(doc.css('img').size).to eq 0
end end
...@@ -60,7 +60,7 @@ describe Banzai::Filter::GollumTagsFilter do ...@@ -60,7 +60,7 @@ describe Banzai::Filter::GollumTagsFilter do
context 'linking external resources' do context 'linking external resources' do
it "the created link's text will be equal to the resource's text" do it "the created link's text will be equal to the resource's text" do
tag = '[[http://example.com]]' tag = '[[http://example.com]]'
doc = filter("See #{tag}", project_wiki: project_wiki) doc = filter("See #{tag}", wiki: wiki)
expect(doc.at_css('a').text).to eq 'http://example.com' expect(doc.at_css('a').text).to eq 'http://example.com'
expect(doc.at_css('a')['href']).to eq 'http://example.com' expect(doc.at_css('a')['href']).to eq 'http://example.com'
...@@ -68,7 +68,7 @@ describe Banzai::Filter::GollumTagsFilter do ...@@ -68,7 +68,7 @@ describe Banzai::Filter::GollumTagsFilter do
it "the created link's text will be link-text" do it "the created link's text will be link-text" do
tag = '[[link-text|http://example.com/pdfs/gollum.pdf]]' tag = '[[link-text|http://example.com/pdfs/gollum.pdf]]'
doc = filter("See #{tag}", project_wiki: project_wiki) doc = filter("See #{tag}", wiki: wiki)
expect(doc.at_css('a').text).to eq 'link-text' expect(doc.at_css('a').text).to eq 'link-text'
expect(doc.at_css('a')['href']).to eq 'http://example.com/pdfs/gollum.pdf' expect(doc.at_css('a')['href']).to eq 'http://example.com/pdfs/gollum.pdf'
...@@ -78,8 +78,8 @@ describe Banzai::Filter::GollumTagsFilter do ...@@ -78,8 +78,8 @@ describe Banzai::Filter::GollumTagsFilter do
context 'linking internal resources' do context 'linking internal resources' do
it "the created link's text includes the resource's text and wiki base path" do it "the created link's text includes the resource's text and wiki base path" do
tag = '[[wiki-slug]]' tag = '[[wiki-slug]]'
doc = filter("See #{tag}", project_wiki: project_wiki) doc = filter("See #{tag}", wiki: wiki)
expected_path = ::File.join(project_wiki.wiki_base_path, 'wiki-slug') expected_path = ::File.join(wiki.wiki_base_path, 'wiki-slug')
expect(doc.at_css('a').text).to eq 'wiki-slug' expect(doc.at_css('a').text).to eq 'wiki-slug'
expect(doc.at_css('a')['href']).to eq expected_path expect(doc.at_css('a')['href']).to eq expected_path
...@@ -87,15 +87,15 @@ describe Banzai::Filter::GollumTagsFilter do ...@@ -87,15 +87,15 @@ describe Banzai::Filter::GollumTagsFilter do
it "the created link's text will be link-text" do it "the created link's text will be link-text" do
tag = '[[link-text|wiki-slug]]' tag = '[[link-text|wiki-slug]]'
doc = filter("See #{tag}", project_wiki: project_wiki) doc = filter("See #{tag}", wiki: wiki)
expected_path = ::File.join(project_wiki.wiki_base_path, 'wiki-slug') expected_path = ::File.join(wiki.wiki_base_path, 'wiki-slug')
expect(doc.at_css('a').text).to eq 'link-text' expect(doc.at_css('a').text).to eq 'link-text'
expect(doc.at_css('a')['href']).to eq expected_path expect(doc.at_css('a')['href']).to eq expected_path
end end
it "inside back ticks will be exempt from linkification" do it "inside back ticks will be exempt from linkification" do
doc = filter('<code>[[link-in-backticks]]</code>', project_wiki: project_wiki) doc = filter('<code>[[link-in-backticks]]</code>', wiki: wiki)
expect(doc.at_css('code').text).to eq '[[link-in-backticks]]' expect(doc.at_css('code').text).to eq '[[link-in-backticks]]'
end end
......
...@@ -12,7 +12,7 @@ describe Banzai::Filter::RepositoryLinkFilter do ...@@ -12,7 +12,7 @@ describe Banzai::Filter::RepositoryLinkFilter do
project: project, project: project,
current_user: user, current_user: user,
group: group, group: group,
project_wiki: project_wiki, wiki: wiki,
ref: ref, ref: ref,
requested_path: requested_path, requested_path: requested_path,
only_path: only_path only_path: only_path
...@@ -53,7 +53,7 @@ describe Banzai::Filter::RepositoryLinkFilter do ...@@ -53,7 +53,7 @@ describe Banzai::Filter::RepositoryLinkFilter do
let(:project_path) { project.full_path } let(:project_path) { project.full_path }
let(:ref) { 'markdown' } let(:ref) { 'markdown' }
let(:commit) { project.commit(ref) } let(:commit) { project.commit(ref) }
let(:project_wiki) { nil } let(:wiki) { nil }
let(:requested_path) { '/' } let(:requested_path) { '/' }
let(:only_path) { true } let(:only_path) { true }
...@@ -94,8 +94,8 @@ describe Banzai::Filter::RepositoryLinkFilter do ...@@ -94,8 +94,8 @@ describe Banzai::Filter::RepositoryLinkFilter do
end end
end end
context 'with a project_wiki' do context 'with a wiki' do
let(:project_wiki) { double('ProjectWiki') } let(:wiki) { double('ProjectWiki') }
include_examples :preserve_unchanged include_examples :preserve_unchanged
end end
......
...@@ -12,13 +12,13 @@ describe Banzai::Filter::WikiLinkFilter do ...@@ -12,13 +12,13 @@ describe Banzai::Filter::WikiLinkFilter do
let(:repository_upload_folder) { Wikis::CreateAttachmentService::ATTACHMENT_PATH } let(:repository_upload_folder) { Wikis::CreateAttachmentService::ATTACHMENT_PATH }
it "doesn't rewrite absolute links" do it "doesn't rewrite absolute links" do
filtered_link = filter("<a href='http://example.com:8000/'>Link</a>", project_wiki: wiki).children[0] filtered_link = filter("<a href='http://example.com:8000/'>Link</a>", wiki: wiki).children[0]
expect(filtered_link.attribute('href').value).to eq('http://example.com:8000/') expect(filtered_link.attribute('href').value).to eq('http://example.com:8000/')
end end
it "doesn't rewrite links to project uploads" do it "doesn't rewrite links to project uploads" do
filtered_link = filter("<a href='/uploads/a.test'>Link</a>", project_wiki: wiki).children[0] filtered_link = filter("<a href='/uploads/a.test'>Link</a>", wiki: wiki).children[0]
expect(filtered_link.attribute('href').value).to eq('/uploads/a.test') expect(filtered_link.attribute('href').value).to eq('/uploads/a.test')
end end
...@@ -26,7 +26,7 @@ describe Banzai::Filter::WikiLinkFilter do ...@@ -26,7 +26,7 @@ describe Banzai::Filter::WikiLinkFilter do
describe "when links point to the #{Wikis::CreateAttachmentService::ATTACHMENT_PATH} folder" do describe "when links point to the #{Wikis::CreateAttachmentService::ATTACHMENT_PATH} folder" do
context 'with an "a" html tag' do context 'with an "a" html tag' do
it 'rewrites links' do it 'rewrites links' do
filtered_link = filter("<a href='#{repository_upload_folder}/a.test'>Link</a>", project_wiki: wiki).children[0] filtered_link = filter("<a href='#{repository_upload_folder}/a.test'>Link</a>", wiki: wiki).children[0]
expect(filtered_link.attribute('href').value).to eq("#{wiki.wiki_base_path}/#{repository_upload_folder}/a.test") expect(filtered_link.attribute('href').value).to eq("#{wiki.wiki_base_path}/#{repository_upload_folder}/a.test")
end end
...@@ -37,7 +37,7 @@ describe Banzai::Filter::WikiLinkFilter do ...@@ -37,7 +37,7 @@ describe Banzai::Filter::WikiLinkFilter do
context 'inside an "a" html tag' do context 'inside an "a" html tag' do
it 'rewrites links' do it 'rewrites links' do
filtered_elements = filter("<a href='#{repository_upload_folder}/a.jpg'><img src='#{repository_upload_folder}/a.jpg'>example</img></a>", project_wiki: wiki) filtered_elements = filter("<a href='#{repository_upload_folder}/a.jpg'><img src='#{repository_upload_folder}/a.jpg'>example</img></a>", wiki: wiki)
expect(filtered_elements.search('img').first.attribute('src').value).to eq(path) expect(filtered_elements.search('img').first.attribute('src').value).to eq(path)
expect(filtered_elements.search('a').first.attribute('href').value).to eq(path) expect(filtered_elements.search('a').first.attribute('href').value).to eq(path)
...@@ -46,7 +46,7 @@ describe Banzai::Filter::WikiLinkFilter do ...@@ -46,7 +46,7 @@ describe Banzai::Filter::WikiLinkFilter do
context 'outside an "a" html tag' do context 'outside an "a" html tag' do
it 'rewrites links' do it 'rewrites links' do
filtered_link = filter("<img src='#{repository_upload_folder}/a.jpg'>example</img>", project_wiki: wiki).children[0] filtered_link = filter("<img src='#{repository_upload_folder}/a.jpg'>example</img>", wiki: wiki).children[0]
expect(filtered_link.attribute('src').value).to eq(path) expect(filtered_link.attribute('src').value).to eq(path)
end end
...@@ -55,7 +55,7 @@ describe Banzai::Filter::WikiLinkFilter do ...@@ -55,7 +55,7 @@ describe Banzai::Filter::WikiLinkFilter do
context 'with "video" html tag' do context 'with "video" html tag' do
it 'rewrites links' do it 'rewrites links' do
filtered_link = filter("<video src='#{repository_upload_folder}/a.mp4'></video>", project_wiki: wiki).children[0] filtered_link = filter("<video src='#{repository_upload_folder}/a.mp4'></video>", wiki: wiki).children[0]
expect(filtered_link.attribute('src').value).to eq("#{wiki.wiki_base_path}/#{repository_upload_folder}/a.mp4") expect(filtered_link.attribute('src').value).to eq("#{wiki.wiki_base_path}/#{repository_upload_folder}/a.mp4")
end end
...@@ -63,7 +63,7 @@ describe Banzai::Filter::WikiLinkFilter do ...@@ -63,7 +63,7 @@ describe Banzai::Filter::WikiLinkFilter do
context 'with "audio" html tag' do context 'with "audio" html tag' do
it 'rewrites links' do it 'rewrites links' do
filtered_link = filter("<audio src='#{repository_upload_folder}/a.wav'></audio>", project_wiki: wiki).children[0] filtered_link = filter("<audio src='#{repository_upload_folder}/a.wav'></audio>", wiki: wiki).children[0]
expect(filtered_link.attribute('src').value).to eq("#{wiki.wiki_base_path}/#{repository_upload_folder}/a.wav") expect(filtered_link.attribute('src').value).to eq("#{wiki.wiki_base_path}/#{repository_upload_folder}/a.wav")
end end
...@@ -75,7 +75,7 @@ describe Banzai::Filter::WikiLinkFilter do ...@@ -75,7 +75,7 @@ describe Banzai::Filter::WikiLinkFilter do
invalid_links.each do |invalid_link| invalid_links.each do |invalid_link|
it "doesn't rewrite invalid invalid_links like #{invalid_link}" do it "doesn't rewrite invalid invalid_links like #{invalid_link}" do
filtered_link = filter("<a href='#{invalid_link}'>Link</a>", project_wiki: wiki).children[0] filtered_link = filter("<a href='#{invalid_link}'>Link</a>", wiki: wiki).children[0]
expect(filtered_link.attribute('href').value).to eq(invalid_link) expect(filtered_link.attribute('href').value).to eq(invalid_link)
end end
......
...@@ -36,12 +36,12 @@ class MarkdownFeature ...@@ -36,12 +36,12 @@ class MarkdownFeature
end end
end end
def project_wiki def wiki
@project_wiki ||= ProjectWiki.new(project, user) @wiki ||= ProjectWiki.new(project, user)
end end
def project_wiki_page def wiki_page
@project_wiki_page ||= build(:wiki_page, wiki: project_wiki) @wiki_page ||= build(:wiki_page, wiki: wiki)
end end
def issue def issue
......
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