From 3f3b5e6b33dd5a45bbd18c9af32e9a3aa4ad82dd Mon Sep 17 00:00:00 2001 From: Stan Hu <stanhu@gmail.com> Date: Fri, 29 Jul 2022 10:28:29 -0700 Subject: [PATCH] Gracefully handle nil created_at values in CI pipelines https://gitlab.com/gitlab-org/gitlab/-/merge_requests/90027 added `CI::Pipeline#age_in_minutes`. This method causes a 500 error for any page that attempts to load an old pipeline that happens to have a `nil` `created_at` value. For these pipelines, just return 0. Relates to https://gitlab.com/gitlab-org/gitlab/-/issues/369124 Changelog: fixed --- app/models/ci/pipeline.rb | 2 ++ spec/models/ci/pipeline_spec.rb | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index 3a8e68c58a67d..81ce3f827967c 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -1322,6 +1322,8 @@ def age_in_minutes raise ArgumentError, 'pipeline not fully loaded' end + return 0 unless created_at + (Time.current - created_at).ceil / 60 end diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index 6ed92c4196f3a..5713e322a8e86 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -5339,6 +5339,16 @@ def create_bridge(upstream, downstream, depend = false) expect(pipeline.age_in_minutes).to eq 120 end end + + context 'when pipeline has no created_at' do + before do + pipeline.update!(created_at: nil) + end + + it 'returns zero' do + expect(pipeline.age_in_minutes).to eq 0 + end + end end context 'when pipeline has been loaded without all attributes' do -- GitLab