2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
module API
2021-01-03 14:25:43 +05:30
class ProjectTemplates < :: API :: Base
2018-12-05 23:21:45 +05:30
include PaginationParams
2021-06-08 01:23:25 +05:30
TEMPLATE_TYPES = %w[ dockerfiles gitignores gitlab_ci_ymls licenses metrics_dashboard_ymls issues merge_requests ] . freeze
2020-05-24 23:13:21 +05:30
# 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-12-05 23:21:45 +05:30
before { authenticate_non_get! }
2021-10-27 15:23:28 +05:30
feature_category :source_code_management
2021-01-29 00:20:46 +05:30
2018-12-05 23:21:45 +05:30
params do
2023-01-13 00:05:48 +05:30
requires :id , types : [ String , Integer ] , desc : 'The ID or URL-encoded path of the project'
2021-06-08 01:23:25 +05:30
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-12-05 23:21:45 +05:30
end
2020-05-24 23:13:21 +05:30
resource :projects , requirements : API :: NAMESPACE_OR_PROJECT_REQUIREMENTS do
2018-12-05 23:21:45 +05:30
desc 'Get a list of templates available to this project' do
detail 'This endpoint was introduced in GitLab 11.4'
2023-01-13 00:05:48 +05:30
is_array true
success Entities :: TemplatesList
failure [
{ code : 401 , message : 'Unauthorized' } ,
{ code : 404 , message : 'Not found' }
]
2018-12-05 23:21:45 +05:30
end
params do
use :pagination
end
get ':id/templates/:type' do
2023-07-09 08:55:56 +05:30
bad_request! if params [ :type ] == 'metrics_dashboard_ymls' && Feature . enabled? ( :remove_monitor_metrics )
2021-09-04 01:27:46 +05:30
templates = TemplateFinder . all_template_names ( user_project , params [ :type ] ) . values . flatten
2018-12-05 23:21:45 +05:30
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'
2023-01-13 00:05:48 +05:30
success Entities :: License
failure [
{ code : 401 , message : 'Unauthorized' } ,
{ code : 404 , message : 'Not found' }
]
2018-12-05 23:21:45 +05:30
end
params do
2023-01-13 00:05:48 +05:30
requires :name , type : String ,
desc : 'The key of the template, as obtained from the collection endpoint.' , documentation : { example : 'MIT' }
2021-03-11 19:13:27 +05:30
optional :source_template_project_id , type : Integer ,
2023-01-13 00:05:48 +05:30
desc : 'The project id where a given template is being stored. This is useful when multiple templates from different projects have the same name' ,
documentation : { example : 1 }
optional :project , type : String ,
desc : 'The project name to use when expanding placeholders in the template. Only affects licenses' ,
documentation : { example : 'GitLab' }
optional :fullname , type : String ,
desc : 'The full name of the copyright holder to use when expanding placeholders in the template. Only affects licenses' ,
documentation : { example : 'GitLab B.V.' }
2018-12-05 23:21:45 +05:30
end
2020-05-24 23:13:21 +05:30
get ':id/templates/:type/:name' , requirements : TEMPLATE_NAMES_ENDPOINT_REQUIREMENTS do
2023-07-09 08:55:56 +05:30
bad_request! if params [ :type ] == 'metrics_dashboard_ymls' && Feature . enabled? ( :remove_monitor_metrics )
2020-10-24 23:57:45 +05:30
begin
2021-03-11 19:13:27 +05:30
template = TemplateFinder . build (
params [ :type ] ,
user_project ,
{
name : params [ :name ] ,
source_template_project_id : params [ :source_template_project_id ]
}
) . execute
2020-10-24 23:57:45 +05:30
rescue :: Gitlab :: Template :: Finders :: RepoTemplateFinder :: FileNotFoundError
not_found! ( 'Template' )
end
2018-12-05 23:21:45 +05:30
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