Commit 09b02e66 authored by Ruben Davila's avatar Ruben Davila

Add ability to remove the time spent through the `remove_time_spent` command.

parent 02a2422e
......@@ -17,7 +17,9 @@ module TimeTrackable
def spend_time=(seconds)
return unless seconds
timelogs.new(time_spent: seconds)
new_time_spent = seconds.zero? ? -(total_time_spent) : seconds
timelogs.new(time_spent: new_time_spent)
@time_spent = seconds
end
......
......@@ -284,6 +284,15 @@ module SlashCommands
@updates[:time_estimate] = 0
end
desc 'Remove the time spent'
condition do
issuable.persisted? &&
current_user.can?(:"admin_#{issuable.to_ability_name}", project)
end
command :remove_time_spent do
@updates[:spend_time] = 0
end
def find_label_ids(labels_param)
label_ids_by_reference = extract_references(labels_param, :label).map(&:id)
labels_ids_by_name = LabelsFinder.new(current_user, project_id: project.id, name: labels_param.split).execute.select(:id)
......
......@@ -150,9 +150,14 @@ module SystemNoteService
def change_time_spent(noteable, project, author)
time_spent = noteable.time_spent
parsed_time = ChronicDuration.output(time_spent.abs, format: :short)
action = time_spent > 0 ? 'Added' : 'Substracted'
body = "#{action} #{parsed_time} of time spent on this #{noteable.human_class_name}"
if time_spent.zero?
body = "Removed time spent on this #{noteable.human_class_name}"
else
parsed_time = ChronicDuration.output(time_spent.abs, format: :short)
action = time_spent > 0 ? 'Added' : 'Substracted'
body = "#{action} #{parsed_time} of time spent on this #{noteable.human_class_name}"
end
create_note(noteable: noteable, project: project, author: author, note: body)
end
......
......@@ -32,3 +32,4 @@ do.
| <code>/estimate &lt;1w 3d 2h 14m&gt;</code> | Set time estimate |
| `/remove_estimation` | Remove estimated time |
| <code>/spend &lt;1h 30m &#124; -1h 5m&gt;</code> | Add or substract spent time |
| `/remove_time_spent` | Remove time spent |
......@@ -234,6 +234,14 @@ describe SlashCommands::InterpretService, services: true do
end
end
shared_examples 'remove_time_spent command' do
it 'populates spend_time: "0" if content contains /remove_time_spent' do
_, updates = service.execute(content, issuable)
expect(updates).to eq(spend_time: 0)
end
end
shared_examples 'empty command' do
it 'populates {} if content contains an unsupported command' do
_, updates = service.execute(content, issuable)
......@@ -510,6 +518,11 @@ describe SlashCommands::InterpretService, services: true do
let(:issuable) { issue }
end
it_behaves_like 'remove_time_spent command' do
let(:content) { '/remove_time_spent' }
let(:issuable) { issue }
end
context 'when current_user cannot :admin_issue' do
let(:visitor) { create(:user) }
let(:issue) { create(:issue, project: project, author: visitor) }
......
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