Commit 80e314be authored by Ruben Davila's avatar Ruben Davila

Refactor formatting of time and add more specs.

parent 4d681488
......@@ -36,11 +36,11 @@ module TimeTrackable
end
def human_total_time_spent
ChronicDuration.output(total_time_spent, format: :short)
Gitlab::TimeTrackingFormatter.output(total_time_spent)
end
def human_time_estimate
ChronicDuration.output(time_estimate, format: :short)
Gitlab::TimeTrackingFormatter.output(time_estimate)
end
private
......
......@@ -254,7 +254,7 @@ module SlashCommands
current_user.can?(:"admin_#{issuable.to_ability_name}", project)
end
command :estimate do |raw_duration|
time_estimate = ChronicDuration.parse(raw_duration, default_unit: 'hours') rescue nil
time_estimate = Gitlab::TimeTrackingFormatter.parse(raw_duration)
if time_estimate
@updates[:time_estimate] = time_estimate
......@@ -268,7 +268,7 @@ module SlashCommands
end
command :spend do |raw_duration|
reduce_time = raw_duration.sub!(/\A-/, '')
time_spent = ChronicDuration.parse(raw_duration, default_unit: 'hours') rescue nil
time_spent = Gitlab::TimeTrackingFormatter.parse(raw_duration)
if time_spent
time_spent *= -1 if reduce_time
......
......@@ -125,7 +125,7 @@ module SystemNoteService
# Returns the created Note object
def change_time_estimate(noteable, project, author)
parsed_time = ChronicDuration.output(noteable.time_estimate, format: :short)
parsed_time = Gitlab::TimeTrackingFormatter.output(noteable.time_estimate)
body = if noteable.time_estimate == 0
"Removed time estimate on this #{noteable.human_class_name}"
else
......@@ -154,7 +154,7 @@ module SystemNoteService
if time_spent == :reset
body = "Removed time spent on this #{noteable.human_class_name}"
else
parsed_time = ChronicDuration.output(time_spent.abs, format: :short)
parsed_time = Gitlab::TimeTrackingFormatter.output(time_spent.abs)
action = time_spent > 0 ? 'Added' : 'Subtracted'
body = "#{action} #{parsed_time} of time spent on this #{noteable.human_class_name}"
end
......
module Gitlab
module TimeTrackingFormatter
extend self
def parse(string)
ChronicDuration.parse(string, default_unit: 'hours')
rescue ChronicDuration::DurationParseError
nil
end
def output(seconds)
ChronicDuration.output(seconds, format: :short, limit_to_hours: false, weeks: true)
end
end
end
......@@ -594,4 +594,69 @@ describe SystemNoteService, services: true do
end
end
end
describe '.change_time_estimate' do
subject { described_class.change_time_estimate(noteable, project, author) }
it_behaves_like 'a system note'
context 'with a time estimate' do
it 'sets the note text' do
noteable.update_attribute(:time_estimate, 277200)
expect(subject.note).to eq "Changed time estimate of this issue to 1w 4d 5h"
end
end
context 'without a time estimate' do
it 'sets the note text' do
expect(subject.note).to eq "Removed time estimate on this issue"
end
end
end
describe '.change_time_spent' do
# We need a custom noteable in order to the shared examples to be green.
let(:noteable) do
mr = create(:merge_request, source_project: project)
mr.spend_time(1, author)
mr.save!
mr
end
subject do
described_class.change_time_spent(noteable, project, author)
end
it_behaves_like 'a system note'
context 'when time was added' do
it 'sets the note text' do
spend_time!(277200)
expect(subject.note).to eq "Added 1w 4d 5h of time spent on this merge request"
end
end
context 'when time was subtracted' do
it 'sets the note text' do
spend_time!(-277200)
expect(subject.note).to eq "Subtracted 1w 4d 5h of time spent on this merge request"
end
end
context 'when time was removed' do
it 'sets the note text' do
spend_time!(:reset)
expect(subject.note).to eq "Removed time spent on this merge request"
end
end
def spend_time!(seconds)
noteable.spend_time(seconds, author)
noteable.save!
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