2020-06-23 00:09:42 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Suggestions
|
|
|
|
class CommitMessage
|
|
|
|
DEFAULT_SUGGESTION_COMMIT_MESSAGE =
|
|
|
|
'Apply %{suggestions_count} suggestion(s) to %{files_count} file(s)'
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
def initialize(user, suggestion_set, custom_message = nil)
|
2020-06-23 00:09:42 +05:30
|
|
|
@user = user
|
|
|
|
@suggestion_set = suggestion_set
|
2021-03-11 19:13:27 +05:30
|
|
|
@custom_message = custom_message
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def message
|
2022-06-21 17:19:12 +05:30
|
|
|
project = suggestion_set.target_project
|
2021-03-11 19:13:27 +05:30
|
|
|
user_defined_message = @custom_message.presence || project.suggestion_commit_message.presence
|
2020-06-23 00:09:42 +05:30
|
|
|
message = user_defined_message || DEFAULT_SUGGESTION_COMMIT_MESSAGE
|
|
|
|
|
|
|
|
Gitlab::StringPlaceholderReplacer
|
|
|
|
.replace_string_placeholders(message, PLACEHOLDERS_REGEX) do |key|
|
|
|
|
PLACEHOLDERS[key].call(user, suggestion_set)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.format_paths(paths)
|
|
|
|
paths.sort.join(', ')
|
|
|
|
end
|
|
|
|
|
|
|
|
private_class_method :format_paths
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :user, :suggestion_set
|
|
|
|
|
|
|
|
PLACEHOLDERS = {
|
|
|
|
'branch_name' => ->(user, suggestion_set) { suggestion_set.branch },
|
|
|
|
'files_count' => ->(user, suggestion_set) { suggestion_set.file_paths.length },
|
|
|
|
'file_paths' => ->(user, suggestion_set) { format_paths(suggestion_set.file_paths) },
|
2022-06-21 17:19:12 +05:30
|
|
|
'project_name' => ->(user, suggestion_set) { suggestion_set.target_project.name },
|
|
|
|
'project_path' => ->(user, suggestion_set) { suggestion_set.target_project.path },
|
2020-06-23 00:09:42 +05:30
|
|
|
'user_full_name' => ->(user, suggestion_set) { user.name },
|
|
|
|
'username' => ->(user, suggestion_set) { user.username },
|
|
|
|
'suggestions_count' => ->(user, suggestion_set) { suggestion_set.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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|