diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-02-08 21:09:49 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-02-08 21:09:49 +0300 |
commit | 89b770bb38aef8c0b895454e940d8f55a3038527 (patch) | |
tree | 83d0d7966b207747091f7ba6d892184f1e33bbcb /lib/gitlab/template | |
parent | 3bc30c280c408f3f31c90961e0fc5809c6246137 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/gitlab/template')
-rw-r--r-- | lib/gitlab/template/base_template.rb | 34 | ||||
-rw-r--r-- | lib/gitlab/template/finders/repo_template_finder.rb | 6 | ||||
-rw-r--r-- | lib/gitlab/template/issue_template.rb | 10 | ||||
-rw-r--r-- | lib/gitlab/template/merge_request_template.rb | 10 |
4 files changed, 45 insertions, 15 deletions
diff --git a/lib/gitlab/template/base_template.rb b/lib/gitlab/template/base_template.rb index b659bff52ad..02b3da54685 100644 --- a/lib/gitlab/template/base_template.rb +++ b/lib/gitlab/template/base_template.rb @@ -95,19 +95,29 @@ module Gitlab File.join(base_dir, categories[category]) end - # If template is organized by category it returns { category_name: [{ name: template_name }, { name: template2_name }] } - # If no category is present returns [{ name: template_name }, { name: template2_name}] - def dropdown_names(project = nil) - return [] if project && !project.repository.exists? + # `repository_template_names` - reads through Gitaly the actual templates names within a + # given project's repository. This is only used by issue and merge request templates, + # that need to call this once and then cache the returned value. + # + # `template_names` - is an alias to `repository_template_names`. It would read through + # Gitaly the actual template names within a given project's repository for all file templates + # other than `issue` and `merge request` description templates, which would instead + # overwrite the `template_names` method to return a redis cached version, by reading cached values + # from `repository.issue_template_names_by_category` and `repository.merge_request_template_names_by_category` + # methods. + def repository_template_names(project) + template_names_by_category(self.all(project)) + end + alias_method :template_names, :repository_template_names - if categories.any? - categories.keys.map do |category| - files = self.by_category(category, project) - [category, files.map { |t| { name: t.name } }] - end.to_h - else - files = self.all(project) - files.map { |t| { name: t.name } } + def template_names_by_category(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.key, key: item.key, project_id: item.try(:project_id) } + end end end diff --git a/lib/gitlab/template/finders/repo_template_finder.rb b/lib/gitlab/template/finders/repo_template_finder.rb index 8e234148a63..9f0ba97bcdf 100644 --- a/lib/gitlab/template/finders/repo_template_finder.rb +++ b/lib/gitlab/template/finders/repo_template_finder.rb @@ -11,8 +11,8 @@ module Gitlab def initialize(project, base_dir, extension, categories = {}) @categories = categories @extension = extension - @repository = project.repository - @commit = @repository.head_commit if @repository.exists? + @repository = project&.repository + @commit = @repository.head_commit if @repository&.exists? super(base_dir) end @@ -51,7 +51,7 @@ module Gitlab private def select_directory(file_name) - return [] unless @commit + return unless @commit # Insert root as directory directories = ["", *@categories.keys] diff --git a/lib/gitlab/template/issue_template.rb b/lib/gitlab/template/issue_template.rb index 01b191733d4..3049f43b322 100644 --- a/lib/gitlab/template/issue_template.rb +++ b/lib/gitlab/template/issue_template.rb @@ -15,6 +15,16 @@ module Gitlab def finder(project) Gitlab::Template::Finders::RepoTemplateFinder.new(project, self.base_dir, self.extension, self.categories) end + + def template_names(project) + return {} unless project&.repository&.exists? + + # here we rely on project.repository caching mechanism. Ideally we would want the template finder to have its + # own caching mechanism to avoid the back and forth call jumps between finder and model. + # + # follow-up issue: https://gitlab.com/gitlab-org/gitlab/-/issues/300279 + project.repository.issue_template_names_by_category + end end end end diff --git a/lib/gitlab/template/merge_request_template.rb b/lib/gitlab/template/merge_request_template.rb index 357b31cd82e..9442f3b13fb 100644 --- a/lib/gitlab/template/merge_request_template.rb +++ b/lib/gitlab/template/merge_request_template.rb @@ -15,6 +15,16 @@ module Gitlab def finder(project) Gitlab::Template::Finders::RepoTemplateFinder.new(project, self.base_dir, self.extension, self.categories) end + + def template_names(project) + return {} unless project&.repository&.exists? + + # here we rely on project.repository caching mechanism. Ideally we would want the template finder to have its + # own caching mechanism to avoid the back and forth call jumps between finder and model. + # + # follow-up issue: https://gitlab.com/gitlab-org/gitlab/-/issues/300279 + project.repository.merge_request_template_names_by_category + end end end end |