From dfa8ab6f4219774104412c76b76c2716c44ccf3c Mon Sep 17 00:00:00 2001 From: Markus Doits Date: Wed, 24 Oct 2018 22:22:10 +0200 Subject: [PATCH] handle old retry format in build (possibly saved in database) --- app/models/ci/build.rb | 15 +++++++++++++-- spec/models/ci/build_spec.rb | 16 ++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 7b220487495..bebf356d1bf 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -317,12 +317,23 @@ module Ci pipeline.builds.retried.where(name: self.name).count end + # The format of the retry option changed in GitLab 11.5. Before it was an + # integer only, after it is a hash. New builds always created have the + # correct format, but builds created before GitLab 11.5 and saved in + # database still have the old integer only format. This helper method makes + # sure that the format is always correct when accessing the retry options, + # even on old builds. + def sanitized_retry_option + value = options&.[](:retry) + value.is_a?(Integer) ? { max: value } : value + end + def retries_max - options&.dig(:retry, :max) || 0 + sanitized_retry_option&.[](:max) || 0 end def retry_when - options&.dig(:retry, :when) || ['always'] + sanitized_retry_option&.[](:when) || ['always'] end def retry_failure? diff --git a/spec/models/ci/build_spec.rb b/spec/models/ci/build_spec.rb index 8a7b06ad21c..6849bc6db7a 100644 --- a/spec/models/ci/build_spec.rb +++ b/spec/models/ci/build_spec.rb @@ -1495,6 +1495,14 @@ describe Ci::Build do expect(subject.retries_max).to eq 0 end end + + context 'with integer only config option' do + subject { create(:ci_build, options: { retry: 1 }) } + + it 'returns the number of configured max retries' do + expect(subject.retries_max).to eq 1 + end + end end describe '#retry_when' do @@ -1513,6 +1521,14 @@ describe Ci::Build do expect(subject.retry_when).to eq ['always'] end end + + context 'with integer only config option' do + subject { create(:ci_build, options: { retry: 1 }) } + + it 'returns always array' do + expect(subject.retry_when).to eq ['always'] + end + end end describe '#retry_failure?' do