summaryrefslogtreecommitdiff
path: root/app/finders
diff options
context:
space:
mode:
authorNick Thomas <nick@gitlab.com>2018-10-03 00:00:38 +0100
committerNick Thomas <nick@gitlab.com>2018-10-05 11:34:43 +0100
commit25bd49e4f57fe15f9d61dc9376a5b7dc35b30f64 (patch)
treefaef4e9d73e9845413462013c868eace19a11abf /app/finders
parentae014e189773f7299c12c1050334b3e8fe7b15d8 (diff)
downloadgitlab-ce-25bd49e4f57fe15f9d61dc9376a5b7dc35b30f64.tar.gz
Backport project template API to CE
Diffstat (limited to 'app/finders')
-rw-r--r--app/finders/license_template_finder.rb40
-rw-r--r--app/finders/template_finder.rb19
2 files changed, 38 insertions, 21 deletions
diff --git a/app/finders/license_template_finder.rb b/app/finders/license_template_finder.rb
index 196922709f7..d735a4c1d69 100644
--- a/app/finders/license_template_finder.rb
+++ b/app/finders/license_template_finder.rb
@@ -5,33 +5,47 @@
# Used to find license templates, which may come from a variety of external
# sources
#
-# Arguments:
+# Params can be any of the following:
# popular: boolean. When set to true, only "popular" licenses are shown. When
# false, all licenses except popular ones are shown. When nil (the
# default), *all* licenses will be shown.
+# name: string. If set, return a single license matching that name (or nil)
class LicenseTemplateFinder
- attr_reader :params
+ include Gitlab::Utils::StrongMemoize
- def initialize(params = {})
+ attr_reader :project, :params
+
+ def initialize(project, params = {})
+ @project = project
@params = params
end
def execute
- Licensee::License.all(featured: popular_only?).map do |license|
- LicenseTemplate.new(
- id: license.key,
- name: license.name,
- nickname: license.nickname,
- category: (license.featured? ? :Popular : :Other),
- content: license.content,
- url: license.url,
- meta: license.meta
- )
+ if params[:name]
+ vendored_licenses.find { |template| template.key == params[:name] }
+ else
+ vendored_licenses
end
end
private
+ def vendored_licenses
+ strong_memoize(:vendored_licenses) do
+ Licensee::License.all(featured: popular_only?).map do |license|
+ LicenseTemplate.new(
+ key: license.key,
+ name: license.name,
+ nickname: license.nickname,
+ category: (license.featured? ? :Popular : :Other),
+ content: license.content,
+ url: license.url,
+ meta: license.meta
+ )
+ end
+ end
+ end
+
def popular_only?
params.fetch(:popular, nil)
end
diff --git a/app/finders/template_finder.rb b/app/finders/template_finder.rb
index c92ee9ca9ac..3e483716064 100644
--- a/app/finders/template_finder.rb
+++ b/app/finders/template_finder.rb
@@ -1,29 +1,32 @@
# frozen_string_literal: true
class TemplateFinder
- VENDORED_TEMPLATES = {
+ include Gitlab::Utils::StrongMemoize
+
+ VENDORED_TEMPLATES = HashWithIndifferentAccess.new(
dockerfiles: ::Gitlab::Template::DockerfileTemplate,
gitignores: ::Gitlab::Template::GitignoreTemplate,
gitlab_ci_ymls: ::Gitlab::Template::GitlabCiYmlTemplate
- }.freeze
+ ).freeze
class << self
- def build(type, params = {})
- if type == :licenses
- LicenseTemplateFinder.new(params) # rubocop: disable CodeReuse/Finder
+ def build(type, project, params = {})
+ if type.to_s == 'licenses'
+ LicenseTemplateFinder.new(project, params) # rubocop: disable CodeReuse/Finder
else
- new(type, params)
+ new(type, project, params)
end
end
end
- attr_reader :type, :params
+ attr_reader :type, :project, :params
attr_reader :vendored_templates
private :vendored_templates
- def initialize(type, params = {})
+ def initialize(type, project, params = {})
@type = type
+ @project = project
@params = params
@vendored_templates = VENDORED_TEMPLATES.fetch(type)