2020-02-13 10:08:52 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class SnippetRepository < ApplicationRecord
|
2020-12-23 07:10:26 -05:00
|
|
|
include EachBatch
|
2020-02-13 10:08:52 -05:00
|
|
|
include Shardable
|
|
|
|
|
2020-02-28 13:09:07 -05:00
|
|
|
DEFAULT_EMPTY_FILE_NAME = 'snippetfile'
|
2020-03-10 08:08:16 -04:00
|
|
|
EMPTY_FILE_PATTERN = /^#{DEFAULT_EMPTY_FILE_NAME}(\d+)\.txt$/.freeze
|
2020-02-28 13:09:07 -05:00
|
|
|
|
|
|
|
CommitError = Class.new(StandardError)
|
2020-05-06 17:10:00 -04:00
|
|
|
InvalidPathError = Class.new(CommitError)
|
2020-05-11 17:09:40 -04:00
|
|
|
InvalidSignatureError = Class.new(CommitError)
|
2020-02-28 13:09:07 -05:00
|
|
|
|
2020-02-13 10:08:52 -05:00
|
|
|
belongs_to :snippet, inverse_of: :snippet_repository
|
|
|
|
|
2020-09-30 23:09:55 -04:00
|
|
|
delegate :repository, :repository_storage, to: :snippet
|
2020-02-28 13:09:07 -05:00
|
|
|
|
2020-02-13 10:08:52 -05:00
|
|
|
class << self
|
|
|
|
def find_snippet(disk_path)
|
|
|
|
find_by(disk_path: disk_path)&.snippet
|
|
|
|
end
|
|
|
|
end
|
2020-02-28 13:09:07 -05:00
|
|
|
|
|
|
|
def multi_files_action(user, files = [], **options)
|
|
|
|
return if files.nil? || files.empty?
|
|
|
|
|
|
|
|
lease_key = "multi_files_action:#{snippet_id}"
|
|
|
|
|
|
|
|
lease = Gitlab::ExclusiveLease.new(lease_key, timeout: 120)
|
|
|
|
raise CommitError, 'Snippet is already being updated' unless uuid = lease.try_obtain
|
|
|
|
|
|
|
|
options[:actions] = transform_file_entries(files)
|
|
|
|
|
2022-09-13 05:13:39 -04:00
|
|
|
capture_git_error { repository.commit_files(user, **options) }
|
2020-02-28 13:09:07 -05:00
|
|
|
ensure
|
|
|
|
Gitlab::ExclusiveLease.cancel(lease_key, uuid)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def capture_git_error(&block)
|
|
|
|
yield block
|
|
|
|
rescue Gitlab::Git::Index::IndexError,
|
|
|
|
Gitlab::Git::CommitError,
|
|
|
|
Gitlab::Git::PreReceiveError,
|
2020-05-11 17:09:40 -04:00
|
|
|
Gitlab::Git::CommandError,
|
2022-08-09 11:11:31 -04:00
|
|
|
ArgumentError => e
|
2020-05-13 17:08:55 -04:00
|
|
|
|
2022-08-09 11:11:31 -04:00
|
|
|
logger.error(message: "Snippet git error. Reason: #{e.message}", snippet: snippet.id)
|
2020-05-13 17:08:55 -04:00
|
|
|
|
2022-08-09 11:11:31 -04:00
|
|
|
raise commit_error_exception(e)
|
2020-02-28 13:09:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def transform_file_entries(files)
|
2020-03-10 08:08:16 -04:00
|
|
|
next_index = get_last_empty_file_index + 1
|
2020-02-28 13:09:07 -05:00
|
|
|
|
2020-05-29 08:08:19 -04:00
|
|
|
files.map do |file_entry|
|
2020-03-23 08:09:47 -04:00
|
|
|
file_entry[:file_path] = file_path_for(file_entry, next_index) { next_index += 1 }
|
2020-02-28 13:09:07 -05:00
|
|
|
file_entry[:action] = infer_action(file_entry) unless file_entry[:action]
|
2020-05-29 08:08:19 -04:00
|
|
|
file_entry[:action] = file_entry[:action].to_sym
|
|
|
|
|
|
|
|
if only_rename_action?(file_entry)
|
|
|
|
file_entry[:infer_content] = true
|
|
|
|
elsif empty_update_action?(file_entry)
|
|
|
|
# There is no need to perform a repository operation
|
|
|
|
# When the update action has no content
|
|
|
|
file_entry = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
file_entry
|
|
|
|
end.compact
|
2020-02-28 13:09:07 -05:00
|
|
|
end
|
|
|
|
|
2020-03-23 08:09:47 -04:00
|
|
|
def file_path_for(file_entry, next_index)
|
|
|
|
return file_entry[:file_path] if file_entry[:file_path].present?
|
|
|
|
return file_entry[:previous_path] if reuse_previous_path?(file_entry)
|
|
|
|
|
|
|
|
build_empty_file_name(next_index).tap { yield }
|
|
|
|
end
|
|
|
|
|
|
|
|
# If the user removed the file_path and the previous_path
|
|
|
|
# matches the EMPTY_FILE_PATTERN, we don't need to
|
|
|
|
# rename the file and build a new empty file name,
|
|
|
|
# we can just reuse the existing file name
|
|
|
|
def reuse_previous_path?(file_entry)
|
|
|
|
file_entry[:file_path].blank? &&
|
|
|
|
EMPTY_FILE_PATTERN.match?(file_entry[:previous_path])
|
|
|
|
end
|
|
|
|
|
2020-02-28 13:09:07 -05:00
|
|
|
def infer_action(file_entry)
|
|
|
|
return :create if file_entry[:previous_path].blank?
|
|
|
|
|
|
|
|
file_entry[:previous_path] != file_entry[:file_path] ? :move : :update
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_last_empty_file_index
|
2020-09-16 08:10:15 -04:00
|
|
|
repository.ls_files(snippet.default_branch).inject(0) do |max, file|
|
2020-03-10 08:08:16 -04:00
|
|
|
idx = file[EMPTY_FILE_PATTERN, 1].to_i
|
|
|
|
[idx, max].max
|
|
|
|
end
|
2020-02-28 13:09:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_empty_file_name(index)
|
|
|
|
"#{DEFAULT_EMPTY_FILE_NAME}#{index}.txt"
|
|
|
|
end
|
2020-05-06 17:10:00 -04:00
|
|
|
|
2020-05-11 17:09:40 -04:00
|
|
|
def commit_error_exception(err)
|
|
|
|
if invalid_path_error?(err)
|
2020-05-13 17:08:55 -04:00
|
|
|
InvalidPathError.new('Invalid file name') # To avoid returning the message with the path included
|
2020-05-11 17:09:40 -04:00
|
|
|
elsif invalid_signature_error?(err)
|
|
|
|
InvalidSignatureError.new(err.message)
|
2020-05-06 17:10:00 -04:00
|
|
|
else
|
2020-05-11 17:09:40 -04:00
|
|
|
CommitError.new(err.message)
|
2020-05-06 17:10:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-11 17:09:40 -04:00
|
|
|
def invalid_path_error?(err)
|
2021-02-22 04:10:46 -05:00
|
|
|
err.is_a?(Gitlab::Git::Index::IndexError) &&
|
|
|
|
err.message.downcase.start_with?('invalid path', 'path cannot include directory traversal')
|
2020-05-11 17:09:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def invalid_signature_error?(err)
|
|
|
|
err.is_a?(ArgumentError) &&
|
|
|
|
err.message.downcase.match?(/failed to parse signature/)
|
2020-05-06 17:10:00 -04:00
|
|
|
end
|
2020-05-29 08:08:19 -04:00
|
|
|
|
|
|
|
def only_rename_action?(action)
|
|
|
|
action[:action] == :move && action[:content].nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
def empty_update_action?(action)
|
|
|
|
action[:action] == :update && action[:content].nil?
|
|
|
|
end
|
2020-02-13 10:08:52 -05:00
|
|
|
end
|
2020-09-11 17:08:44 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
SnippetRepository.prepend_mod_with('SnippetRepository')
|