BigW Consortium Gitlab

templates.rb 4.31 KB
Newer Older
1 2
module API
  class Templates < Grape::API
3
    GLOBAL_TEMPLATE_TYPES = {
4 5 6 7 8 9 10
      gitignores: {
        klass: Gitlab::Template::GitignoreTemplate,
        gitlab_version: 8.8
      },
      gitlab_ci_ymls: {
        klass: Gitlab::Template::GitlabCiYmlTemplate,
        gitlab_version: 8.9
11
      },
Luke "Jared" Bennett committed
12 13
      dockerfiles: {
        klass: Gitlab::Template::DockerfileTemplate,
Kamil Trzciński committed
14
        gitlab_version: 8.15
Luke "Jared" Bennett committed
15
      }
16
    }.freeze
17 18 19 20 21 22 23 24 25 26 27
    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
    DEPRECATION_MESSAGE = ' This endpoint is deprecated and will be removed in GitLab 9.0.'.freeze
28

29
    helpers do
30 31 32 33 34 35 36 37 38 39 40 41 42
      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

43 44 45 46 47 48
      def render_response(template_type, template)
        not_found!(template_type.to_s.singularize) unless template
        present template, with: Entities::Template
      end
    end

49 50 51 52 53 54 55 56 57
    { "licenses" => :deprecated, "templates/licenses" => :ok }.each do |route, status|
      desc 'Get the list of the available license template' do
        detailed_desc = 'This feature was introduced in GitLab 8.7.'
        detailed_desc << DEPRECATION_MESSAGE unless status == :ok
        detail detailed_desc
        success Entities::RepoLicense
      end
      params do
        optional :popular, type: Boolean, desc: 'If passed, returns only popular licenses'
58
      end
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
      get route do
        options = {
          featured: declared(params).popular.present? ? true : nil
        }
        present Licensee::License.all(options), with: Entities::RepoLicense
      end
    end

    { "licenses/:name" => :deprecated, "templates/licenses/:name" => :ok }.each do |route, status|
      desc 'Get the text for a specific license' do
        detailed_desc = 'This feature was introduced in GitLab 8.7.'
        detailed_desc << DEPRECATION_MESSAGE unless status == :ok
        detail detailed_desc
        success Entities::RepoLicense
      end
      params do
        requires :name, type: String, desc: 'The name of the template'
76
      end
77 78 79 80 81 82 83 84
      get route, requirements: { name: /[\w\.-]+/ } do
        not_found!('License') unless Licensee::License.find(declared(params).name)

        template = parsed_license_template

        present template, with: Entities::RepoLicense
      end
    end
85

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    GLOBAL_TEMPLATE_TYPES.each do |template_type, properties|
      klass = properties[:klass]
      gitlab_version = properties[:gitlab_version]

      { template_type => :deprecated, "templates/#{template_type}" => :ok }.each do |route, status|
        desc 'Get the list of the available template' do
          detailed_desc = "This feature was introduced in GitLab #{gitlab_version}."
          detailed_desc << DEPRECATION_MESSAGE unless status == :ok
          detail detailed_desc
          success Entities::TemplatesList
        end
        get route do
          present klass.all, with: Entities::TemplatesList
        end
      end

      { "#{template_type}/:name" => :deprecated, "templates/#{template_type}/:name" => :ok }.each do |route, status|
        desc 'Get the text for a specific template present in local filesystem' do
          detailed_desc = "This feature was introduced in GitLab #{gitlab_version}."
          detailed_desc << DEPRECATION_MESSAGE unless status == :ok
          detail detailed_desc
          success Entities::Template
        end
        params do
          requires :name, type: String, desc: 'The name of the template'
111
        end
112 113 114 115 116
        get route do
          new_template = klass.find(declared(params).name)

          render_response(template_type, new_template)
        end
117 118 119 120
      end
    end
  end
end