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