Commit 6a2f8a9a authored by Grzegorz Bizon's avatar Grzegorz Bizon

Ensure that an imported pipeline stage can be updated

parent 10df7014
...@@ -20,10 +20,18 @@ module Ci ...@@ -20,10 +20,18 @@ module Ci
validates :index, presence: true validates :index, presence: true
end end
after_initialize do |stage| after_initialize do
self.status = DEFAULT_STATUS if self.status.nil? self.status = DEFAULT_STATUS if self.status.nil?
end end
before_validation do
next unless index.nil?
statuses.pluck(:stage_idx).tap do |indices|
self.index = indices.max_by { |index| indices.count(index) }
end
end
state_machine :status, initial: :created do state_machine :status, initial: :created do
event :enqueue do event :enqueue do
transition created: :pending transition created: :pending
......
...@@ -51,7 +51,7 @@ describe Ci::Stage, :models do ...@@ -51,7 +51,7 @@ describe Ci::Stage, :models do
end end
end end
describe 'update_status' do describe '#update_status' do
context 'when stage objects needs to be updated' do context 'when stage objects needs to be updated' do
before do before do
create(:ci_build, :success, stage_id: stage.id) create(:ci_build, :success, stage_id: stage.id)
...@@ -87,4 +87,21 @@ describe Ci::Stage, :models do ...@@ -87,4 +87,21 @@ describe Ci::Stage, :models do
end end
end end
end end
describe '#index' do
context 'when stage has been imported and does not have index set' do
before do
create(:ci_build, :running, stage_id: stage.id, stage_idx: 10)
stage.update_column(:index, nil)
end
it 'recalculates index before updating status' do
expect(stage.reload.index).to be_nil
stage.update_status
expect(stage.reload.index).to eq 10
end
end
end
end end
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