2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
class TemplateFinder
|
2018-12-05 23:21:45 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
VENDORED_TEMPLATES = HashWithIndifferentAccess.new(
|
2018-11-20 20:47:30 +05:30
|
|
|
dockerfiles: ::Gitlab::Template::DockerfileTemplate,
|
|
|
|
gitignores: ::Gitlab::Template::GitignoreTemplate,
|
2020-10-24 23:57:45 +05:30
|
|
|
gitlab_ci_ymls: ::Gitlab::Template::GitlabCiYmlTemplate,
|
2021-03-08 18:12:59 +05:30
|
|
|
gitlab_ci_syntax_ymls: ::Gitlab::Template::GitlabCiSyntaxYmlTemplate,
|
2020-10-24 23:57:45 +05:30
|
|
|
metrics_dashboard_ymls: ::Gitlab::Template::MetricsDashboardTemplate,
|
|
|
|
issues: ::Gitlab::Template::IssueTemplate,
|
|
|
|
merge_requests: ::Gitlab::Template::MergeRequestTemplate
|
2018-12-05 23:21:45 +05:30
|
|
|
).freeze
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
class << self
|
2018-12-05 23:21:45 +05:30
|
|
|
def build(type, project, params = {})
|
|
|
|
if type.to_s == 'licenses'
|
|
|
|
LicenseTemplateFinder.new(project, params) # rubocop: disable CodeReuse/Finder
|
2018-11-20 20:47:30 +05:30
|
|
|
else
|
2018-12-05 23:21:45 +05:30
|
|
|
new(type, project, params)
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
# This is temporary and will be removed once we introduce group level inherited templates and
|
|
|
|
# remove the inherited_issuable_templates FF
|
|
|
|
def all_template_names_hash_or_array(project, issuable_type)
|
|
|
|
if project.inherited_issuable_templates_enabled?
|
|
|
|
all_template_names(project, issuable_type.pluralize)
|
|
|
|
else
|
|
|
|
all_template_names_array(project, issuable_type.pluralize)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
def all_template_names(project, type)
|
|
|
|
return {} if !VENDORED_TEMPLATES.key?(type.to_s) && type.to_s != 'licenses'
|
|
|
|
|
|
|
|
build(type, project).template_names
|
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
# This is for issues and merge requests description templates only.
|
|
|
|
# This will be removed once we introduce group level inherited templates and remove the inherited_issuable_templates FF
|
2021-03-11 19:13:27 +05:30
|
|
|
def all_template_names_array(project, type)
|
2021-04-17 20:07:23 +05:30
|
|
|
all_template_names(project, type).values.flatten.select { |tmpl| tmpl[:project_id] == project.id }.compact.uniq
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
attr_reader :type, :project, :params
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
attr_reader :vendored_templates
|
|
|
|
private :vendored_templates
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
def initialize(type, project, params = {})
|
2018-11-20 20:47:30 +05:30
|
|
|
@type = type
|
2018-12-05 23:21:45 +05:30
|
|
|
@project = project
|
2018-11-20 20:47:30 +05:30
|
|
|
@params = params
|
|
|
|
|
|
|
|
@vendored_templates = VENDORED_TEMPLATES.fetch(type)
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
if params[:name]
|
2020-10-24 23:57:45 +05:30
|
|
|
vendored_templates.find(params[:name], project)
|
2018-11-20 20:47:30 +05:30
|
|
|
else
|
2020-10-24 23:57:45 +05:30
|
|
|
vendored_templates.all(project)
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
def template_names
|
|
|
|
vendored_templates.template_names(project)
|
|
|
|
end
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
TemplateFinder.prepend_if_ee('::EE::TemplateFinder')
|