debian-mirror-gitlab/lib/api/wikis.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

159 lines
5.1 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
module API
2021-01-03 14:25:43 +05:30
class Wikis < ::API::Base
2020-07-28 23:09:34 +05:30
helpers ::API::Helpers::WikisHelpers
2021-01-29 00:20:46 +05:30
feature_category :wiki
2018-03-17 18:26:18 +05:30
helpers do
2020-07-28 23:09:34 +05:30
attr_reader :container
2018-11-20 20:47:30 +05:30
2019-07-07 11:18:12 +05:30
params :common_wiki_page_params do
2018-03-17 18:26:18 +05:30
optional :format,
type: String,
2022-06-21 17:19:12 +05:30
values: Wiki::VALID_USER_MARKUPS.keys.map(&:to_s),
2018-03-17 18:26:18 +05:30
default: 'markdown',
2020-03-13 15:44:24 +05:30
desc: 'Format of a wiki page. Available formats are markdown, rdoc, asciidoc and org'
2018-03-17 18:26:18 +05:30
end
end
2019-03-02 22:35:43 +05:30
WIKI_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS.merge(slug: API::NO_SLASH_URL_PART_REGEX)
2020-07-28 23:09:34 +05:30
::API::Helpers::WikisHelpers.wiki_resource_kinds.each do |container_resource|
resource container_resource, requirements: WIKI_ENDPOINT_REQUIREMENTS do
after_validation do
@container = Gitlab::Lazy.new { find_container(container_resource) }
end
2019-07-31 22:56:46 +05:30
2020-07-28 23:09:34 +05:30
desc 'Get a list of wiki pages' do
success Entities::WikiPageBasic
end
params do
optional :with_content, type: Boolean, default: false, desc: "Include pages' content"
end
2021-12-11 22:18:48 +05:30
get ':id/wikis', urgency: :low do
2020-07-28 23:09:34 +05:30
authorize! :read_wiki, container
2018-03-17 18:26:18 +05:30
2020-07-28 23:09:34 +05:30
entity = params[:with_content] ? Entities::WikiPage : Entities::WikiPageBasic
2018-03-17 18:26:18 +05:30
2022-07-23 23:45:48 +05:30
options = {
with: entity,
current_user: current_user
}
present container.wiki.list_pages(load_content: params[:with_content]), options
2020-07-28 23:09:34 +05:30
end
2018-03-17 18:26:18 +05:30
2020-07-28 23:09:34 +05:30
desc 'Get a wiki page' do
success Entities::WikiPage
end
params do
requires :slug, type: String, desc: 'The slug of a wiki page'
2022-05-07 20:08:51 +05:30
optional :version, type: String, desc: 'The version hash of a wiki page'
optional :render_html, type: Boolean, default: false, desc: 'Render content to HTML'
2020-07-28 23:09:34 +05:30
end
2022-06-21 17:19:12 +05:30
get ':id/wikis/:slug', urgency: :low do
2020-07-28 23:09:34 +05:30
authorize! :read_wiki, container
2018-03-17 18:26:18 +05:30
2022-07-23 23:45:48 +05:30
options = {
with: Entities::WikiPage,
render_html: params[:render_html],
current_user: current_user
}
present wiki_page(params[:version]), options
2020-07-28 23:09:34 +05:30
end
2018-03-17 18:26:18 +05:30
2020-07-28 23:09:34 +05:30
desc 'Create a wiki page' do
success Entities::WikiPage
2018-03-17 18:26:18 +05:30
end
2020-07-28 23:09:34 +05:30
params do
requires :title, type: String, desc: 'Title of a wiki page'
requires :content, type: String, desc: 'Content of a wiki page'
use :common_wiki_page_params
end
post ':id/wikis' do
authorize! :create_wiki, container
2018-03-17 18:26:18 +05:30
2020-10-24 23:57:45 +05:30
response = WikiPages::CreateService.new(container: container, current_user: current_user, params: params).execute
page = response.payload[:page]
2018-03-17 18:26:18 +05:30
2020-10-24 23:57:45 +05:30
if response.success?
2020-07-28 23:09:34 +05:30
present page, with: Entities::WikiPage
else
render_validation_error!(page)
end
end
2018-03-17 18:26:18 +05:30
2020-07-28 23:09:34 +05:30
desc 'Update a wiki page' do
success Entities::WikiPage
end
params do
optional :title, type: String, desc: 'Title of a wiki page'
optional :content, type: String, desc: 'Content of a wiki page'
use :common_wiki_page_params
at_least_one_of :content, :title, :format
end
put ':id/wikis/:slug' do
authorize! :create_wiki, container
2020-11-24 15:15:51 +05:30
response = WikiPages::UpdateService
2020-07-28 23:09:34 +05:30
.new(container: container, current_user: current_user, params: params)
.execute(wiki_page)
2020-11-24 15:15:51 +05:30
page = response.payload[:page]
2020-07-28 23:09:34 +05:30
2020-11-24 15:15:51 +05:30
if response.success?
2020-07-28 23:09:34 +05:30
present page, with: Entities::WikiPage
else
render_validation_error!(page)
end
2018-03-17 18:26:18 +05:30
end
2020-07-28 23:09:34 +05:30
desc 'Delete a wiki page'
params do
requires :slug, type: String, desc: 'The slug of a wiki page'
end
delete ':id/wikis/:slug' do
authorize! :admin_wiki, container
2018-03-17 18:26:18 +05:30
2020-11-24 15:15:51 +05:30
response = WikiPages::DestroyService
2020-07-28 23:09:34 +05:30
.new(container: container, current_user: current_user)
.execute(wiki_page)
2020-03-13 15:44:24 +05:30
2020-11-24 15:15:51 +05:30
if response.success?
no_content!
else
2021-04-17 20:07:23 +05:30
unprocessable_entity!(response.message)
2020-11-24 15:15:51 +05:30
end
2020-07-28 23:09:34 +05:30
end
2018-11-20 20:47:30 +05:30
2020-07-28 23:09:34 +05:30
desc 'Upload an attachment to the wiki repository' do
detail 'This feature was introduced in GitLab 11.3.'
success Entities::WikiAttachment
end
params do
2022-11-25 23:54:43 +05:30
requires :file, types: [Rack::Multipart::UploadedFile, ::API::Validations::Types::WorkhorseFile], desc: 'The attachment file to be uploaded', documentation: { type: 'file' }
2020-07-28 23:09:34 +05:30
optional :branch, type: String, desc: 'The name of the branch'
end
post ":id/wikis/attachments" do
authorize! :create_wiki, container
result = ::Wikis::CreateAttachmentService.new(
container: container,
current_user: current_user,
params: commit_params(declared_params(include_missing: false))
).execute
if result[:status] == :success
status(201)
2022-06-21 17:19:12 +05:30
present result[:result], with: Entities::WikiAttachment
2020-07-28 23:09:34 +05:30
else
render_api_error!(result[:message], 400)
end
2018-11-20 20:47:30 +05:30
end
end
2018-03-17 18:26:18 +05:30
end
end
end