2018-12-13 14:17:19 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Suggestions
|
|
|
|
class ApplyService < ::BaseService
|
2021-01-26 10:08:58 -05:00
|
|
|
def initialize(current_user, *suggestions, message: nil)
|
2018-12-13 14:17:19 -05:00
|
|
|
@current_user = current_user
|
2021-01-26 10:08:58 -05:00
|
|
|
@message = message
|
2020-06-08 20:08:47 -04:00
|
|
|
@suggestion_set = Gitlab::Suggestions::SuggestionSet.new(suggestions)
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
def execute
|
|
|
|
if suggestion_set.valid?
|
|
|
|
result
|
|
|
|
else
|
|
|
|
error(suggestion_set.error_message)
|
2019-01-10 07:38:15 -05:00
|
|
|
end
|
2020-06-08 20:08:47 -04:00
|
|
|
end
|
2019-01-10 07:38:15 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
private
|
2019-02-20 16:20:08 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
attr_reader :current_user, :suggestion_set
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
def result
|
|
|
|
multi_service.execute.tap do |result|
|
|
|
|
update_suggestions(result)
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
def update_suggestions(result)
|
|
|
|
return unless result[:status] == :success
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
Suggestion.id_in(suggestion_set.suggestions)
|
|
|
|
.update_all(commit_id: result[:result], applied: true)
|
2021-01-25 13:09:03 -05:00
|
|
|
|
|
|
|
Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter
|
2021-08-20 17:10:36 -04:00
|
|
|
.track_apply_suggestion_action(user: current_user, suggestions: suggestion_set.suggestions)
|
2019-01-10 07:38:15 -05:00
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2021-04-23 14:10:18 -04:00
|
|
|
def author
|
|
|
|
authors = suggestion_set.authors
|
|
|
|
|
|
|
|
return unless authors.one?
|
|
|
|
|
|
|
|
Gitlab::Git::User.from_gitlab(authors.first)
|
|
|
|
end
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
def multi_service
|
|
|
|
params = {
|
2018-12-13 14:17:19 -05:00
|
|
|
commit_message: commit_message,
|
2020-06-08 20:08:47 -04:00
|
|
|
branch_name: suggestion_set.branch,
|
|
|
|
start_branch: suggestion_set.branch,
|
2021-04-23 14:10:18 -04:00
|
|
|
actions: suggestion_set.actions,
|
|
|
|
author_name: author&.name,
|
|
|
|
author_email: author&.email
|
2018-12-13 14:17:19 -05:00
|
|
|
}
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
::Files::MultiService.new(suggestion_set.project, current_user, params)
|
2020-01-13 07:08:04 -05:00
|
|
|
end
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
def commit_message
|
2021-01-26 10:08:58 -05:00
|
|
|
Gitlab::Suggestions::CommitMessage.new(current_user, suggestion_set, @message).message
|
2020-01-13 07:08:04 -05:00
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
end
|