Fix merge train skip validation for semi-linear history
What does this MR do and why?
Fixes #573986 (closed)
When merge trains are enabled with semi-linear history (ff_merge_must_be_possible), the system currently allows immediate merges even when the MR is not up-to-date with the target branch and "Allow skipping the merge train" is disabled. This violates the semi-linear history constraint.
Root Cause
The should_be_rebased? override in ee/app/models/ee/merge_request.rb was too broad - it returned false for ALL merge train scenarios, even when the MR was not actively on a train. This prevented the rebase requirement from being enforced for immediate merge attempts.
Solution
This MR fixes the issue by:
-
Updating
should_be_rebased?logic: Only skip the rebase check when actively on a merge train. When merge trains are enabled but the MR is not on a train, still enforce the rebase requirement for semi-linear history. -
Adding
can_skip_merge_train?helper: Provides a clear method to check if skipping the merge train is allowed based on project settings. -
Adding merge service validation: Prevents immediate merges via API when skip is not allowed, ensuring the MR must go through the merge train.
How to set up and validate locally
-
Create a project with:
- Merge Trains enabled
- Merge method set to "Merge commit with semi-linear history"
- "Allow skipping the merge train" disabled
-
Create an MR that's not up-to-date with the target branch
-
Before this fix: "Merge immediately" button is shown and allows merging outdated MR
-
After this fix:
-
should_be_rebased?returnstrue - "Merge immediately" button is hidden
- Only "Add to merge train" option is available
- Attempting to merge immediately via API fails with validation error
-
MR acceptance checklist
-
Code review guidelines -
Merge request title and description are up to date -
Tests added for new functionality -
Documentation created/updated -
Changelog entry added