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
13 files
+ 450
235
Compare changes
  • Side-by-side
  • Inline
Files
13
# frozen_string_literal: true
module Gitlab
module Suggestions
module CommitMessage
DEFAULT_SUGGESTION_COMMIT_MESSAGE =
'Apply %{suggestions_count} suggestion(s) to %{files_count} file(s)'
PLACEHOLDERS = {
'branch_name' => ->(suggestions, user) { suggestions.first.branch },
'files_count' => ->(suggestions, user) { paths(suggestions).length },
'file_paths' => lambda do |suggestions, user|
paths = paths(suggestions)
format_paths(paths)
end,
'project_name' => ->(suggestions, user) { suggestions.first.project.name },
'project_path' => ->(suggestions, user) { suggestions.first.project.path },
'user_full_name' => ->(suggestions, user) { user.name },
'username' => ->(suggestions, user) { user.username },
'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 do |key|
Regexp.new(Regexp.escape(key))
end).freeze
def self.paths(suggestions)
suggestions.map(&:file_path).uniq
end
def self.format_paths(paths)
paths.sort.join(", ")
end
def self.message(suggestions, current_user)
project = suggestions.first.project
user_defined_message = project.suggestion_commit_message.presence
message = user_defined_message || DEFAULT_SUGGESTION_COMMIT_MESSAGE
Gitlab::StringPlaceholderReplacer
.replace_string_placeholders(message, PLACEHOLDERS_REGEX) do |key|
PLACEHOLDERS[key].call(suggestions, current_user)
end
end
end
end
end
Loading