Commit cf83cb2e authored by Ragnar Hardarson's avatar Ragnar Hardarson Committed by Markus Koller

Move storage alert helpers to dedicated file

In preparation of display_namespace_storage_limit_alert helper
we want a dedicated file to contain the logic for the
namespace_storage_limit_alert.
parent ea0e277f
# frozen_string_literal: true
module NamespaceStorageLimitAlertHelper
# Overridden in EE
def display_namespace_storage_limit_alert!
end
end
NamespaceStorageLimitAlertHelper.prepend_if_ee('EE::NamespaceStorageLimitAlertHelper')
= content_for :flash_message do
= render_if_exists 'shared/shared_runners_minutes_limit', namespace: @group, classes: [container_class, ("limit-container-width" unless fluid_layout)]
= render_if_exists 'shared/namespace_storage_limit_alert', namespace: @group, classes: [container_class, ("limit-container-width" unless fluid_layout)]
......@@ -15,6 +15,7 @@
= render "shared/ping_consent"
= render_account_recovery_regular_check
= render_if_exists "layouts/header/ee_subscribable_banner"
= render_if_exists "shared/namespace_storage_limit_alert"
- unless @hide_breadcrumbs
= render "layouts/nav/breadcrumbs"
.d-flex
......
......@@ -3,6 +3,7 @@
- header_title group_title(@group) unless header_title
- nav "group"
- display_subscription_banner!
- display_namespace_storage_limit_alert!
- @left_sidebar = true
- content_for :page_specific_javascripts do
......
......@@ -9,4 +9,3 @@
= render 'shared/auto_devops_implicitly_enabled_banner', project: project
= render_if_exists 'projects/above_size_limit_warning', project: project
= render_if_exists 'shared/shared_runners_minutes_limit', project: project, classes: [container_class, ("limit-container-width" unless fluid_layout)]
= render_if_exists 'shared/namespace_storage_limit_alert', namespace: project.namespace, classes: [container_class, ("limit-container-width" unless fluid_layout)]
- breadcrumb_title _("Details")
- page_title _("Projects")
- @content_class = "limit-container-width" unless fluid_layout
- display_namespace_storage_limit_alert!
= content_for :meta_tags do
= auto_discovery_link_tag(:atom, project_path(@project, rss_url_options), title: "#{@project.name} activity")
......
import $ from 'jquery';
import initEETrialBanner from 'ee/ee_trial_banner';
import trackNavbarEvents from 'ee/event_tracking/navbar';
import initNamespaceStorageLimitAlert from 'ee/namespace_storage_limit_alert';
$(() => {
/**
......@@ -10,6 +11,7 @@ $(() => {
// EE specific calls
initEETrialBanner();
initNamespaceStorageLimitAlert();
trackNavbarEvents();
});
......@@ -3,7 +3,6 @@ import leaveByUrl from '~/namespaces/leave_by_url';
import initGroupDetails from '~/pages/groups/shared/group_details';
import initGroupAnalytics from 'ee/analytics/group_analytics/group_analytics_bundle';
import initVueAlerts from '~/vue_alerts';
import initNamespaceStorageLimitAlert from 'ee/namespace_storage_limit_alert';
document.addEventListener('DOMContentLoaded', () => {
leaveByUrl('group');
......@@ -16,5 +15,4 @@ document.addEventListener('DOMContentLoaded', () => {
initGroupAnalytics();
initVueAlerts();
initNamespaceStorageLimitAlert();
});
import '~/pages/projects/show/index';
import initVueAlerts from '~/vue_alerts';
import initNamespaceStorageLimitAlert from 'ee/namespace_storage_limit_alert';
document.addEventListener('DOMContentLoaded', () => {
initVueAlerts();
initNamespaceStorageLimitAlert();
});
# frozen_string_literal: true
module EE
module NamespaceStorageLimitAlertHelper
extend ::Gitlab::Utils::Override
override :display_namespace_storage_limit_alert!
def display_namespace_storage_limit_alert!
@display_namespace_storage_limit_alert = true
end
def display_namespace_storage_limit_alert?
@display_namespace_storage_limit_alert
end
def namespace_storage_alert(namespace)
return {} if current_user.nil?
payload = Namespaces::CheckStorageSizeService.new(namespace, current_user).execute.payload
return {} if payload.empty?
alert_level = payload[:alert_level]
root_namespace = payload[:root_namespace]
return {} if cookies["hide_storage_limit_alert_#{root_namespace.id}_#{alert_level}"] == 'true'
payload
end
def namespace_storage_alert_style(alert_level)
if alert_level == :error || alert_level == :alert
'danger'
else
alert_level.to_s
end
end
def namespace_storage_alert_icon(alert_level)
if alert_level == :error || alert_level == :alert
'error'
elsif alert_level == :info
'information-o'
else
alert_level.to_s
end
end
def namespace_storage_usage_link(namespace)
if namespace.group?
group_usage_quotas_path(namespace, anchor: 'storage-quota-tab')
else
profile_usage_quotas_path(anchor: 'storage-quota-tab')
end
end
def purchase_storage_url
return unless ::Gitlab.dev_env_or_com?
return unless ::Feature.enabled?(:buy_storage_link)
EE::SUBSCRIPTIONS_MORE_STORAGE_URL
end
end
end
......@@ -32,59 +32,11 @@ module EE
end
end
def namespace_storage_alert(namespace)
return {} if current_user.nil?
payload = Namespaces::CheckStorageSizeService.new(namespace, current_user).execute.payload
return {} if payload.empty?
alert_level = payload[:alert_level]
root_namespace = payload[:root_namespace]
return {} if cookies["hide_storage_limit_alert_#{root_namespace.id}_#{alert_level}"] == 'true'
payload
end
def namespace_storage_alert_style(alert_level)
if alert_level == :error || alert_level == :alert
'danger'
else
alert_level.to_s
end
end
def namespace_storage_alert_icon(alert_level)
if alert_level == :error || alert_level == :alert
'error'
elsif alert_level == :info
'information-o'
else
alert_level.to_s
end
end
def temporary_storage_increase_visible?(namespace)
return false unless ::Gitlab.dev_env_or_com?
return false unless ::Feature.enabled?(:temporary_storage_increase, namespace)
current_user.can?(:admin_namespace, namespace.root_ancestor)
end
def namespace_storage_usage_link(namespace)
if namespace.group?
group_usage_quotas_path(namespace, anchor: 'storage-quota-tab')
else
profile_usage_quotas_path(anchor: 'storage-quota-tab')
end
end
def purchase_storage_url
return unless ::Gitlab.dev_env_or_com?
return unless ::Feature.enabled?(:buy_storage_link)
EE::SUBSCRIPTIONS_MORE_STORAGE_URL
end
end
end
= content_for :flash_message do
= render_if_exists 'shared/shared_runners_minutes_limit', namespace: @group, classes: [container_class, ("limit-container-width" unless fluid_layout)]
= render_if_exists 'shared/namespace_storage_limit_alert', namespace: @group, classes: [container_class, ("limit-container-width" unless fluid_layout)]
- return unless current_user
- return unless display_namespace_storage_limit_alert?
- namespace = @project&.namespace || @group
- return unless namespace.present?
- payload = namespace_storage_alert(namespace)
- return if payload.empty?
......@@ -10,7 +14,7 @@
- icon = namespace_storage_alert_icon(alert_level)
- link = namespace_storage_usage_link(root_namespace)
%div{ class: [classes, 'js-namespace-storage-alert'] }
%div{ class: ['js-namespace-storage-alert', [container_class, ("limit-container-width" unless fluid_layout)]] }
.gl-pt-5.gl-pb-3
.gl-alert{ class: "gl-alert-#{style}", role: 'alert' }
= sprite_icon(icon, css_class: "gl-icon gl-alert-icon")
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe EE::NamespaceStorageLimitAlertHelper do
using RSpec::Parameterized::TableSyntax
let!(:admin) { create(:admin) }
describe '#display_namespace_storage_limit_alert!' do
it 'sets @display_namespace_storage_limit_alert to true' do
expect(helper.instance_variable_get(:@display_namespace_storage_limit_alert)).to be nil
helper.display_namespace_storage_limit_alert!
expect(helper.instance_variable_get(:@display_namespace_storage_limit_alert)).to be true
end
end
describe '#namespace_storage_usage_link' do
subject { helper.namespace_storage_usage_link(namespace) }
context 'when namespace is a group' do
let(:namespace) { build(:group) }
it { is_expected.to eq(group_usage_quotas_path(namespace, anchor: 'storage-quota-tab')) }
end
context 'when namespace is a user' do
let(:namespace) { build(:namespace) }
it { is_expected.to eq(profile_usage_quotas_path(anchor: 'storage-quota-tab')) }
end
end
describe '#purchase_storage_url' do
subject { helper.purchase_storage_url }
context 'when on .com' do
before do
allow(::Gitlab).to receive(:com?).and_return(true)
end
it { is_expected.to eq(EE::SUBSCRIPTIONS_MORE_STORAGE_URL) }
context 'when feature flag disabled' do
before do
stub_feature_flags(buy_storage_link: false)
end
it { is_expected.to be_nil }
end
end
context 'when not on .com' do
before do
allow(::Gitlab).to receive(:com?).and_return(false)
end
it { is_expected.to be_nil }
end
end
describe '#namespace_storage_alert' do
subject { helper.namespace_storage_alert(namespace) }
let(:namespace) { build(:namespace) }
let(:payload) do
{
alert_level: :info,
usage_message: "Usage",
explanation_message: "Explanation",
root_namespace: namespace
}
end
before do
allow(helper).to receive(:current_user).and_return(admin)
allow_next_instance_of(Namespaces::CheckStorageSizeService, namespace, admin) do |check_storage_size_service|
expect(check_storage_size_service).to receive(:execute).and_return(ServiceResponse.success(payload: payload))
end
end
context 'when payload is not empty and no cookie is set' do
it { is_expected.to eq(payload) }
end
context 'when there is no current_user' do
before do
allow(helper).to receive(:current_user).and_return(nil)
end
it { is_expected.to eq({}) }
end
context 'when payload is empty' do
let(:payload) { {} }
it { is_expected.to eq({}) }
end
context 'when cookie is set' do
before do
helper.request.cookies["hide_storage_limit_alert_#{namespace.id}_info"] = 'true'
end
it { is_expected.to eq({}) }
end
context 'when payload is empty and cookie is set' do
let(:payload) { {} }
before do
helper.request.cookies["hide_storage_limit_alert_#{namespace.id}_info"] = 'true'
end
it { is_expected.to eq({}) }
end
end
describe '#namespace_storage_alert_style' do
subject { helper.namespace_storage_alert_style(alert_level) }
where(:alert_level, :result) do
:info | 'info'
:warning | 'warning'
:error | 'danger'
:alert | 'danger'
end
with_them do
it { is_expected.to eq(result) }
end
end
describe '#namespace_storage_alert_icon' do
subject { helper.namespace_storage_alert_icon(alert_level) }
where(:alert_level, :result) do
:info | 'information-o'
:warning | 'warning'
:error | 'error'
:alert | 'error'
end
with_them do
it { is_expected.to eq(result) }
end
end
end
......@@ -151,140 +151,4 @@ RSpec.describe EE::NamespacesHelper do
end
end
end
describe '#namespace_storage_usage_link' do
subject { helper.namespace_storage_usage_link(namespace) }
context 'when namespace is a group' do
let(:namespace) { build(:group) }
it { is_expected.to eq(group_usage_quotas_path(namespace, anchor: 'storage-quota-tab')) }
end
context 'when namespace is a user' do
let(:namespace) { build(:namespace) }
it { is_expected.to eq(profile_usage_quotas_path(anchor: 'storage-quota-tab')) }
end
end
describe '#purchase_storage_url' do
subject { helper.purchase_storage_url }
context 'when on .com' do
before do
allow(::Gitlab).to receive(:com?).and_return(true)
end
it { is_expected.to eq(EE::SUBSCRIPTIONS_MORE_STORAGE_URL) }
context 'when feature flag disabled' do
before do
stub_feature_flags(buy_storage_link: false)
end
it { is_expected.to be_nil }
end
end
context 'when not on .com' do
before do
allow(::Gitlab).to receive(:com?).and_return(false)
end
it { is_expected.to be_nil }
end
end
describe '#namespace_storage_alert' do
subject { helper.namespace_storage_alert(namespace) }
let(:namespace) { build(:namespace) }
let(:payload) do
{
alert_level: :info,
usage_message: "Usage",
explanation_message: "Explanation",
root_namespace: namespace
}
end
before do
allow(helper).to receive(:current_user).and_return(admin)
allow_next_instance_of(Namespaces::CheckStorageSizeService, namespace, admin) do |check_storage_size_service|
expect(check_storage_size_service).to receive(:execute).and_return(ServiceResponse.success(payload: payload))
end
end
context 'when payload is not empty and no cookie is set' do
it { is_expected.to eq(payload) }
end
context 'when there is no current_user' do
before do
allow(helper).to receive(:current_user).and_return(nil)
end
it { is_expected.to eq({}) }
end
context 'when payload is empty' do
let(:payload) { {} }
it { is_expected.to eq({}) }
end
context 'when cookie is set' do
before do
helper.request.cookies["hide_storage_limit_alert_#{namespace.id}_info"] = 'true'
end
it { is_expected.to eq({}) }
end
context 'when payload is empty and cookie is set' do
let(:payload) { {} }
before do
helper.request.cookies["hide_storage_limit_alert_#{namespace.id}_info"] = 'true'
end
it { is_expected.to eq({}) }
end
end
describe '#namespace_storage_alert_style' do
using RSpec::Parameterized::TableSyntax
subject { helper.namespace_storage_alert_style(alert_level) }
where(:alert_level, :result) do
:info | 'info'
:warning | 'warning'
:error | 'danger'
:alert | 'danger'
end
with_them do
it { is_expected.to eq(result) }
end
end
describe '#namespace_storage_alert_icon' do
using RSpec::Parameterized::TableSyntax
subject { helper.namespace_storage_alert_icon(alert_level) }
where(:alert_level, :result) do
:info | 'information-o'
:warning | 'warning'
:error | 'error'
:alert | 'error'
end
with_them do
it { is_expected.to eq(result) }
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe NamespaceStorageLimitAlertHelper do
describe '#display_namespace_storage_limit_alert!' do
it 'is defined in CE' do
expect { helper.display_namespace_storage_limit_alert! }.not_to raise_error
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