diff options
-rw-r--r-- | app/helpers/blame_helper.rb | 16 | ||||
-rw-r--r-- | changelogs/unreleased/35441-fix-division-by-zero.yml | 5 | ||||
-rw-r--r-- | spec/helpers/blame_helper_spec.rb | 16 |
3 files changed, 30 insertions, 7 deletions
diff --git a/app/helpers/blame_helper.rb b/app/helpers/blame_helper.rb index d1dc4d94560..089d9e3e387 100644 --- a/app/helpers/blame_helper.rb +++ b/app/helpers/blame_helper.rb @@ -11,11 +11,15 @@ module BlameHelper end def age_map_class(commit_date, duration) - commit_date_days_ago = (duration[:now] - commit_date).to_i / 1.day - # Numbers 0 to 10 come from this calculation, but only commits on the oldest - # day get number 10 (all other numbers can be multiple days), so the range - # is normalized to 0-9 - age_group = [(10 * commit_date_days_ago) / duration[:started_days_ago], 9].min - "blame-commit-age-#{age_group}" + if duration[:started_days_ago] == 0 + "blame-commit-age-0" + else + commit_date_days_ago = (duration[:now] - commit_date).to_i / 1.day + # Numbers 0 to 10 come from this calculation, but only commits on the oldest + # day get number 10 (all other numbers can be multiple days), so the range + # is normalized to 0-9 + age_group = [(10 * commit_date_days_ago) / duration[:started_days_ago], 9].min + "blame-commit-age-#{age_group}" + end end end diff --git a/changelogs/unreleased/35441-fix-division-by-zero.yml b/changelogs/unreleased/35441-fix-division-by-zero.yml new file mode 100644 index 00000000000..335b2d40494 --- /dev/null +++ b/changelogs/unreleased/35441-fix-division-by-zero.yml @@ -0,0 +1,5 @@ +--- +title: Fix division by zero error in blame age mapping +merge_request: 13803 +author: Jeff Stubler +type: fixed diff --git a/spec/helpers/blame_helper_spec.rb b/spec/helpers/blame_helper_spec.rb index b4368516d83..659c53b1b5c 100644 --- a/spec/helpers/blame_helper_spec.rb +++ b/spec/helpers/blame_helper_spec.rb @@ -36,17 +36,26 @@ describe BlameHelper do describe '#age_map_class' do let(:dates) do - [Time.zone.local(2014, 3, 17, 0, 0, 0)] + [Time.zone.local(2014, 3, 17, 0, 0, 0), Time.zone.now] end let(:blame_groups) do [ { commit: double(committed_date: dates[0]) } ] end + let(:today_blame_groups) do + [ + { commit: double(committed_date: dates[1]) } + ] + end let(:duration) do project = double(created_at: dates[0]) helper.age_map_duration(blame_groups, project) end + let(:same_day_duration) do + project = double(created_at: dates[1]) + helper.age_map_duration(today_blame_groups, project) + end it 'returns blame-commit-age-9 when oldest' do expect(helper.age_map_class(dates[0], duration)).to eq 'blame-commit-age-9' @@ -55,5 +64,10 @@ describe BlameHelper do it 'returns blame-commit-age-0 class when newest' do expect(helper.age_map_class(duration[:now], duration)).to eq 'blame-commit-age-0' end + + it 'returns blame-commit-age-0 class when created on same day' do + puts(same_day_duration.inspect) + expect(helper.age_map_class(duration[:now], same_day_duration)).to eq 'blame-commit-age-0' + end end end |