Class: Gitlab::Checks::SnippetCheck

Inherits:
BaseSingleChecker show all
Defined in:
lib/gitlab/checks/snippet_check.rb

Constant Summary collapse

ERROR_MESSAGES =
{
  create_delete_branch: 'You can not create or delete branches.'
}.freeze
ATTRIBUTES =
%i[oldrev newrev ref branch_name tag_name logger].freeze

Instance Attribute Summary

Attributes inherited from BaseSingleChecker

#change_access

Instance Method Summary collapse

Constructor Details

#initialize(change, default_branch:, root_ref:, logger:) ⇒ SnippetCheck

Returns a new instance of SnippetCheck.



13
14
15
16
17
18
19
20
21
22
# File 'lib/gitlab/checks/snippet_check.rb', line 13

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

Instance Method Details

#validate!Object



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

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