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