Class: Gitlab::Checks::PushFileCountCheck

Inherits:
BaseChecker
  • Object
show all
Defined in:
lib/gitlab/checks/push_file_count_check.rb

Constant Summary collapse

LOG_MESSAGES =
{
  diff_content_check: "Validating diff contents being single file..."
}.freeze
ERROR_MESSAGES =
{
  upper_limit: "The repository can contain at most %{limit} file(s).",
  lower_limit: "The repository must contain at least 1 file."
}.freeze

Instance Attribute Summary collapse

Attributes inherited from BaseChecker

#change_access

Instance Method Summary collapse

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(change, repository:, limit:, logger:) ⇒ PushFileCountCheck

Returns a new instance of PushFileCountCheck.


17
18
19
20
21
22
# File 'lib/gitlab/checks/push_file_count_check.rb', line 17

def initialize(change, repository:, limit:, logger:)
  @repository = repository
  @newrev = change[:newrev]
  @limit = limit
  @logger = logger
end

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit


6
7
8
# File 'lib/gitlab/checks/push_file_count_check.rb', line 6

def limit
  @limit
end

#loggerObject (readonly)

Returns the value of attribute logger


6
7
8
# File 'lib/gitlab/checks/push_file_count_check.rb', line 6

def logger
  @logger
end

#newrevObject (readonly)

Returns the value of attribute newrev


6
7
8
# File 'lib/gitlab/checks/push_file_count_check.rb', line 6

def newrev
  @newrev
end

#repositoryObject (readonly)

Returns the value of attribute repository


6
7
8
# File 'lib/gitlab/checks/push_file_count_check.rb', line 6

def repository
  @repository
end

Instance Method Details

#validate!Object


24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gitlab/checks/push_file_count_check.rb', line 24

def validate!
  file_count = repository.ls_files(newrev).size

  if file_count > limit
    raise ::Gitlab::GitAccess::ForbiddenError, ERROR_MESSAGES[:upper_limit] % { limit: limit }
  end

  if file_count == 0
    raise ::Gitlab::GitAccess::ForbiddenError, ERROR_MESSAGES[:lower_limit]
  end
end