Replace wiki#find_file RPC with Gitaly call

In this commit we introduce a feature flag that will toggle
using the RPC or the new Gitaly method.
parent a33dd1ce
......@@ -159,8 +159,17 @@ class Wiki
find_page(SIDEBAR, version)
end
def find_file(name, version = 'HEAD')
wiki.file(name, version)
def find_file(name, version = 'HEAD', load_content: true)
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
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
if url?(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)
end
......
......@@ -12,6 +12,19 @@ module Gitlab
@name = gitaly_file.name
@path = gitaly_file.path
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
......@@ -56,7 +56,7 @@ RSpec.describe 'GitLab Markdown Benchmark', :aggregate_failures do
it 'benchmarks several pipelines' do
path = 'images/example.jpg'
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' }
puts "\n--> Benchmarking Full, Wiki, and Plain pipelines\n"
......
......@@ -290,7 +290,7 @@ RSpec.describe 'GitLab Markdown', :aggregate_failures do
path = 'images/example.jpg'
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' }
@html = markdown(@feat.raw_markdown, { pipeline: :wiki, wiki: @wiki, page_slug: @wiki_page.slug })
......
......@@ -22,7 +22,7 @@ RSpec.describe Banzai::Filter::GollumTagsFilter do
path: 'images/image.jpg',
raw_data: '')
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]]'
doc = filter("See #{tag}", wiki: wiki)
......@@ -31,7 +31,7 @@ RSpec.describe Banzai::Filter::GollumTagsFilter do
end
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]]'
doc = filter("See #{tag}", wiki: wiki)
......
......@@ -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')
end
it 'returns the latest version of the file if it exists' do
file = subject.find_file('image.png')
shared_examples 'find_file results' do
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')
end
expect(file.mime_type).to eq('image/png')
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
expect(subject.find_file('non-existent')).to eq(nil)
it 'returns the whole file' do
file = subject.find_file('image.png')
image.rewind
expect(file.raw_data.b).to eq(image.read.b)
end
end
it 'returns a Gitlab::Git::WikiFile instance' do
file = subject.find_file('image.png')
it_behaves_like 'find_file results'
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
it 'returns the whole file' do
file = subject.find_file('image.png')
image.rewind
context 'when feature flag :gitaly_find_file is disabled' do
before do
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
......
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