debian-mirror-gitlab/lib/gitlab/gitaly_client/wiki_service.rb

229 lines
7.3 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
require 'stringio'
module Gitlab
module GitalyClient
class WikiService
include Gitlab::EncodingHelper
MAX_MSG_SIZE = 128.kilobytes.freeze
def initialize(repository)
@gitaly_repo = repository.gitaly_repository
@repository = repository
end
def write_page(name, format, content, commit_details)
request = Gitaly::WikiWritePageRequest.new(
repository: @gitaly_repo,
name: encode_binary(name),
format: format.to_s,
commit_details: gitaly_commit_details(commit_details)
)
2019-07-07 11:18:12 +05:30
strio = binary_io(content)
2018-03-17 18:26:18 +05:30
enum = Enumerator.new do |y|
until strio.eof?
request.content = strio.read(MAX_MSG_SIZE)
y.yield request
request = Gitaly::WikiWritePageRequest.new
end
end
2019-12-21 20:55:43 +05:30
response = GitalyClient.call(@repository.storage, :wiki_service, :wiki_write_page, enum, timeout: GitalyClient.medium_timeout)
2018-03-17 18:26:18 +05:30
if error = response.duplicate_error.presence
raise Gitlab::Git::Wiki::DuplicatePageError, error
end
end
def update_page(page_path, title, format, content, commit_details)
request = Gitaly::WikiUpdatePageRequest.new(
repository: @gitaly_repo,
page_path: encode_binary(page_path),
title: encode_binary(title),
format: format.to_s,
commit_details: gitaly_commit_details(commit_details)
)
2019-07-07 11:18:12 +05:30
strio = binary_io(content)
2018-03-17 18:26:18 +05:30
enum = Enumerator.new do |y|
until strio.eof?
request.content = strio.read(MAX_MSG_SIZE)
y.yield request
request = Gitaly::WikiUpdatePageRequest.new
end
end
2019-12-21 20:55:43 +05:30
GitalyClient.call(@repository.storage, :wiki_service, :wiki_update_page, enum, timeout: GitalyClient.medium_timeout)
2018-03-17 18:26:18 +05:30
end
def delete_page(page_path, commit_details)
request = Gitaly::WikiDeletePageRequest.new(
repository: @gitaly_repo,
page_path: encode_binary(page_path),
commit_details: gitaly_commit_details(commit_details)
)
2018-11-08 19:23:39 +05:30
GitalyClient.call(@repository.storage, :wiki_service, :wiki_delete_page, request, timeout: GitalyClient.medium_timeout)
2018-03-17 18:26:18 +05:30
end
def find_page(title:, version: nil, dir: nil)
request = Gitaly::WikiFindPageRequest.new(
repository: @gitaly_repo,
title: encode_binary(title),
revision: encode_binary(version),
directory: encode_binary(dir)
)
2018-11-08 19:23:39 +05:30
response = GitalyClient.call(@repository.storage, :wiki_service, :wiki_find_page, request, timeout: GitalyClient.fast_timeout)
2018-03-17 18:26:18 +05:30
wiki_page_from_iterator(response)
end
2019-07-31 22:56:46 +05:30
def list_all_pages(limit: 0, sort: nil, direction_desc: false)
sort_value = Gitaly::WikiListPagesRequest::SortBy.resolve(sort.to_s.upcase.to_sym)
params = { repository: @gitaly_repo, limit: limit, direction_desc: direction_desc }
params[:sort] = sort_value if sort_value
request = Gitaly::WikiListPagesRequest.new(params)
stream = GitalyClient.call(@repository.storage, :wiki_service, :wiki_list_pages, request, timeout: GitalyClient.medium_timeout)
stream.each_with_object([]) do |message, pages|
page = message.page
next unless page
wiki_page = GitalyClient::WikiPage.new(page.to_h)
version = new_wiki_page_version(page.version)
pages << [wiki_page, version]
end
end
def load_all_pages(limit: 0, sort: nil, direction_desc: false)
2019-07-07 11:18:12 +05:30
sort_value = Gitaly::WikiGetAllPagesRequest::SortBy.resolve(sort.to_s.upcase.to_sym)
params = { repository: @gitaly_repo, limit: limit, direction_desc: direction_desc }
params[:sort] = sort_value if sort_value
request = Gitaly::WikiGetAllPagesRequest.new(params)
2018-11-08 19:23:39 +05:30
response = GitalyClient.call(@repository.storage, :wiki_service, :wiki_get_all_pages, request, timeout: GitalyClient.medium_timeout)
2019-07-31 22:56:46 +05:30
2018-03-17 18:26:18 +05:30
pages = []
loop do
page, version = wiki_page_from_iterator(response) { |message| message.end_of_page }
break unless page && version
pages << [page, version]
end
pages
end
# options:
# :page - The Integer page number.
# :per_page - The number of items per page.
# :limit - Total number of items to return.
def page_versions(page_path, options)
request = Gitaly::WikiGetPageVersionsRequest.new(
repository: @gitaly_repo,
page_path: encode_binary(page_path),
page: options[:page] || 1,
2018-12-05 23:21:45 +05:30
per_page: options[:per_page] || Gitlab::Git::Wiki::DEFAULT_PAGINATION
2018-03-17 18:26:18 +05:30
)
2018-11-08 19:23:39 +05:30
stream = GitalyClient.call(@repository.storage, :wiki_service, :wiki_get_page_versions, request, timeout: GitalyClient.medium_timeout)
2018-03-17 18:26:18 +05:30
versions = []
stream.each do |message|
message.versions.each do |version|
versions << new_wiki_page_version(version)
end
end
versions
end
def find_file(name, revision)
request = Gitaly::WikiFindFileRequest.new(
repository: @gitaly_repo,
name: encode_binary(name),
revision: encode_binary(revision)
)
2018-11-08 19:23:39 +05:30
response = GitalyClient.call(@repository.storage, :wiki_service, :wiki_find_file, request, timeout: GitalyClient.fast_timeout)
2018-03-17 18:26:18 +05:30
wiki_file = nil
response.each do |message|
2018-05-09 12:01:36 +05:30
next unless message.name.present? || wiki_file
2018-03-17 18:26:18 +05:30
if wiki_file
2019-02-15 15:39:39 +05:30
wiki_file.raw_data = "#{wiki_file.raw_data}#{message.raw_data}"
2018-03-17 18:26:18 +05:30
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
# stopped early at that point; else the iterator is consumed entirely.
# The iterator is traversed with `next` to allow resuming the iteration.
def wiki_page_from_iterator(iterator)
wiki_page = version = nil
while message = iterator.next
break if block_given? && yield(message)
page = message.page
next unless page
if wiki_page
wiki_page.raw_data << page.raw_data
else
wiki_page = GitalyClient::WikiPage.new(page.to_h)
version = new_wiki_page_version(page.version)
end
end
[wiki_page, version]
rescue StopIteration
[wiki_page, version]
end
def new_wiki_page_version(version)
Gitlab::Git::WikiPageVersion.new(
Gitlab::Git::Commit.decorate(@repository, version.commit),
version.format
)
end
def gitaly_commit_details(commit_details)
Gitaly::WikiCommitDetails.new(
2018-10-15 14:42:47 +05:30
user_id: commit_details.user_id,
user_name: encode_binary(commit_details.username),
2018-03-17 18:26:18 +05:30
name: encode_binary(commit_details.name),
email: encode_binary(commit_details.email),
message: encode_binary(commit_details.message)
)
end
end
end
end