diff options
author | Robert Speicher <robert@gitlab.com> | 2018-10-05 17:40:32 +0000 |
---|---|---|
committer | Robert Speicher <robert@gitlab.com> | 2018-10-05 17:40:32 +0000 |
commit | b407061a281607e0c4bf22d2b7e94a068768ccac (patch) | |
tree | 7bfb1d7ffbde4d9aa40f3a05c1bbfb829732c807 /app/finders | |
parent | 2c9c7d72383c6e7232077b4949b98bb71c90e521 (diff) | |
parent | 25bd49e4f57fe15f9d61dc9376a5b7dc35b30f64 (diff) | |
download | gitlab-ce-b407061a281607e0c4bf22d2b7e94a068768ccac.tar.gz |
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 'app/finders')
-rw-r--r-- | app/finders/license_template_finder.rb | 40 | ||||
-rw-r--r-- | app/finders/template_finder.rb | 19 |
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) |