Commit 9f852c5a authored by Ash McKenzie's avatar Ash McKenzie

Merge branch 'cancel-merge-train-pipeline-when-dropped' into 'master'

Cancel running pipelines when merge request is dropped from merge train

Closes #35138

See merge request gitlab-org/gitlab!22146
parents f1f4e535 e6319ad7
---
title: Cancel running pipelines when merge request is dropped from merge train
merge_request: 22146
author:
type: fixed
......@@ -12,7 +12,10 @@ class MergeTrain < ApplicationRecord
after_commit :refresh_async, if: -> { saved_change_to_status? && stale? }
after_destroy do |merge_train|
run_after_commit { merge_train.cleanup_ref }
run_after_commit do
merge_train.pipeline&.cancel_running(retries: 1)
merge_train.cleanup_ref
end
end
enum status: %i[created merged stale fresh]
......
......@@ -543,6 +543,20 @@ describe MergeTrain do
end
end
describe '#destroy' do
subject { merge_train.destroy }
context 'when merge train has a pipeline' do
let(:merge_train) { create(:merge_train, pipeline: pipeline) }
let(:pipeline) { create(:ci_pipeline, :running) }
let(:build) { create(:ci_build, :running, pipeline: pipeline) }
it 'cancels the jobs in the pipeline' do
expect { subject }.to change { build.reload.status }.from('running').to('canceled')
end
end
end
describe '#cleanup_ref' do
subject { merge_train.cleanup_ref }
......
......@@ -125,6 +125,19 @@ describe AutoMerge::MergeTrainService do
subject
end
context 'when pipeline exists' do
before do
merge_request.merge_train.update!(pipeline: pipeline)
end
let(:pipeline) { create(:ci_pipeline) }
let(:build) { create(:ci_build, :running, pipeline: pipeline) }
it 'cancels the jobs in the pipeline' do
expect { subject }.to change { build.reload.status }.from('running').to('canceled')
end
end
context 'when train ref exists' do
before do
merge_request.project.repository.create_ref(merge_request.target_branch, merge_request.train_ref_path)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment