Commit 59216076 authored by Matija Čupić's avatar Matija Čupić Committed by Phil Hughes

Resolve "Reset Pipeline Minutes / Pipeline Quota in the Admin UI"

parent 51cd05e9
......@@ -22,6 +22,10 @@
- if @group.shared_runner_minutes_supported?
= render 'namespaces/shared_runners_minutes_setting', f: f
- unless @group.new_record?
.form-group
.col-sm-offset-2.col-sm-10
= render 'namespaces/shared_runners_minutes_setting_reset', link_reset_minutes: admin_group_reset_runners_minutes_path(@group)
- if @group.new_record?
.form-group
......
---
title: Add reset pipeline minutes button to admin overview of groups and users
merge_request: 3656
author:
type: added
......@@ -16,6 +16,7 @@ namespace :admin do
put :unlock
put :confirm
post :impersonate
post :reset_runners_minutes
patch :disable_two_factor
delete 'remove/:email_id', action: 'remove_email', as: 'remove_email'
end
......@@ -44,6 +45,7 @@ namespace :admin do
scope(as: :group) do
put :members_update
post :reset_runners_minutes
get :edit, action: :edit
get '/', action: :show
patch '/', action: :update
......
module EE
module Admin
module GroupsController
def reset_runners_minutes
group
if ClearNamespaceSharedRunnersMinutesService.new(@group).execute
redirect_to [:admin, @group], notice: 'Group pipeline minutes were successfully reset.'
else
flash.now[:error] = 'There was an error resetting group pipeline minutes.'
render "edit"
end
end
private
def allowed_group_params
......
module EE
module Admin
module UsersController
def reset_runners_minutes
user
if ClearNamespaceSharedRunnersMinutesService.new(@user.namespace).execute
redirect_to [:admin, @user], notice: 'User pipeline minutes were successfully reset.'
else
flash.now[:error] = 'There was an error resetting user pipeline minutes.'
render "edit"
end
end
private
def allowed_user_params
......
class ClearNamespaceSharedRunnersMinutesService < BaseService
def initialize(namespace)
@namespace = namespace
end
def execute
NamespaceStatistics.where(namespace: @namespace).update_all(
shared_runners_seconds: 0,
shared_runners_seconds_last_reset: Time.now
)
end
end
......@@ -2,4 +2,7 @@
= namespace_form.hidden_field :id
%fieldset
%legend Limits
= render "namespaces/shared_runners_minutes_setting", f: namespace_form
= render 'namespaces/shared_runners_minutes_setting', f: namespace_form
.form-group.append-bottom-0
.col-sm-offset-2.col-sm-10
= render 'namespaces/shared_runners_minutes_setting_reset', link_reset_minutes: reset_runners_minutes_admin_user_path(@user), css_class: 'append-bottom-0'
- css_class = local_assigns.fetch(:css_class, '')
.bs-callout.clearfix.prepend-top-0{ class: css_class }
%h4= s_("SharedRunnersMinutesSettings|Reset used pipeline minutes")
%p= s_("SharedRunnersMinutesSettings|By resetting the pipeline minutes for this namespace, the currently used minutes will be set to zero.")
= link_to s_("SharedRunnersMinutesSettings|Reset pipeline minutes"), link_reset_minutes, method: :post, class: "btn btn-default"
require 'spec_helper'
describe Admin::GroupsController do
let(:admin) { create(:admin) }
let(:group) { create(:group) }
before do
sign_in(admin)
end
describe 'POST #reset_runner_minutes' do
subject { post :reset_runners_minutes, id: group }
before do
allow_any_instance_of(ClearNamespaceSharedRunnersMinutesService)
.to receive(:execute).and_return(clear_runners_minutes_service_result)
end
context 'when the reset is successful' do
let(:clear_runners_minutes_service_result) { true }
it 'redirects to group path' do
subject
expect(response).to redirect_to(admin_group_path(group))
expect(response).to set_flash[:notice]
end
end
context 'when the reset is not successful' do
let(:clear_runners_minutes_service_result) { false }
it 'redirects back to group edit page' do
subject
expect(response).to render_template(:edit)
expect(response).to set_flash.now[:error]
end
end
end
end
require 'spec_helper'
describe Admin::UsersController do
let(:admin) { create(:admin) }
let(:user) { create(:user) }
before do
sign_in(admin)
end
describe 'POST #reset_runner_minutes' do
subject { post :reset_runners_minutes, id: user }
before do
allow_any_instance_of(ClearNamespaceSharedRunnersMinutesService)
.to receive(:execute).and_return(clear_runners_minutes_service_result)
end
context 'when the reset is successful' do
let(:clear_runners_minutes_service_result) { true }
it 'redirects to group path' do
subject
expect(response).to redirect_to(admin_user_path(user))
expect(response).to set_flash[:notice]
end
end
context 'when the reset is not successful' do
let(:clear_runners_minutes_service_result) { false }
it 'redirects back to group edit page' do
subject
expect(response).to render_template(:edit)
expect(response).to set_flash.now[:error]
end
end
end
end
require 'spec_helper'
describe 'Reset namespace pipeline minutes' do
let(:admin) { create(:admin) }
before do
sign_in(admin)
end
shared_examples 'resetting pipeline minutes' do
context 'when namespace has namespace statistics' do
before do
namespace.create_namespace_statistics(shared_runners_seconds: 100)
end
it 'resets pipeline minutes' do
click_link 'Reset pipeline minutes'
expect(page).to have_selector('.flash-notice')
expect(current_path).to include(namespace.name)
expect(namespace.namespace_statistics.reload.shared_runners_seconds).to eq(0)
expect(namespace.namespace_statistics.reload.shared_runners_seconds_last_reset).to be_like_time(Time.now)
end
end
end
shared_examples 'rendering error' do
context 'when resetting pipeline minutes fails' do
before do
allow_any_instance_of(ClearNamespaceSharedRunnersMinutesService).to receive(:execute).and_return(false)
end
it 'renders edit page with an error' do
click_link 'Reset pipeline minutes'
expect(current_path).to include(namespace.name)
expect(page).to have_selector('.flash-error')
end
end
end
describe 'for user namespace' do
let(:user) { create(:user) }
let(:namespace) { user.namespace }
before do
visit admin_user_path(user)
click_link 'Edit'
end
it 'reset pipeline minutes button is visible' do
expect(page).to have_link('Reset pipeline minutes', href: reset_runners_minutes_admin_user_path(user))
end
include_examples "resetting pipeline minutes"
include_examples "rendering error"
end
describe 'when creating a new group' do
before do
visit admin_groups_path
page.within '#content-body' do
click_link "New group"
end
end
it 'does not display reset pipeline minutes callout' do
expect(page).not_to have_link('Reset pipeline minutes')
end
end
describe 'for group namespace' do
let(:group) { create(:group) }
let(:namespace) { group }
before do
visit admin_group_path(group)
click_link 'Edit'
end
it 'reset pipeline minutes button is visible' do
expect(page).to have_link('Reset pipeline minutes', href: admin_group_reset_runners_minutes_path(group))
end
include_examples "resetting pipeline minutes"
include_examples "rendering error"
end
end
require 'spec_helper'
describe ClearNamespaceSharedRunnersMinutesService do
describe '#execute' do
subject { described_class.new(namespace).execute }
context 'when project has namespace_statistics' do
let(:namespace) { create(:namespace, :with_used_build_minutes_limit) }
it 'clears counters' do
subject
expect(namespace.namespace_statistics.reload.shared_runners_seconds).to eq(0)
end
it 'resets timer' do
subject
expect(namespace.namespace_statistics.reload.shared_runners_seconds_last_reset).to be_like_time(Time.now)
end
it 'successfully clears minutes' do
expect(subject).to be_truthy
end
end
context 'when project does not have namespace_statistics' do
let(:namespace) { create(:namespace) }
it 'successfully clears minutes' do
expect(subject).to be_truthy
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