Class: Gitlab::GitalyClient::WikiService

Inherits:
Object
  • Object
show all
Includes:
EncodingHelper
Defined in:
lib/gitlab/gitaly_client/wiki_service.rb

Constant Summary collapse

MAX_MSG_SIZE =
128.kilobytes.freeze

Constants included from EncodingHelper

EncodingHelper::ENCODING_CONFIDENCE_THRESHOLD, EncodingHelper::ESCAPED_CHARS, EncodingHelper::UNICODE_REPLACEMENT_CHARACTER

Instance Method Summary collapse

Methods included from EncodingHelper

#binary_io, #detect_binary?, #detect_encoding, #detect_libgit2_binary?, #encode!, #encode_binary, #encode_utf8, #encode_utf8_no_detect, #encode_utf8_with_replacement_character, #unquote_path

Constructor Details

#initialize(repository) ⇒ WikiService

Returns a new instance of WikiService.


12
13
14
15
# File 'lib/gitlab/gitaly_client/wiki_service.rb', line 12

def initialize(repository)
  @gitaly_repo = repository.gitaly_repository
  @repository = repository
end

Instance Method Details

#find_page(title:, version: nil, dir: nil, load_content: true) ⇒ Object


67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/gitlab/gitaly_client/wiki_service.rb', line 67

def find_page(title:, version: nil, dir: nil, load_content: true)
  request = Gitaly::WikiFindPageRequest.new(
    repository: @gitaly_repo,
    title: encode_binary(title),
    revision: encode_binary(version),
    directory: encode_binary(dir),
    skip_content: !load_content
  )

  response = GitalyClient.call(@repository.storage, :wiki_service, :wiki_find_page, request, timeout: GitalyClient.fast_timeout)

  wiki_page_from_iterator(response)
end

#list_all_pages(limit: 0, sort: nil, direction_desc: false) ⇒ Object


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/gitlab/gitaly_client/wiki_service.rb', line 81

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

#load_all_pages(limit: 0, sort: nil, direction_desc: false) ⇒ Object


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/gitlab/gitaly_client/wiki_service.rb', line 101

def load_all_pages(limit: 0, sort: nil, direction_desc: false)
  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)
  response = GitalyClient.call(@repository.storage, :wiki_service, :wiki_get_all_pages, request, timeout: GitalyClient.medium_timeout)

  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

#update_page(page_path, title, format, content, commit_details) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gitlab/gitaly_client/wiki_service.rb', line 43

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)
  )

  strio = binary_io(content)

  enum = Enumerator.new do |y|
    until strio.eof?
      request.content = strio.read(MAX_MSG_SIZE)

      y.yield request

      request = Gitaly::WikiUpdatePageRequest.new
    end
  end

  GitalyClient.call(@repository.storage, :wiki_service, :wiki_update_page, enum, timeout: GitalyClient.medium_timeout)
end

#write_page(name, format, content, commit_details) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gitlab/gitaly_client/wiki_service.rb', line 17

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)
  )

  strio = binary_io(content)

  enum = Enumerator.new do |y|
    until strio.eof?
      request.content = strio.read(MAX_MSG_SIZE)

      y.yield request

      request = Gitaly::WikiWritePageRequest.new
    end
  end

  response = GitalyClient.call(@repository.storage, :wiki_service, :wiki_write_page, enum, timeout: GitalyClient.medium_timeout)
  if error = response.duplicate_error.presence
    raise Gitlab::Git::Wiki::DuplicatePageError, error
  end
end