2018-12-04 06:55:49 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Checks
|
2021-06-15 14:09:57 -04:00
|
|
|
class DiffCheck < BaseSingleChecker
|
2018-12-04 06:55:49 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
LOG_MESSAGES = {
|
2021-01-05 10:10:02 -05:00
|
|
|
validate_file_paths: "Validating diffs' file paths..."
|
2018-12-04 06:55:49 -05:00
|
|
|
}.freeze
|
|
|
|
|
|
|
|
def validate!
|
2018-12-20 11:45:06 -05:00
|
|
|
return if deletion?
|
2021-01-05 10:10:02 -05:00
|
|
|
return unless should_run_validations?
|
2018-12-04 06:55:49 -05:00
|
|
|
return if commits.empty?
|
|
|
|
|
2021-01-05 10:10:02 -05:00
|
|
|
paths = project.repository.find_changed_paths(commits.map(&:sha))
|
|
|
|
paths.each do |path|
|
|
|
|
validate_path(path)
|
2018-12-04 06:55:49 -05:00
|
|
|
end
|
|
|
|
|
2021-01-05 10:10:02 -05:00
|
|
|
validate_file_paths(paths.map(&:path).uniq)
|
2018-12-04 06:55:49 -05:00
|
|
|
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-01-05 10:10:02 -05:00
|
|
|
def should_run_validations?
|
|
|
|
validations_for_path.present? || file_paths_validations.present?
|
2018-12-04 06:55:49 -05:00
|
|
|
end
|
|
|
|
|
2021-01-05 10:10:02 -05:00
|
|
|
def validate_path(path)
|
|
|
|
validations_for_path.each do |validation|
|
|
|
|
if error = validation.call(path)
|
2020-02-26 13:09:24 -05:00
|
|
|
raise ::Gitlab::GitAccess::ForbiddenError, error
|
2018-12-04 06:55:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Method overwritten in EE to inject custom validations
|
2021-01-05 10:10:02 -05:00
|
|
|
def validations_for_path
|
2018-12-04 06:55:49 -05:00
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2021-01-05 10:10:02 -05:00
|
|
|
def file_paths_validations
|
2018-12-04 06:55:49 -05:00
|
|
|
validate_lfs_file_locks? ? [lfs_file_locks_validation] : []
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_file_paths(file_paths)
|
|
|
|
logger.log_timed(LOG_MESSAGES[__method__]) do
|
2021-01-05 10:10:02 -05:00
|
|
|
file_paths_validations.each do |validation|
|
2018-12-04 06:55:49 -05:00
|
|
|
if error = validation.call(file_paths)
|
2020-02-26 13:09:24 -05:00
|
|
|
raise ::Gitlab::GitAccess::ForbiddenError, error
|
2018-12-04 06:55:49 -05:00
|
|
|
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-04-28 08:09:44 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Gitlab::Checks::DiffCheck.prepend_mod_with('Gitlab::Checks::DiffCheck')
|