2018-09-29 22:34:47 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-27 09:00:56 +00:00
|
|
|
module API
|
2020-04-28 03:09:53 +00:00
|
|
|
class Templates < Grape::API
|
2017-01-17 04:45:07 +00:00
|
|
|
include PaginationParams
|
|
|
|
|
2016-06-24 19:43:46 +00:00
|
|
|
GLOBAL_TEMPLATE_TYPES = {
|
2016-08-08 11:42:24 +00:00
|
|
|
gitignores: {
|
|
|
|
gitlab_version: 8.8
|
|
|
|
},
|
|
|
|
gitlab_ci_ymls: {
|
|
|
|
gitlab_version: 8.9
|
2016-11-02 15:41:32 +00:00
|
|
|
},
|
2016-11-08 12:42:58 +00:00
|
|
|
dockerfiles: {
|
2016-12-16 09:17:15 +00:00
|
|
|
gitlab_version: 8.15
|
2016-11-08 12:42:58 +00:00
|
|
|
}
|
2016-05-27 09:00:56 +00:00
|
|
|
}.freeze
|
|
|
|
|
2016-06-24 19:43:46 +00:00
|
|
|
helpers do
|
|
|
|
def render_response(template_type, template)
|
|
|
|
not_found!(template_type.to_s.singularize) unless template
|
|
|
|
present template, with: Entities::Template
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-27 18:53:27 +00:00
|
|
|
desc 'Get the list of the available license template' do
|
|
|
|
detail 'This feature was introduced in GitLab 8.7.'
|
2017-10-05 08:48:05 +00:00
|
|
|
success ::API::Entities::License
|
2017-01-27 18:53:27 +00:00
|
|
|
end
|
|
|
|
params do
|
|
|
|
optional :popular, type: Boolean, desc: 'If passed, returns only popular licenses'
|
2017-01-17 04:45:07 +00:00
|
|
|
use :pagination
|
2017-01-27 18:53:27 +00:00
|
|
|
end
|
|
|
|
get "templates/licenses" do
|
2018-08-14 21:31:38 +00:00
|
|
|
popular = declared(params)[:popular]
|
|
|
|
popular = to_boolean(popular) if popular.present?
|
|
|
|
|
2018-10-02 23:00:38 +00:00
|
|
|
templates = TemplateFinder.build(:licenses, nil, popular: popular).execute
|
2018-08-14 21:31:38 +00:00
|
|
|
|
|
|
|
present paginate(::Kaminari.paginate_array(templates)), with: ::API::Entities::License
|
2016-08-08 11:42:24 +00:00
|
|
|
end
|
|
|
|
|
2017-01-27 18:53:27 +00:00
|
|
|
desc 'Get the text for a specific license' do
|
|
|
|
detail 'This feature was introduced in GitLab 8.7.'
|
2017-10-05 08:48:05 +00:00
|
|
|
success ::API::Entities::License
|
2017-01-27 18:53:27 +00:00
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :name, type: String, desc: 'The name of the template'
|
|
|
|
end
|
|
|
|
get "templates/licenses/:name", requirements: { name: /[\w\.-]+/ } do
|
2018-10-02 23:00:38 +00:00
|
|
|
template = TemplateFinder.build(:licenses, nil, name: params[:name]).execute
|
2018-08-14 21:31:38 +00:00
|
|
|
|
|
|
|
not_found!('License') unless template.present?
|
2016-08-08 11:42:24 +00:00
|
|
|
|
2018-08-14 21:31:38 +00:00
|
|
|
template.resolve!(
|
|
|
|
project_name: params[:project].presence,
|
|
|
|
fullname: params[:fullname].presence || current_user&.name
|
|
|
|
)
|
2016-08-08 11:42:24 +00:00
|
|
|
|
2017-10-05 08:48:05 +00:00
|
|
|
present template, with: ::API::Entities::License
|
2016-08-08 11:42:24 +00:00
|
|
|
end
|
2016-11-02 15:41:32 +00:00
|
|
|
|
2016-08-08 11:42:24 +00:00
|
|
|
GLOBAL_TEMPLATE_TYPES.each do |template_type, properties|
|
|
|
|
gitlab_version = properties[:gitlab_version]
|
|
|
|
|
2017-01-27 18:53:27 +00:00
|
|
|
desc 'Get the list of the available template' do
|
|
|
|
detail "This feature was introduced in GitLab #{gitlab_version}."
|
|
|
|
success Entities::TemplatesList
|
|
|
|
end
|
2017-01-17 04:45:07 +00:00
|
|
|
params do
|
|
|
|
use :pagination
|
|
|
|
end
|
2017-01-27 18:53:27 +00:00
|
|
|
get "templates/#{template_type}" do
|
2018-10-02 23:00:38 +00:00
|
|
|
templates = ::Kaminari.paginate_array(TemplateFinder.build(template_type, nil).execute)
|
2017-02-06 18:38:17 +00:00
|
|
|
present paginate(templates), with: Entities::TemplatesList
|
2016-08-08 11:42:24 +00:00
|
|
|
end
|
|
|
|
|
2017-01-27 18:53:27 +00:00
|
|
|
desc 'Get the text for a specific template present in local filesystem' do
|
|
|
|
detail "This feature was introduced in GitLab #{gitlab_version}."
|
|
|
|
success Entities::Template
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :name, type: String, desc: 'The name of the template'
|
|
|
|
end
|
2018-12-04 15:59:01 +00:00
|
|
|
get "templates/#{template_type}/:name", requirements: { name: /[\w\.-]+/ } do
|
2018-10-02 23:00:38 +00:00
|
|
|
finder = TemplateFinder.build(template_type, nil, name: declared(params)[:name])
|
2018-08-28 13:14:39 +00:00
|
|
|
new_template = finder.execute
|
2016-08-08 11:42:24 +00:00
|
|
|
|
2017-01-27 18:53:27 +00:00
|
|
|
render_response(template_type, new_template)
|
2016-05-27 09:00:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|