summaryrefslogtreecommitdiff
path: root/app/services/suggestions/apply_service.rb
blob: 6836700a67d748ea726d9ef1ff5b40e7545d9d0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true

module Suggestions
  class ApplyService < ::BaseService
    def initialize(current_user, *suggestions, message: nil)
      @current_user = current_user
      @message = message
      @suggestion_set = Gitlab::Suggestions::SuggestionSet.new(suggestions)
    end

    def execute
      if suggestion_set.valid?
        result
      else
        error(suggestion_set.error_message)
      end
    end

    private

    attr_reader :current_user, :suggestion_set

    def result
      multi_service.execute.tap do |result|
        update_suggestions(result)
      end
    end

    def update_suggestions(result)
      return unless result[:status] == :success

      Suggestion.id_in(suggestion_set.suggestions)
        .update_all(commit_id: result[:result], applied: true)

      Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter
        .track_apply_suggestion_action(user: current_user)
    end

    def author
      authors = suggestion_set.authors

      return unless authors.one?

      Gitlab::Git::User.from_gitlab(authors.first)
    end

    def multi_service
      params = {
        commit_message: commit_message,
        branch_name: suggestion_set.branch,
        start_branch: suggestion_set.branch,
        actions: suggestion_set.actions,
        author_name: author&.name,
        author_email: author&.email
      }

      ::Files::MultiService.new(suggestion_set.project, current_user, params)
    end

    def commit_message
      Gitlab::Suggestions::CommitMessage.new(current_user, suggestion_set, @message).message
    end
  end
end