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 ...@@ -15,13 +15,15 @@ module EE
override :limit_exceeded? override :limit_exceeded?
def 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 end
def render_size_error def render_size_error
render( render(
json: { 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 documentation_url: help_url
}, },
content_type: ::LfsRequest::CONTENT_TYPE, content_type: ::LfsRequest::CONTENT_TYPE,
...@@ -29,20 +31,14 @@ module EE ...@@ -29,20 +31,14 @@ module EE
) )
end 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 def size_checker
project.repository_size_checker project.repository_size_checker
end end
def lfs_push_size
strong_memoize(:lfs_push_size) do
objects.sum { |o| o[:size] } # rubocop: disable CodeReuse/ActiveRecord
end
end
end end
end end
...@@ -52,15 +52,15 @@ describe 'Git LFS API and storage' do ...@@ -52,15 +52,15 @@ describe 'Git LFS API and storage' do
allow_next_instance_of(Gitlab::RepositorySizeChecker) do |checker| allow_next_instance_of(Gitlab::RepositorySizeChecker) do |checker|
allow(checker).to receive_messages( allow(checker).to receive_messages(
enabled?: true, enabled?: true,
current_size: 100.megabytes, current_size: 110.megabytes,
limit: 99.megabytes limit: 100.megabytes
) )
end end
end end
it 'responds with status 406' do it 'responds with status 406' do
expect(response).to have_gitlab_http_status(:not_acceptable) 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
end end
......
...@@ -4,7 +4,7 @@ module Gitlab ...@@ -4,7 +4,7 @@ module Gitlab
class RepositorySizeErrorMessage class RepositorySizeErrorMessage
include ActiveSupport::NumberHelper include ActiveSupport::NumberHelper
delegate :current_size, :limit, to: :@checker delegate :current_size, :limit, :exceeded_size, to: :@checker
# @param checher [RepositorySizeChecker] # @param checher [RepositorySizeChecker]
def initialize(checker) def initialize(checker)
...@@ -19,8 +19,8 @@ module Gitlab ...@@ -19,8 +19,8 @@ module Gitlab
"This merge request cannot be merged, #{base_message}" "This merge request cannot be merged, #{base_message}"
end end
def push_error(exceeded_size = nil) def push_error(change_size = 0)
"Your push has been rejected, #{base_message(exceeded_size)}. #{more_info_message}" "Your push has been rejected, #{base_message(change_size)}. #{more_info_message}"
end end
def new_changes_error def new_changes_error
...@@ -32,17 +32,13 @@ module Gitlab ...@@ -32,17 +32,13 @@ module Gitlab
end end
def above_size_limit_message 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 end
private private
def base_message(exceeded_size = nil) def base_message(change_size = 0)
"because this repository has exceeded its size limit of #{formatted(limit)} by #{formatted(size_to_remove(exceeded_size))}" "because this repository has exceeded its size limit of #{formatted(limit)} by #{formatted(exceeded_size(change_size))}"
end
def size_to_remove(exceeded_size = nil)
exceeded_size || checker.exceeded_size
end end
def formatted(number) def formatted(number)
......
...@@ -33,7 +33,7 @@ describe Gitlab::RepositorySizeErrorMessage do ...@@ -33,7 +33,7 @@ describe Gitlab::RepositorySizeErrorMessage do
end end
it 'returns the correct message' do 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}") .to eq("Your push has been rejected, #{rejection_message}. #{message.more_info_message}")
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