summaryrefslogtreecommitdiff
path: root/lib/gitlab/checks/push_file_count_check.rb
blob: 707d4cfbcbe2ac1dd656fb4c9b03e7e8ab2e244f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# frozen_string_literal: true

module Gitlab
  module Checks
    class PushFileCountCheck < BaseSingleChecker
      attr_reader :repository, :newrev, :limit, :logger

      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

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

      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
    end
  end
end