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

Instance Method Summary collapse

Methods included from EncodingHelper

#binary_io, #detect_binary?, #detect_libgit2_binary?, #encode!, #encode_binary, #encode_utf8

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

#delete_page(page_path, commit_details) ⇒ Object


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

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

  GitalyClient.call(@repository.storage, :wiki_service, :wiki_delete_page, request, timeout: GitalyClient.medium_timeout)
end

#find_file(name, revision) ⇒ Object


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/gitlab/gitaly_client/wiki_service.rb', line 156

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

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


77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/gitlab/gitaly_client/wiki_service.rb', line 77

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

  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


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/gitlab/gitaly_client/wiki_service.rb', line 90

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


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/gitlab/gitaly_client/wiki_service.rb', line 110

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

#page_versions(page_path, options) ⇒ Object

options:

:page     - The Integer page number.
:per_page - The number of items per page.
:limit    - Total number of items to return.

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/gitlab/gitaly_client/wiki_service.rb', line 136

def page_versions(page_path, options)
  request = Gitaly::WikiGetPageVersionsRequest.new(
    repository: @gitaly_repo,
    page_path: encode_binary(page_path),
    page: options[:page] || 1,
    per_page: options[:per_page] || Gitlab::Git::Wiki::DEFAULT_PAGINATION
  )

  stream = GitalyClient.call(@repository.storage, :wiki_service, :wiki_get_page_versions, request, timeout: GitalyClient.medium_timeout)

  versions = []
  stream.each do |message|
    message.versions.each do |version|
      versions << new_wiki_page_version(version)
    end
  end

  versions
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