Skip to content

Extract MergeService strategies into its own class

Max Fan requested to merge 420668-refactor-mergeService into master

What does this MR do and why?

Currently MergeRequests::MergeService is bloated, handling multiple responsibilities and relying on circular monkey patching inheritance. This makes it hard to expand and add new features.

This MR splits out the responsibility for the git merge operation into a class MergeRequests::MergeStrategies::FromSourceBranch. This class is responsible for preparing the source for merge (including an optional squash), and performing the git merge operation (including determining whether a fast-forward or merge commit is needed). This leaves MergeRequests::MergeService exclusively responsible for the reusable application logic associated with a merge, such as updating database models and triggering any post-merge actions.

The strategy for a merge is configurable, but currently FromSourceBranch is the only available strategy. As a follow-up, we will add a FromTrainRef strategy, which merges from a fully prepared commit SHA associated with a successful pipeline on a merge train.

This change is behind a Feature Flag.

Screenshots or screen recordings

N/A - refactoring, merge requests should still work

Before After

How to set up and validate locally

  1. In a rails console, enable the feature flag:
    Feature.enable(:refactor_merge_service)
  2. Pick a project for testing
  3. With standard merge commits enabled, merge a merge request
  4. With fast-forward merges enabled, merge a merge request
  5. With semi-linear merges enabled, merge a merge requst
  6. With a merge method of your choice, merge a merge request with a squash merge

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #420668 (closed)

Edited by Hordur Freyr Yngvason

Merge request reports