Commit b531616e authored by Rémy Coutable's avatar Rémy Coutable

Cache PerformanceBar data using RequestStore

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent 186048a4
...@@ -489,7 +489,7 @@ Settings.gitaly['enabled'] = true if Settings.gitaly['enabled'].nil? ...@@ -489,7 +489,7 @@ Settings.gitaly['enabled'] = true if Settings.gitaly['enabled'].nil?
# Performance bar # Performance bar
# #
Settings['performance_bar'] ||= Settingslogic.new({}) Settings['performance_bar'] ||= Settingslogic.new({})
Settings.performance_bar['allowed_group'] = 'gitlab-org' if Settings.performance_bar['allowed_group'].nil? Settings.performance_bar['allowed_group'] ||= nil
# #
# Webpack settings # Webpack settings
......
...@@ -5,19 +5,34 @@ module Gitlab ...@@ -5,19 +5,34 @@ module Gitlab
end end
def self.allowed_actor?(actor) def self.allowed_actor?(actor)
group = allowed_group return false unless actor.thing&.is_a?(User) && allowed_group
return false unless actor&.is_a?(User) && group
GroupMembersFinder.new(group) if RequestStore.active?
.execute RequestStore.fetch('performance_bar:user_member_of_allowed_group') do
.where(user_id: actor.id) user_member_of_allowed_group?(actor.thing)
.any? end
else
user_member_of_allowed_group?(actor.thing)
end
end end
def self.allowed_group def self.allowed_group
return nil unless Gitlab.config.performance_bar.allowed_group return nil unless Gitlab.config.performance_bar.allowed_group
Group.by_path(Gitlab.config.performance_bar.allowed_group) if RequestStore.active?
RequestStore.fetch('performance_bar:allowed_group') do
Group.by_path(Gitlab.config.performance_bar.allowed_group)
end
else
Group.by_path(Gitlab.config.performance_bar.allowed_group)
end
end
def self.user_member_of_allowed_group?(user)
GroupMembersFinder.new(allowed_group)
.execute
.where(user_id: user.id)
.any?
end end
end end
end end
...@@ -2,38 +2,38 @@ require 'spec_helper' ...@@ -2,38 +2,38 @@ require 'spec_helper'
describe Gitlab::PerformanceBar do describe Gitlab::PerformanceBar do
describe '.enabled?' do describe '.enabled?' do
it 'returns false when given user is nil' do it 'returns false when given actor is nil' do
expect(described_class.enabled?(nil)).to be_falsy expect(described_class.enabled?(nil)).to be_falsy
end end
it 'returns false when feature is disabled' do it 'returns false when feature is disabled' do
user = double('user') actor = double('actor')
expect(Feature).to receive(:enabled?) expect(Feature).to receive(:enabled?)
.with(:gitlab_performance_bar, user).and_return(false) .with(:gitlab_performance_bar, actor).and_return(false)
expect(described_class.enabled?(user)).to be_falsy expect(described_class.enabled?(actor)).to be_falsy
end end
it 'returns true when feature is enabled' do it 'returns true when feature is enabled' do
user = double('user') actor = double('actor')
expect(Feature).to receive(:enabled?) expect(Feature).to receive(:enabled?)
.with(:gitlab_performance_bar, user).and_return(true) .with(:gitlab_performance_bar, actor).and_return(true)
expect(described_class.enabled?(user)).to be_truthy expect(described_class.enabled?(actor)).to be_truthy
end end
end end
describe '.allowed_actor?' do describe '.allowed_actor?' do
it 'returns false when given actor is not a User' do it 'returns false when given actor is not a User' do
actor = double actor = double('actor', thing: double)
expect(described_class.allowed_actor?(actor)).to be_falsy expect(described_class.allowed_actor?(actor)).to be_falsy
end end
context 'when given actor is a User' do context 'when given actor is a User' do
let(:actor) { create(:user) } let(:actor) { double('actor', thing: create(:user)) }
before do before do
stub_performance_bar_setting(allowed_group: 'my-group') stub_performance_bar_setting(allowed_group: 'my-group')
...@@ -56,7 +56,7 @@ describe Gitlab::PerformanceBar do ...@@ -56,7 +56,7 @@ describe Gitlab::PerformanceBar do
context 'when user is a member of the allowed group' do context 'when user is a member of the allowed group' do
before do before do
my_group.add_developer(actor) my_group.add_developer(actor.thing)
end end
it 'returns true' do it 'returns true' do
......
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