Commit 03c73384 authored by Nick Thomas's avatar Nick Thomas

Convert global templates to vendored templates via a ::TemplateFinder

parent db28db41
class TemplateFinder
VENDORED_TEMPLATES = {
dockerfiles: ::Gitlab::Template::DockerfileTemplate,
gitignores: ::Gitlab::Template::GitignoreTemplate,
gitlab_ci_ymls: ::Gitlab::Template::GitlabCiYmlTemplate
}.freeze
attr_reader :type, :params
attr_reader :vendored_templates
private :vendored_templates
def initialize(type, params = {})
@type = type
@params = params
@vendored_templates = VENDORED_TEMPLATES.fetch(type)
end
def execute
if params[:name]
vendored_templates.find(params[:name])
else
vendored_templates.all
end
end
end
...@@ -158,32 +158,35 @@ module BlobHelper ...@@ -158,32 +158,35 @@ module BlobHelper
end end
def licenses_for_select def licenses_for_select
return @licenses_for_select if defined?(@licenses_for_select) @licenses_for_select ||= dropdown_names(LicenseTemplateFinder.new.execute)
grouped_licenses = LicenseTemplateFinder.new.execute.group_by(&:category)
categories = grouped_licenses.keys
@licenses_for_select = categories.each_with_object({}) do |category, hash|
hash[category] = grouped_licenses[category].map do |license|
{ name: license.name, id: license.id }
end
end
end end
def ref_project def ref_project
@ref_project ||= @target_project || @project @ref_project ||= @target_project || @project
end end
def dropdown_names(items)
grouped = items.group_by(&:category)
categories = grouped.keys
categories.each_with_object({}) do |category, hash|
hash[category] = grouped[category].map do |item|
{ name: item.name, id: item.id }
end
end
end
private :dropdown_names
def gitignore_names def gitignore_names
@gitignore_names ||= Gitlab::Template::GitignoreTemplate.dropdown_names @gitignore_names ||= dropdown_names(TemplateFinder.new(:gitignores).execute)
end end
def gitlab_ci_ymls def gitlab_ci_ymls
@gitlab_ci_ymls ||= Gitlab::Template::GitlabCiYmlTemplate.dropdown_names(params[:context]) @gitlab_ci_ymls ||= dropdown_names(TemplateFinder.new(:gitlab_ci_ymls).execute)
end end
def dockerfile_names def dockerfile_names
@dockerfile_names ||= Gitlab::Template::DockerfileTemplate.dropdown_names @dockerfile_names ||= dropdown_names(TemplateFinder.new(:dockerfiles).execute)
end end
def blob_editor_paths def blob_editor_paths
......
...@@ -4,15 +4,12 @@ module API ...@@ -4,15 +4,12 @@ module API
GLOBAL_TEMPLATE_TYPES = { GLOBAL_TEMPLATE_TYPES = {
gitignores: { gitignores: {
klass: Gitlab::Template::GitignoreTemplate,
gitlab_version: 8.8 gitlab_version: 8.8
}, },
gitlab_ci_ymls: { gitlab_ci_ymls: {
klass: Gitlab::Template::GitlabCiYmlTemplate,
gitlab_version: 8.9 gitlab_version: 8.9
}, },
dockerfiles: { dockerfiles: {
klass: Gitlab::Template::DockerfileTemplate,
gitlab_version: 8.15 gitlab_version: 8.15
} }
}.freeze }.freeze
...@@ -63,7 +60,6 @@ module API ...@@ -63,7 +60,6 @@ module API
end end
GLOBAL_TEMPLATE_TYPES.each do |template_type, properties| GLOBAL_TEMPLATE_TYPES.each do |template_type, properties|
klass = properties[:klass]
gitlab_version = properties[:gitlab_version] gitlab_version = properties[:gitlab_version]
desc 'Get the list of the available template' do desc 'Get the list of the available template' do
...@@ -74,7 +70,7 @@ module API ...@@ -74,7 +70,7 @@ module API
use :pagination use :pagination
end end
get "templates/#{template_type}" do get "templates/#{template_type}" do
templates = ::Kaminari.paginate_array(klass.all) templates = ::Kaminari.paginate_array(TemplateFinder.new(template_type).execute)
present paginate(templates), with: Entities::TemplatesList present paginate(templates), with: Entities::TemplatesList
end end
...@@ -86,7 +82,8 @@ module API ...@@ -86,7 +82,8 @@ module API
requires :name, type: String, desc: 'The name of the template' requires :name, type: String, desc: 'The name of the template'
end end
get "templates/#{template_type}/:name" do get "templates/#{template_type}/:name" do
new_template = klass.find(declared(params)[:name]) finder = TemplateFinder.new(template_type, name: declared(params)[:name])
new_template = finder.execute
render_response(template_type, new_template) render_response(template_type, new_template)
end end
......
require 'spec_helper'
describe TemplateFinder do
using RSpec::Parameterized::TableSyntax
describe '#execute' do
where(:type, :vendored_name) do
:dockerfiles | 'Binary'
:gitignores | 'Actionscript'
:gitlab_ci_ymls | 'Android'
end
with_them do
it 'returns all vendored templates when no name is specified' do
result = described_class.new(type).execute
expect(result).to include(have_attributes(name: vendored_name))
end
it 'returns only the specified vendored template when a name is specified' do
result = described_class.new(type, name: vendored_name).execute
expect(result).to have_attributes(name: vendored_name)
end
it 'returns nil when an unknown name is specified' do
result = described_class.new(type, name: 'unknown').execute
expect(result).to be_nil
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment