Welcome to mirror list, hosted at ThFree Co, Russian Federation.

push_file_count_check.rb « checks « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 288a7e0d41a55d024c5a87d291d9b794c6661efc (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 < BaseChecker
      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