2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-06 18:21:52 -04:00
|
|
|
module API
|
2020-04-27 23:09:53 -04:00
|
|
|
class Wikis < Grape::API
|
2017-09-06 18:21:52 -04:00
|
|
|
helpers do
|
2018-09-04 06:39:08 -04:00
|
|
|
def commit_params(attrs)
|
2019-09-02 12:41:18 -04:00
|
|
|
# In order to avoid service disruption this can work with an old workhorse without the acceleration
|
|
|
|
# the first branch of this if must be removed when we drop support for non accelerated uploads
|
|
|
|
if attrs[:file].is_a?(Hash)
|
|
|
|
{
|
|
|
|
file_name: attrs[:file][:filename],
|
|
|
|
file_content: attrs[:file][:tempfile].read,
|
|
|
|
branch_name: attrs[:branch]
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
file_name: attrs[:file].original_filename,
|
|
|
|
file_content: attrs[:file].read,
|
|
|
|
branch_name: attrs[:branch]
|
|
|
|
}
|
|
|
|
end
|
2018-09-04 06:39:08 -04:00
|
|
|
end
|
|
|
|
|
2019-02-13 11:35:27 -05:00
|
|
|
params :common_wiki_page_params do
|
2017-09-06 18:21:52 -04:00
|
|
|
optional :format,
|
|
|
|
type: String,
|
|
|
|
values: ProjectWiki::MARKUPS.values.map(&:to_s),
|
|
|
|
default: 'markdown',
|
2020-01-15 22:08:47 -05:00
|
|
|
desc: 'Format of a wiki page. Available formats are markdown, rdoc, asciidoc and org'
|
2017-09-06 18:21:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-15 02:59:17 -05:00
|
|
|
WIKI_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS.merge(slug: API::NO_SLASH_URL_PART_REGEX)
|
|
|
|
|
|
|
|
resource :projects, requirements: WIKI_ENDPOINT_REQUIREMENTS do
|
2017-09-06 18:21:52 -04:00
|
|
|
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
|
|
|
|
get ':id/wikis' do
|
|
|
|
authorize! :read_wiki, user_project
|
|
|
|
|
|
|
|
entity = params[:with_content] ? Entities::WikiPage : Entities::WikiPageBasic
|
2019-04-25 00:19:07 -04:00
|
|
|
|
|
|
|
present user_project.wiki.list_pages(load_content: params[:with_content]), with: entity
|
2017-09-06 18:21:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Get a wiki page' do
|
|
|
|
success Entities::WikiPage
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :slug, type: String, desc: 'The slug of a wiki page'
|
|
|
|
end
|
|
|
|
get ':id/wikis/:slug' do
|
|
|
|
authorize! :read_wiki, user_project
|
|
|
|
|
|
|
|
present wiki_page, with: Entities::WikiPage
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Create a wiki page' do
|
|
|
|
success Entities::WikiPage
|
|
|
|
end
|
|
|
|
params do
|
2019-02-13 11:35:27 -05:00
|
|
|
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
|
2017-09-06 18:21:52 -04:00
|
|
|
end
|
|
|
|
post ':id/wikis' do
|
|
|
|
authorize! :create_wiki, user_project
|
|
|
|
|
|
|
|
page = WikiPages::CreateService.new(user_project, current_user, params).execute
|
|
|
|
|
|
|
|
if page.valid?
|
|
|
|
present page, with: Entities::WikiPage
|
|
|
|
else
|
|
|
|
render_validation_error!(page)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Update a wiki page' do
|
|
|
|
success Entities::WikiPage
|
|
|
|
end
|
|
|
|
params do
|
2019-02-13 11:35:27 -05:00
|
|
|
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
|
2017-09-06 18:21:52 -04:00
|
|
|
end
|
|
|
|
put ':id/wikis/:slug' do
|
|
|
|
authorize! :create_wiki, user_project
|
|
|
|
|
|
|
|
page = WikiPages::UpdateService.new(user_project, current_user, params).execute(wiki_page)
|
|
|
|
|
|
|
|
if page.valid?
|
|
|
|
present page, with: Entities::WikiPage
|
|
|
|
else
|
|
|
|
render_validation_error!(page)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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, user_project
|
|
|
|
|
|
|
|
WikiPages::DestroyService.new(user_project, current_user).execute(wiki_page)
|
2020-01-16 13:08:46 -05:00
|
|
|
|
|
|
|
no_content!
|
2017-09-06 18:21:52 -04:00
|
|
|
end
|
2018-09-04 06:39:08 -04:00
|
|
|
|
|
|
|
desc 'Upload an attachment to the wiki repository' do
|
|
|
|
detail 'This feature was introduced in GitLab 11.3.'
|
|
|
|
success Entities::WikiAttachment
|
|
|
|
end
|
|
|
|
params do
|
2020-04-27 23:09:53 -04:00
|
|
|
requires :file, types: [::API::Validations::Types::SafeFile, ::API::Validations::Types::WorkhorseFile], desc: 'The attachment file to be uploaded'
|
2018-09-04 06:39:08 -04:00
|
|
|
optional :branch, type: String, desc: 'The name of the branch'
|
|
|
|
end
|
2019-01-15 02:59:17 -05:00
|
|
|
post ":id/wikis/attachments" do
|
2018-09-04 06:39:08 -04:00
|
|
|
authorize! :create_wiki, user_project
|
|
|
|
|
|
|
|
result = ::Wikis::CreateAttachmentService.new(user_project,
|
|
|
|
current_user,
|
|
|
|
commit_params(declared_params(include_missing: false))).execute
|
|
|
|
|
|
|
|
if result[:status] == :success
|
|
|
|
status(201)
|
|
|
|
present OpenStruct.new(result[:result]), with: Entities::WikiAttachment
|
|
|
|
else
|
|
|
|
render_api_error!(result[:message], 400)
|
|
|
|
end
|
|
|
|
end
|
2017-09-06 18:21:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|