Commit 3e060c30 authored by Markus Koller's avatar Markus Koller

Merge branch '324165-fj-replace-wiki-find-file-call-with-gitaly' into 'master'

Replace wiki#find_file RPC with Gitaly call [RUN ALL RSPEC] [RUN AS-IF-FOSS]

See merge request gitlab-org/gitlab!56321
parents 8bc3ba7a 02d83508
...@@ -159,8 +159,17 @@ class Wiki ...@@ -159,8 +159,17 @@ class Wiki
find_page(SIDEBAR, version) find_page(SIDEBAR, version)
end end
def find_file(name, version = 'HEAD') def find_file(name, version = 'HEAD', load_content: true)
wiki.file(name, version) if Feature.enabled?(:gitaly_find_file, user, default_enabled: :yaml)
data_limit = load_content ? -1 : 0
blobs = repository.blobs_at([[version, name]], blob_size_limit: data_limit)
return if blobs.empty?
Gitlab::Git::WikiFile.from_blob(blobs.first)
else
wiki.file(name, version)
end
end end
def create_page(title, content, format = :markdown, message = nil) def create_page(title, content, format = :markdown, message = nil)
......
---
name: gitaly_find_file
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/56321
rollout_issue_url:
milestone: '13.10'
type: development
group: group::editor
default_enabled: false
...@@ -100,7 +100,7 @@ module Banzai ...@@ -100,7 +100,7 @@ module Banzai
if url?(content) if url?(content)
path = content path = content
elsif file = wiki.find_file(content) elsif file = wiki.find_file(content, load_content: false)
path = ::File.join(wiki_base_path, file.path) path = ::File.join(wiki_base_path, file.path)
end end
......
...@@ -12,6 +12,19 @@ module Gitlab ...@@ -12,6 +12,19 @@ module Gitlab
@name = gitaly_file.name @name = gitaly_file.name
@path = gitaly_file.path @path = gitaly_file.path
end end
def self.from_blob(blob)
hash = {
name: File.basename(blob.name),
mime_type: blob.mime_type,
path: blob.path,
raw_data: blob.data
}
gitaly_file = Gitlab::GitalyClient::WikiFile.new(hash)
Gitlab::Git::WikiFile.new(gitaly_file)
end
end end
end end
end end
...@@ -56,7 +56,7 @@ RSpec.describe 'GitLab Markdown Benchmark', :aggregate_failures do ...@@ -56,7 +56,7 @@ RSpec.describe 'GitLab Markdown Benchmark', :aggregate_failures do
it 'benchmarks several pipelines' do it 'benchmarks several pipelines' do
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)
allow(wiki).to receive(:find_file).with(path).and_return(Gitlab::Git::WikiFile.new(gitaly_wiki_file)) allow(wiki).to receive(:find_file).with(path, load_content: false).and_return(Gitlab::Git::WikiFile.new(gitaly_wiki_file))
allow(wiki).to receive(:wiki_base_path) { '/namespace1/gitlabhq/wikis' } allow(wiki).to receive(:wiki_base_path) { '/namespace1/gitlabhq/wikis' }
puts "\n--> Benchmarking Full, Wiki, and Plain pipelines\n" puts "\n--> Benchmarking Full, Wiki, and Plain pipelines\n"
......
...@@ -290,7 +290,7 @@ RSpec.describe 'GitLab Markdown', :aggregate_failures do ...@@ -290,7 +290,7 @@ RSpec.describe 'GitLab Markdown', :aggregate_failures do
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(@wiki).to receive(:find_file).with(path).and_return(Gitlab::Git::WikiFile.new(gitaly_wiki_file)) expect(@wiki).to receive(:find_file).with(path, load_content: false).and_return(Gitlab::Git::WikiFile.new(gitaly_wiki_file))
allow(@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, wiki: @wiki, page_slug: @wiki_page.slug }) @html = markdown(@feat.raw_markdown, { pipeline: :wiki, wiki: @wiki, page_slug: @wiki_page.slug })
......
...@@ -22,7 +22,7 @@ RSpec.describe Banzai::Filter::GollumTagsFilter do ...@@ -22,7 +22,7 @@ RSpec.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(wiki).to receive(:find_file).with('images/image.jpg').and_return(wiki_file) expect(wiki).to receive(:find_file).with('images/image.jpg', load_content: false).and_return(wiki_file)
tag = '[[images/image.jpg]]' tag = '[[images/image.jpg]]'
doc = filter("See #{tag}", wiki: wiki) doc = filter("See #{tag}", wiki: wiki)
...@@ -31,7 +31,7 @@ RSpec.describe Banzai::Filter::GollumTagsFilter do ...@@ -31,7 +31,7 @@ RSpec.describe Banzai::Filter::GollumTagsFilter do
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(wiki).to receive(:find_file).with('images/image.jpg').and_return(nil) expect(wiki).to receive(:find_file).with('images/image.jpg', load_content: false).and_return(nil)
tag = '[[images/image.jpg]]' tag = '[[images/image.jpg]]'
doc = filter("See #{tag}", wiki: wiki) doc = filter("See #{tag}", wiki: wiki)
......
...@@ -354,27 +354,47 @@ RSpec.shared_examples 'wiki model' do ...@@ -354,27 +354,47 @@ RSpec.shared_examples 'wiki model' do
subject.repository.create_file(user, 'image.png', image, branch_name: subject.default_branch, message: 'add image') subject.repository.create_file(user, 'image.png', image, branch_name: subject.default_branch, message: 'add image')
end end
it 'returns the latest version of the file if it exists' do shared_examples 'find_file results' do
file = subject.find_file('image.png') it 'returns the latest version of the file if it exists' do
file = subject.find_file('image.png')
expect(file.mime_type).to eq('image/png') expect(file.mime_type).to eq('image/png')
end end
it 'returns nil if the page does not exist' do
expect(subject.find_file('non-existent')).to eq(nil)
end
it 'returns a Gitlab::Git::WikiFile instance' do
file = subject.find_file('image.png')
expect(file).to be_a Gitlab::Git::WikiFile
end
it 'returns nil if the page does not exist' do it 'returns the whole file' do
expect(subject.find_file('non-existent')).to eq(nil) file = subject.find_file('image.png')
image.rewind
expect(file.raw_data.b).to eq(image.read.b)
end
end end
it 'returns a Gitlab::Git::WikiFile instance' do it_behaves_like 'find_file results'
file = subject.find_file('image.png')
context 'when load_content is disabled' do
it 'includes the file data in the Gitlab::Git::WikiFile' do
file = subject.find_file('image.png', load_content: false)
expect(file).to be_a Gitlab::Git::WikiFile expect(file.raw_data).to be_empty
end
end end
it 'returns the whole file' do context 'when feature flag :gitaly_find_file is disabled' do
file = subject.find_file('image.png') before do
image.rewind stub_feature_flags(gitaly_find_file: false)
end
expect(file.raw_data.b).to eq(image.read.b) it_behaves_like 'find_file results'
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