Cancel pipelines in merge train when the merge request is dropped

Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.

Problem

When a merge request is dropped from a merge train and a merge train pipeline is running, the pipeline keeps running even after the merge request is no longer on train. We should cancel such pipeline for saving some compute resources.

Proposal

Cancel merge train pipeline when the merge request is dropped from a merge train.

diff --git a/ee/app/services/merge_trains/refresh_merge_request_service.rb b/ee/app/services/merge_trains/refresh_merge_request_service.rb
index 7abb415f63d..2f07f10c611 100644
--- a/ee/app/services/merge_trains/refresh_merge_request_service.rb
+++ b/ee/app/services/merge_trains/refresh_merge_request_service.rb
@@ -114,7 +114,11 @@ module MergeTrains
     end
 
     def cancel_pipeline_for_merge_train(new_pipeline)
-      pipeline_for_merge_train&.auto_cancel_running(new_pipeline, retries: 1)
+      if new_pipeline
+        pipeline_for_merge_train&.auto_cancel_running(new_pipeline, retries: 1)
+      else
+        pipeline_for_merge_train&.cancel_running(retries: 1)
+      end
     rescue ActiveRecord::StaleObjectError
       # Often the pipeline has already been canceled by the default cancelaltion
       # mechanizm `Ci::CreatePipelineService#cancel_pending_pipelines`. In this
@@ -160,6 +164,8 @@ module MergeTrains
     end
 
     def drop(error)
+      cancel_pipeline_for_merge_train(nil)
+
       AutoMerge::MergeTrainService.new(project, merge_user)
         .abort(merge_request, error.message)
Edited by 🤖 GitLab Bot 🤖