debian-mirror-gitlab/lib/gitlab/checks/snippet_check.rb

49 lines
1.6 KiB
Ruby
Raw Normal View History

2020-04-08 14:13:33 +05:30
# frozen_string_literal: true
module Gitlab
module Checks
class SnippetCheck < BaseChecker
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)
2021-02-22 17:27:13 +05:30
def initialize(change, default_branch:, root_ref:, logger:)
2020-04-08 14:13:33 +05:30
@oldrev, @newrev, @ref = change.values_at(:oldrev, :newrev, :ref)
@branch_name = Gitlab::Git.branch_name(@ref)
@tag_name = Gitlab::Git.tag_name(@ref)
2020-11-24 15:15:51 +05:30
@default_branch = default_branch
2021-02-22 17:27:13 +05:30
@root_ref = root_ref
2020-04-08 14:13:33 +05:30
@logger = logger
@logger.append_message("Running checks for ref: #{@branch_name || @tag_name}")
end
def validate!
2020-11-24 15:15:51 +05:30
if !@default_branch || creation? || deletion?
2020-04-08 14:13:33 +05:30
raise GitAccess::ForbiddenError, ERROR_MESSAGES[:create_delete_branch]
end
true
2021-02-22 17:27:13 +05:30
rescue GitAccess::ForbiddenError => e
Gitlab::ErrorTracking.log_exception(e, default_branch: @default_branch, branch_name: @branch_name, creation: creation?, deletion: deletion?)
raise e
2020-04-08 14:13:33 +05:30
end
private
2021-02-22 17:27:13 +05:30
# 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.
2020-04-08 14:13:33 +05:30
def creation?
2021-02-22 17:27:13 +05:30
super && @root_ref && (@branch_name != @default_branch)
2020-04-08 14:13:33 +05:30
end
end
end
end