diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 09b03278939..b133c756fcf 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -324,6 +324,11 @@ module Ci retries.is_a?(Hash) && retries.fetch(:max, 0) || retries || 0 end + def retry_when + retries = self.options[:retry] + retries.is_a?(Hash) && retries.fetch(:when, 'always').to_s || 'always' + end + def latest? !retried? end diff --git a/spec/models/ci/build_spec.rb b/spec/models/ci/build_spec.rb index 88a466317a5..ba1de3612ad 100644 --- a/spec/models/ci/build_spec.rb +++ b/spec/models/ci/build_spec.rb @@ -1512,6 +1512,32 @@ describe Ci::Build do end end end + + describe '#retry_when' do + context 'when value is defined' do + subject { create(:ci_build, options: { retry: { when: :something } }) } + + it 'returns the configured value' do + expect(subject.retry_when).to eq :something + end + end + + context 'when value is not defined' do + subject { create(:ci_build) } + + it 'returns `always`' do + expect(subject.retry_when).to eq :always + end + end + + context 'when retry is only defined as an integer' do + subject { create(:ci_build, options: { retry: 1 }) } + + it 'returns `always`' do + expect(subject.retry_when).to eq :always + end + end + end end describe '#keep_artifacts!' do