2016-05-27 05:00:56 -04:00
|
|
|
module API
|
|
|
|
class Templates < Grape::API
|
2017-01-16 23:45:07 -05:00
|
|
|
include PaginationParams
|
|
|
|
|
2016-06-24 15:43:46 -04:00
|
|
|
GLOBAL_TEMPLATE_TYPES = {
|
2016-08-08 07:42:24 -04:00
|
|
|
gitignores: {
|
|
|
|
klass: Gitlab::Template::GitignoreTemplate,
|
|
|
|
gitlab_version: 8.8
|
|
|
|
},
|
|
|
|
gitlab_ci_ymls: {
|
|
|
|
klass: Gitlab::Template::GitlabCiYmlTemplate,
|
|
|
|
gitlab_version: 8.9
|
2016-11-02 11:41:32 -04:00
|
|
|
},
|
2016-11-08 07:42:58 -05:00
|
|
|
dockerfiles: {
|
|
|
|
klass: Gitlab::Template::DockerfileTemplate,
|
2016-12-16 04:17:15 -05:00
|
|
|
gitlab_version: 8.15
|
2016-11-08 07:42:58 -05:00
|
|
|
}
|
2016-05-27 05:00:56 -04:00
|
|
|
}.freeze
|
2016-08-08 07:42:24 -04:00
|
|
|
PROJECT_TEMPLATE_REGEX =
|
|
|
|
/[\<\{\[]
|
|
|
|
(project|description|
|
|
|
|
one\sline\s.+\swhat\sit\sdoes\.) # matching the start and end is enough here
|
|
|
|
[\>\}\]]/xi.freeze
|
|
|
|
YEAR_TEMPLATE_REGEX = /[<{\[](year|yyyy)[>}\]]/i.freeze
|
|
|
|
FULLNAME_TEMPLATE_REGEX =
|
|
|
|
/[\<\{\[]
|
|
|
|
(fullname|name\sof\s(author|copyright\sowner))
|
|
|
|
[\>\}\]]/xi.freeze
|
2016-05-27 05:00:56 -04:00
|
|
|
|
2016-06-24 15:43:46 -04:00
|
|
|
helpers do
|
2016-08-08 07:42:24 -04:00
|
|
|
def parsed_license_template
|
|
|
|
# We create a fresh Licensee::License object since we'll modify its
|
|
|
|
# content in place below.
|
|
|
|
template = Licensee::License.new(params[:name])
|
|
|
|
|
|
|
|
template.content.gsub!(YEAR_TEMPLATE_REGEX, Time.now.year.to_s)
|
|
|
|
template.content.gsub!(PROJECT_TEMPLATE_REGEX, params[:project]) if params[:project].present?
|
|
|
|
|
|
|
|
fullname = params[:fullname].presence || current_user.try(:name)
|
|
|
|
template.content.gsub!(FULLNAME_TEMPLATE_REGEX, fullname) if fullname
|
|
|
|
template
|
|
|
|
end
|
|
|
|
|
2016-06-24 15:43:46 -04:00
|
|
|
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 13:53:27 -05:00
|
|
|
desc 'Get the list of the available license template' do
|
|
|
|
detail 'This feature was introduced in GitLab 8.7.'
|
|
|
|
success ::API::Entities::RepoLicense
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
optional :popular, type: Boolean, desc: 'If passed, returns only popular licenses'
|
2017-01-16 23:45:07 -05:00
|
|
|
use :pagination
|
2017-01-27 13:53:27 -05:00
|
|
|
end
|
|
|
|
get "templates/licenses" do
|
|
|
|
options = {
|
|
|
|
featured: declared(params).popular.present? ? true : nil
|
|
|
|
}
|
2017-01-16 23:45:07 -05:00
|
|
|
licences = ::Kaminari.paginate_array(Licensee::License.all(options))
|
|
|
|
present paginate(licences), with: Entities::RepoLicense
|
2016-08-08 07:42:24 -04:00
|
|
|
end
|
|
|
|
|
2017-01-27 13:53:27 -05:00
|
|
|
desc 'Get the text for a specific license' do
|
|
|
|
detail 'This feature was introduced in GitLab 8.7.'
|
|
|
|
success ::API::Entities::RepoLicense
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :name, type: String, desc: 'The name of the template'
|
|
|
|
end
|
|
|
|
get "templates/licenses/:name", requirements: { name: /[\w\.-]+/ } do
|
|
|
|
not_found!('License') unless Licensee::License.find(declared(params).name)
|
2016-08-08 07:42:24 -04:00
|
|
|
|
2017-01-27 13:53:27 -05:00
|
|
|
template = parsed_license_template
|
2016-08-08 07:42:24 -04:00
|
|
|
|
2017-01-27 13:53:27 -05:00
|
|
|
present template, with: ::API::Entities::RepoLicense
|
2016-08-08 07:42:24 -04:00
|
|
|
end
|
2016-11-02 11:41:32 -04:00
|
|
|
|
2016-08-08 07:42:24 -04:00
|
|
|
GLOBAL_TEMPLATE_TYPES.each do |template_type, properties|
|
|
|
|
klass = properties[:klass]
|
|
|
|
gitlab_version = properties[:gitlab_version]
|
|
|
|
|
2017-01-27 13:53:27 -05: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-16 23:45:07 -05:00
|
|
|
params do
|
|
|
|
use :pagination
|
|
|
|
end
|
2017-01-27 13:53:27 -05:00
|
|
|
get "templates/#{template_type}" do
|
2017-01-16 23:45:07 -05:00
|
|
|
templates = ::Kaminari.paginate_array(klass.all)
|
2017-02-06 13:38:17 -05:00
|
|
|
present paginate(templates), with: Entities::TemplatesList
|
2016-08-08 07:42:24 -04:00
|
|
|
end
|
|
|
|
|
2017-01-27 13:53:27 -05: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
|
|
|
|
get "templates/#{template_type}/:name" do
|
|
|
|
new_template = klass.find(declared(params).name)
|
2016-08-08 07:42:24 -04:00
|
|
|
|
2017-01-27 13:53:27 -05:00
|
|
|
render_response(template_type, new_template)
|
2016-05-27 05:00:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|