Cache project user defined rules by branch

parent 6be04431
...@@ -418,7 +418,13 @@ module EE ...@@ -418,7 +418,13 @@ module EE
return user_defined_rules.take(1) unless multiple_approval_rules_available? return user_defined_rules.take(1) unless multiple_approval_rules_available?
return user_defined_rules unless branch return user_defined_rules unless branch
user_defined_rules.applicable_to_branch(branch) rules = strong_memoize(:visible_user_defined_rules) do
Hash.new do |h, key|
h[key] = user_defined_rules.applicable_to_branch(key)
end
end
rules[branch]
end end
def visible_user_defined_inapplicable_rules(branch) def visible_user_defined_inapplicable_rules(branch)
......
---
title: Cache project user defined rules by branch
merge_request: 41564
author:
type: performance
...@@ -1099,13 +1099,16 @@ RSpec.describe Project do ...@@ -1099,13 +1099,16 @@ RSpec.describe Project do
let(:project) { create(:project) } let(:project) { create(:project) }
let!(:approval_rules) { create_list(:approval_project_rule, 2, project: project) } let!(:approval_rules) { create_list(:approval_project_rule, 2, project: project) }
let!(:any_approver_rule) { create(:approval_project_rule, rule_type: :any_approver, project: project) } let!(:any_approver_rule) { create(:approval_project_rule, rule_type: :any_approver, project: project) }
let(:branch) { nil }
subject { project.visible_user_defined_rules(branch: branch) }
before do before do
stub_licensed_features(multiple_approval_rules: true) stub_licensed_features(multiple_approval_rules: true)
end end
it 'returns all approval rules' do it 'returns all approval rules' do
expect(project.visible_user_defined_rules).to eq([any_approver_rule, *approval_rules]) expect(subject).to eq([any_approver_rule, *approval_rules])
end end
context 'when multiple approval rules is not available' do context 'when multiple approval rules is not available' do
...@@ -1114,7 +1117,19 @@ RSpec.describe Project do ...@@ -1114,7 +1117,19 @@ RSpec.describe Project do
end end
it 'returns the first approval rule' do it 'returns the first approval rule' do
expect(project.visible_user_defined_rules).to eq([any_approver_rule]) expect(subject).to eq([any_approver_rule])
end
end
context 'when branch is provided' do
let(:branch) { 'master' }
it 'caches the rules' do
expect(project).to receive(:user_defined_rules).and_call_original
subject
expect(project).not_to receive(:user_defined_rules)
subject
end end
end end
end end
......
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