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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

80 lines
2.1 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
module Gitlab
module Checks
2021-09-04 01:27:46 +05:30
class DiffCheck < BaseSingleChecker
2019-02-15 15:39:39 +05:30
include Gitlab::Utils::StrongMemoize
LOG_MESSAGES = {
2021-03-08 18:12:59 +05:30
validate_file_paths: "Validating diffs' file paths..."
2019-02-15 15:39:39 +05:30
}.freeze
def validate!
return if deletion?
2021-03-08 18:12:59 +05:30
return unless should_run_validations?
2019-02-15 15:39:39 +05:30
return if commits.empty?
2021-03-08 18:12:59 +05:30
paths = project.repository.find_changed_paths(commits.map(&:sha))
paths.each do |path|
validate_path(path)
2019-02-15 15:39:39 +05:30
end
2021-03-08 18:12:59 +05:30
validate_file_paths(paths.map(&:path).uniq)
2019-02-15 15:39:39 +05:30
end
private
def validate_lfs_file_locks?
strong_memoize(:validate_lfs_file_locks) do
project.lfs_enabled? && project.any_lfs_file_locks?
end
end
2021-03-08 18:12:59 +05:30
def should_run_validations?
validations_for_path.present? || file_paths_validations.present?
2019-02-15 15:39:39 +05:30
end
2021-03-08 18:12:59 +05:30
def validate_path(path)
validations_for_path.each do |validation|
if error = validation.call(path)
2020-04-08 14:13:33 +05:30
raise ::Gitlab::GitAccess::ForbiddenError, error
2019-02-15 15:39:39 +05:30
end
end
end
# Method overwritten in EE to inject custom validations
2021-03-08 18:12:59 +05:30
def validations_for_path
2019-02-15 15:39:39 +05:30
[]
end
2021-03-08 18:12:59 +05:30
def file_paths_validations
2019-02-15 15:39:39 +05:30
validate_lfs_file_locks? ? [lfs_file_locks_validation] : []
end
def validate_file_paths(file_paths)
logger.log_timed(LOG_MESSAGES[__method__]) do
2021-03-08 18:12:59 +05:30
file_paths_validations.each do |validation|
2019-02-15 15:39:39 +05:30
if error = validation.call(file_paths)
2020-04-08 14:13:33 +05:30
raise ::Gitlab::GitAccess::ForbiddenError, error
2019-02-15 15:39:39 +05:30
end
end
end
end
# rubocop: disable CodeReuse/ActiveRecord
def lfs_file_locks_validation
lambda do |paths|
lfs_lock = project.lfs_file_locks.where(path: paths).where.not(user_id: user_access.user.id).take
if lfs_lock
return "The path '#{lfs_lock.path}' is locked in Git LFS by #{lfs_lock.user.name}"
end
end
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
end
2020-05-24 23:13:21 +05:30
2021-06-08 01:23:25 +05:30
Gitlab::Checks::DiffCheck.prepend_mod_with('Gitlab::Checks::DiffCheck')