Commit 671f30cc authored by Fabio Pitino's avatar Fabio Pitino

Move shared_runners_minutes_limit_enabled to Ci::Minutes::Quota

- Rename `Namespace#shared_runner_minutes_supported?` to `root?`
- move check if quota is enabled inside `Ci::Minutes::Quota`
parent ea2ce3eb
......@@ -41,14 +41,14 @@ module Ci
PERCENTAGES[stage]
end
private
attr_reader :context, :stage
def eligible_for_notifications?
context.shared_runners_minutes_limit_enabled?
end
private
attr_reader :context, :stage
def calculate_notification_stage
percentage = context.percent_total_minutes_remaining.to_i
......
......@@ -13,9 +13,13 @@ module Ci
@namespace = namespace
end
def enabled?
namespace_eligible? && total_minutes.nonzero?
end
# Status of the monthly allowance being used.
def monthly_minutes_report
if namespace.shared_runners_minutes_limit_enabled? # TODO: try to refactor this
if enabled?
status = monthly_minutes_used_up? ? :over_quota : :under_quota
Report.new(monthly_minutes_used, monthly_minutes, status)
else
......@@ -24,7 +28,7 @@ module Ci
end
def monthly_percent_used
return 0 unless namespace.shared_runners_minutes_limit_enabled?
return 0 unless enabled?
return 0 if monthly_minutes == 0
100 * monthly_minutes_used.to_i / monthly_minutes
......@@ -37,15 +41,14 @@ module Ci
end
def purchased_percent_used
return 0 unless namespace.shared_runners_minutes_limit_enabled?
return 0 unless enabled?
return 0 if purchased_minutes == 0
100 * purchased_minutes_used.to_i / purchased_minutes
end
def minutes_used_up?
namespace.shared_runners_minutes_limit_enabled? &&
total_minutes_used >= total_minutes
enabled? && total_minutes_used >= total_minutes
end
def total_minutes
......@@ -64,19 +67,24 @@ module Ci
private
def namespace_eligible?
namespace.root? && namespace.any_project_with_shared_runners_enabled?
end
def total_minutes_remaining
[total_minutes.to_i - total_minutes_used, 0].max
end
def monthly_minutes_used_up?
namespace.shared_runners_minutes_limit_enabled? &&
return false unless enabled?
monthly_minutes_used >= monthly_minutes
end
def purchased_minutes_used_up?
namespace.shared_runners_minutes_limit_enabled? &&
any_minutes_purchased? &&
purchased_minutes_used >= purchased_minutes
return false unless enabled?
any_minutes_purchased? && purchased_minutes_used >= purchased_minutes
end
def monthly_minutes_used
......
......@@ -245,14 +245,13 @@ module EE
@ci_minutes_quota ||= ::Ci::Minutes::Quota.new(self)
end
def shared_runner_minutes_supported?
def root?
!has_parent?
end
# The same method name is used also at project and job level
def shared_runners_minutes_limit_enabled?
shared_runner_minutes_supported? &&
any_project_with_shared_runners_enabled? &&
ci_minutes_quota.total_minutes.nonzero?
ci_minutes_quota.enabled?
end
def any_project_with_shared_runners_enabled?
......@@ -376,7 +375,7 @@ module EE
end
def validate_shared_runner_minutes_support
return if shared_runner_minutes_supported?
return if root?
if shared_runners_minutes_limit_changed?
errors.add(:shared_runners_minutes_limit, 'is not supported for this namespace')
......
......@@ -4,18 +4,18 @@ module Ci
module Minutes
class EmailNotificationService < ::BaseService
def execute
return unless namespace.shared_runners_minutes_limit_enabled?
return unless notification.eligible_for_notifications?
notify
end
private
attr_reader :notification
def notification
@notification ||= ::Ci::Minutes::Notification.new(project, nil)
end
def notify
@notification = ::Ci::Minutes::Notification.new(project, nil)
if notification.no_remaining_minutes?
notify_total_usage
elsif notification.running_out?
......
- group = local_assigns.fetch(:group)
- form = local_assigns.fetch(:form)
- return unless group.shared_runner_minutes_supported?
- return unless group.root?
= render 'namespaces/shared_runners_minutes_setting', form: form
- unless group.new_record?
......
- return unless Gitlab.com? && namespace.shared_runners_minutes_limit_enabled?
- return unless Gitlab.com?
- minutes_quota = namespace.ci_minutes_quota
- return unless minutes_quota.enabled?
.row
.col-sm-6
......
......@@ -27,7 +27,7 @@
= link_to sprite_icon('question-o'), help_page_path('user/admin_area/settings/continuous_integration', anchor: 'shared-runners-pipeline-minutes-quota'), target: '_blank', 'aria-label': _('Shared runners help link')
.col-sm-6.right
- if namespace.shared_runners_minutes_limit_enabled?
- if minutes_quota.enabled?
#{minutes_quota.monthly_percent_used}% used
- elsif !namespace.any_project_with_shared_runners_enabled?
0% used
......
......@@ -73,7 +73,7 @@ RSpec.describe EE::NamespacesHelper do
context "when it's limited" do
before do
allow(user_group).to receive(:shared_runners_minutes_limit_enabled?).and_return(true)
allow(user_group).to receive(:any_project_with_shared_runners_enabled?).and_return(true)
allow(user_group).to receive(:shared_runners_seconds).and_return(100 * 60)
user_group.update!(shared_runners_minutes_limit: 500)
......
......@@ -10,10 +10,63 @@ RSpec.describe Ci::Minutes::Quota do
let(:quota) { described_class.new(namespace) }
describe '#enabled?' do
let_it_be(:project) { create(:project, namespace: namespace) }
subject { quota.enabled? }
context 'when namespace is root' do
context 'when namespace has any project with shared runners enabled' do
before do
project.update!(shared_runners_enabled: true)
end
context 'when namespace has minutes limit' do
before do
allow(namespace).to receive(:shared_runners_minutes_limit).and_return(1000)
end
it { is_expected.to be_truthy }
end
context 'when namespace has unlimited minutes' do
before do
allow(namespace).to receive(:shared_runners_minutes_limit).and_return(0)
end
it { is_expected.to be_falsey }
end
end
context 'when namespace does not have projects with shared runners enabled' do
before do
project.update!(shared_runners_enabled: false)
allow(namespace).to receive(:shared_runners_minutes_limit).and_return(1000)
end
it { is_expected.to be_falsey }
end
end
context 'when namespace is not root' do
let(:parent) { create(:group) }
let!(:namespace) { create(:group, parent: parent) }
let!(:project) { create(:project, namespace: namespace, shared_runners_enabled: false) }
before do
namespace.update!(parent: parent)
project.update!(shared_runners_enabled: false)
allow(namespace).to receive(:shared_runners_minutes_limit).and_return(1000)
end
it { is_expected.to be_falsey }
end
end
describe '#monthly_minutes_report' do
context 'when unlimited' do
before do
allow(namespace).to receive(:shared_runners_minutes_limit_enabled?).and_return(false)
allow(quota).to receive(:enabled?).and_return(false)
end
context 'when minutes are not used' do
......@@ -43,7 +96,7 @@ RSpec.describe Ci::Minutes::Quota do
context 'when limited' do
before do
allow(namespace).to receive(:shared_runners_minutes_limit_enabled?).and_return(true)
allow(quota).to receive(:enabled?).and_return(true)
namespace.shared_runners_minutes_limit = 100
end
......@@ -80,7 +133,7 @@ RSpec.describe Ci::Minutes::Quota do
describe '#purchased_minutes_report' do
context 'when limit enabled' do
before do
allow(namespace).to receive(:shared_runners_minutes_limit_enabled?).and_return(true)
allow(quota).to receive(:enabled?).and_return(true)
namespace.shared_runners_minutes_limit = 200
end
......@@ -186,7 +239,7 @@ RSpec.describe Ci::Minutes::Quota do
with_them do
before do
allow(namespace).to receive(:shared_runners_minutes_limit_enabled?).and_return(limit_enabled)
allow(quota).to receive(:enabled?).and_return(limit_enabled)
namespace.shared_runners_minutes_limit = monthly_limit
namespace.extra_shared_runners_minutes_limit = purchased_limit
namespace.namespace_statistics.shared_runners_seconds = minutes_used.minutes
......@@ -216,7 +269,7 @@ RSpec.describe Ci::Minutes::Quota do
with_them do
before do
allow(namespace).to receive(:shared_runners_minutes_limit_enabled?).and_return(limit_enabled)
allow(quota).to receive(:enabled?).and_return(limit_enabled)
namespace.shared_runners_minutes_limit = monthly_limit
namespace.extra_shared_runners_minutes_limit = purchased_limit
namespace.namespace_statistics.shared_runners_seconds = minutes_used.minutes
......@@ -246,7 +299,7 @@ RSpec.describe Ci::Minutes::Quota do
with_them do
before do
allow(namespace).to receive(:shared_runners_minutes_limit_enabled?).and_return(limit_enabled)
allow(quota).to receive(:enabled?).and_return(limit_enabled)
namespace.shared_runners_minutes_limit = monthly_limit
namespace.extra_shared_runners_minutes_limit = purchased_limit
namespace.namespace_statistics.shared_runners_seconds = minutes_used.minutes
......
......@@ -621,31 +621,33 @@ RSpec.describe Namespace do
it { is_expected.to be_falsey }
end
context 'with project' do
context 'and disabled shared runners' do
let!(:project) do
create(:project,
namespace: namespace,
shared_runners_enabled: false)
context 'group with shared runners enabled project' do
let!(:project) { create(:project, namespace: namespace, shared_runners_enabled: true) }
it { is_expected.to be_truthy }
end
it { is_expected.to be_falsey }
context 'subgroup with shared runners enabled project' do
let(:namespace) { create(:group) }
let(:subgroup) { create(:group, parent: namespace) }
let!(:subproject) { create(:project, namespace: subgroup, shared_runners_enabled: true) }
it { is_expected.to be_truthy }
end
context 'and enabled shared runners' do
context 'with project and disabled shared runners' do
let!(:project) do
create(:project,
namespace: namespace,
shared_runners_enabled: true)
shared_runners_enabled: false)
end
it { is_expected.to be_truthy }
end
it { is_expected.to be_falsey }
end
end
describe '#shared_runner_minutes_supported?' do
subject { namespace.shared_runner_minutes_supported? }
describe '#root?' do
subject { namespace.root? }
context 'when is subgroup' do
before do
......@@ -700,34 +702,6 @@ RSpec.describe Namespace do
end
end
describe '#any_project_with_shared_runners_enabled?' do
subject { namespace.any_project_with_shared_runners_enabled? }
context 'subgroup with shared runners enabled project' do
let(:namespace) { create(:group) }
let(:subgroup) { create(:group, parent: namespace) }
let!(:subproject) { create(:project, namespace: subgroup, shared_runners_enabled: true) }
it "returns true" do
is_expected.to eq(true)
end
end
context 'group with shared runners enabled project' do
let!(:project) { create(:project, namespace: namespace, shared_runners_enabled: true) }
it "returns true" do
is_expected.to eq(true)
end
end
context 'group without projects' do
it "returns false" do
is_expected.to eq(false)
end
end
end
describe '#actual_plan' do
context 'when namespace does not have a subscription associated' do
it 'generates a subscription and returns default plan' 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