Commit b7303b65 authored by Sean McGivern's avatar Sean McGivern

Cache issuable template names

We were looking these up on each request to an issue page, because the form is
pre-filled, as is the template dropdown. That was unnecessary: we could just
treat these as 'special' repository files (like the rendered README) and cache
them in Redis until they change on a push.
parent 218e1f09
Pipeline #12724097 passed with stages
in 81 minutes and 45 seconds
...@@ -314,20 +314,12 @@ module IssuablesHelper ...@@ -314,20 +314,12 @@ module IssuablesHelper
@issuable_templates ||= @issuable_templates ||=
case issuable case issuable
when Issue when Issue
issue_template_names ref_project.repository.issue_template_names
when MergeRequest when MergeRequest
merge_request_template_names ref_project.repository.merge_request_template_names
end end
end end
def merge_request_template_names
@merge_request_templates ||= Gitlab::Template::MergeRequestTemplate.dropdown_names(ref_project)
end
def issue_template_names
@issue_templates ||= Gitlab::Template::IssueTemplate.dropdown_names(ref_project)
end
def selected_template(issuable) def selected_template(issuable)
params[:issuable_template] if issuable_templates(issuable).any? { |template| template[:name] == params[:issuable_template] } params[:issuable_template] if issuable_templates(issuable).any? { |template| template[:name] == params[:issuable_template] }
end end
......
...@@ -34,7 +34,8 @@ class Repository ...@@ -34,7 +34,8 @@ class Repository
CACHED_METHODS = %i(size commit_count rendered_readme contribution_guide CACHED_METHODS = %i(size commit_count rendered_readme contribution_guide
changelog license_blob license_key gitignore koding_yml changelog license_blob license_key gitignore koding_yml
gitlab_ci_yml branch_names tag_names branch_count gitlab_ci_yml branch_names tag_names branch_count
tag_count avatar exists? empty? root_ref has_visible_content?).freeze tag_count avatar exists? empty? root_ref has_visible_content?
issue_template_names merge_request_template_names).freeze
# Methods that use cache_method but only memoize the value # Methods that use cache_method but only memoize the value
MEMOIZED_CACHED_METHODS = %i(license empty_repo?).freeze MEMOIZED_CACHED_METHODS = %i(license empty_repo?).freeze
...@@ -50,7 +51,9 @@ class Repository ...@@ -50,7 +51,9 @@ class Repository
gitignore: :gitignore, gitignore: :gitignore,
koding: :koding_yml, koding: :koding_yml,
gitlab_ci: :gitlab_ci_yml, gitlab_ci: :gitlab_ci_yml,
avatar: :avatar avatar: :avatar,
issue_template: :issue_template_names,
merge_request_template: :merge_request_template_names
}.freeze }.freeze
# Wraps around the given method and caches its output in Redis and an instance # Wraps around the given method and caches its output in Redis and an instance
...@@ -535,6 +538,16 @@ class Repository ...@@ -535,6 +538,16 @@ class Repository
end end
cache_method :avatar cache_method :avatar
def issue_template_names
Gitlab::Template::IssueTemplate.dropdown_names(project)
end
cache_method :issue_template_names, fallback: []
def merge_request_template_names
Gitlab::Template::MergeRequestTemplate.dropdown_names(project)
end
cache_method :merge_request_template_names, fallback: []
def readme def readme
if readme = tree(:head)&.readme if readme = tree(:head)&.readme
ReadmeBlob.new(readme, self) ReadmeBlob.new(readme, self)
......
---
title: Cache issue and MR template names in Redis
merge_request:
author:
type: other
...@@ -12,6 +12,8 @@ module Gitlab ...@@ -12,6 +12,8 @@ module Gitlab
contributing: /\Acontributing[^\/]*\z/i, contributing: /\Acontributing[^\/]*\z/i,
version: 'version', version: 'version',
avatar: /\Alogo\.(png|jpg|gif)\z/, avatar: /\Alogo\.(png|jpg|gif)\z/,
issue_template: /\A\.gitlab\/issue_templates\/[^\/]+\.md\z/,
merge_request_template: /\A\.gitlab\/merge_request_templates\/[^\/]+\.md\z/,
# Configuration files # Configuration files
gitignore: '.gitignore', gitignore: '.gitignore',
......
...@@ -56,6 +56,14 @@ describe Gitlab::FileDetector do ...@@ -56,6 +56,14 @@ describe Gitlab::FileDetector do
end end
end end
it 'returns the type of an issue template' do
expect(described_class.type_of('.gitlab/issue_templates/foo.md')).to eq(:issue_template)
end
it 'returns the type of a merge request template' do
expect(described_class.type_of('.gitlab/merge_request_templates/foo.md')).to eq(:merge_request_template)
end
it 'returns nil for an unknown file' do it 'returns nil for an unknown file' do
expect(described_class.type_of('foo.txt')).to be_nil expect(described_class.type_of('foo.txt')).to be_nil
end end
......
...@@ -1509,7 +1509,9 @@ describe Repository do ...@@ -1509,7 +1509,9 @@ describe Repository do
:gitignore, :gitignore,
:koding, :koding,
:gitlab_ci, :gitlab_ci,
:avatar :avatar,
:issue_template,
:merge_request_template
]) ])
repository.after_change_head repository.after_change_head
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment