summaryrefslogtreecommitdiff
path: root/lib/gitlab/checks/snippet_check.rb
blob: 43168600ec91f38e3cbd86ef53c53f1811f20479 (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
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true

module Gitlab
  module Checks
    class SnippetCheck < BaseSingleChecker
      ERROR_MESSAGES = {
        create_delete_branch: 'You can not create or delete branches.'
      }.freeze

      ATTRIBUTES = %i[oldrev newrev ref branch_name tag_name logger].freeze
      attr_reader(*ATTRIBUTES)

      def initialize(change, default_branch:, root_ref:, logger:)
        @oldrev, @newrev, @ref = change.values_at(:oldrev, :newrev, :ref)
        @branch_name = Gitlab::Git.branch_name(@ref)
        @tag_name = Gitlab::Git.tag_name(@ref)

        @default_branch = default_branch
        @root_ref = root_ref
        @logger = logger
        @logger.append_message("Running checks for ref: #{@branch_name || @tag_name}")
      end

      def validate!
        if !@default_branch || creation? || deletion?
          raise GitAccess::ForbiddenError, ERROR_MESSAGES[:create_delete_branch]
        end

        true
      rescue GitAccess::ForbiddenError => e
        Gitlab::ErrorTracking.log_exception(e, default_branch: @default_branch, branch_name: @branch_name, creation: creation?, deletion: deletion?)

        raise e
      end

      private

      # If the `root_ref` is not present means that this is the first commit to the
      # repository and when the default branch is going to be created.
      # We allow the first branch creation no matter the name because
      # it can be even an imported snippet from an instance with a different
      # default branch.
      def creation?
        super && @root_ref && (@branch_name != @default_branch)
      end
    end
  end
end