2018-09-11 15:08:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-08-14 17:29:59 -04:00
|
|
|
# LicenseTemplateFinder
|
|
|
|
#
|
|
|
|
# Used to find license templates, which may come from a variety of external
|
|
|
|
# sources
|
|
|
|
#
|
2018-10-02 19:00:38 -04:00
|
|
|
# Params can be any of the following:
|
2018-08-14 17:29:59 -04:00
|
|
|
# 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.
|
2018-10-02 19:00:38 -04:00
|
|
|
# name: string. If set, return a single license matching that name (or nil)
|
2018-08-14 17:29:59 -04:00
|
|
|
class LicenseTemplateFinder
|
2018-10-02 19:00:38 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2018-08-14 17:29:59 -04:00
|
|
|
|
2019-09-13 09:26:31 -04:00
|
|
|
prepend_if_ee('::EE::LicenseTemplateFinder') # rubocop: disable Cop/InjectEnterpriseEditionModule
|
|
|
|
|
2018-10-02 19:00:38 -04:00
|
|
|
attr_reader :project, :params
|
|
|
|
|
|
|
|
def initialize(project, params = {})
|
|
|
|
@project = project
|
2018-08-14 17:29:59 -04:00
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2018-10-02 19:00:38 -04:00
|
|
|
if params[:name]
|
|
|
|
vendored_licenses.find { |template| template.key == params[:name] }
|
|
|
|
else
|
|
|
|
vendored_licenses
|
2018-08-14 17:29:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-10-02 19:00:38 -04:00
|
|
|
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
|
|
|
|
|
2018-08-14 17:29:59 -04:00
|
|
|
def popular_only?
|
|
|
|
params.fetch(:popular, nil)
|
|
|
|
end
|
|
|
|
end
|