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

To test the fix, use the git push options to create a merge request with auto merge enabled:

git push -o merge_request.create \
  -o merge_request.target=main \
  -o merge_request.title="Generate MR to test automerge $(date +%Y-%m-%d-%H%M%S)" \
  -o merge_request.remove_source_branch=true \
  -o merge_request.auto_merge=true \
  origin "$BRANCH_NAME"

Expected: The MR should automatically merge once the pipeline passes, without needing to view it in the browser first.

Edited by Marc Shaw

Merge request reports

Loading