Commit 2624b4d0 authored by Alishan Ladhani's avatar Alishan Ladhani Committed by Shinya Maeda

Disallow enqueuing builds that are waiting for deployment approval

parent 9e971c74
...@@ -268,6 +268,10 @@ module Ci ...@@ -268,6 +268,10 @@ module Ci
!build.any_unmet_prerequisites? # If false is returned, it stops the transition !build.any_unmet_prerequisites? # If false is returned, it stops the transition
end end
before_transition on: :enqueue do |build|
!build.waiting_for_deployment_approval? # If false is returned, it stops the transition
end
after_transition created: :scheduled do |build| after_transition created: :scheduled do |build|
build.run_after_commit do build.run_after_commit do
Ci::BuildScheduleWorker.perform_at(build.scheduled_at, build.id) Ci::BuildScheduleWorker.perform_at(build.scheduled_at, build.id)
...@@ -424,7 +428,7 @@ module Ci ...@@ -424,7 +428,7 @@ module Ci
end end
def playable? def playable?
action? && !archived? && (manual? || scheduled? || retryable?) action? && !archived? && (manual? || scheduled? || retryable?) && !waiting_for_deployment_approval?
end end
def waiting_for_deployment_approval? def waiting_for_deployment_approval?
......
...@@ -70,6 +70,10 @@ class Deployment < ApplicationRecord ...@@ -70,6 +70,10 @@ class Deployment < ApplicationRecord
transition created: :blocked transition created: :blocked
end end
event :unblock do
transition blocked: :created
end
event :succeed do event :succeed do
transition any - [:success] => :success transition any - [:success] => :success
end end
......
...@@ -32,6 +32,7 @@ module Deployments ...@@ -32,6 +32,7 @@ module Deployments
if approval.rejected? if approval.rejected?
deployment.deployable.drop!(:deployment_rejected) deployment.deployable.drop!(:deployment_rejected)
elsif deployment.pending_approval_count <= 0 elsif deployment.pending_approval_count <= 0
deployment.unblock!
deployment.deployable.enqueue! deployment.deployable.enqueue!
end end
end end
......
...@@ -103,9 +103,11 @@ RSpec.describe Deployments::ApprovalService do ...@@ -103,9 +103,11 @@ RSpec.describe Deployments::ApprovalService do
let(:required_approval_count) { 1 } let(:required_approval_count) { 1 }
it 'enqueues the build' do it 'enqueues the build' do
subject expect { subject }.to change { deployment.deployable.status }.from('manual').to('pending')
end
expect(deployment.deployable.status).to eq('pending') it 'unblocks the deployment' do
expect { subject }.to change { deployment.status }.from('blocked').to('created')
end end
end end
......
...@@ -2468,6 +2468,16 @@ RSpec.describe Ci::Build do ...@@ -2468,6 +2468,16 @@ RSpec.describe Ci::Build do
it { is_expected.not_to be_playable } it { is_expected.not_to be_playable }
end end
context 'when build is waiting for deployment approval' do
subject { build_stubbed(:ci_build, :manual, environment: 'production') }
before do
create(:deployment, :blocked, deployable: subject)
end
it { is_expected.not_to be_playable }
end
end end
describe 'project settings' do describe 'project settings' do
...@@ -3792,6 +3802,18 @@ RSpec.describe Ci::Build do ...@@ -3792,6 +3802,18 @@ RSpec.describe Ci::Build do
end end
end end
describe 'when the build is waiting for deployment approval' do
let(:build) { create(:ci_build, :manual, environment: 'production') }
before do
create(:deployment, :blocked, deployable: build)
end
it 'does not allow the build to be enqueued' do
expect { build.enqueue! }.to raise_error(StateMachines::InvalidTransition)
end
end
describe 'state transition: any => [:pending]' do describe 'state transition: any => [:pending]' do
let(:build) { create(:ci_build, :created) } let(:build) { create(:ci_build, :created) }
......
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