Skip to content
Snippets Groups Projects

Users can apply multiple suggestions at once.

Merged Jesse Hall requested to merge jessehall3/gitlab-ee:25486-batch-suggestions into master
5 files
+ 169
149
Compare changes
  • Side-by-side
  • Inline
Files
5
@@ -2,22 +2,27 @@
module Suggestions
class ApplyService < ::BaseService
DEFAULT_SUGGESTION_COMMIT_MESSAGE = "Apply %{suggestions_count} suggestion(s) to:\n%{file_paths}"
DEFAULT_SUGGESTION_COMMIT_MESSAGE =
"Apply %{suggestions_count} suggestion(s) to:\n%{file_paths}"
PLACEHOLDERS = {
'project_path' => ->(suggestions, user) { suggestions[0].project.path },
'project_name' => ->(suggestions, user) { suggestions[0].project.name },
'file_paths' => ->(suggestions, user) { format_paths(suggestions.map(&:file_path))},
'branch_name' => ->(suggestions, user) { suggestions[0].branch },
'suggestions_count' => ->(suggestions, user) { suggestions.size },
'file_paths' => lambda do |suggestions, user|
format_paths(suggestions.map(&:file_path))
end,
'project_name' => ->(suggestions, user) { suggestions[0].project.name },
'project_path' => ->(suggestions, user) { suggestions[0].project.path },
'user_full_name' => ->(suggestions, user) { user.name },
'username' => ->(suggestions, user) { user.username },
'user_full_name' => ->(suggestions, user) { user.name }
'suggestions_count' => ->(suggestions, user) { suggestions.size }
}.freeze
# This regex is built dynamically using the keys from the PLACEHOLDER struct.
# So, we can easily add new placeholder just by modifying the PLACEHOLDER hash.
# This regex will build the new PLACEHOLDER_REGEX with the new information
PLACEHOLDERS_REGEX = Regexp.union(PLACEHOLDERS.keys.map { |key| Regexp.new(Regexp.escape(key)) }).freeze
PLACEHOLDERS_REGEX = Regexp.union(PLACEHOLDERS.keys.map do |key|
Regexp.new(Regexp.escape(key))
end).freeze
attr_reader :current_user
@@ -50,9 +55,7 @@ def build_blobs_data(suggestions)
suggestions.each do |suggestion|
error_message = detect_suggestion_error(suggestion, blob_data_by_path)
if error_message
break
end
break if error_message
add_line_range_info(suggestion, blob_data_by_path)
end
@@ -76,7 +79,9 @@ def add_line_range_info(suggestion, blob_data_by_path)
def result(suggestions, blobs_data)
file_update_params = file_update_params(suggestions, blobs_data)
multi_service = ::Files::MultiService.new(suggestions[0].project, current_user, file_update_params)
multi_service = ::Files::MultiService.new(suggestions[0].project,
current_user,
file_update_params)
multi_service.execute.tap do |result|
update_suggestions(suggestions, result) if result[:status] == :success
@@ -106,7 +111,9 @@ def actions(project, blobs_data)
blobs_data.map do |blob_data|
blob = blob_data[:blob]
file_content = new_file_content(blob_data)
file_last_commit = Gitlab::Git::Commit.last_for_path(project.repository, blob.commit_id, blob.path)
file_last_commit = Gitlab::Git::Commit.last_for_path(project.repository,
blob.commit_id,
blob.path)
{
action: 'update',
@@ -168,7 +175,9 @@ def has_range_conflict?(suggestion, blobs_data)
new_range = suggestion.from_line_index..suggestion.to_line_index
current_ranges = blob_data[:line_ranges_info].map { |range_info| range_info[:range] }
current_ranges = blob_data[:line_ranges_info].map do |range_info|
range_info[:range]
end
current_ranges.any? do |current_range|
current_range.overlaps?(new_range)
Loading