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
-
Feature Flag: Added
auto_merge_on_merge_status_changefeature flag (gitlab_com_derisk type) -
MergeRequest Model: Added an
after_transitioncallback in themerge_statusstate machine that:- Triggers when transitioning to
can_be_mergedstate - Only fires if
auto_merge_enabled?is true - Only fires if the feature flag is enabled for the project
- Enqueues
AutoMergeProcessWorkerwith the merge request ID
- Triggers when transitioning to
-
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
- Enable the feature flag:
Feature.enable(:auto_merge_on_merge_status_change)
Test Scenario 1: Auto merge triggers on merge status change
- Create a merge request with a passing pipeline
- Set up "Merge when pipeline succeeds" (MWPS) on the MR
- Introduce a merge conflict (e.g., push a conflicting change to the target branch)
- Verify the MR shows as having conflicts and cannot be merged
- Resolve the conflict (rebase or manually resolve)
-
Expected: Once the merge status changes to
can_be_merged, theAutoMergeProcessWorkershould be enqueued and the MR should automatically merge
Test Scenario 2: Feature flag disabled
- Disable the feature flag:
Feature.disable(:auto_merge_on_merge_status_change) - Repeat the steps from Scenario 1
-
Expected: The
AutoMergeProcessWorkershould NOT be enqueued when merge status changes tocan_be_merged
Test Scenario 3: Auto merge not enabled
- Enable the feature flag
- Create a merge request WITHOUT auto merge enabled
- Trigger a merge status change to
can_be_merged -
Expected: The
AutoMergeProcessWorkershould 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' }