2018-09-29 18:34:47 -04:00
# frozen_string_literal: true
2017-09-06 18:21:52 -04:00
module API
2020-10-14 20:08:42 -04:00
class Wikis < :: API :: Base
2020-06-24 23:08:52 -04:00
helpers :: API :: Helpers :: WikisHelpers
2020-10-30 14:08:56 -04:00
feature_category :wiki
2017-09-06 18:21:52 -04:00
helpers do
2020-06-24 23:08:52 -04:00
attr_reader :container
2018-09-04 06:39:08 -04:00
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 ,
2022-04-08 08:08:48 -04:00
values : Wiki :: VALID_USER_MARKUPS . keys . map ( & :to_s ) ,
2017-09-06 18:21:52 -04:00
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 )
2020-06-24 23:08:52 -04:00
:: 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-04-25 00:19:07 -04:00
2020-06-24 23:08: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
2021-10-27 08:09:41 -04:00
get ':id/wikis' , urgency : :low do
2020-06-24 23:08:52 -04:00
authorize! :read_wiki , container
2017-09-06 18:21:52 -04:00
2020-06-24 23:08:52 -04:00
entity = params [ :with_content ] ? Entities :: WikiPage : Entities :: WikiPageBasic
2017-09-06 18:21:52 -04:00
2022-05-20 11:09:10 -04:00
options = {
with : entity ,
current_user : current_user
}
present container . wiki . list_pages ( load_content : params [ :with_content ] ) , options
2020-06-24 23:08:52 -04:00
end
2017-09-06 18:21:52 -04:00
2020-06-24 23:08:52 -04:00
desc 'Get a wiki page' do
success Entities :: WikiPage
end
params do
requires :slug , type : String , desc : 'The slug of a wiki page'
2022-03-16 17:09:14 -04:00
optional :version , type : String , desc : 'The version hash of a wiki page'
2022-03-08 10:15:55 -05:00
optional :render_html , type : Boolean , default : false , desc : 'Render content to HTML'
2020-06-24 23:08:52 -04:00
end
2022-03-22 14:08:29 -04:00
get ':id/wikis/:slug' , urgency : :low do
2020-06-24 23:08:52 -04:00
authorize! :read_wiki , container
2017-09-06 18:21:52 -04:00
2022-05-20 11:09:10 -04:00
options = {
with : Entities :: WikiPage ,
render_html : params [ :render_html ] ,
current_user : current_user
}
present wiki_page ( params [ :version ] ) , options
2020-06-24 23:08:52 -04:00
end
2017-09-06 18:21:52 -04:00
2020-06-24 23:08:52 -04:00
desc 'Create a wiki page' do
success Entities :: WikiPage
2017-09-06 18:21:52 -04:00
end
2020-06-24 23:08:52 -04:00
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
2017-09-06 18:21:52 -04:00
2020-08-17 14:10:01 -04:00
response = WikiPages :: CreateService . new ( container : container , current_user : current_user , params : params ) . execute
page = response . payload [ :page ]
2017-09-06 18:21:52 -04:00
2020-08-17 14:10:01 -04:00
if response . success?
2020-06-24 23:08:52 -04:00
present page , with : Entities :: WikiPage
else
render_validation_error! ( page )
end
end
2017-09-06 18:21:52 -04:00
2020-06-24 23:08:52 -04:00
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-08-27 11:10:21 -04:00
response = WikiPages :: UpdateService
2020-06-24 23:08:52 -04:00
. new ( container : container , current_user : current_user , params : params )
. execute ( wiki_page )
2020-08-27 11:10:21 -04:00
page = response . payload [ :page ]
2020-06-24 23:08:52 -04:00
2020-08-27 11:10:21 -04:00
if response . success?
2020-06-24 23:08:52 -04:00
present page , with : Entities :: WikiPage
else
render_validation_error! ( page )
end
2017-09-06 18:21:52 -04:00
end
2020-06-24 23:08:52 -04:00
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
2017-09-06 18:21:52 -04:00
2020-08-20 23:10:16 -04:00
response = WikiPages :: DestroyService
2020-06-24 23:08:52 -04:00
. new ( container : container , current_user : current_user )
. execute ( wiki_page )
2020-01-16 13:08:46 -05:00
2020-08-20 23:10:16 -04:00
if response . success?
no_content!
else
2021-03-11 07:09:28 -05:00
unprocessable_entity! ( response . message )
2020-08-20 23:10:16 -04:00
end
2020-06-24 23:08:52 -04:00
end
2018-09-04 06:39:08 -04:00
2020-06-24 23:08:52 -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
2022-10-18 05:11:01 -04:00
requires :file , types : [ Rack :: Multipart :: UploadedFile , :: API :: Validations :: Types :: WorkhorseFile ] , desc : 'The attachment file to be uploaded' , documentation : { type : 'file' }
2020-06-24 23:08:52 -04:00
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-04-01 05:10:01 -04:00
present result [ :result ] , with : Entities :: WikiAttachment
2020-06-24 23:08:52 -04:00
else
render_api_error! ( result [ :message ] , 400 )
end
2018-09-04 06:39:08 -04:00
end
end
2017-09-06 18:21:52 -04:00
end
end
end