templates.rb 3.6 KB
Newer Older
Z
ZJ van de Weg 已提交
1 2
module API
  class Templates < Grape::API
3 4
    include PaginationParams

5
    GLOBAL_TEMPLATE_TYPES = {
6 7 8 9 10 11 12
      gitignores: {
        klass: Gitlab::Template::GitignoreTemplate,
        gitlab_version: 8.8
      },
      gitlab_ci_ymls: {
        klass: Gitlab::Template::GitlabCiYmlTemplate,
        gitlab_version: 8.9
13
      },
L
Luke "Jared" Bennett 已提交
14 15
      dockerfiles: {
        klass: Gitlab::Template::DockerfileTemplate,
K
Kamil Trzciński 已提交
16
        gitlab_version: 8.15
L
Luke "Jared" Bennett 已提交
17
      }
Z
ZJ van de Weg 已提交
18
    }.freeze
19 20 21 22 23 24 25 26 27 28
    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
Z
ZJ van de Weg 已提交
29

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

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

50 51 52 53 54 55
    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'
56
      use :pagination
57 58 59 60 61
    end
    get "templates/licenses" do
      options = {
        featured: declared(params).popular.present? ? true : nil
      }
62 63
      licences = ::Kaminari.paginate_array(Licensee::License.all(options))
      present paginate(licences), with: Entities::RepoLicense
64 65
    end

66 67 68 69 70 71 72 73 74
    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)
75

76
      template = parsed_license_template
77

78
      present template, with: ::API::Entities::RepoLicense
79
    end
80

81 82 83 84
    GLOBAL_TEMPLATE_TYPES.each do |template_type, properties|
      klass = properties[:klass]
      gitlab_version = properties[:gitlab_version]

85 86 87 88
      desc 'Get the list of the available template' do
        detail "This feature was introduced in GitLab #{gitlab_version}."
        success Entities::TemplatesList
      end
89 90 91
      params do
        use :pagination
      end
92
      get "templates/#{template_type}" do
93
        templates = ::Kaminari.paginate_array(klass.all)
R
Robert Schilling 已提交
94
        present paginate(templates), with: Entities::TemplatesList
95 96
      end

97 98 99 100 101 102 103 104 105
      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)
106

107
        render_response(template_type, new_template)
Z
ZJ van de Weg 已提交
108 109 110 111
      end
    end
  end
end