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
!build.any_unmet_prerequisites? # If false is returned, it stops the transition
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|
build.run_after_commit do
Ci::BuildScheduleWorker.perform_at(build.scheduled_at, build.id)
......@@ -424,7 +428,7 @@ module Ci
end
def playable?
action? && !archived? && (manual? || scheduled? || retryable?)
action? && !archived? && (manual? || scheduled? || retryable?) && !waiting_for_deployment_approval?
end
def waiting_for_deployment_approval?
......
......@@ -70,6 +70,10 @@ class Deployment < ApplicationRecord
transition created: :blocked
end
event :unblock do
transition blocked: :created
end
event :succeed do
transition any - [:success] => :success
end
......
......@@ -32,6 +32,7 @@ module Deployments
if approval.rejected?
deployment.deployable.drop!(:deployment_rejected)
elsif deployment.pending_approval_count <= 0
deployment.unblock!
deployment.deployable.enqueue!
end
end
......
......@@ -103,9 +103,11 @@ RSpec.describe Deployments::ApprovalService do
let(:required_approval_count) { 1 }
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
......
......@@ -2468,6 +2468,16 @@ RSpec.describe Ci::Build do
it { is_expected.not_to be_playable }
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
describe 'project settings' do
......@@ -3792,6 +3802,18 @@ RSpec.describe Ci::Build do
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
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