Commit b7fa38f8 authored by Fabio Pitino's avatar Fabio Pitino Committed by Nick Thomas

Move actual_shared_runners_minutes_limit to Ci::Minutes::Quota

Moved to Ci::Minutes::Quota#total_minutes
parent 53e51823
......@@ -48,6 +48,10 @@ module Ci
total_minutes_used >= total_minutes
end
def total_minutes
@total_minutes ||= monthly_minutes + purchased_minutes
end
def total_minutes_used
@total_minutes_used ||= namespace.shared_runners_seconds.to_i / 60
end
......@@ -97,12 +101,6 @@ module Ci
purchased_minutes > 0
end
# TODO: maps to Namespace#actual_shared_runners_minutes_limit(include_extra: true)
def total_minutes
@total_minutes ||= monthly_minutes + purchased_minutes
end
# TODO: maps to Namespace#actual_shared_runners_minutes_limit(include_extra: false)
def monthly_minutes
@monthly_minutes ||= (namespace.shared_runners_minutes_limit || ::Gitlab::CurrentSettings.shared_runners_minutes).to_i
end
......
......@@ -249,20 +249,10 @@ module EE
!has_parent?
end
def actual_shared_runners_minutes_limit(include_extra: true)
extra_minutes = include_extra ? extra_shared_runners_minutes_limit.to_i : 0
if shared_runners_minutes_limit
shared_runners_minutes_limit + extra_minutes
else
::Gitlab::CurrentSettings.shared_runners_minutes + extra_minutes
end
end
def shared_runners_minutes_limit_enabled?
shared_runner_minutes_supported? &&
any_project_with_shared_runners_enabled? &&
actual_shared_runners_minutes_limit.nonzero?
ci_minutes_quota.total_minutes.nonzero?
end
def any_project_with_shared_runners_enabled?
......
......@@ -184,7 +184,7 @@ module EE
delegate :shared_runners_minutes, :shared_runners_seconds, :shared_runners_seconds_last_reset,
to: :statistics, allow_nil: true
delegate :actual_shared_runners_minutes_limit, to: :shared_runners_limit_namespace
delegate :ci_minutes_quota, to: :shared_runners_limit_namespace
delegate :last_update_succeeded?, :last_update_failed?,
:ever_updated_successfully?, :hard_failed?,
......
......@@ -8,11 +8,11 @@ module EE
expose :runners do
expose :quota, if: -> (*) { project.shared_runners_minutes_limit_enabled? } do
expose :used do |runner|
::Ci::Minutes::Quota.new(project.shared_runners_limit_namespace).total_minutes_used
project.ci_minutes_quota.total_minutes_used
end
expose :limit do |runner|
project.shared_runners_limit_namespace.actual_shared_runners_minutes_limit.to_i
project.ci_minutes_quota.total_minutes
end
end
end
......
......@@ -5,7 +5,7 @@
- if project.shared_runners_enabled?
Enabled
- if project.shared_runners_minutes_limit_enabled?
- limit = project.actual_shared_runners_minutes_limit.to_i
- limit = project.ci_minutes_quota.total_minutes
(Limited to #{limit} pipeline minutes per month)
- else
(Unlimited pipeline minutes)
......
......@@ -6,7 +6,7 @@ RSpec.describe Ci::Minutes::Notification do
let_it_be(:user) { create(:user) }
let(:shared_runners_enabled) { true }
let!(:project) { create(:project, :repository, namespace: group, shared_runners_enabled: shared_runners_enabled) }
let_it_be(:group) { create(:group) }
let_it_be(:group, refind: true) { create(:group) }
let(:injected_group) { group }
let(:injected_project) { project }
......
......@@ -256,6 +256,28 @@ RSpec.describe Ci::Minutes::Quota do
end
end
describe '#total_minutes' do
subject { quota.total_minutes }
where(:namespace_monthly_limit, :application_monthly_limit, :purchased_minutes, :result) do
20 | 100 | 30 | 50
nil | 100 | 30 | 130
20 | 100 | 0 | 20
0 | 0 | 30 | 30
nil | 0 | 30 | 30
end
with_them do
before do
namespace.shared_runners_minutes_limit = namespace_monthly_limit
allow(::Gitlab::CurrentSettings).to receive(:shared_runners_minutes).and_return(application_monthly_limit)
allow(namespace).to receive(:extra_shared_runners_minutes_limit).and_return(purchased_minutes)
end
it { is_expected.to eq(result) }
end
end
describe '#total_minutes_used' do
subject { quota.total_minutes_used }
......
......@@ -644,48 +644,6 @@ RSpec.describe Namespace do
end
end
describe '#actual_shared_runners_minutes_limit' do
subject { namespace.actual_shared_runners_minutes_limit }
context 'when no limit defined' do
it { is_expected.to be_zero }
end
context 'when application settings limit is set' do
before do
stub_application_setting(shared_runners_minutes: 1000)
end
it 'returns global limit' do
is_expected.to eq(1000)
end
context 'when namespace limit is set' do
before do
namespace.shared_runners_minutes_limit = 500
end
it 'returns namespace limit' do
is_expected.to eq(500)
end
end
context 'when extra minutes limit is set' do
before do
namespace.update_attribute(:extra_shared_runners_minutes_limit, 100)
end
it 'returns the extra minutes by default' do
is_expected.to eq(1100)
end
it 'can exclude the extra minutes if required' do
expect(namespace.actual_shared_runners_minutes_limit(include_extra: false)).to eq(1000)
end
end
end
end
describe '#shared_runner_minutes_supported?' do
subject { namespace.shared_runner_minutes_supported? }
......
......@@ -14,7 +14,7 @@ RSpec.describe Project do
it { is_expected.to delegate_method(:shared_runners_seconds).to(:statistics) }
it { is_expected.to delegate_method(:shared_runners_seconds_last_reset).to(:statistics) }
it { is_expected.to delegate_method(:actual_shared_runners_minutes_limit).to(:shared_runners_limit_namespace) }
it { is_expected.to delegate_method(:ci_minutes_quota).to(:shared_runners_limit_namespace) }
it { is_expected.to delegate_method(:shared_runners_minutes_limit_enabled?).to(:shared_runners_limit_namespace) }
it { is_expected.to delegate_method(:closest_gitlab_subscription).to(:namespace) }
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe BuildDetailsEntity do
let_it_be(:user) { create(:user) }
let(:namespace) { create(:namespace) }
let(:project) { create(:project, namespace: namespace) }
let(:request) { double('request', project: project) }
let(:build) { create(:ci_build, project: project) }
let(:entity) do
described_class.new(build, request: request, current_user: user, project: project)
end
subject { entity.as_json }
before do
allow(request).to receive(:current_user).and_return(user)
allow(namespace).to receive(:shared_runners_seconds).and_return(800.minutes)
end
context 'when namespace has CI minutes limit enabled' do
before do
allow(namespace).to receive(:shared_runners_minutes_limit).and_return(1000)
end
it 'contains CI minutes quota details' do
quota = subject.dig(:runners, :quota)
expect(quota).to be_present
expect(quota.fetch(:used)).to eq(800)
expect(quota.fetch(:limit)).to eq(1000)
end
end
context 'when namespace does not qualify for CI minutes' do
before do
allow(namespace).to receive(:shared_runners_minutes_limit).and_return(0)
end
it 'does not contain CI minutes quota details' do
expect(subject.dig(:runners, :quota)).not_to be_present
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