apply_service.rb 2.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
# 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

14
      unless latest_source_head?(suggestion)
15 16 17
        return error('The file has been changed')
      end

18 19 20 21 22 23 24 25
      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
26 27
    rescue Files::UpdateService::FileChangedError
      error('The file has been changed')
28 29 30 31
    end

    private

32 33 34 35 36 37 38
    # Checks whether the latest source branch HEAD matches with
    # the position HEAD we're using to update the file content. Since
    # the persisted HEAD is updated async (for MergeRequest),
    # it's more consistent to fetch this data directly from the
    # repository.
    def latest_source_head?(suggestion)
      suggestion.position.head_sha == suggestion.noteable.source_branch_sha
39
    end
40

41 42 43 44 45
    def file_update_params(suggestion)
      blob = suggestion.diff_file.new_blob
      file_path = suggestion.file_path
      branch_name = suggestion.branch
      file_content = new_file_content(suggestion, blob)
46 47
      commit_message = "Apply suggestion to #{file_path}"

48 49 50 51 52
      file_last_commit =
        Gitlab::Git::Commit.last_for_path(suggestion.project.repository,
                                          blob.commit_id,
                                          blob.path)

53 54 55 56 57
      {
        file_path: file_path,
        branch_name: branch_name,
        start_branch: branch_name,
        commit_message: commit_message,
58 59
        file_content: file_content,
        last_commit_sha: file_last_commit&.id
60 61 62
      }
    end

63
    def new_file_content(suggestion, blob)
64 65 66 67 68 69 70 71 72 73
      range = suggestion.from_line_index..suggestion.to_line_index

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

      content.join
    end
  end
end