Fix broken merge train merge when target branch deleted
What does this MR do and why?
A merge request could not be merged because a train car existed that was never deleted by the abort
process.
Based on the system note in that MR: Tarun Vellishetty aborted automatic add to merge train because target branch was changed Sep 4, 2024, 6:30 AM
Means the request to abort used: AutoMerge::AddToMergeTrainWhenPipelineSucceedsService
or
Here we destroy
the merge train car in AutoMerge::AddToMergeTrainWhenPipelineSucceedsService#abort
similar to how we do in AutoMerge::MergeTrainService#abort
.
AutoMerge::AddToMergeTrainWhenPipelineSucceedsService
seems to be built with the assumption that the abort
will only happen prior to the train car being created, but we see that MergeRequests::UpdateService#handle_target_branch_change
can abort a merge request after it's already been added to the train when the strategy is Add To Merge Train When Pipeline Succeeds
.
We use the AutoMerge::MergeTrainService#abort
service directly in AutoMerge::AddToMergeTrainWhenPipelineSucceedsService#abort
so that the proper system notes are added when there is an existing train. These system notes indicate that the actual merge train car pipeline was aborted and not just that the automatic add to merge train was aborted
.
How to set up and validate locally
- Create an project with the following setup:
- Create 2 MR's
- Ensure the merge train pipeline will take a significant amount of time via sleep 100
- Place a debugger in the abort method
- Trigger the auto-merge
- Delete the target branch via re-targeting. i.e. by merging a dependent MR
- When you hit the debugger wait until the train starts and then exit the debugger (to simulate the race)
- You should not see
A new merge train has started and this merge request is the first of the queue. View merge train details.
Related to #483097 (closed)