Skip to content

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

  1. Create an project with the following setup:
  1. Create 2 MR's
  2. Ensure the merge train pipeline will take a significant amount of time via sleep 100
  3. Place a debugger in the abort method
  4. Trigger the auto-merge
  5. Delete the target branch via re-targeting. i.e. by merging a dependent MR
  6. When you hit the debugger wait until the train starts and then exit the debugger (to simulate the race)
  7. 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

Edited by Allison Browne

Merge request reports

Loading