Commit e6319ad7 authored by Shinya Maeda's avatar Shinya Maeda

Cancel running pipelines when merge request is dropped from merge train

This commit improves the canceletion mechanizm of merge train pipelines
parent f63a8586
---
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 ...@@ -12,7 +12,10 @@ class MergeTrain < ApplicationRecord
after_commit :refresh_async, if: -> { saved_change_to_status? && stale? } after_commit :refresh_async, if: -> { saved_change_to_status? && stale? }
after_destroy do |merge_train| 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 end
enum status: %i[created merged stale fresh] enum status: %i[created merged stale fresh]
......
...@@ -543,6 +543,20 @@ describe MergeTrain do ...@@ -543,6 +543,20 @@ describe MergeTrain do
end end
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 describe '#cleanup_ref' do
subject { merge_train.cleanup_ref } subject { merge_train.cleanup_ref }
......
...@@ -125,6 +125,19 @@ describe AutoMerge::MergeTrainService do ...@@ -125,6 +125,19 @@ describe AutoMerge::MergeTrainService do
subject subject
end 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 context 'when train ref exists' do
before do before do
merge_request.project.repository.create_ref(merge_request.target_branch, merge_request.train_ref_path) 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