apply_service.rb 1.3 KB
Newer Older
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
# frozen_string_literal: true

module Suggestions
  class ApplyService < ::BaseService
    def initialize(current_user)
      @current_user = current_user
    end

    def execute(suggestion)
      unless suggestion.appliable?
        return error('Suggestion is not appliable')
      end

      params = file_update_params(suggestion)
      result = ::Files::UpdateService.new(suggestion.project, @current_user, params).execute

      if result[:status] == :success
        suggestion.update(commit_id: result[:result], applied: true)
      end

      result
    end

    private

    def file_update_params(suggestion)
      diff_file = suggestion.diff_file

      file_path = diff_file.file_path
      branch_name = suggestion.noteable.source_branch
      file_content = new_file_content(suggestion)
      commit_message = "Apply suggestion to #{file_path}"

      {
        file_path: file_path,
        branch_name: branch_name,
        start_branch: branch_name,
        commit_message: commit_message,
        file_content: file_content
      }
    end

    def new_file_content(suggestion)
      range = suggestion.from_line_index..suggestion.to_line_index
      blob = suggestion.diff_file.new_blob

      blob.load_all_data!
      content = blob.data.lines
      content[range] = suggestion.to_content

      content.join
    end
  end
end