From 03c733849c1fad9885b0b947e60744633b7f5bd6 Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Tue, 28 Aug 2018 15:14:39 +0200 Subject: [PATCH] Convert global templates to vendored templates via a ::TemplateFinder --- app/finders/template_finder.rb | 27 +++++++++++++++++++++++ app/helpers/blob_helper.rb | 29 +++++++++++++----------- lib/api/templates.rb | 9 +++----- spec/finders/template_finder_spec.rb | 33 ++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 19 deletions(-) create mode 100644 app/finders/template_finder.rb create mode 100644 spec/finders/template_finder_spec.rb diff --git a/app/finders/template_finder.rb b/app/finders/template_finder.rb new file mode 100644 index 00000000000..a9e6fc9edb3 --- /dev/null +++ b/app/finders/template_finder.rb @@ -0,0 +1,27 @@ +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 diff --git a/app/helpers/blob_helper.rb b/app/helpers/blob_helper.rb index 00ebafd177b..c9f2f1e3caf 100644 --- a/app/helpers/blob_helper.rb +++ b/app/helpers/blob_helper.rb @@ -158,32 +158,35 @@ module BlobHelper end def licenses_for_select - return @licenses_for_select if defined?(@licenses_for_select) - - 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 + @licenses_for_select ||= dropdown_names(LicenseTemplateFinder.new.execute) end def ref_project @ref_project ||= @target_project || @project 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 - @gitignore_names ||= Gitlab::Template::GitignoreTemplate.dropdown_names + @gitignore_names ||= dropdown_names(TemplateFinder.new(:gitignores).execute) end 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 def dockerfile_names - @dockerfile_names ||= Gitlab::Template::DockerfileTemplate.dropdown_names + @dockerfile_names ||= dropdown_names(TemplateFinder.new(:dockerfiles).execute) end def blob_editor_paths diff --git a/lib/api/templates.rb b/lib/api/templates.rb index 927baaea652..5d817d36032 100644 --- a/lib/api/templates.rb +++ b/lib/api/templates.rb @@ -4,15 +4,12 @@ module API GLOBAL_TEMPLATE_TYPES = { gitignores: { - klass: Gitlab::Template::GitignoreTemplate, gitlab_version: 8.8 }, gitlab_ci_ymls: { - klass: Gitlab::Template::GitlabCiYmlTemplate, gitlab_version: 8.9 }, dockerfiles: { - klass: Gitlab::Template::DockerfileTemplate, gitlab_version: 8.15 } }.freeze @@ -63,7 +60,6 @@ module API end GLOBAL_TEMPLATE_TYPES.each do |template_type, properties| - klass = properties[:klass] gitlab_version = properties[:gitlab_version] desc 'Get the list of the available template' do @@ -74,7 +70,7 @@ module API use :pagination end 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 end @@ -86,7 +82,8 @@ module API requires :name, type: String, desc: 'The name of the template' end 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) end diff --git a/spec/finders/template_finder_spec.rb b/spec/finders/template_finder_spec.rb new file mode 100644 index 00000000000..59c5427ee50 --- /dev/null +++ b/spec/finders/template_finder_spec.rb @@ -0,0 +1,33 @@ +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