Class: Gitlab::Git::Wiki

Inherits:
Object
  • Object
show all
Includes:
WrapsGitalyErrors
Defined in:
lib/gitlab/git/wiki.rb

Defined Under Namespace

Classes: CommitDetails, GollumSlug

Constant Summary collapse

DuplicatePageError =
Class.new(StandardError)
DEFAULT_PAGINATION =
Kaminari.config.default_per_page

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WrapsGitalyErrors

#wrapped_gitaly_errors

Constructor Details

#initialize(repository) ⇒ Wiki

Initialize with a Gitlab::Git::Repository instance


62
63
64
# File 'lib/gitlab/git/wiki.rb', line 62

def initialize(repository)
  @repository = repository
end

Instance Attribute Details

#repositoryObject (readonly)

Returns the value of attribute repository


55
56
57
# File 'lib/gitlab/git/wiki.rb', line 55

def repository
  @repository
end

Class Method Details

.default_refObject


57
58
59
# File 'lib/gitlab/git/wiki.rb', line 57

def self.default_ref
  'master'
end

Instance Method Details

#count_page_versions(page_path) ⇒ Object


131
132
133
# File 'lib/gitlab/git/wiki.rb', line 131

def count_page_versions(page_path)
  @repository.count_commits(ref: 'HEAD', path: page_path)
end

#delete_page(page_path, commit_details) ⇒ Object


76
77
78
79
80
# File 'lib/gitlab/git/wiki.rb', line 76

def delete_page(page_path, commit_details)
  wrapped_gitaly_errors do
    gitaly_delete_page(page_path, commit_details)
  end
end

#file(name, version) ⇒ Object


109
110
111
112
113
# File 'lib/gitlab/git/wiki.rb', line 109

def file(name, version)
  wrapped_gitaly_errors do
    gitaly_find_file(name, version)
  end
end

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


88
89
90
91
92
93
94
95
96
97
# File 'lib/gitlab/git/wiki.rb', line 88

def list_pages(limit: 0, sort: nil, direction_desc: false, load_content: false)
  wrapped_gitaly_errors do
    gitaly_list_pages(
      limit: limit,
      sort: sort,
      direction_desc: direction_desc,
      load_content: load_content
    )
  end
end

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


99
100
101
102
103
104
105
106
107
# File 'lib/gitlab/git/wiki.rb', line 99

def page(title:, version: nil, dir: nil)
  wrapped_gitaly_errors do
    gitaly_find_page(title: title, version: version, dir: dir)
  end
rescue Gitlab::Git::CommandError
  # Return nil for invalid versions.
  # This can be removed with https://gitlab.com/gitlab-org/gitaly/-/merge_requests/2323 in place.
  nil
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.

119
120
121
122
123
124
125
126
127
128
129
# File 'lib/gitlab/git/wiki.rb', line 119

def page_versions(page_path, options = {})
  versions = wrapped_gitaly_errors do
    gitaly_wiki_client.page_versions(page_path, options)
  end

  # Gitaly uses gollum-lib to get the versions. Gollum defaults to 20
  # per page, but also fetches 20 if `limit` or `per_page` < 20.
  # Slicing returns an array with the expected number of items.
  slice_bound = options[:limit] || options[:per_page] || DEFAULT_PAGINATION
  versions[0..slice_bound]
end

#preview_slug(title, format) ⇒ Object


135
136
137
# File 'lib/gitlab/git/wiki.rb', line 135

def preview_slug(title, format)
  GollumSlug.generate(title, format)
end

#repository_exists?Boolean

Returns:

  • (Boolean)

66
67
68
# File 'lib/gitlab/git/wiki.rb', line 66

def repository_exists?
  @repository.exists?
end

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


82
83
84
85
86
# File 'lib/gitlab/git/wiki.rb', line 82

def update_page(page_path, title, format, content, commit_details)
  wrapped_gitaly_errors do
    gitaly_update_page(page_path, title, format, content, commit_details)
  end
end

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


70
71
72
73
74
# File 'lib/gitlab/git/wiki.rb', line 70

def write_page(name, format, content, commit_details)
  wrapped_gitaly_errors do
    gitaly_write_page(name, format, content, commit_details)
  end
end