Commit d3f46313 authored by Markus Koller's avatar Markus Koller

Merge branch '324306-fj-enable-gitaly-find-file-feature-flag' into 'master'

Enable new RPC call to retrieve wiki files [RUN ALL RSPEC] [RUN AS-IF-FOSS]

See merge request gitlab-org/gitlab!56491
parents 1ff17bc2 9a7a66c3
......@@ -160,16 +160,12 @@ class Wiki
end
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)
data_limit = load_content ? -1 : 0
blobs = repository.blobs_at([[version, name]], blob_size_limit: data_limit)
return if blobs.empty?
return if blobs.empty?
Gitlab::Git::WikiFile.from_blob(blobs.first)
else
wiki.file(name, version)
end
Gitlab::Git::WikiFile.new(blobs.first)
end
def create_page(title, content, format = :markdown, message = nil)
......
---
title: Enable new RPC call to retrieve wiki files
merge_request: 56491
author:
type: changed
---
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
......@@ -102,12 +102,6 @@ module Gitlab
end
end
def file(name, version)
wrapped_gitaly_errors do
gitaly_find_file(name, version)
end
end
# options:
# :page - The Integer page number.
# :per_page - The number of items per page.
......@@ -161,13 +155,6 @@ module Gitlab
nil
end
def gitaly_find_file(name, version)
wiki_file = gitaly_wiki_client.find_file(name, version)
return unless wiki_file
Gitlab::Git::WikiFile.new(wiki_file)
end
def gitaly_list_pages(limit: 0, sort: nil, direction_desc: false, load_content: false)
params = { limit: limit, sort: sort, direction_desc: direction_desc }
......
......@@ -5,25 +5,11 @@ module Gitlab
class WikiFile
attr_reader :mime_type, :raw_data, :name, :path
# This class wraps Gitlab::GitalyClient::WikiFile
def initialize(gitaly_file)
@mime_type = gitaly_file.mime_type
@raw_data = gitaly_file.raw_data
@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)
def initialize(blob)
@mime_type = blob.mime_type
@raw_data = blob.data
@name = File.basename(blob.name)
@path = blob.path
end
end
end
......
......@@ -3,7 +3,7 @@
module Gitlab
module GitalyClient
# This module expects an `ATTRS` const to be defined on the subclass
# See GitalyClient::WikiFile for an example
# See GitalyClient::WikiPage for an example
module AttributesBag
extend ActiveSupport::Concern
......
# frozen_string_literal: true
module Gitlab
module GitalyClient
class WikiFile
ATTRS = %i(name mime_type path raw_data).freeze
include AttributesBag
end
end
end
......@@ -153,32 +153,6 @@ module Gitlab
versions
end
def find_file(name, revision)
request = Gitaly::WikiFindFileRequest.new(
repository: @gitaly_repo,
name: encode_binary(name),
revision: encode_binary(revision)
)
response = GitalyClient.call(@repository.storage, :wiki_service, :wiki_find_file, request, timeout: GitalyClient.fast_timeout)
wiki_file = nil
response.each do |message|
next unless message.name.present? || wiki_file
if wiki_file
wiki_file.raw_data = "#{wiki_file.raw_data}#{message.raw_data}"
else
wiki_file = GitalyClient::WikiFile.new(message.to_h)
# All gRPC strings in a response are frozen, so we get
# an unfrozen version here so appending in the else clause below doesn't blow up.
wiki_file.raw_data = wiki_file.raw_data.dup
end
end
wiki_file
end
private
# If a block is given and the yielded value is truthy, iteration will be
......
......@@ -54,9 +54,10 @@ RSpec.describe 'GitLab Markdown Benchmark', :aggregate_failures do
context 'pipelines' 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, load_content: false).and_return(Gitlab::Git::WikiFile.new(gitaly_wiki_file))
name = 'example.jpg'
path = "images/#{name}"
blob = double(name: name, path: path, mime_type: 'image/jpeg', data: nil)
allow(wiki).to receive(:find_file).with(path, load_content: false).and_return(Gitlab::Git::WikiFile.new(blob))
allow(wiki).to receive(:wiki_base_path) { '/namespace1/gitlabhq/wikis' }
puts "\n--> Benchmarking Full, Wiki, and Plain pipelines\n"
......
......@@ -288,9 +288,10 @@ RSpec.describe 'GitLab Markdown', :aggregate_failures do
@wiki = @feat.wiki
@wiki_page = @feat.wiki_page
path = 'images/example.jpg'
gitaly_wiki_file = Gitlab::GitalyClient::WikiFile.new(path: path)
expect(@wiki).to receive(:find_file).with(path, load_content: false).and_return(Gitlab::Git::WikiFile.new(gitaly_wiki_file))
name = 'example.jpg'
path = "images/#{name}"
blob = double(name: name, path: path, mime_type: 'image/jpeg', data: nil)
expect(@wiki).to receive(:find_file).with(path, load_content: false).and_return(Gitlab::Git::WikiFile.new(blob))
allow(@wiki).to receive(:wiki_base_path) { '/namespace1/gitlabhq/wikis' }
@html = markdown(@feat.raw_markdown, { pipeline: :wiki, wiki: @wiki, page_slug: @wiki_page.slug })
......
......@@ -16,12 +16,8 @@ RSpec.describe Banzai::Filter::GollumTagsFilter do
context 'linking internal images' do
it 'creates img tag if image exists' do
gollum_file_double = double('Gollum::File',
mime_type: 'image/jpeg',
name: 'images/image.jpg',
path: 'images/image.jpg',
raw_data: '')
wiki_file = Gitlab::Git::WikiFile.new(gollum_file_double)
blob = double(mime_type: 'image/jpeg', name: 'images/image.jpg', path: 'images/image.jpg', data: '')
wiki_file = Gitlab::Git::WikiFile.new(blob)
expect(wiki).to receive(:find_file).with('images/image.jpg', load_content: false).and_return(wiki_file)
tag = '[[images/image.jpg]]'
......
......@@ -297,7 +297,7 @@ RSpec.describe Banzai::Pipeline::WikiPipeline do
mime_type: 'image/jpeg',
name: 'images/image.jpg',
path: 'images/image.jpg',
raw_data: '')
data: '')
wiki_file = Gitlab::Git::WikiFile.new(gollum_file_double)
markdown = "[[#{wiki_file.path}]]"
......
......@@ -354,33 +354,29 @@ RSpec.shared_examples 'wiki model' do
subject.repository.create_file(user, 'image.png', image, branch_name: subject.default_branch, message: 'add image')
end
shared_examples 'find_file results' do
it 'returns the latest version of the file if it exists' 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')
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 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')
it 'returns a Gitlab::Git::WikiFile instance' do
file = subject.find_file('image.png')
expect(file).to be_a Gitlab::Git::WikiFile
end
expect(file).to be_a Gitlab::Git::WikiFile
end
it 'returns the whole file' do
file = subject.find_file('image.png')
image.rewind
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
expect(file.raw_data.b).to eq(image.read.b)
end
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)
......@@ -388,14 +384,6 @@ RSpec.shared_examples 'wiki model' do
expect(file.raw_data).to be_empty
end
end
context 'when feature flag :gitaly_find_file is disabled' do
before do
stub_feature_flags(gitaly_find_file: false)
end
it_behaves_like 'find_file results'
end
end
describe '#create_page' do
......
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