2018-10-02 19:00:38 -04:00
# frozen_string_literal: true
module API
2020-10-14 20:08:42 -04:00
class ProjectTemplates < :: API :: Base
2018-10-02 19:00:38 -04:00
include PaginationParams
2021-05-06 11:10:17 -04:00
TEMPLATE_TYPES = %w[ dockerfiles gitignores gitlab_ci_ymls licenses metrics_dashboard_ymls issues merge_requests ] . freeze
2020-05-13 11:08:23 -04:00
# The regex is needed to ensure a period (e.g. agpl-3.0)
# isn't confused with a format type. We also need to allow encoded
# values (e.g. C%2B%2B for C++), so allow % and + as well.
TEMPLATE_NAMES_ENDPOINT_REQUIREMENTS = API :: NAMESPACE_OR_PROJECT_REQUIREMENTS . merge ( name : / [ \ w%.+-]+ / )
2018-10-02 19:00:38 -04:00
before { authenticate_non_get! }
2021-07-30 08:10:12 -04:00
feature_category :source_code_management
2020-10-30 14:08:56 -04:00
2018-10-02 19:00:38 -04:00
params do
requires :id , type : String , desc : 'The ID of a project'
2021-05-06 11:10:17 -04:00
requires :type , type : String , values : TEMPLATE_TYPES , desc : 'The type (dockerfiles|gitignores|gitlab_ci_ymls|licenses|metrics_dashboard_ymls|issues|merge_requests) of the template'
2018-10-02 19:00:38 -04:00
end
2020-04-28 11:09:29 -04:00
resource :projects , requirements : API :: NAMESPACE_OR_PROJECT_REQUIREMENTS do
2018-10-02 19:00:38 -04:00
desc 'Get a list of templates available to this project' do
detail 'This endpoint was introduced in GitLab 11.4'
end
params do
use :pagination
end
get ':id/templates/:type' do
2021-05-28 02:10:48 -04:00
templates = TemplateFinder . all_template_names ( user_project , params [ :type ] ) . values . flatten
2018-10-02 19:00:38 -04:00
present paginate ( :: Kaminari . paginate_array ( templates ) ) , with : Entities :: TemplatesList
end
desc 'Download a template available to this project' do
detail 'This endpoint was introduced in GitLab 11.4'
end
params do
requires :name , type : String , desc : 'The name of the template'
2021-02-11 13:09:10 -05:00
optional :source_template_project_id , type : Integer ,
desc : 'The project id where a given template is being stored. This is useful when multiple templates from different projects have the same name'
2018-10-02 19:00:38 -04:00
optional :project , type : String , desc : 'The project name to use when expanding placeholders in the template. Only affects licenses'
optional :fullname , type : String , desc : 'The full name of the copyright holder to use when expanding placeholders in the template. Only affects licenses'
end
2020-05-13 11:08:23 -04:00
get ':id/templates/:type/:name' , requirements : TEMPLATE_NAMES_ENDPOINT_REQUIREMENTS do
2020-08-11 23:10:17 -04:00
begin
2021-02-11 13:09:10 -05:00
template = TemplateFinder . build (
params [ :type ] ,
user_project ,
{
name : params [ :name ] ,
source_template_project_id : params [ :source_template_project_id ]
}
) . execute
2020-08-11 23:10:17 -04:00
rescue :: Gitlab :: Template :: Finders :: RepoTemplateFinder :: FileNotFoundError
not_found! ( 'Template' )
end
2018-10-02 19:00:38 -04:00
not_found! ( 'Template' ) unless template . present?
template . resolve! (
project_name : params [ :project ] . presence ,
fullname : params [ :fullname ] . presence || current_user & . name
)
if template . is_a? ( :: LicenseTemplate )
present template , with : Entities :: License
else
present template , with : Entities :: Template
end
end
end
end
end