PageRenderTime 41ms CodeModel.GetById 14ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  1module 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
 25    helpers do
 26      def parsed_license_template
 27        # We create a fresh Licensee::License object since we'll modify its
 28        # content in place below.
 29        template = Licensee::License.new(params[:name])
 30
 31        template.content.gsub!(YEAR_TEMPLATE_REGEX, Time.now.year.to_s)
 32        template.content.gsub!(PROJECT_TEMPLATE_REGEX, params[:project]) if params[:project].present?
 33
 34        fullname = params[:fullname].presence || current_user.try(:name)
 35        template.content.gsub!(FULLNAME_TEMPLATE_REGEX, fullname) if fullname
 36        template
 37      end
 38
 39      def render_response(template_type, template)
 40        not_found!(template_type.to_s.singularize) unless template
 41        present template, with: Entities::Template
 42      end
 43    end
 44
 45    { "licenses" => :deprecated, "templates/licenses" => :ok }.each do |route, status|
 46      desc 'Get the list of the available license template' do
 47        detailed_desc = 'This feature was introduced in GitLab 8.7.'
 48        detailed_desc << DEPRECATION_MESSAGE unless status == :ok
 49        detail detailed_desc
 50        success Entities::RepoLicense
 51      end
 52      params do
 53        optional :popular, type: Boolean, desc: 'If passed, returns only popular licenses'
 54      end 
 55      get route do
 56        options = {
 57          featured: declared(params).popular.present? ? true : nil
 58        }
 59        present Licensee::License.all(options), with: Entities::RepoLicense
 60      end
 61    end
 62
 63    { "licenses/:name" => :deprecated, "templates/licenses/:name" => :ok }.each do |route, status|
 64      desc 'Get the text for a specific license' do
 65        detailed_desc = 'This feature was introduced in GitLab 8.7.'
 66        detailed_desc << DEPRECATION_MESSAGE unless status == :ok
 67        detail detailed_desc
 68        success Entities::RepoLicense
 69      end
 70      params do
 71        requires :name, type: String, desc: 'The name of the template'
 72      end 
 73      get route, requirements: { name: /[\w\.-]+/ } do
 74        not_found!('License') unless Licensee::License.find(declared(params).name)
 75
 76        template = parsed_license_template
 77
 78        present template, with: Entities::RepoLicense
 79      end
 80    end
 81      
 82    GLOBAL_TEMPLATE_TYPES.each do |template_type, properties|
 83      klass = properties[:klass]
 84      gitlab_version = properties[:gitlab_version]
 85
 86      { template_type => :deprecated, "templates/#{template_type}" => :ok }.each do |route, status|
 87        desc 'Get the list of the available template' do
 88          detailed_desc = "This feature was introduced in GitLab #{gitlab_version}."
 89          detailed_desc << DEPRECATION_MESSAGE unless status == :ok
 90          detail detailed_desc
 91          success Entities::TemplatesList
 92        end
 93        get route do
 94          present klass.all, with: Entities::TemplatesList
 95        end
 96      end
 97
 98      { "#{template_type}/:name" => :deprecated, "templates/#{template_type}/:name" => :ok }.each do |route, status|
 99        desc 'Get the text for a specific template present in local filesystem' do
100          detailed_desc = "This feature was introduced in GitLab #{gitlab_version}."
101          detailed_desc << DEPRECATION_MESSAGE unless status == :ok
102          detail detailed_desc
103          success Entities::Template
104        end
105        params do
106          requires :name, type: String, desc: 'The name of the template'
107        end 
108        get route do
109          new_template = klass.find(declared(params).name)
110
111          render_response(template_type, new_template)
112        end
113      end
114    end
115  end
116end