debian-mirror-gitlab/app/services/files/multi_service.rb

72 lines
1.9 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2016-11-03 12:29:30 +05:30
module Files
class MultiService < Files::BaseService
2018-12-05 23:21:45 +05:30
UPDATE_FILE_ACTIONS = %w(update move delete chmod).freeze
2018-03-17 18:26:18 +05:30
2017-08-17 22:00:37 +05:30
def create_commit!
2018-05-09 12:01:36 +05:30
transformer = Lfs::FileTransformer.new(project, @branch_name)
actions = actions_after_lfs_transformation(transformer, params[:actions])
2018-12-23 12:14:25 +05:30
actions = transform_move_actions(actions)
2018-05-09 12:01:36 +05:30
commit_actions!(actions)
end
private
def actions_after_lfs_transformation(transformer, actions)
actions.map do |action|
if action[:action] == 'create'
result = transformer.new_file(action[:file_path], action[:content], encoding: action[:encoding])
action[:content] = result.content
action[:encoding] = result.encoding
end
action
end
end
2018-12-23 12:14:25 +05:30
# When moving a file, `content: nil` means "use the contents of the previous
# file", while `content: ''` means "move the file and set it to empty"
def transform_move_actions(actions)
actions.map do |action|
action[:infer_content] = true if action[:content].nil?
action
end
end
2018-05-09 12:01:36 +05:30
def commit_actions!(actions)
2016-11-03 12:29:30 +05:30
repository.multi_action(
2018-03-17 18:26:18 +05:30
current_user,
2016-11-03 12:29:30 +05:30
message: @commit_message,
2017-08-17 22:00:37 +05:30
branch_name: @branch_name,
2018-05-09 12:01:36 +05:30
actions: actions,
2016-11-03 12:29:30 +05:30
author_email: @author_email,
2017-08-17 22:00:37 +05:30
author_name: @author_name,
start_project: @start_project,
start_branch_name: @start_branch
2016-11-03 12:29:30 +05:30
)
2018-03-17 18:26:18 +05:30
rescue ArgumentError => e
raise_error(e)
2016-11-03 12:29:30 +05:30
end
2017-08-17 22:00:37 +05:30
def validate!
2016-11-03 12:29:30 +05:30
super
2018-03-17 18:26:18 +05:30
params[:actions].each { |action| validate_file_status!(action) }
2016-11-03 12:29:30 +05:30
end
2018-03-17 18:26:18 +05:30
def validate_file_status!(action)
return unless UPDATE_FILE_ACTIONS.include?(action[:action])
file_path = action[:previous_path] || action[:file_path]
if file_has_changed?(file_path, action[:last_commit_id])
raise_error("The file has changed since you started editing it: #{file_path}")
2016-11-03 12:29:30 +05:30
end
end
end
end