2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
module API
|
2021-01-03 14:25:43 +05:30
|
|
|
class Templates < ::API::Base
|
2017-08-17 22:00:37 +05:30
|
|
|
include PaginationParams
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
GLOBAL_TEMPLATE_TYPES = {
|
2016-11-03 12:29:30 +05:30
|
|
|
gitignores: {
|
2021-10-27 15:23:28 +05:30
|
|
|
gitlab_version: 8.8,
|
2022-11-25 23:54:43 +05:30
|
|
|
feature_category: :source_code_management,
|
|
|
|
file_type: '.gitignore'
|
2016-11-03 12:29:30 +05:30
|
|
|
},
|
|
|
|
gitlab_ci_ymls: {
|
2021-10-27 15:23:28 +05:30
|
|
|
gitlab_version: 8.9,
|
2022-11-25 23:54:43 +05:30
|
|
|
feature_category: :pipeline_authoring,
|
|
|
|
file_type: 'GitLab CI/CD YAML'
|
2017-08-17 22:00:37 +05:30
|
|
|
},
|
|
|
|
dockerfiles: {
|
2021-10-27 15:23:28 +05:30
|
|
|
gitlab_version: 8.15,
|
2022-11-25 23:54:43 +05:30
|
|
|
feature_category: :source_code_management,
|
|
|
|
file_type: 'Dockerfile'
|
2016-11-03 12:29:30 +05:30
|
|
|
}
|
2016-06-22 15:30:34 +05:30
|
|
|
}.freeze
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
helpers do
|
|
|
|
def render_response(template_type, template)
|
|
|
|
not_found!(template_type.to_s.singularize) unless template
|
|
|
|
present template, with: Entities::Template
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
desc 'Get all license templates' do
|
2017-08-17 22:00:37 +05:30
|
|
|
detail 'This feature was introduced in GitLab 8.7.'
|
2018-03-17 18:26:18 +05:30
|
|
|
success ::API::Entities::License
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
params do
|
|
|
|
optional :popular, type: Boolean, desc: 'If passed, returns only popular licenses'
|
|
|
|
use :pagination
|
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
get "templates/licenses", feature_category: :source_code_management do
|
2018-11-20 20:47:30 +05:30
|
|
|
popular = declared(params)[:popular]
|
|
|
|
popular = to_boolean(popular) if popular.present?
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
templates = TemplateFinder.build(:licenses, nil, popular: popular).execute
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
present paginate(::Kaminari.paginate_array(templates)), with: ::API::Entities::License
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
desc 'Get a single license template' do
|
2017-08-17 22:00:37 +05:30
|
|
|
detail 'This feature was introduced in GitLab 8.7.'
|
2018-03-17 18:26:18 +05:30
|
|
|
success ::API::Entities::License
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
params do
|
2022-11-25 23:54:43 +05:30
|
|
|
requires :name, type: String, desc: 'The name of the license template'
|
|
|
|
optional :project, type: String, desc: 'The copyrighted project name'
|
|
|
|
optional :fullname, type: String, desc: 'The full-name of the copyright holder'
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
get "templates/licenses/:name", requirements: { name: /[\w\.-]+/ }, feature_category: :source_code_management do
|
2018-12-05 23:21:45 +05:30
|
|
|
template = TemplateFinder.build(:licenses, nil, name: params[:name]).execute
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
not_found!('License') unless template.present?
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
template.resolve!(
|
|
|
|
project_name: params[:project].presence,
|
|
|
|
fullname: params[:fullname].presence || current_user&.name
|
|
|
|
)
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
present template, with: ::API::Entities::License
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
GLOBAL_TEMPLATE_TYPES.each do |template_type, properties|
|
|
|
|
gitlab_version = properties[:gitlab_version]
|
2022-11-25 23:54:43 +05:30
|
|
|
file_type = properties[:file_type]
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
desc "Get all #{file_type} templates" do
|
2017-08-17 22:00:37 +05:30
|
|
|
detail "This feature was introduced in GitLab #{gitlab_version}."
|
|
|
|
success Entities::TemplatesList
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
use :pagination
|
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
get "templates/#{template_type}", feature_category: properties[:feature_category] do
|
2018-12-05 23:21:45 +05:30
|
|
|
templates = ::Kaminari.paginate_array(TemplateFinder.build(template_type, nil).execute)
|
2017-08-17 22:00:37 +05:30
|
|
|
present paginate(templates), with: Entities::TemplatesList
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
desc "Get a single #{file_type} template" do
|
2017-08-17 22:00:37 +05:30
|
|
|
detail "This feature was introduced in GitLab #{gitlab_version}."
|
|
|
|
success Entities::Template
|
|
|
|
end
|
|
|
|
params do
|
2022-11-25 23:54:43 +05:30
|
|
|
requires :name, type: String, desc: "The name of the #{file_type} template"
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
get "templates/#{template_type}/:name", requirements: { name: /[\w\.-]+/ }, feature_category: properties[:feature_category] do
|
2018-12-05 23:21:45 +05:30
|
|
|
finder = TemplateFinder.build(template_type, nil, name: declared(params)[:name])
|
2018-11-20 20:47:30 +05:30
|
|
|
new_template = finder.execute
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
render_response(template_type, new_template)
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|