Commit 9ae2428f authored by Sheldon Led's avatar Sheldon Led Committed by Douglas Barbosa Alexandre

Add backend code to support storage_enforcement_banner

parent 74bbdea9
...@@ -23,4 +23,42 @@ module StorageHelper ...@@ -23,4 +23,42 @@ module StorageHelper
_("Repository: %{counter_repositories} / Wikis: %{counter_wikis} / Build Artifacts: %{counter_build_artifacts} / Pipeline Artifacts: %{counter_pipeline_artifacts} / LFS: %{counter_lfs_objects} / Snippets: %{counter_snippets} / Packages: %{counter_packages} / Uploads: %{counter_uploads}") % counters _("Repository: %{counter_repositories} / Wikis: %{counter_wikis} / Build Artifacts: %{counter_build_artifacts} / Pipeline Artifacts: %{counter_pipeline_artifacts} / LFS: %{counter_lfs_objects} / Snippets: %{counter_snippets} / Packages: %{counter_packages} / Uploads: %{counter_uploads}") % counters
end end
def storage_enforcement_banner_info(namespace)
return if namespace.paid?
return unless namespace.storage_enforcement_date && namespace.storage_enforcement_date >= Date.today
return if user_dismissed_storage_enforcement_banner?(namespace)
{
text: html_escape_once(s_("UsageQuota|From %{storage_enforcement_date} storage limits will apply to this namespace. " \
"View and manage your usage in %{strong_start}Group Settings > Usage quotas%{strong_end}.")).html_safe %
{ storage_enforcement_date: namespace.storage_enforcement_date, strong_start: "<strong>".html_safe, strong_end: "</strong>".html_safe },
variant: 'warning',
callouts_path: group_callouts_path,
callouts_feature_name: storage_enforcement_banner_user_callouts_feature_name(namespace),
learn_more_link: link_to(_('Learn more.'), help_page_path('/'), rel: 'noopener noreferrer', target: '_blank') # TBD: https://gitlab.com/gitlab-org/gitlab/-/issues/350632
}
end
private
def storage_enforcement_banner_user_callouts_feature_name(namespace)
"storage_enforcement_banner_#{storage_enforcement_banner_threshold(namespace)}_enforcement_threshold"
end
def storage_enforcement_banner_threshold(namespace)
days_to_enforcement_date = (namespace.storage_enforcement_date - Date.today)
return :first if days_to_enforcement_date > 30
return :second if days_to_enforcement_date > 15 && days_to_enforcement_date <= 30
return :third if days_to_enforcement_date > 7 && days_to_enforcement_date <= 15
return :fourth if days_to_enforcement_date > 0 && days_to_enforcement_date <= 7
end
def user_dismissed_storage_enforcement_banner?(namespace)
return false unless current_user
current_user.dismissed_callout_for_group?(feature_name: storage_enforcement_banner_user_callouts_feature_name(namespace),
group: namespace)
end
end end
...@@ -513,6 +513,12 @@ class Namespace < ApplicationRecord ...@@ -513,6 +513,12 @@ class Namespace < ApplicationRecord
Feature.enabled?(:create_project_namespace_on_project_create, self, default_enabled: :yaml) Feature.enabled?(:create_project_namespace_on_project_create, self, default_enabled: :yaml)
end end
def storage_enforcement_date
# should return something like Date.new(2022, 02, 03)
# TBD: https://gitlab.com/gitlab-org/gitlab/-/issues/350632
nil
end
private private
def expire_child_caches def expire_child_caches
......
...@@ -10,7 +10,11 @@ module Users ...@@ -10,7 +10,11 @@ module Users
enum feature_name: { enum feature_name: {
invite_members_banner: 1, invite_members_banner: 1,
approaching_seat_count_threshold: 2 # EE-only approaching_seat_count_threshold: 2, # EE-only
storage_enforcement_banner_first_enforcement_threshold: 43,
storage_enforcement_banner_second_enforcement_threshold: 44,
storage_enforcement_banner_third_enforcement_threshold: 45,
storage_enforcement_banner_fourth_enforcement_threshold: 46
} }
validates :group, presence: true validates :group, presence: true
......
...@@ -39153,6 +39153,9 @@ msgstr "" ...@@ -39153,6 +39153,9 @@ msgstr ""
msgid "UsageQuota|File attachments and smaller design graphics." msgid "UsageQuota|File attachments and smaller design graphics."
msgstr "" msgstr ""
msgid "UsageQuota|From %{storage_enforcement_date} storage limits will apply to this namespace. View and manage your usage in %{strong_start}Group Settings &gt; Usage quotas%{strong_end}."
msgstr ""
msgid "UsageQuota|Git repository." msgid "UsageQuota|Git repository."
msgstr "" msgstr ""
......
...@@ -50,4 +50,87 @@ RSpec.describe StorageHelper do ...@@ -50,4 +50,87 @@ RSpec.describe StorageHelper do
expect(helper.storage_counters_details(namespace_stats)).to eq(message) expect(helper.storage_counters_details(namespace_stats)).to eq(message)
end end
end end
describe "storage_enforcement_banner" do
let_it_be_with_refind(:current_user) { create(:user) }
let_it_be(:free_group) { create(:group) }
let_it_be(:paid_group) { create(:group) }
before do
allow(helper).to receive(:current_user) { current_user }
allow(Gitlab).to receive(:com?).and_return(true)
allow(paid_group).to receive(:paid?).and_return(true)
end
describe "#storage_enforcement_banner_info" do
it 'returns nil when namespace is not free' do
expect(storage_enforcement_banner_info(paid_group)).to be(nil)
end
it 'returns nil when storage_enforcement_date is not set' do
allow(free_group).to receive(:storage_enforcement_date).and_return(nil)
expect(storage_enforcement_banner_info(free_group)).to be(nil)
end
it 'returns a hash when storage_enforcement_date is set' do
storage_enforcement_date = Date.today + 30
allow(free_group).to receive(:storage_enforcement_date).and_return(storage_enforcement_date)
expect(storage_enforcement_banner_info(free_group)).to eql({
text: "From #{storage_enforcement_date} storage limits will apply to this namespace. View and manage your usage in <strong>Group Settings &gt; Usage quotas</strong>.",
variant: 'warning',
callouts_feature_name: 'storage_enforcement_banner_second_enforcement_threshold',
callouts_path: '/-/users/group_callouts',
learn_more_link: '<a rel="noopener noreferrer" target="_blank" href="/help//">Learn more.</a>'
})
end
context 'when storage_enforcement_date is set and dismissed callout exists' do
before do
create(:group_callout,
user: current_user,
group_id: free_group.id,
feature_name: 'storage_enforcement_banner_second_enforcement_threshold')
storage_enforcement_date = Date.today + 30
allow(free_group).to receive(:storage_enforcement_date).and_return(storage_enforcement_date)
end
it { expect(storage_enforcement_banner_info(free_group)).to be(nil) }
end
context 'callouts_feature_name' do
let(:days_from_now) { 45 }
subject do
storage_enforcement_date = Date.today + days_from_now
allow(free_group).to receive(:storage_enforcement_date).and_return(storage_enforcement_date)
storage_enforcement_banner_info(free_group)[:callouts_feature_name]
end
it 'returns first callouts_feature_name' do
is_expected.to eq('storage_enforcement_banner_first_enforcement_threshold')
end
context 'returns second callouts_feature_name' do
let(:days_from_now) { 20 }
it { is_expected.to eq('storage_enforcement_banner_second_enforcement_threshold') }
end
context 'returns third callouts_feature_name' do
let(:days_from_now) { 13 }
it { is_expected.to eq('storage_enforcement_banner_third_enforcement_threshold') }
end
context 'returns fourth callouts_feature_name' do
let(:days_from_now) { 3 }
it { is_expected.to eq('storage_enforcement_banner_fourth_enforcement_threshold') }
end
end
end
end
end end
...@@ -2219,4 +2219,13 @@ RSpec.describe Namespace do ...@@ -2219,4 +2219,13 @@ RSpec.describe Namespace do
end end
end end
end end
describe 'storage_enforcement_date' do
let_it_be(:namespace) { create(:group) }
# Date TBD: https://gitlab.com/gitlab-org/gitlab/-/issues/350632
it 'returns false' do
expect(namespace.storage_enforcement_date).to be(nil)
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