2016-08-29 19:58:32 -04:00
|
|
|
module Files
|
|
|
|
class MultiService < Files::BaseService
|
2017-03-01 06:00:37 -05:00
|
|
|
FileChangedError = Class.new(StandardError)
|
2016-08-29 19:58:32 -04:00
|
|
|
|
2017-02-13 16:09:36 -05:00
|
|
|
ACTIONS = %w[create update delete move].freeze
|
|
|
|
|
2016-08-29 19:58:32 -04:00
|
|
|
def commit
|
|
|
|
repository.multi_action(
|
|
|
|
user: current_user,
|
|
|
|
message: @commit_message,
|
2016-12-08 04:08:25 -05:00
|
|
|
branch_name: @target_branch,
|
2016-08-29 19:58:32 -04:00
|
|
|
actions: params[:actions],
|
|
|
|
author_email: @author_email,
|
2016-11-14 15:02:10 -05:00
|
|
|
author_name: @author_name,
|
2017-01-06 10:29:13 -05:00
|
|
|
start_project: @start_project,
|
|
|
|
start_branch_name: @start_branch
|
2016-08-29 19:58:32 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def validate
|
|
|
|
super
|
2017-02-24 15:11:10 -05:00
|
|
|
|
2016-08-29 19:58:32 -04:00
|
|
|
params[:actions].each_with_index do |action, index|
|
2017-02-24 10:32:10 -05:00
|
|
|
if ACTIONS.include?(action[:action].to_s)
|
|
|
|
action[:action] = action[:action].to_sym
|
|
|
|
else
|
|
|
|
raise_error("Unknown action type `#{action[:action]}`.")
|
|
|
|
end
|
2017-02-24 10:38:44 -05:00
|
|
|
|
2016-08-29 19:58:32 -04:00
|
|
|
unless action[:file_path].present?
|
|
|
|
raise_error("You must specify a file_path.")
|
|
|
|
end
|
|
|
|
|
2017-02-13 16:09:36 -05:00
|
|
|
action[:file_path].slice!(0) if action[:file_path] && action[:file_path].start_with?('/')
|
|
|
|
action[:previous_path].slice!(0) if action[:previous_path] && action[:previous_path].start_with?('/')
|
|
|
|
|
2016-08-29 19:58:32 -04:00
|
|
|
regex_check(action[:file_path])
|
|
|
|
regex_check(action[:previous_path]) if action[:previous_path]
|
|
|
|
|
|
|
|
if project.empty_repo? && action[:action] != :create
|
|
|
|
raise_error("No files to #{action[:action]}.")
|
|
|
|
end
|
|
|
|
|
|
|
|
validate_file_exists(action)
|
|
|
|
|
|
|
|
case action[:action]
|
|
|
|
when :create
|
|
|
|
validate_create(action)
|
|
|
|
when :update
|
|
|
|
validate_update(action)
|
|
|
|
when :delete
|
|
|
|
validate_delete(action)
|
|
|
|
when :move
|
|
|
|
validate_move(action, index)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_file_exists(action)
|
|
|
|
return if action[:action] == :create
|
|
|
|
|
|
|
|
file_path = action[:file_path]
|
|
|
|
file_path = action[:previous_path] if action[:action] == :move
|
|
|
|
|
2017-02-02 09:24:30 -05:00
|
|
|
blob = repository.blob_at_branch(params[:branch], file_path)
|
2016-08-29 19:58:32 -04:00
|
|
|
|
|
|
|
unless blob
|
|
|
|
raise_error("File to be #{action[:action]}d `#{file_path}` does not exist.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_commit
|
2017-01-06 10:29:13 -05:00
|
|
|
Gitlab::Git::Commit.last_for_path(repository, @start_branch, @file_path)
|
2016-08-29 19:58:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def regex_check(file)
|
|
|
|
if file =~ Gitlab::Regex.directory_traversal_regex
|
|
|
|
raise_error(
|
|
|
|
'Your changes could not be committed, because the file name, `' +
|
|
|
|
file +
|
|
|
|
'` ' +
|
|
|
|
Gitlab::Regex.directory_traversal_regex_message
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
unless file =~ Gitlab::Regex.file_path_regex
|
|
|
|
raise_error(
|
|
|
|
'Your changes could not be committed, because the file name, `' +
|
|
|
|
file +
|
|
|
|
'` ' +
|
|
|
|
Gitlab::Regex.file_path_regex_message
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_create(action)
|
|
|
|
return if project.empty_repo?
|
|
|
|
|
2017-02-02 09:24:30 -05:00
|
|
|
if repository.blob_at_branch(params[:branch], action[:file_path])
|
2016-08-29 19:58:32 -04:00
|
|
|
raise_error("Your changes could not be committed because a file with the name `#{action[:file_path]}` already exists.")
|
|
|
|
end
|
2017-02-24 10:38:44 -05:00
|
|
|
|
2017-02-24 15:11:10 -05:00
|
|
|
if action[:content].nil?
|
2017-02-24 10:38:44 -05:00
|
|
|
raise_error("You must provide content.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_update(action)
|
2017-02-24 15:11:10 -05:00
|
|
|
if action[:content].nil?
|
2017-02-24 10:38:44 -05:00
|
|
|
raise_error("You must provide content.")
|
|
|
|
end
|
|
|
|
|
|
|
|
if file_has_changed?
|
|
|
|
raise FileChangedError.new("You are attempting to update a file `#{action[:file_path]}` that has changed since you started editing it.")
|
|
|
|
end
|
2016-08-29 19:58:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def validate_delete(action)
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_move(action, index)
|
|
|
|
if action[:previous_path].nil?
|
|
|
|
raise_error("You must supply the original file path when moving file `#{action[:file_path]}`.")
|
|
|
|
end
|
|
|
|
|
2017-02-02 09:24:30 -05:00
|
|
|
blob = repository.blob_at_branch(params[:branch], action[:file_path])
|
2016-08-29 19:58:32 -04:00
|
|
|
|
|
|
|
if blob
|
|
|
|
raise_error("Move destination `#{action[:file_path]}` already exists.")
|
|
|
|
end
|
|
|
|
|
|
|
|
if action[:content].nil?
|
2017-02-02 09:24:30 -05:00
|
|
|
blob = repository.blob_at_branch(params[:branch], action[:previous_path])
|
2016-08-29 19:58:32 -04:00
|
|
|
blob.load_all_data!(repository) if blob.truncated?
|
|
|
|
params[:actions][index][:content] = blob.data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|