Commit 1e5a2831 authored by Robert Speicher's avatar Robert Speicher Committed by Simon Knox

Merge branch '35441-fix-division-by-zero' into 'master'

Fix division by zero for blame age map

Closes #35441

See merge request !13803
parent 8ee57308
...@@ -11,11 +11,15 @@ module BlameHelper ...@@ -11,11 +11,15 @@ module BlameHelper
end end
def age_map_class(commit_date, duration) def age_map_class(commit_date, duration)
commit_date_days_ago = (duration[:now] - commit_date).to_i / 1.day if duration[:started_days_ago] == 0
# Numbers 0 to 10 come from this calculation, but only commits on the oldest "blame-commit-age-0"
# day get number 10 (all other numbers can be multiple days), so the range else
# is normalized to 0-9 commit_date_days_ago = (duration[:now] - commit_date).to_i / 1.day
age_group = [(10 * commit_date_days_ago) / duration[:started_days_ago], 9].min # Numbers 0 to 10 come from this calculation, but only commits on the oldest
"blame-commit-age-#{age_group}" # 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
end end
---
title: Fix division by zero error in blame age mapping
merge_request: 13803
author: Jeff Stubler
type: fixed
...@@ -35,25 +35,32 @@ describe BlameHelper do ...@@ -35,25 +35,32 @@ describe BlameHelper do
end end
describe '#age_map_class' do describe '#age_map_class' do
let(:dates) do let(:date) { Time.zone.local(2014, 3, 17, 0, 0, 0) }
[Time.zone.local(2014, 3, 17, 0, 0, 0)] let(:blame_groups) { [{ commit: double(committed_date: date) }] }
end
let(:blame_groups) do
[
{ commit: double(committed_date: dates[0]) }
]
end
let(:duration) do let(:duration) do
project = double(created_at: dates[0]) project = double(created_at: date)
helper.age_map_duration(blame_groups, project) helper.age_map_duration(blame_groups, project)
end end
it 'returns blame-commit-age-9 when oldest' do it 'returns blame-commit-age-9 when oldest' do
expect(helper.age_map_class(dates[0], duration)).to eq 'blame-commit-age-9' expect(helper.age_map_class(date, duration)).to eq 'blame-commit-age-9'
end end
it 'returns blame-commit-age-0 class when newest' 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' expect(helper.age_map_class(duration[:now], duration)).to eq 'blame-commit-age-0'
end end
context 'when called on the same day as project creation' do
let(:same_day_duration) do
project = double(created_at: now)
helper.age_map_duration(today_blame_groups, project)
end
let(:today_blame_groups) { [{ commit: double(committed_date: now) }] }
let(:now) { Time.zone.now }
it 'returns blame-commit-age-0 class' do
expect(helper.age_map_class(duration[:now], same_day_duration)).to eq 'blame-commit-age-0'
end
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