summaryrefslogtreecommitdiff
path: root/app/services/suggestions
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-01-13 12:08:04 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-01-13 12:08:04 +0000
commiteb30dd6e28f6fc9eb8021d205f6ed84511f001e2 (patch)
tree9557e4782c762f4d08f57c9e04991bf988695085 /app/services/suggestions
parentc3ad57034cc1cbd6d0ad02de7ac57f6004440c83 (diff)
downloadgitlab-ce-eb30dd6e28f6fc9eb8021d205f6ed84511f001e2.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services/suggestions')
-rw-r--r--app/services/suggestions/apply_service.rb37
1 files changed, 34 insertions, 3 deletions
diff --git a/app/services/suggestions/apply_service.rb b/app/services/suggestions/apply_service.rb
index 8ba50e22b09..a6485e42bdb 100644
--- a/app/services/suggestions/apply_service.rb
+++ b/app/services/suggestions/apply_service.rb
@@ -2,6 +2,24 @@
module Suggestions
class ApplyService < ::BaseService
+ DEFAULT_SUGGESTION_COMMIT_MESSAGE = 'Apply suggestion to %{file_path}'
+
+ PLACEHOLDERS = {
+ 'project_path' => ->(suggestion, user) { suggestion.project.path },
+ 'project_name' => ->(suggestion, user) { suggestion.project.name },
+ 'file_path' => ->(suggestion, user) { suggestion.file_path },
+ 'branch_name' => ->(suggestion, user) { suggestion.branch },
+ 'username' => ->(suggestion, user) { user.username },
+ 'user_full_name' => ->(suggestion, user) { user.name }
+ }.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
+
+ attr_reader :current_user
+
def initialize(current_user)
@current_user = current_user
end
@@ -22,7 +40,7 @@ module Suggestions
end
params = file_update_params(suggestion, diff_file)
- result = ::Files::UpdateService.new(suggestion.project, @current_user, params).execute
+ result = ::Files::UpdateService.new(suggestion.project, current_user, params).execute
if result[:status] == :success
suggestion.update(commit_id: result[:result], applied: true)
@@ -46,13 +64,14 @@ module Suggestions
def file_update_params(suggestion, diff_file)
blob = diff_file.new_blob
+ project = suggestion.project
file_path = suggestion.file_path
branch_name = suggestion.branch
file_content = new_file_content(suggestion, blob)
- commit_message = "Apply suggestion to #{file_path}"
+ commit_message = processed_suggestion_commit_message(suggestion)
file_last_commit =
- Gitlab::Git::Commit.last_for_path(suggestion.project.repository,
+ Gitlab::Git::Commit.last_for_path(project.repository,
blob.commit_id,
blob.path)
@@ -75,5 +94,17 @@ module Suggestions
content.join
end
+
+ def suggestion_commit_message(project)
+ project.suggestion_commit_message || DEFAULT_SUGGESTION_COMMIT_MESSAGE
+ end
+
+ def processed_suggestion_commit_message(suggestion)
+ message = suggestion_commit_message(suggestion.project)
+
+ Gitlab::StringPlaceholderReplacer.replace_string_placeholders(message, PLACEHOLDERS_REGEX) do |key|
+ PLACEHOLDERS[key].call(suggestion, current_user)
+ end
+ end
end
end