merge_service.rb 3.82 KB
Newer Older
1 2
# frozen_string_literal: true

Dmytro Zaporozhets's avatar
Dmytro Zaporozhets committed
3 4 5
module MergeRequests
  # MergeService class
  #
6 7 8 9
  # Do git merge and in case of success
  # mark merge request as merged and execute all hooks and notifications
  # Executed when you do merge via GitLab UI
  #
10
  class MergeService < MergeRequests::MergeBaseService
11 12
    delegate :merge_jid, :state, to: :@merge_request

13
    def execute(merge_request)
14 15 16 17 18
      if project.merge_requests_ff_only_enabled && !self.is_a?(FfMergeService)
        FfMergeService.new(project, current_user, params).execute(merge_request)
        return
      end

19
      @merge_request = merge_request
Dmytro Zaporozhets's avatar
Dmytro Zaporozhets committed
20

21
      validate!
22

23
      merge_request.in_locked_state do
24
        if commit
25
          after_merge
26
          clean_merge_jid
27 28 29
          success
        end
      end
30
      log_info("Merge process finished on JID #{merge_jid} with state #{state}")
31
    rescue MergeError => e
32
      handle_merge_error(log_message: e.message, save_message_on_model: true)
33 34 35 36
    end

    private

37 38 39 40 41 42 43 44 45 46 47
    def validate!
      authorization_check!
      error_check!
    end

    def authorization_check!
      unless @merge_request.can_be_merged_by?(current_user)
        raise_error('You are not allowed to merge this merge request')
      end
    end

48
    def error_check!
49 50
      super

51 52 53 54 55 56 57 58 59
      error =
        if @merge_request.should_be_rebased?
          'Only fast-forward merge is allowed for your project. Please update your source branch'
        elsif !@merge_request.mergeable?
          'Merge request is not mergeable'
        elsif !source
          'No source for merge'
        end

60
      raise_error(error) if error
61 62
    end

63
    def commit
64
      log_info("Git merge started on JID #{merge_jid}")
65 66 67 68 69
      commit_id = try_merge

      if commit_id
        log_info("Git merge finished on JID #{merge_jid} commit #{commit_id}")
      else
70
        raise_error('Conflicts detected during merge')
71
      end
72

73 74 75 76
      merge_request.update!(merge_commit_sha: commit_id)
    end

    def try_merge
77
      repository.merge(current_user, source, merge_request, commit_message)
78
    rescue Gitlab::Git::PreReceiveError => e
79 80
      raise MergeError,
            "Something went wrong during merge pre-receive hook. #{e.message}".strip
81 82
    rescue => e
      handle_merge_error(log_message: e.message)
83
      raise_error('Something went wrong during merge')
84
    ensure
85
      merge_request.update!(in_progress_merge_commit_sha: nil)
86
    end
Dmytro Zaporozhets's avatar
Dmytro Zaporozhets committed
87

88
    def after_merge
89
      log_info("Post merge started on JID #{merge_jid} with state #{state}")
90
      MergeRequests::PostMergeService.new(project, current_user).execute(merge_request)
91
      log_info("Post merge finished on JID #{merge_jid} with state #{state}")
92

93 94 95
      if delete_source_branch?
        DeleteBranchService.new(@merge_request.source_project, branch_deletion_user)
          .execute(merge_request.source_branch)
96
      end
Dmytro Zaporozhets's avatar
Dmytro Zaporozhets committed
97
    end
98

99 100 101 102
    def clean_merge_jid
      merge_request.update_column(:merge_jid, nil)
    end

103 104 105
    def branch_deletion_user
      @merge_request.force_remove_source_branch? ? @merge_request.author : current_user
    end
Micaël Bergeron's avatar
Micaël Bergeron committed
106

Micaël Bergeron's avatar
Micaël Bergeron committed
107 108 109
    # Verify again that the source branch can be removed, since branch may be protected,
    # or the source branch may have been updated, or the user may not have permission
    #
110 111 112 113 114
    def delete_source_branch?
      params.fetch('should_remove_source_branch', @merge_request.force_remove_source_branch?) &&
        @merge_request.can_remove_source_branch?(branch_deletion_user)
    end

115 116
    def handle_merge_error(log_message:, save_message_on_model: false)
      Rails.logger.error("MergeService ERROR: #{merge_request_info} - #{log_message}")
117
      @merge_request.update(merge_error: log_message) if save_message_on_model
118
    end
119

Oswaldo Ferreira's avatar
Oswaldo Ferreira committed
120 121 122 123 124
    def log_info(message)
      @logger ||= Rails.logger
      @logger.info("#{merge_request_info} - #{message}")
    end

125
    def merge_request_info
126 127
      merge_request.to_reference(full: true)
    end
Dmytro Zaporozhets's avatar
Dmytro Zaporozhets committed
128 129
  end
end