Implement method that updates a stage status

This commit is contained in:
Grzegorz Bizon 2017-07-20 13:30:05 +02:00
parent e389507650
commit bbdc35717c
2 changed files with 67 additions and 2 deletions

View File

@ -3,6 +3,7 @@ module Ci
extend Ci::Model
include Importable
include HasStatus
include Gitlab::OptimisticLocking
enumerate_status!
@ -15,5 +16,51 @@ module Ci
validates :project, presence: true, unless: :importing?
validates :pipeline, presence: true, unless: :importing?
validates :name, presence: true, unless: :importing?
state_machine :status, initial: :created do
event :enqueue do
transition created: :pending
transition [:success, :failed, :canceled, :skipped] => :running
end
event :run do
transition any - [:running] => :running
end
event :skip do
transition any - [:skipped] => :skipped
end
event :drop do
transition any - [:failed] => :failed
end
event :succeed do
transition any - [:success] => :success
end
event :cancel do
transition any - [:canceled] => :canceled
end
event :block do
transition any - [:manual] => :manual
end
end
def update!
retry_optimistic_lock(self) do
case commit_statuses.latest.status
when 'pending' then enqueue
when 'running' then run
when 'success' then succeed
when 'failed' then drop
when 'canceled' then cancel
when 'manual' then block
when 'skipped' then skip
else skip
end
end
end
end
end

View File

@ -1,9 +1,9 @@
require 'spec_helper'
describe Ci::Stage, :models do
describe 'associations' do
let(:stage) { create(:ci_stage_entity) }
let(:stage) { create(:ci_stage_entity) }
describe 'associations' do
before do
create(:ci_build, stage_id: stage.id)
create(:commit_status, stage_id: stage.id)
@ -39,4 +39,22 @@ describe Ci::Stage, :models do
end
end
end
describe 'update!' do
context 'when stage objects needs to be updated' do
before do
create(:ci_build, :success, stage_id: stage.id)
create(:ci_build, :running, stage_id: stage.id)
end
it 'updates stage status correctly' do
expect { stage.update! }
.to change { stage.reload.status }
.to 'running'
end
end
context 'when stage object is locked' do
end
end
end