Commit 3413cce2 authored by Mark Chao's avatar Mark Chao

Refactor lfs request size checking

Replace size comparison with size checker methods
parent 641d3f0a
......@@ -15,13 +15,15 @@ module EE
override :limit_exceeded?
def limit_exceeded?
size_checker.above_size_limit? || objects_exceed_repo_limit?
strong_memoize(:limit_exceeded) do
size_checker.changes_will_exceed_size_limit?(lfs_push_size)
end
end
def render_size_error
render(
json: {
message: size_checker.error_message.push_error(@exceeded_limit), # rubocop:disable Gitlab/ModuleWithInstanceVariables
message: size_checker.error_message.push_error(lfs_push_size),
documentation_url: help_url
},
content_type: ::LfsRequest::CONTENT_TYPE,
......@@ -29,20 +31,14 @@ module EE
)
end
# rubocop: disable CodeReuse/ActiveRecord
def objects_exceed_repo_limit?
return false unless size_checker.enabled?
strong_memoize(:limit_exceeded) do
lfs_push_size = objects.sum { |o| o[:size] }
@exceeded_limit = size_checker.exceeded_size(lfs_push_size) # rubocop:disable Gitlab/ModuleWithInstanceVariables
@exceeded_limit > 0 # rubocop:disable Gitlab/ModuleWithInstanceVariables
end
end
# rubocop: enable CodeReuse/ActiveRecord
def size_checker
project.repository_size_checker
end
def lfs_push_size
strong_memoize(:lfs_push_size) do
objects.sum { |o| o[:size] } # rubocop: disable CodeReuse/ActiveRecord
end
end
end
end
......@@ -52,15 +52,15 @@ describe 'Git LFS API and storage' do
allow_next_instance_of(Gitlab::RepositorySizeChecker) do |checker|
allow(checker).to receive_messages(
enabled?: true,
current_size: 100.megabytes,
limit: 99.megabytes
current_size: 110.megabytes,
limit: 100.megabytes
)
end
end
it 'responds with status 406' do
expect(response).to have_gitlab_http_status(:not_acceptable)
expect(json_response['message']).to eql('Your push has been rejected, because this repository has exceeded its size limit of 99 MB by 1 MB. Please contact your GitLab administrator for more information.')
expect(json_response['message']).to eql('Your push has been rejected, because this repository has exceeded its size limit of 100 MB by 160 MB. Please contact your GitLab administrator for more information.')
end
end
......
......@@ -4,7 +4,7 @@ module Gitlab
class RepositorySizeErrorMessage
include ActiveSupport::NumberHelper
delegate :current_size, :limit, to: :@checker
delegate :current_size, :limit, :exceeded_size, to: :@checker
# @param checher [RepositorySizeChecker]
def initialize(checker)
......@@ -19,8 +19,8 @@ module Gitlab
"This merge request cannot be merged, #{base_message}"
end
def push_error(exceeded_size = nil)
"Your push has been rejected, #{base_message(exceeded_size)}. #{more_info_message}"
def push_error(change_size = 0)
"Your push has been rejected, #{base_message(change_size)}. #{more_info_message}"
end
def new_changes_error
......@@ -32,17 +32,13 @@ module Gitlab
end
def above_size_limit_message
"The size of this repository (#{formatted(current_size)}) exceeds the limit of #{formatted(limit)} by #{formatted(size_to_remove)}. You won't be able to push new code to this project. #{more_info_message}"
"The size of this repository (#{formatted(current_size)}) exceeds the limit of #{formatted(limit)} by #{formatted(exceeded_size)}. You won't be able to push new code to this project. #{more_info_message}"
end
private
def base_message(exceeded_size = nil)
"because this repository has exceeded its size limit of #{formatted(limit)} by #{formatted(size_to_remove(exceeded_size))}"
end
def size_to_remove(exceeded_size = nil)
exceeded_size || checker.exceeded_size
def base_message(change_size = 0)
"because this repository has exceeded its size limit of #{formatted(limit)} by #{formatted(exceeded_size(change_size))}"
end
def formatted(number)
......
......@@ -33,7 +33,7 @@ describe Gitlab::RepositorySizeErrorMessage do
end
it 'returns the correct message' do
expect(message.push_error(15.megabytes))
expect(message.push_error(10.megabytes))
.to eq("Your push has been rejected, #{rejection_message}. #{message.more_info_message}")
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