diff options
author | Robert Speicher <robert@gitlab.com> | 2018-10-05 20:40:32 +0300 |
---|---|---|
committer | Robert Speicher <robert@gitlab.com> | 2018-10-05 20:40:32 +0300 |
commit | b407061a281607e0c4bf22d2b7e94a068768ccac (patch) | |
tree | 7bfb1d7ffbde4d9aa40f3a05c1bbfb829732c807 /lib | |
parent | 2c9c7d72383c6e7232077b4949b98bb71c90e521 (diff) | |
parent | 25bd49e4f57fe15f9d61dc9376a5b7dc35b30f64 (diff) |
Merge branch 'ce-5987-project-templates-api' into 'master'
Add a new project-specific templates API endpoint
See merge request gitlab-org/gitlab-ce!22118
Diffstat (limited to 'lib')
-rw-r--r-- | lib/api/api.rb | 1 | ||||
-rw-r--r-- | lib/api/entities.rb | 1 | ||||
-rw-r--r-- | lib/api/project_templates.rb | 59 | ||||
-rw-r--r-- | lib/api/templates.rb | 9 | ||||
-rw-r--r-- | lib/gitlab/template/base_template.rb | 11 |
5 files changed, 74 insertions, 7 deletions
diff --git a/lib/api/api.rb b/lib/api/api.rb index 06c8b48b8cc..c49c52213bf 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -132,6 +132,7 @@ module API mount ::API::Projects mount ::API::ProjectSnapshots mount ::API::ProjectSnippets + mount ::API::ProjectTemplates mount ::API::ProtectedBranches mount ::API::ProtectedTags mount ::API::Repositories diff --git a/lib/api/entities.rb b/lib/api/entities.rb index a78a93cbfd9..120545792f2 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -1221,6 +1221,7 @@ module API end class TemplatesList < Grape::Entity + expose :key expose :name end diff --git a/lib/api/project_templates.rb b/lib/api/project_templates.rb new file mode 100644 index 00000000000..d05ddad7466 --- /dev/null +++ b/lib/api/project_templates.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +module API + class ProjectTemplates < Grape::API + include PaginationParams + + TEMPLATE_TYPES = %w[dockerfiles gitignores gitlab_ci_ymls licenses].freeze + + before { authenticate_non_get! } + + params do + requires :id, type: String, desc: 'The ID of a project' + requires :type, type: String, values: TEMPLATE_TYPES, desc: 'The type (dockerfiles|gitignores|gitlab_ci_ymls|licenses) of the template' + end + resource :projects do + desc 'Get a list of templates available to this project' do + detail 'This endpoint was introduced in GitLab 11.4' + end + params do + use :pagination + end + get ':id/templates/:type' do + templates = TemplateFinder + .build(params[:type], user_project) + .execute + + present paginate(::Kaminari.paginate_array(templates)), with: Entities::TemplatesList + end + + desc 'Download a template available to this project' do + detail 'This endpoint was introduced in GitLab 11.4' + end + params do + requires :name, type: String, desc: 'The name of the template' + + optional :project, type: String, desc: 'The project name to use when expanding placeholders in the template. Only affects licenses' + optional :fullname, type: String, desc: 'The full name of the copyright holder to use when expanding placeholders in the template. Only affects licenses' + end + get ':id/templates/:type/:name', requirements: { name: /[\w\.-]+/ } do + template = TemplateFinder + .build(params[:type], user_project, name: params[:name]) + .execute + + not_found!('Template') unless template.present? + + template.resolve!( + project_name: params[:project].presence, + fullname: params[:fullname].presence || current_user&.name + ) + + if template.is_a?(::LicenseTemplate) + present template, with: Entities::License + else + present template, with: Entities::Template + end + end + end + end +end diff --git a/lib/api/templates.rb b/lib/api/templates.rb index 8ff3b2ac33c..8dab19d50c2 100644 --- a/lib/api/templates.rb +++ b/lib/api/templates.rb @@ -35,7 +35,7 @@ module API popular = declared(params)[:popular] popular = to_boolean(popular) if popular.present? - templates = TemplateFinder.build(:licenses, popular: popular).execute + templates = TemplateFinder.build(:licenses, nil, popular: popular).execute present paginate(::Kaminari.paginate_array(templates)), with: ::API::Entities::License end @@ -48,8 +48,7 @@ module API requires :name, type: String, desc: 'The name of the template' end get "templates/licenses/:name", requirements: { name: /[\w\.-]+/ } do - templates = TemplateFinder.build(:licenses).execute - template = templates.find { |template| template.key == params[:name] } + template = TemplateFinder.build(:licenses, nil, name: params[:name]).execute not_found!('License') unless template.present? @@ -72,7 +71,7 @@ module API use :pagination end get "templates/#{template_type}" do - templates = ::Kaminari.paginate_array(TemplateFinder.new(template_type).execute) + templates = ::Kaminari.paginate_array(TemplateFinder.build(template_type, nil).execute) present paginate(templates), with: Entities::TemplatesList end @@ -84,7 +83,7 @@ module API requires :name, type: String, desc: 'The name of the template' end get "templates/#{template_type}/:name" do - finder = TemplateFinder.build(template_type, name: declared(params)[:name]) + finder = TemplateFinder.build(template_type, nil, name: declared(params)[:name]) new_template = finder.execute render_response(template_type, new_template) diff --git a/lib/gitlab/template/base_template.rb b/lib/gitlab/template/base_template.rb index 3770f3f250b..4456217017f 100644 --- a/lib/gitlab/template/base_template.rb +++ b/lib/gitlab/template/base_template.rb @@ -12,14 +12,21 @@ module Gitlab def name File.basename(@path, self.class.extension) end - alias_method :id, :name + alias_method :key, :name def content @finder.read(@path) end + # Present for compatibility with license templates, which can replace text + # like `[fullname]` with a user-specified string. This is a no-op for + # other templates + def resolve!(_placeholders = {}) + self + end + def to_json - { name: name, content: content } + { key: key, name: name, content: content } end def <=>(other) |