/lib/api/templates.rb

https://gitlab.com/vicvega/gitlab-ce · Ruby · 116 lines · 102 code · 12 blank · 2 comment · 7 complexity · 274b65d9f170ab87b0d1d5bc70fc84eb MD5 · raw file

  1. module API
  2. class Templates < Grape::API
  3. GLOBAL_TEMPLATE_TYPES = {
  4. gitignores: {
  5. klass: Gitlab::Template::GitignoreTemplate,
  6. gitlab_version: 8.8
  7. },
  8. gitlab_ci_ymls: {
  9. klass: Gitlab::Template::GitlabCiYmlTemplate,
  10. gitlab_version: 8.9
  11. }
  12. }.freeze
  13. PROJECT_TEMPLATE_REGEX =
  14. /[\<\{\[]
  15. (project|description|
  16. one\sline\s.+\swhat\sit\sdoes\.) # matching the start and end is enough here
  17. [\>\}\]]/xi.freeze
  18. YEAR_TEMPLATE_REGEX = /[<{\[](year|yyyy)[>}\]]/i.freeze
  19. FULLNAME_TEMPLATE_REGEX =
  20. /[\<\{\[]
  21. (fullname|name\sof\s(author|copyright\sowner))
  22. [\>\}\]]/xi.freeze
  23. DEPRECATION_MESSAGE = ' This endpoint is deprecated and will be removed in GitLab 9.0.'.freeze
  24. helpers do
  25. def parsed_license_template
  26. # We create a fresh Licensee::License object since we'll modify its
  27. # content in place below.
  28. template = Licensee::License.new(params[:name])
  29. template.content.gsub!(YEAR_TEMPLATE_REGEX, Time.now.year.to_s)
  30. template.content.gsub!(PROJECT_TEMPLATE_REGEX, params[:project]) if params[:project].present?
  31. fullname = params[:fullname].presence || current_user.try(:name)
  32. template.content.gsub!(FULLNAME_TEMPLATE_REGEX, fullname) if fullname
  33. template
  34. end
  35. def render_response(template_type, template)
  36. not_found!(template_type.to_s.singularize) unless template
  37. present template, with: Entities::Template
  38. end
  39. end
  40. { "licenses" => :deprecated, "templates/licenses" => :ok }.each do |route, status|
  41. desc 'Get the list of the available license template' do
  42. detailed_desc = 'This feature was introduced in GitLab 8.7.'
  43. detailed_desc << DEPRECATION_MESSAGE unless status == :ok
  44. detail detailed_desc
  45. success Entities::RepoLicense
  46. end
  47. params do
  48. optional :popular, type: Boolean, desc: 'If passed, returns only popular licenses'
  49. end
  50. get route do
  51. options = {
  52. featured: declared(params).popular.present? ? true : nil
  53. }
  54. present Licensee::License.all(options), with: Entities::RepoLicense
  55. end
  56. end
  57. { "licenses/:name" => :deprecated, "templates/licenses/:name" => :ok }.each do |route, status|
  58. desc 'Get the text for a specific license' do
  59. detailed_desc = 'This feature was introduced in GitLab 8.7.'
  60. detailed_desc << DEPRECATION_MESSAGE unless status == :ok
  61. detail detailed_desc
  62. success Entities::RepoLicense
  63. end
  64. params do
  65. requires :name, type: String, desc: 'The name of the template'
  66. end
  67. get route, requirements: { name: /[\w\.-]+/ } do
  68. not_found!('License') unless Licensee::License.find(declared(params).name)
  69. template = parsed_license_template
  70. present template, with: Entities::RepoLicense
  71. end
  72. end
  73. GLOBAL_TEMPLATE_TYPES.each do |template_type, properties|
  74. klass = properties[:klass]
  75. gitlab_version = properties[:gitlab_version]
  76. { template_type => :deprecated, "templates/#{template_type}" => :ok }.each do |route, status|
  77. desc 'Get the list of the available template' do
  78. detailed_desc = "This feature was introduced in GitLab #{gitlab_version}."
  79. detailed_desc << DEPRECATION_MESSAGE unless status == :ok
  80. detail detailed_desc
  81. success Entities::TemplatesList
  82. end
  83. get route do
  84. present klass.all, with: Entities::TemplatesList
  85. end
  86. end
  87. { "#{template_type}/:name" => :deprecated, "templates/#{template_type}/:name" => :ok }.each do |route, status|
  88. desc 'Get the text for a specific template present in local filesystem' do
  89. detailed_desc = "This feature was introduced in GitLab #{gitlab_version}."
  90. detailed_desc << DEPRECATION_MESSAGE unless status == :ok
  91. detail detailed_desc
  92. success Entities::Template
  93. end
  94. params do
  95. requires :name, type: String, desc: 'The name of the template'
  96. end
  97. get route do
  98. new_template = klass.find(declared(params).name)
  99. render_response(template_type, new_template)
  100. end
  101. end
  102. end
  103. end
  104. end