Add auto merge process trigger on merge status change to can_be_merged

Summary

This MR adds a check for AutoMergeProcessWorker.perform_async when the merge status changes to can_be_merged and the merge request has auto merge enabled.

Changes

  1. Feature Flag: Added auto_merge_on_merge_status_change feature flag (gitlab_com_derisk type)

  2. MergeRequest Model: Added an after_transition callback in the merge_status state machine that:

    • Triggers when transitioning to can_be_merged state
    • Only fires if auto_merge_enabled? is true
    • Only fires if the feature flag is enabled for the project
    • Enqueues AutoMergeProcessWorker with the merge request ID
  3. Specs: Added comprehensive specs covering:

    • Feature flag enabled/disabled scenarios
    • Auto merge enabled/disabled scenarios
    • Transitions from all valid source states

Why

Currently, AutoMergeProcessWorker is triggered when pipelines complete. However, there are scenarios where a merge request becomes mergeable due to merge status changes (e.g., conflict resolution) without a pipeline completion event. This change ensures auto merge is processed in those cases as well.

Feature Flag

The feature is behind the auto_merge_on_merge_status_change feature flag, allowing for safe rollout.

Test Instructions

Prerequisites

  1. Enable the feature flag:
    Feature.enable(:auto_merge_on_merge_status_change)

Test Scenario 1: Auto merge triggers on merge status change

  1. Create a merge request with a passing pipeline
  2. Set up "Merge when pipeline succeeds" (MWPS) on the MR
  3. Introduce a merge conflict (e.g., push a conflicting change to the target branch)
  4. Verify the MR shows as having conflicts and cannot be merged
  5. Resolve the conflict (rebase or manually resolve)
  6. Expected: Once the merge status changes to can_be_merged, the AutoMergeProcessWorker should be enqueued and the MR should automatically merge

Test Scenario 2: Feature flag disabled

  1. Disable the feature flag:
    Feature.disable(:auto_merge_on_merge_status_change)
  2. Repeat the steps from Scenario 1
  3. Expected: The AutoMergeProcessWorker should NOT be enqueued when merge status changes to can_be_merged

Test Scenario 3: Auto merge not enabled

  1. Enable the feature flag
  2. Create a merge request WITHOUT auto merge enabled
  3. Trigger a merge status change to can_be_merged
  4. Expected: The AutoMergeProcessWorker should NOT be enqueued

Verification via Rails console

You can verify the worker is being enqueued by checking Sidekiq logs or using:

# Watch for AutoMergeProcessWorker jobs
Sidekiq::Queue.new('default').select { |job| job.klass == 'AutoMergeProcessWorker' }
Edited by Marc Shaw

Merge request reports

Loading